Fix attribute nbt

Signed-off-by: TheMode <themode@outlook.fr>
This commit is contained in:
TheMode 2022-06-30 23:21:32 +02:00
parent 9a28e6ba87
commit 4a40805ca0
2 changed files with 53 additions and 3 deletions

View File

@ -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<ItemAttribute> ATTRIBUTE_SERIALIZER = new TagSerializer<>() {
static final Tag<UUID> ID = Tag.UUID("UUID");
static final Tag<Double> AMOUNT = Tag.Double("Amount");
static final Tag<String> SLOT = Tag.String("Slot").defaultValue("MAINHAND");
static final Tag<String> SLOT = Tag.String("Slot").defaultValue("mainhand");
static final Tag<String> ATTRIBUTE_NAME = Tag.String("AttributeName");
static final Tag<Integer> OPERATION = Tag.Integer("Operation");
static final Tag<String> 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());

View File

@ -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());
}
}