diff --git a/core/src/main/java/dev/compactmods/machines/data/CodecBackedSavedData.java b/core/src/main/java/dev/compactmods/machines/data/CodecBackedSavedData.java index 92523f4..ad6b1f3 100644 --- a/core/src/main/java/dev/compactmods/machines/data/CodecBackedSavedData.java +++ b/core/src/main/java/dev/compactmods/machines/data/CodecBackedSavedData.java @@ -10,44 +10,36 @@ import java.util.function.Supplier; -public class CodecBackedSavedData extends SavedData { +public abstract class CodecBackedSavedData extends SavedData { private static final Logger LOGS = LogManager.getLogger(); - protected final CodecWrappedFactory factory; + protected final CodecWrappedSavedData, D> factory; - public CodecBackedSavedData(CodecWrappedFactory factory) { - this.factory = factory; - } - - public static CodecWrappedFactory codecFactory(Codec codec, Supplier factory) { - return new CodecWrappedFactory<>(codec, factory); + public CodecBackedSavedData(Codec codec, Supplier factory) { + this.factory = new CodecWrappedSavedData<>(codec, factory); } @Override public @NotNull CompoundTag save(@NotNull CompoundTag compoundTag) { - return factory.save(compoundTag, (T) this); - } - - public record CodecWrappedFactory(Codec codec, Supplier factory) { + final var data = factory.codec + .encodeStart(NbtOps.INSTANCE, (D) this) + .getOrThrow(false, LOGS::error); - public SavedData.Factory asSDFactory() { - return new SavedData.Factory<>(factory, this::load, null); + if (data instanceof CompoundTag dataTag) { + compoundTag.merge(dataTag); } - @NotNull - public CompoundTag save(@NotNull CompoundTag compoundTag, @NotNull T instance) { - final var data = codec.encodeStart(NbtOps.INSTANCE, instance) - .getOrThrow(false, LOGS::error); + return compoundTag; + } - if (data instanceof CompoundTag dataTag) { - compoundTag.merge(dataTag); - } + public record CodecWrappedSavedData, D extends SavedData>(Codec codec, Supplier factory) { - return compoundTag; + public SavedData.Factory sd() { + return new SavedData.Factory<>(factory, this::load, null); } - public T load(CompoundTag tag) { + public D load(CompoundTag tag) { return codec.parse(NbtOps.INSTANCE, tag).getOrThrow(false, LOGS::error); } } diff --git a/core/src/main/java/dev/compactmods/machines/player/PlayerEntryPointHistory.java b/core/src/main/java/dev/compactmods/machines/player/PlayerEntryPointHistory.java index 1af8428..730aaa0 100644 --- a/core/src/main/java/dev/compactmods/machines/player/PlayerEntryPointHistory.java +++ b/core/src/main/java/dev/compactmods/machines/player/PlayerEntryPointHistory.java @@ -1,6 +1,8 @@ package dev.compactmods.machines.player; import com.mojang.serialization.Codec; +import com.mojang.serialization.DynamicOps; +import com.mojang.serialization.Keyable; import com.mojang.serialization.codecs.RecordCodecBuilder; import dev.compactmods.feather.MemoryGraph; import dev.compactmods.feather.edge.impl.EmptyEdge; @@ -18,6 +20,7 @@ import net.minecraft.server.MinecraftServer; import net.minecraft.server.level.ServerPlayer; import net.minecraft.world.entity.player.Player; +import net.minecraft.world.level.saveddata.SavedData; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.jetbrains.annotations.NotNull; @@ -44,12 +47,11 @@ public class PlayerEntryPointHistory extends CodecBackedSavedData CODEC = RecordCodecBuilder.create(inst -> inst.group( Codec.INT.fieldOf("max_depth").forGetter(x -> x.maxDepth), - Codec.unboundedMap(UUIDUtil.CODEC, PlayerRoomHistoryEntry.CODEC.listOf()) + Codec.unboundedMap(UUIDUtil.STRING_CODEC, PlayerRoomHistoryEntry.CODEC.listOf()) .fieldOf("history") .forGetter(PlayerEntryPointHistory::playerRoomHistory) ).apply(inst, PlayerEntryPointHistory::new)); - - private static final CodecWrappedFactory DEFAULT_FACTORY = factory(DEFAULT_MAX_DEPTH); + public static final Factory FACTORY = new CodecWrappedSavedData<>(CODEC, () -> new PlayerEntryPointHistory(DEFAULT_MAX_DEPTH)).sd(); private final MemoryGraph graph; private final int maxDepth; @@ -75,7 +77,7 @@ public PlayerEntryPointHistory(int maxDepth) { } private PlayerEntryPointHistory(int maxDepth, Map> history) { - super(factory(maxDepth)); + super(CODEC, FACTORY.constructor()); this.graph = new MemoryGraph(); this.maxDepth = maxDepth; this.roomNodes = new HashMap<>(); @@ -93,23 +95,12 @@ private PlayerEntryPointHistory(int maxDepth, Map factory(int maxDepth) { - return CodecBackedSavedData.codecFactory(CODEC, () -> new PlayerEntryPointHistory(maxDepth)); - } - public static PlayerEntryPointHistory forServer(MinecraftServer server) throws MissingDimensionException { return CompactDimension.forServer(server) .getDataStorage() - .computeIfAbsent(DEFAULT_FACTORY.asSDFactory(), DATA_NAME); - } - - public static PlayerEntryPointHistory forServer(MinecraftServer server, int maxDepth) throws MissingDimensionException { - return CompactDimension.forServer(server) - .getDataStorage() - .computeIfAbsent(factory(maxDepth).asSDFactory(), DATA_NAME); + .computeIfAbsent(FACTORY, DATA_NAME); } - private static PlayerRoomHistoryEntry fromEdge(PlayerRoomEntryEdge edge) { return new PlayerRoomHistoryEntry(edge.target().get().code(), edge.entryTime(), edge.source().get().data()); } diff --git a/core/src/main/java/dev/compactmods/machines/room/RoomRegistrar.java b/core/src/main/java/dev/compactmods/machines/room/RoomRegistrar.java index d16109f..d49ab85 100644 --- a/core/src/main/java/dev/compactmods/machines/room/RoomRegistrar.java +++ b/core/src/main/java/dev/compactmods/machines/room/RoomRegistrar.java @@ -41,13 +41,13 @@ public class RoomRegistrar extends CodecBackedSavedData implement private final Map registrationNodes; private RoomRegistrar() { - super(CodecBackedSavedData.codecFactory(CODEC, RoomRegistrar::new)); + super(CODEC, RoomRegistrar::new); this.graph = new MemoryGraph(); this.registrationNodes = new HashMap<>(); } private RoomRegistrar(List regNodes) { - super(CodecBackedSavedData.codecFactory(CODEC, RoomRegistrar::new)); + super(CODEC, RoomRegistrar::new); this.graph = new MemoryGraph(); this.registrationNodes = new HashMap<>(); regNodes.forEach(this::registerDirty); @@ -56,7 +56,7 @@ private RoomRegistrar(List regNodes) { public static RoomRegistrar forServer(MinecraftServer server) throws MissingDimensionException { return CompactDimension.forServer(server) .getDataStorage() - .computeIfAbsent(CodecBackedSavedData.codecFactory(CODEC, RoomRegistrar::new).asSDFactory(), DATA_NAME); + .computeIfAbsent(new CodecWrappedSavedData<>(CODEC, RoomRegistrar::new).sd(), DATA_NAME); } @Override diff --git a/core/src/main/java/dev/compactmods/machines/room/spawn/SpawnManager.java b/core/src/main/java/dev/compactmods/machines/room/spawn/SpawnManager.java index a21edf6..c99d458 100644 --- a/core/src/main/java/dev/compactmods/machines/room/spawn/SpawnManager.java +++ b/core/src/main/java/dev/compactmods/machines/room/spawn/SpawnManager.java @@ -51,7 +51,7 @@ public SpawnManager(String roomCode) { } public SpawnManager(String roomCode, Map playerSpawns, RoomSpawn defaultSpawn) { - super(CodecBackedSavedData.codecFactory(CODEC, () -> new SpawnManager(roomCode))); + super(CODEC, () -> new SpawnManager(roomCode)); this.roomCode = roomCode; this.playerSpawns = new HashMap<>(playerSpawns); this.defaultSpawn = defaultSpawn; @@ -61,7 +61,7 @@ public static SpawnManager forRoom(MinecraftServer server, String roomCode, IRoo String roomFilename = Constants.MOD_ID + "_room_" + roomCode; var manager = CompactDimension.forServer(server) .getDataStorage() - .computeIfAbsent(CodecBackedSavedData.codecFactory(CODEC, () -> new SpawnManager(roomCode)).asSDFactory(), roomFilename); + .computeIfAbsent(new CodecWrappedSavedData<>(CODEC, () -> new SpawnManager(roomCode)).sd(), roomFilename); manager.setBoundaries(roomBounds.innerBounds()); manager.setDefaultSpawn(roomBounds.defaultSpawn(), Vec2.ZERO);