Skip to content

Commit

Permalink
Make waypoints storage future-proof + rely on level id for data persi…
Browse files Browse the repository at this point in the history
…stence
  • Loading branch information
xpple committed Jan 7, 2025
1 parent 390721b commit b28ca0c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.mojang.serialization.Dynamic;
import net.earthcomputer.clientcommands.ClientCommands;
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
import net.minecraft.SharedConstants;
import net.minecraft.Util;
import net.minecraft.commands.SharedSuggestionProvider;
import net.minecraft.core.BlockPos;
Expand Down Expand Up @@ -90,7 +91,8 @@ public static void register(CommandDispatcher<FabricClientCommandSource> dispatc
private static String getWorldIdentifier(FabricClientCommandSource source) {
String worldIdentifier;
if (source.getClient().hasSingleplayerServer()) {
worldIdentifier = source.getClient().getSingleplayerServer().getWorldData().getLevelName();
// the level id remains the same even after the level is renamed
worldIdentifier = source.getClient().getSingleplayerServer().storageSource.getLevelId();
} else {
worldIdentifier = source.getClient().getConnection().getConnection().getRemoteAddress().toString();
}
Expand Down Expand Up @@ -193,15 +195,18 @@ private static int list(FabricClientCommandSource source, boolean current) {
private static void saveFile() throws CommandSyntaxException {
try {
CompoundTag rootTag = new CompoundTag();
waypoints.forEach((worldIdentifier, worldWaypoints) -> rootTag.put(worldIdentifier, worldWaypoints.entrySet().stream()
rootTag.putInt("DataVersion", SharedConstants.getCurrentVersion().getDataVersion().getVersion());
CompoundTag compoundTag = new CompoundTag();
waypoints.forEach((worldIdentifier, worldWaypoints) -> compoundTag.put(worldIdentifier, worldWaypoints.entrySet().stream()
.collect(CompoundTag::new, (result, entry) -> {
CompoundTag waypoint = new CompoundTag();
Tag pos = NbtUtils.writeBlockPos(entry.getValue().getLeft());
waypoint.put("pos", pos);
String dimension = entry.getValue().getRight().location().toString();
waypoint.put("Pos", pos);
waypoint.putString("Dimension", dimension);
result.put(entry.getKey(), waypoint);
}, CompoundTag::merge)));
rootTag.put("Waypoints", compoundTag);
Path newFile = Files.createTempFile(ClientCommands.configDir, "waypoints", ".dat");
NbtIo.write(rootTag, newFile);
Path backupFile = ClientCommands.configDir.resolve("waypoints.dat_old");
Expand All @@ -218,12 +223,14 @@ private static void loadFile() throws IOException {
if (rootTag == null) {
return;
}
rootTag.getAllKeys().forEach(worldIdentifier -> {
CompoundTag worldWaypoints = rootTag.getCompound(worldIdentifier);
// TODO: update-sensitive: apply custom data fixes when it becomes necessary
CompoundTag compoundTag = rootTag.getCompound("Waypoints");
compoundTag.getAllKeys().forEach(worldIdentifier -> {
CompoundTag worldWaypoints = compoundTag.getCompound(worldIdentifier);
waypoints.put(worldIdentifier, worldWaypoints.getAllKeys().stream()
.collect(Collectors.toMap(Function.identity(), name -> {
CompoundTag waypoint = worldWaypoints.getCompound(name);
BlockPos pos = NbtUtils.readBlockPos(waypoint, "Pos").orElseThrow();
BlockPos pos = NbtUtils.readBlockPos(waypoint, "pos").orElseThrow();
ResourceKey<Level> dimension = Level.RESOURCE_KEY_CODEC.parse(new Dynamic<>(NbtOps.INSTANCE, waypoint.get("Dimension"))).resultOrPartial(LOGGER::error).orElseThrow();
return Pair.of(pos, dimension);
})));
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/clientcommands.aw
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ accessible field net/minecraft/network/codec/IdDispatchCodec toId Lit/unimi/dsi/
# cpermissionlevel
accessible method net/minecraft/client/player/LocalPlayer getPermissionLevel ()I

# cwaypoint
accessible field net/minecraft/server/MinecraftServer storageSource Lnet/minecraft/world/level/storage/LevelStorageSource$LevelStorageAccess;

# Game Options
accessible field net/minecraft/client/OptionInstance value Ljava/lang/Object;

Expand Down

0 comments on commit b28ca0c

Please sign in to comment.