Skip to content
This repository has been archived by the owner on May 4, 2024. It is now read-only.

Commit

Permalink
Shrink codec saved data code
Browse files Browse the repository at this point in the history
  • Loading branch information
robotgryphon committed Feb 21, 2024
1 parent 1763724 commit c754635
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,44 +10,36 @@

import java.util.function.Supplier;

public class CodecBackedSavedData<T extends SavedData> extends SavedData {
public abstract class CodecBackedSavedData<D extends SavedData> extends SavedData {

private static final Logger LOGS = LogManager.getLogger();

protected final CodecWrappedFactory<T> factory;
protected final CodecWrappedSavedData<CodecBackedSavedData<D>, D> factory;

public CodecBackedSavedData(CodecWrappedFactory<T> factory) {
this.factory = factory;
}

public static <T extends SavedData> CodecWrappedFactory<T> codecFactory(Codec<T> codec, Supplier<T> factory) {
return new CodecWrappedFactory<>(codec, factory);
public CodecBackedSavedData(Codec<D> codec, Supplier<D> factory) {
this.factory = new CodecWrappedSavedData<>(codec, factory);
}

@Override
public @NotNull CompoundTag save(@NotNull CompoundTag compoundTag) {
return factory.save(compoundTag, (T) this);
}

public record CodecWrappedFactory<T extends SavedData>(Codec<T> codec, Supplier<T> factory) {
final var data = factory.codec
.encodeStart(NbtOps.INSTANCE, (D) this)
.getOrThrow(false, LOGS::error);

public SavedData.Factory<T> 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<T extends CodecBackedSavedData<D>, D extends SavedData>(Codec<D> codec, Supplier<D> factory) {

return compoundTag;
public SavedData.Factory<D> 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);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -44,12 +47,11 @@ public class PlayerEntryPointHistory extends CodecBackedSavedData<PlayerEntryPoi

public static final Codec<PlayerEntryPointHistory> 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<PlayerEntryPointHistory> DEFAULT_FACTORY = factory(DEFAULT_MAX_DEPTH);
public static final Factory<PlayerEntryPointHistory> FACTORY = new CodecWrappedSavedData<>(CODEC, () -> new PlayerEntryPointHistory(DEFAULT_MAX_DEPTH)).sd();

private final MemoryGraph graph;
private final int maxDepth;
Expand All @@ -75,7 +77,7 @@ public PlayerEntryPointHistory(int maxDepth) {
}

private PlayerEntryPointHistory(int maxDepth, Map<UUID, List<PlayerRoomHistoryEntry>> history) {
super(factory(maxDepth));
super(CODEC, FACTORY.constructor());
this.graph = new MemoryGraph();
this.maxDepth = maxDepth;
this.roomNodes = new HashMap<>();
Expand All @@ -93,23 +95,12 @@ private PlayerEntryPointHistory(int maxDepth, Map<UUID, List<PlayerRoomHistoryEn
this.setDirty();
}

private static CodecBackedSavedData.CodecWrappedFactory<PlayerEntryPointHistory> 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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ public class RoomRegistrar extends CodecBackedSavedData<RoomRegistrar> implement
private final Map<String, RoomRegistrationNode> registrationNodes;

private RoomRegistrar() {
super(CodecBackedSavedData.codecFactory(CODEC, RoomRegistrar::new));
super(CODEC, RoomRegistrar::new);
this.graph = new MemoryGraph();
this.registrationNodes = new HashMap<>();
}

private RoomRegistrar(List<RoomRegistrationNode> regNodes) {
super(CodecBackedSavedData.codecFactory(CODEC, RoomRegistrar::new));
super(CODEC, RoomRegistrar::new);
this.graph = new MemoryGraph();
this.registrationNodes = new HashMap<>();
regNodes.forEach(this::registerDirty);
Expand All @@ -56,7 +56,7 @@ private RoomRegistrar(List<RoomRegistrationNode> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public SpawnManager(String roomCode) {
}

public SpawnManager(String roomCode, Map<UUID, RoomSpawn> 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;
Expand All @@ -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);
Expand Down

0 comments on commit c754635

Please sign in to comment.