Posted October 29, 20205 yr Howdy folks While converting particles I've run into the Minecraft Codec. It appears to be a convenient way to serialise and deserialise objects without having to hard code a readNBT() and writeNBT()? But I don't understand why Particles have both Codec and DESERIALIZER as well, whereas in 1.15.3 there was no Codec. Likewise, ItemStack has both a Codec and write(CompoundNBT), but they are different- capability info in NBT but not in Codec. And the Codec appears to write .tag but to construct with .capNBT Is this just a work-in-progress thing? (i.e. eventually Codecs will replace NBT?) Or is there some difference in usage I'm not seeing? The Codecs are bristling with so many lambdas and wrappers I haven't been able to track down where they are actually used / what they're actually used for. Google has been surprisingly silent on the topic. Cheers TGG public static final Codec<ItemStack> field_234691_a_ = RecordCodecBuilder.create((p_234698_0_) -> { return p_234698_0_.group(Registry.ITEM.fieldOf("id").forGetter((p_234706_0_) -> { return p_234706_0_.item; }), Codec.INT.fieldOf("Count").forGetter((p_234705_0_) -> { return p_234705_0_.count; }), CompoundNBT.field_240597_a_.optionalFieldOf("tag").forGetter((p_234704_0_) -> { return Optional.ofNullable(p_234704_0_.tag); })).apply(p_234698_0_, ItemStack::new); }); public ItemStack(IItemProvider itemIn, int count, @Nullable CompoundNBT capNBT) { // doesn't appear to properly match the Codec? i.e. tag vs capNBT? ... etc ... } /** * Write the stack fields to a NBT object. Return the new NBT object. */ public CompoundNBT write(CompoundNBT nbt) { ResourceLocation resourcelocation = Registry.ITEM.getKey(this.getItem()); nbt.putString("id", resourcelocation == null ? "minecraft:air" : resourcelocation.toString()); nbt.putByte("Count", (byte)this.count); if (this.tag != null) { nbt.put("tag", this.tag.copy()); } CompoundNBT cnbt = this.serializeCaps(); if (cnbt != null && !cnbt.isEmpty()) { nbt.put("ForgeCaps", cnbt); } return nbt; }
October 29, 20205 yr 7 hours ago, TheGreyGhost said: Is this just a work-in-progress thing? (i.e. eventually Codecs will replace NBT?) Or is there some difference in usage I'm not seeing? The Codecs are bristling with so many lambdas and wrappers I haven't been able to track down where they are actually used / what they're actually used for. Google has been surprisingly silent on the topic. From my (limited) understanding, Codec is not a replacement for NBT, but rather a new system for coding and decoding NBT (and other formats, such as json) via an appropriate instance of DynamicOps. For example, a Codec is used in CompassItem to save/load a RegistryKey<World> to/from NBT. The new Codec system seems to be heavily used in the new data driven dimension and world gen system.
October 30, 20205 yr Author Ah, that makes more sense. I guess that will allow the serialisation of object instances to be coded in a much more high-level (and error-proof) way, rather than hard-coding (eg nbtCompound.putInt(xxx) etc) for each type of serialisation you want. Sounds like a good idea to me.. It looks like they have written a platform for it and are just dipping their toe in the water for refactoring the existing code, which explains why some of the defined codecs don't appear to be used anywhere yet. thx for the tip TGG
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.