From 4a40805ca0c4bd0ad2a706679b8dd55723c338a2 Mon Sep 17 00:00:00 2001 From: TheMode Date: Thu, 30 Jun 2022 23:21:32 +0200 Subject: [PATCH] Fix attribute nbt Signed-off-by: TheMode --- .../minestom/server/item/ItemSerializers.java | 7 +-- .../server/item/ItemAttributeTest.java | 49 +++++++++++++++++++ 2 files changed, 53 insertions(+), 3 deletions(-) create mode 100644 src/test/java/net/minestom/server/item/ItemAttributeTest.java diff --git a/src/main/java/net/minestom/server/item/ItemSerializers.java b/src/main/java/net/minestom/server/item/ItemSerializers.java index be914cc2b..51ce819a2 100644 --- a/src/main/java/net/minestom/server/item/ItemSerializers.java +++ b/src/main/java/net/minestom/server/item/ItemSerializers.java @@ -12,6 +12,7 @@ import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import java.util.Locale; import java.util.UUID; @ApiStatus.Internal @@ -42,7 +43,7 @@ public final class ItemSerializers { static final TagSerializer ATTRIBUTE_SERIALIZER = new TagSerializer<>() { static final Tag ID = Tag.UUID("UUID"); static final Tag AMOUNT = Tag.Double("Amount"); - static final Tag SLOT = Tag.String("Slot").defaultValue("MAINHAND"); + static final Tag SLOT = Tag.String("Slot").defaultValue("mainhand"); static final Tag ATTRIBUTE_NAME = Tag.String("AttributeName"); static final Tag OPERATION = Tag.Integer("Operation"); static final Tag NAME = Tag.String("Name"); @@ -56,7 +57,7 @@ public final class ItemSerializers { final int operation = reader.getTag(OPERATION); final String name = reader.getTag(NAME); - final Attribute attribute = Attribute.fromKey(attributeName); + final Attribute attribute = Attribute.fromKey(attributeName.toUpperCase(Locale.ROOT)); // Wrong attribute name, stop here if (attribute == null) return null; final AttributeOperation attributeOperation = AttributeOperation.fromId(operation); @@ -77,7 +78,7 @@ public final class ItemSerializers { public void write(@NotNull TagWritable writer, @NotNull ItemAttribute value) { writer.setTag(ID, value.uuid()); writer.setTag(AMOUNT, value.amount()); - writer.setTag(SLOT, value.slot().name()); + writer.setTag(SLOT, value.slot().name().toLowerCase(Locale.ROOT)); writer.setTag(ATTRIBUTE_NAME, value.attribute().key()); writer.setTag(OPERATION, value.operation().getId()); writer.setTag(NAME, value.name()); diff --git a/src/test/java/net/minestom/server/item/ItemAttributeTest.java b/src/test/java/net/minestom/server/item/ItemAttributeTest.java new file mode 100644 index 000000000..991e892b6 --- /dev/null +++ b/src/test/java/net/minestom/server/item/ItemAttributeTest.java @@ -0,0 +1,49 @@ +package net.minestom.server.item; + +import net.minestom.server.attribute.Attribute; +import net.minestom.server.attribute.AttributeOperation; +import net.minestom.server.item.attribute.AttributeSlot; +import net.minestom.server.item.attribute.ItemAttribute; +import org.junit.jupiter.api.Test; + +import java.util.List; +import java.util.UUID; + +import static net.minestom.server.api.TestUtils.assertEqualsSNBT; +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class ItemAttributeTest { + + @Test + public void attribute() { + var attributes = List.of(new ItemAttribute( + new UUID(0, 0), "generic.attack_damage", Attribute.ATTACK_DAMAGE, + AttributeOperation.ADDITION, 2, AttributeSlot.MAINHAND)); + var item = ItemStack.builder(Material.STICK) + .meta(builder -> builder.attributes(attributes)) + .build(); + assertEquals(attributes, item.meta().getAttributes()); + } + + @Test + public void attributeNbt() { + var item = ItemStack.builder(Material.STICK) + .meta(builder -> builder.attributes( + List.of(new ItemAttribute( + new UUID(0, 0), "generic.attack_damage", Attribute.ATTACK_DAMAGE, + AttributeOperation.ADDITION, 2, AttributeSlot.MAINHAND)))) + .build(); + assertEqualsSNBT(""" + {"AttributeModifiers":[ + { + "Amount":2.0D, + "UUID":[I;0,0,0,0], + "Slot":"mainhand", + "Operation":0, + "AttributeName":"generic.attack_damage", + "Name":"generic.attack_damage" + } + ]} + """, item.meta().toNBT()); + } +}