-
Notifications
You must be signed in to change notification settings - Fork 55
refactor: switch to codecs for nbt serialization #340
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
dfe446e
remove java version detection for placeholders parser
John-Paul-R 3b41364
round one (need DFU for warped/named location)
John-Paul-R 36286de
convert buncha other things to codecs, and actually track the codecs
John-Paul-R 58997e4
world data ctor accessibility thing
John-Paul-R c5dfc2b
codec: reuse mc RegistryKey codec builder, rm unused VEC3D codec
John-Paul-R 323a944
address more comments in nbt methods
John-Paul-R 2608d09
missed NamedMinecraftLocation
John-Paul-R File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
55 changes: 55 additions & 0 deletions
55
src/main/java/com/fibermc/essentialcommands/WorldData.java
This file contains hidden or 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 com.fibermc.essentialcommands; | ||
|
|
||
| import com.fibermc.essentialcommands.codec.Codecs; | ||
| import com.fibermc.essentialcommands.types.MinecraftLocation; | ||
| import com.fibermc.essentialcommands.types.WarpStorage; | ||
| import org.jetbrains.annotations.NotNull; | ||
| import org.jetbrains.annotations.Nullable; | ||
|
|
||
| import com.mojang.serialization.Codec; | ||
| import com.mojang.serialization.codecs.RecordCodecBuilder; | ||
|
|
||
| import net.minecraft.nbt.NbtCompound; | ||
| import net.minecraft.nbt.NbtOps; | ||
|
|
||
| public class WorldData { | ||
| private @Nullable MinecraftLocation spawnLocation; | ||
| private final @NotNull WarpStorage warps; | ||
|
|
||
| WorldData() { | ||
| this.spawnLocation = null; | ||
| this.warps = new WarpStorage(); | ||
| } | ||
|
|
||
| WorldData(@Nullable MinecraftLocation spawnLocation, @NotNull WarpStorage warps) { | ||
| this.spawnLocation = spawnLocation; | ||
| this.warps = warps; | ||
| } | ||
|
|
||
| public WarpStorage warps() { | ||
| return this.warps; | ||
| } | ||
|
|
||
| public MinecraftLocation getSpawn() { | ||
| return this.spawnLocation; | ||
| } | ||
|
|
||
| public void setSpawn(@Nullable MinecraftLocation spawn) { | ||
| this.spawnLocation = spawn; | ||
| } | ||
|
|
||
| public static WorldData fromNbt(NbtCompound nbt) { | ||
| return CODEC.parse(NbtOps.INSTANCE, nbt).getOrThrow(); | ||
| } | ||
|
|
||
| public NbtCompound toNbt() { | ||
| return CODEC.encodeStart(NbtOps.INSTANCE, this).getOrThrow().asCompound().orElseThrow(); | ||
| } | ||
|
|
||
| public static final Codec<WorldData> CODEC = RecordCodecBuilder.create(instance -> | ||
| instance.group( | ||
| Codecs.MINECRAFT_LOCATION.fieldOf("spawn").forGetter(WorldData::getSpawn), | ||
| Codecs.WARP_STORAGE.fieldOf("warps").forGetter(WorldData::warps) | ||
| ).apply(instance, WorldData::new) | ||
| ); | ||
| } | ||
This file contains hidden or 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
95 changes: 95 additions & 0 deletions
95
src/main/java/com/fibermc/essentialcommands/codec/Codecs.java
This file contains hidden or 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,95 @@ | ||
| package com.fibermc.essentialcommands.codec; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.Optional; | ||
|
|
||
| import com.fibermc.essentialcommands.WorldData; | ||
| import com.fibermc.essentialcommands.types.*; | ||
|
|
||
| import com.mojang.serialization.Codec; | ||
| import com.mojang.serialization.codecs.RecordCodecBuilder; | ||
|
|
||
| import net.minecraft.registry.RegistryKey; | ||
| import net.minecraft.registry.RegistryKeys; | ||
| import net.minecraft.world.World; | ||
|
|
||
| public final class Codecs { | ||
| private Codecs() {} | ||
|
|
||
| public static final Codec<RegistryKey<World>> WORLD_KEY = RegistryKey.createCodec(RegistryKeys.WORLD); | ||
|
|
||
| public static final Codec<MinecraftLocation> MINECRAFT_LOCATION = RecordCodecBuilder.create(instance -> | ||
| instance.group( | ||
| WORLD_KEY.fieldOf("WorldRegistryKey").forGetter(MinecraftLocation::dim), | ||
| Codec.DOUBLE.fieldOf("x").forGetter(MinecraftLocation::x), | ||
| Codec.DOUBLE.fieldOf("y").forGetter(MinecraftLocation::y), | ||
| Codec.DOUBLE.fieldOf("z").forGetter(MinecraftLocation::z), | ||
| Codec.FLOAT.optionalFieldOf("headYaw", 0.0f).forGetter(MinecraftLocation::headYaw), | ||
| Codec.FLOAT.optionalFieldOf("pitch", 0.0f).forGetter(MinecraftLocation::pitch) | ||
| ).apply(instance, MinecraftLocation::new) | ||
| ); | ||
|
|
||
| public static final Codec<NamedMinecraftLocation> NAMED_MINECRAFT_LOCATION = RecordCodecBuilder.create(instance -> | ||
| instance.group( | ||
| // Inherit all fields from NamedMinecraftLocation | ||
| WORLD_KEY.fieldOf("WorldRegistryKey").forGetter(NamedMinecraftLocation::dim), | ||
| Codec.DOUBLE.fieldOf("x").forGetter(MinecraftLocation::x), | ||
| Codec.DOUBLE.fieldOf("y").forGetter(MinecraftLocation::y), | ||
| Codec.DOUBLE.fieldOf("z").forGetter(MinecraftLocation::z), | ||
| Codec.FLOAT.optionalFieldOf("headYaw", 0.0f).forGetter(NamedMinecraftLocation::headYaw), | ||
| Codec.FLOAT.optionalFieldOf("pitch", 0.0f).forGetter(NamedMinecraftLocation::pitch), | ||
| // loaded from the map | ||
| Codec.STRING.optionalFieldOf("name").forGetter(home -> Optional.of(home.getName())) | ||
|
|
||
| ).apply(instance, NamedMinecraftLocation::new) | ||
| ); | ||
|
|
||
| public static final Codec<WarpLocation> WARP_LOCATION = RecordCodecBuilder.create(instance -> | ||
| instance.group( | ||
| // Inherit all fields from NamedMinecraftLocation | ||
| WORLD_KEY.fieldOf("WorldRegistryKey").forGetter(WarpLocation::dim), | ||
| Codec.DOUBLE.fieldOf("x").forGetter(MinecraftLocation::x), | ||
| Codec.DOUBLE.fieldOf("y").forGetter(MinecraftLocation::y), | ||
| Codec.DOUBLE.fieldOf("z").forGetter(MinecraftLocation::z), | ||
| Codec.FLOAT.optionalFieldOf("headYaw", 0.0f).forGetter(WarpLocation::headYaw), | ||
| Codec.FLOAT.optionalFieldOf("pitch", 0.0f).forGetter(WarpLocation::pitch), | ||
| // loaded from the map | ||
| Codec.STRING.optionalFieldOf("name").forGetter(warp -> Optional.of(warp.getName())), | ||
|
|
||
| Codec.STRING.optionalFieldOf("permissionString").forGetter(warp -> Optional.ofNullable(warp.getPermissionString())) | ||
|
|
||
| ).apply(instance, WarpLocation::new) | ||
| ); | ||
|
|
||
| public static final Codec<NamedLocationStorage> NAMED_LOCATION_STORAGE = | ||
| Codec.unboundedMap(Codec.STRING, MINECRAFT_LOCATION) | ||
| .xmap( | ||
| // Convert Map to NamedLocationStorage | ||
| map -> { | ||
| NamedLocationStorage storage = new NamedLocationStorage(); | ||
| map.forEach( | ||
| (key, value) -> storage.put(key, new NamedMinecraftLocation(value, key)) | ||
| ); | ||
| return storage; | ||
| }, | ||
| // Convert NamedLocationStorage to Map for serialization | ||
| HashMap::new | ||
| ); | ||
|
|
||
| public static final Codec<WarpStorage> WARP_STORAGE = | ||
| Codec.unboundedMap(Codec.STRING, WARP_LOCATION) | ||
| .xmap( | ||
| // Convert Map to WarpStorage | ||
| map -> { | ||
| WarpStorage storage = new WarpStorage(); | ||
| map.forEach( | ||
| (key, value) -> storage.put(key, WarpLocation.setName(value, key)) | ||
| ); | ||
| return storage; | ||
| }, | ||
| // Convert WarpStorage to Map for serialization | ||
| HashMap::new | ||
| ); | ||
|
|
||
| public static final Codec<WorldData> WORLD_DATA = WorldData.CODEC; | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.