-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
15e36a6
commit 3d81108
Showing
61 changed files
with
956 additions
and
564 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 0 additions & 2 deletions
2
neoforge-main/src/main/java/dev/compactmods/machines/CMRegistries.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
neoforge-main/src/main/java/dev/compactmods/machines/data/CMDataAttachmentFileManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package dev.compactmods.machines.data; | ||
|
||
import dev.compactmods.machines.LoggingUtil; | ||
import net.minecraft.core.HolderLookup; | ||
import net.minecraft.nbt.CompoundTag; | ||
import net.minecraft.nbt.NbtIo; | ||
import net.minecraft.server.MinecraftServer; | ||
import net.neoforged.neoforge.attachment.AttachmentHolder; | ||
|
||
import java.io.IOException; | ||
import java.util.HashMap; | ||
import java.util.function.BiFunction; | ||
|
||
public class CMDataAttachmentFileManager<Key, T extends AttachmentHolder & CMDataFile> { | ||
|
||
protected final MinecraftServer server; | ||
private final BiFunction<MinecraftServer, Key, T> creator; | ||
private final HashMap<Key, T> cache; | ||
|
||
protected CMDataAttachmentFileManager(MinecraftServer server, BiFunction<MinecraftServer, Key, T> creator) { | ||
this.server = server; | ||
this.creator = creator; | ||
this.cache = new HashMap<>(); | ||
} | ||
|
||
public T data(Key key) { | ||
return cache.computeIfAbsent(key, k -> creator.apply(server, key)); | ||
} | ||
|
||
public void save(HolderLookup.Provider provider) { | ||
cache.forEach((key, data) -> { | ||
CompoundTag fullTag = new CompoundTag(); | ||
fullTag.putString("version", "1.0"); | ||
|
||
if(data.hasAttachments()) { | ||
var ad = data.serializeAttachments(provider); | ||
if(ad != null) | ||
fullTag.put("attachments", ad); | ||
} | ||
|
||
try { | ||
var path = data.getDataLocation(server).resolve(key.toString() + ".dat"); | ||
NbtIo.writeCompressed(fullTag, path); | ||
} catch (IOException e) { | ||
LoggingUtil.modLog().error("Failed to write data: " + e.getMessage(), e); | ||
} | ||
}); | ||
} | ||
|
||
public boolean hasData(String code) { | ||
return cache.containsKey(code); | ||
} | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
neoforge-main/src/main/java/dev/compactmods/machines/data/CMDataFile.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package dev.compactmods.machines.data; | ||
|
||
import dev.compactmods.machines.api.CompactMachinesApi; | ||
import net.minecraft.server.MinecraftServer; | ||
import net.minecraft.world.level.storage.LevelResource; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
|
||
public interface CMDataFile { | ||
|
||
default String getDataVersion() { | ||
return "1.0.0"; | ||
} | ||
|
||
default Path getDataLocation(MinecraftServer server) { | ||
return server.getWorldPath(LevelResource.ROOT) | ||
.resolve(CompactMachinesApi.MOD_ID) | ||
.resolve("data"); | ||
} | ||
|
||
static void ensureDirExists(Path dir) { | ||
if(!Files.exists(dir)) { | ||
try { | ||
Files.createDirectories(dir); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} | ||
} |
47 changes: 0 additions & 47 deletions
47
neoforge-main/src/main/java/dev/compactmods/machines/data/CodecBackedSavedData.java
This file was deleted.
Oops, something went wrong.
8 changes: 8 additions & 0 deletions
8
neoforge-main/src/main/java/dev/compactmods/machines/data/CodecHolder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package dev.compactmods.machines.data; | ||
|
||
import com.mojang.serialization.Codec; | ||
|
||
public interface CodecHolder<T> { | ||
|
||
Codec<T> codec(); | ||
} |
55 changes: 55 additions & 0 deletions
55
neoforge-main/src/main/java/dev/compactmods/machines/data/DataManagerBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package dev.compactmods.machines.data; | ||
|
||
import dev.compactmods.machines.data.manager.CMKeyedDataFileManager; | ||
import dev.compactmods.machines.data.manager.CMSingletonDataFileManager; | ||
import dev.compactmods.machines.data.manager.IDataFileManager; | ||
import dev.compactmods.machines.data.manager.IKeyedDataFileManager; | ||
import dev.compactmods.machines.data.room.RoomDataAttachments; | ||
import net.minecraft.resources.ResourceLocation; | ||
import net.minecraft.server.MinecraftServer; | ||
|
||
import java.util.function.BiFunction; | ||
import java.util.function.Function; | ||
|
||
public class DataManagerBuilder { | ||
|
||
public static <Key, T extends CMDataFile & CodecHolder<T>> DataManagerBuilder.Keyed<Key, T> keyed(MinecraftServer server, ResourceLocation id) { | ||
return new Keyed<>(server, id); | ||
} | ||
|
||
public static <T extends CMDataFile & CodecHolder<T>> DataManagerBuilder.Singleton<T> singleton(MinecraftServer server, ResourceLocation id) { | ||
return new Singleton<>(server, id); | ||
} | ||
|
||
public static class Keyed<Key, T extends CMDataFile & CodecHolder<T>> { | ||
|
||
private final MinecraftServer server; | ||
private final ResourceLocation id; | ||
private BiFunction<MinecraftServer, Key, T> factory; | ||
|
||
public Keyed(MinecraftServer server, ResourceLocation id) { | ||
this.server = server; | ||
this.id = id; | ||
} | ||
|
||
public IKeyedDataFileManager<Key, T> build() { | ||
return new CMKeyedDataFileManager<>(server, factory); | ||
} | ||
} | ||
|
||
public static class Singleton<T extends CMDataFile & CodecHolder<T>> { | ||
|
||
private final MinecraftServer server; | ||
private final ResourceLocation id; | ||
private Function<MinecraftServer, T> factory; | ||
|
||
public Singleton(MinecraftServer server, ResourceLocation id) { | ||
this.server = server; | ||
this.id = id; | ||
} | ||
|
||
public IDataFileManager<T> build() { | ||
return new CMSingletonDataFileManager<>(server, "", factory); | ||
} | ||
} | ||
} |
Oops, something went wrong.