From 3f1841b66949bb2e3c0ab40ce17aa50b27ae0c23 Mon Sep 17 00:00:00 2001 From: mworzala Date: Tue, 22 Oct 2024 22:09:59 -0400 Subject: [PATCH] fix: add new customName field to potion contents --- .../java/net/minestom/demo/PlayerInit.java | 2 +- .../minestom/server/item/ItemComponent.java | 1 + .../server/item/component/PotionContents.java | 56 +++++++++---------- .../item/component/PotionContentsTest.java | 2 +- 4 files changed, 31 insertions(+), 30 deletions(-) diff --git a/demo/src/main/java/net/minestom/demo/PlayerInit.java b/demo/src/main/java/net/minestom/demo/PlayerInit.java index 7b77abd76..d82c81a0b 100644 --- a/demo/src/main/java/net/minestom/demo/PlayerInit.java +++ b/demo/src/main/java/net/minestom/demo/PlayerInit.java @@ -147,7 +147,7 @@ public class PlayerInit { Enchantment.SHARPNESS, 10 ))) .build()); - +// player.getInventory().addItemStack(ItemStack.builder(Material.STONE_SWORD) .build()); diff --git a/src/main/java/net/minestom/server/item/ItemComponent.java b/src/main/java/net/minestom/server/item/ItemComponent.java index 02982ade9..4ecea7c68 100644 --- a/src/main/java/net/minestom/server/item/ItemComponent.java +++ b/src/main/java/net/minestom/server/item/ItemComponent.java @@ -95,6 +95,7 @@ public final class ItemComponent { public static final DataComponent> CONTAINER = register("container", ItemStack.NETWORK_TYPE.list(256), BinaryTagSerializer.ITEM.list()); public static final DataComponent BLOCK_STATE = register("block_state", ItemBlockState.NETWORK_TYPE, ItemBlockState.NBT_TYPE); public static final DataComponent> BEES = register("bees", Bee.NETWORK_TYPE.list(Short.MAX_VALUE), Bee.NBT_TYPE.list()); + // TODO(1.21.2) Updated NBT format > https://minecraft.wiki/w/Java_Edition_1.21.2#Data_components_3 public static final DataComponent LOCK = register("lock", null, BinaryTagSerializer.STRING); public static final DataComponent CONTAINER_LOOT = register("container_loot", null, SeededContainerLoot.NBT_TYPE); diff --git a/src/main/java/net/minestom/server/item/component/PotionContents.java b/src/main/java/net/minestom/server/item/component/PotionContents.java index 27679ef77..e4f45fa58 100644 --- a/src/main/java/net/minestom/server/item/component/PotionContents.java +++ b/src/main/java/net/minestom/server/item/component/PotionContents.java @@ -4,6 +4,7 @@ import net.kyori.adventure.nbt.*; import net.kyori.adventure.util.RGBLike; import net.minestom.server.color.Color; import net.minestom.server.network.NetworkBuffer; +import net.minestom.server.network.NetworkBufferTemplate; import net.minestom.server.potion.CustomPotionEffect; import net.minestom.server.potion.PotionType; import net.minestom.server.utils.nbt.BinaryTagSerializer; @@ -16,30 +17,17 @@ import java.util.List; public record PotionContents( @Nullable PotionType potion, @Nullable RGBLike customColor, - @NotNull List customEffects + @NotNull List customEffects, + @Nullable String customName ) { - public static final int POTION_DRINK_TIME = 32; // 32 ticks, in ms - public static final PotionContents EMPTY = new PotionContents(null, null, List.of()); + public static final PotionContents EMPTY = new PotionContents(null, null, List.of(), null); - public static final NetworkBuffer.Type NETWORK_TYPE = new NetworkBuffer.Type<>() { - @Override - public void write(@NotNull NetworkBuffer buffer, PotionContents value) { - Integer typeId = value.potion == null ? null : value.potion.id(); - buffer.write(NetworkBuffer.VAR_INT.optional(), typeId); - buffer.write(Color.NETWORK_TYPE.optional(), value.customColor); - buffer.write(CustomPotionEffect.NETWORK_TYPE.list(), value.customEffects); - } - - @Override - public PotionContents read(@NotNull NetworkBuffer buffer) { - Integer typeId = buffer.read(NetworkBuffer.VAR_INT.optional()); - return new PotionContents( - typeId == null ? null : PotionType.fromId(typeId), - buffer.read(Color.NETWORK_TYPE.optional()), - buffer.read(CustomPotionEffect.NETWORK_TYPE.list(Short.MAX_VALUE)) - ); - } - }; + public static final NetworkBuffer.Type NETWORK_TYPE = NetworkBufferTemplate.template( + PotionType.NETWORK_TYPE.optional(), PotionContents::potion, + Color.NETWORK_TYPE.optional(), PotionContents::customColor, + CustomPotionEffect.NETWORK_TYPE.list(Short.MAX_VALUE), PotionContents::customEffects, + NetworkBuffer.STRING.optional(), PotionContents::customName, + PotionContents::new); public static final BinaryTagSerializer NBT_TYPE = new BinaryTagSerializer<>() { @Override @@ -62,6 +50,10 @@ public record PotionContents( builder.put("custom_effects", effectsBuilder.build()); } + if (value.customName != null) { + builder.putString("custom_name", value.customName); + } + return builder.build(); } @@ -69,7 +61,7 @@ public record PotionContents( public @NotNull PotionContents read(@NotNull BinaryTag tag) { // Can be a string with just a potion effect id if (tag instanceof StringBinaryTag string) { - return new PotionContents(PotionType.fromNamespaceId(string.value()), null, List.of()); + return new PotionContents(PotionType.fromNamespaceId(string.value()), null, List.of(), null); } // Otherwise must be a compound @@ -95,7 +87,11 @@ public record PotionContents( customEffects.add(CustomPotionEffect.NBT_TYPE.read(customEffectCompound)); } - return new PotionContents(potion, customColor, customEffects); + String customName = null; + if (compound.get("custom_name") instanceof StringBinaryTag customNameTag) + customName = customNameTag.value(); + + return new PotionContents(potion, customColor, customEffects, customName); } }; @@ -104,19 +100,23 @@ public record PotionContents( } public PotionContents(@NotNull PotionType potion) { - this(potion, null, List.of()); + this(potion, null, List.of(), null); } public PotionContents(@NotNull PotionType potion, @NotNull RGBLike customColor) { - this(potion, customColor, List.of()); + this(potion, customColor, List.of(), null); } public PotionContents(@NotNull List customEffects) { - this(null, null, customEffects); + this(null, null, customEffects, null); } public PotionContents(@NotNull CustomPotionEffect customEffect) { - this(null, null, List.of(customEffect)); + this(null, null, List.of(customEffect), null); + } + + public PotionContents(@Nullable PotionType potion, @Nullable RGBLike customColor, @NotNull List customEffects) { + this(potion, customColor, customEffects, null); } } diff --git a/src/test/java/net/minestom/server/item/component/PotionContentsTest.java b/src/test/java/net/minestom/server/item/component/PotionContentsTest.java index 4913fb118..3fedc7cfe 100644 --- a/src/test/java/net/minestom/server/item/component/PotionContentsTest.java +++ b/src/test/java/net/minestom/server/item/component/PotionContentsTest.java @@ -43,7 +43,7 @@ public class PotionContentsTest extends AbstractItemComponentTest