Skip to content

Commit

Permalink
Fix attachment codec wrappers not using registry ops (#1165)
Browse files Browse the repository at this point in the history
  • Loading branch information
dhyces authored Jun 23, 2024
1 parent 6481868 commit 412c6ab
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -202,13 +202,13 @@ public Builder<T> serialize(Codec<T> codec, Predicate<? super T> shouldSerialize
return serialize(new IAttachmentSerializer<>() {
@Override
public T read(IAttachmentHolder holder, Tag tag, HolderLookup.Provider provider) {
return codec.parse(NbtOps.INSTANCE, tag).result().get();
return codec.parse(provider.createSerializationContext(NbtOps.INSTANCE), tag).result().get();
}

@Nullable
@Override
public Tag write(T attachment, HolderLookup.Provider provider) {
return shouldSerialize.test(attachment) ? codec.encodeStart(NbtOps.INSTANCE, attachment).result().get() : null;
return shouldSerialize.test(attachment) ? codec.encodeStart(provider.createSerializationContext(NbtOps.INSTANCE), attachment).result().get() : null;
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,16 @@
import net.minecraft.commands.Commands;
import net.minecraft.core.BlockPos;
import net.minecraft.core.HolderLookup;
import net.minecraft.core.component.DataComponents;
import net.minecraft.core.registries.Registries;
import net.minecraft.gametest.framework.GameTest;
import net.minecraft.nbt.IntTag;
import net.minecraft.network.chat.Component;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.enchantment.Enchantments;
import net.minecraft.world.item.enchantment.ItemEnchantments;
import net.minecraft.world.level.GameType;
import net.minecraft.world.level.chunk.LevelChunk;
import net.neoforged.neoforge.attachment.AttachmentType;
Expand Down Expand Up @@ -127,4 +133,24 @@ static void playerAttachmentCopyOnRespawn(DynamicTest test, RegistrationHelper r
helper.succeed();
});
}

@GameTest
@EmptyTemplate
@TestHolder(description = "Tests that attachments with dynamic data are de/serialized well")
static void dynamicDataContentSerialization(DynamicTest test, RegistrationHelper reg) {
var stackType = reg.attachments()
.register("stack", () -> AttachmentType.builder(() -> new ItemStack(Items.IRON_AXE)).serialize(ItemStack.CODEC).build());
test.onGameTest(helper -> {
var player = helper.makeMockPlayer();
var stack = new ItemStack(Items.IRON_SWORD);
var enchantments = new ItemEnchantments.Mutable(ItemEnchantments.EMPTY);
enchantments.set(helper.getLevel().registryAccess().registryOrThrow(Registries.ENCHANTMENT).getHolderOrThrow(Enchantments.SHARPNESS), 3);
stack.set(DataComponents.ENCHANTMENTS, enchantments.toImmutable());
player.setData(stackType, stack);
helper.catchException(() -> {
player.serializeAttachments(helper.getLevel().registryAccess()); // This will throw if it fails
});
helper.succeed();
});
}
}

0 comments on commit 412c6ab

Please sign in to comment.