From 9bf193988f6eb2d6df524e503207ed343dad7b37 Mon Sep 17 00:00:00 2001 From: Felix Cravic Date: Wed, 27 May 2020 12:33:12 +0200 Subject: [PATCH] Added ItemStack potion nbt --- .../minestom/server/entity/EntityManager.java | 4 +- .../server/entity/type/EntityPotion.java | 51 +++++++++++++++++++ .../net/minestom/server/item/Enchantment.java | 4 -- .../net/minestom/server/item/ItemStack.java | 25 ++++++--- .../server/network/ConnectionManager.java | 2 +- .../java/net/minestom/server/utils/Utils.java | 19 ++++++- .../utils/{ => item}/NbtReaderUtils.java | 15 +++++- 7 files changed, 105 insertions(+), 15 deletions(-) create mode 100644 src/main/java/net/minestom/server/entity/type/EntityPotion.java rename src/main/java/net/minestom/server/utils/{ => item}/NbtReaderUtils.java (89%) diff --git a/src/main/java/net/minestom/server/entity/EntityManager.java b/src/main/java/net/minestom/server/entity/EntityManager.java index a3da70a1c..59626ab07 100644 --- a/src/main/java/net/minestom/server/entity/EntityManager.java +++ b/src/main/java/net/minestom/server/entity/EntityManager.java @@ -154,7 +154,9 @@ public class EntityManager { } } - // Add connected clients after the handshake (used to free the networking threads) + /** + * Add connected clients after the handshake (used to free the networking threads) + */ private void waitingPlayersTick() { Player waitingPlayer; while ((waitingPlayer = waitingPlayers.poll()) != null) { diff --git a/src/main/java/net/minestom/server/entity/type/EntityPotion.java b/src/main/java/net/minestom/server/entity/type/EntityPotion.java new file mode 100644 index 000000000..a3c631904 --- /dev/null +++ b/src/main/java/net/minestom/server/entity/type/EntityPotion.java @@ -0,0 +1,51 @@ +package net.minestom.server.entity.type; + +import net.minestom.server.entity.EntityType; +import net.minestom.server.entity.ObjectEntity; +import net.minestom.server.item.ItemStack; +import net.minestom.server.network.packet.PacketWriter; +import net.minestom.server.utils.Position; + +import java.util.function.Consumer; + +public class EntityPotion extends ObjectEntity { + + private ItemStack potion; + + public EntityPotion(Position spawnPosition, ItemStack potion) { + super(EntityType.POTION, spawnPosition); + setPotion(potion); + } + + @Override + public Consumer getMetadataConsumer() { + return packet -> { + super.getMetadataConsumer().accept(packet); + fillMetadataIndex(packet, 7); + }; + } + + @Override + protected void fillMetadataIndex(PacketWriter packet, int index) { + super.fillMetadataIndex(packet, index); + if (index == 7) { + packet.writeByte((byte) 7); + packet.writeByte(METADATA_SLOT); + packet.writeItemStack(potion); + } + } + + @Override + public int getObjectData() { + return 0; + } + + public ItemStack getPotion() { + return potion; + } + + public void setPotion(ItemStack potion) { + this.potion = potion; + sendMetadataIndex(15); + } +} diff --git a/src/main/java/net/minestom/server/item/Enchantment.java b/src/main/java/net/minestom/server/item/Enchantment.java index b09c96486..53a1caf92 100644 --- a/src/main/java/net/minestom/server/item/Enchantment.java +++ b/src/main/java/net/minestom/server/item/Enchantment.java @@ -60,8 +60,4 @@ public enum Enchantment { return id; } - public String toMinecraftNamespaceId() { - return "minecraft:" + name().toLowerCase(); - } - } diff --git a/src/main/java/net/minestom/server/item/ItemStack.java b/src/main/java/net/minestom/server/item/ItemStack.java index 8360d99f9..3748365b9 100644 --- a/src/main/java/net/minestom/server/item/ItemStack.java +++ b/src/main/java/net/minestom/server/item/ItemStack.java @@ -3,12 +3,15 @@ package net.minestom.server.item; import net.minestom.server.data.Data; import net.minestom.server.data.DataContainer; import net.minestom.server.item.rule.VanillaStackingRule; +import net.minestom.server.potion.PotionType; import net.minestom.server.utils.validate.Check; import java.util.*; public class ItemStack implements DataContainer { + private static final StackingRule DEFAULT_STACKING_RULE = new VanillaStackingRule(127); + public static ItemStack getAirItem() { return new ItemStack((short) 0, (byte) 0); } @@ -16,11 +19,7 @@ public class ItemStack implements DataContainer { private static StackingRule defaultStackingRule; private short materialId; - - { - if (defaultStackingRule == null) - defaultStackingRule = new VanillaStackingRule(127); - } + private Set potionTypes; private byte amount; private short damage; @@ -31,6 +30,11 @@ public class ItemStack implements DataContainer { private Map enchantmentMap; + { + if (defaultStackingRule == null) + defaultStackingRule = DEFAULT_STACKING_RULE; + } + private int hideFlag; private StackingRule stackingRule; @@ -43,6 +47,7 @@ public class ItemStack implements DataContainer { this.lore = new ArrayList<>(); this.enchantmentMap = new HashMap<>(); + this.potionTypes = new HashSet<>(); this.stackingRule = defaultStackingRule; } @@ -145,6 +150,14 @@ public class ItemStack implements DataContainer { return this.enchantmentMap.getOrDefault(enchantment, (short) 0); } + public Set getPotionTypes() { + return Collections.unmodifiableSet(potionTypes); + } + + public void addPotionType(PotionType potionType) { + this.potionTypes.add(potionType); + } + public int getHideFlag() { return hideFlag; } @@ -187,7 +200,7 @@ public class ItemStack implements DataContainer { } public boolean hasNbtTag() { - return hasDisplayName() || hasLore() || isUnbreakable() || !getEnchantmentMap().isEmpty(); + return hasDisplayName() || hasLore() || isUnbreakable() || !getEnchantmentMap().isEmpty() || !potionTypes.isEmpty(); } public ItemStack clone() { diff --git a/src/main/java/net/minestom/server/network/ConnectionManager.java b/src/main/java/net/minestom/server/network/ConnectionManager.java index f579e7d0d..8188ae006 100644 --- a/src/main/java/net/minestom/server/network/ConnectionManager.java +++ b/src/main/java/net/minestom/server/network/ConnectionManager.java @@ -60,7 +60,7 @@ public class ConnectionManager { } /** - * Shouldn't be override if not already defined + * Shouldn't be override if already defined * * @param uuidProvider the new player connection uuid provider */ diff --git a/src/main/java/net/minestom/server/utils/Utils.java b/src/main/java/net/minestom/server/utils/Utils.java index b509c53ae..11d9a64e6 100644 --- a/src/main/java/net/minestom/server/utils/Utils.java +++ b/src/main/java/net/minestom/server/utils/Utils.java @@ -7,10 +7,13 @@ import net.minestom.server.item.Enchantment; import net.minestom.server.item.ItemStack; import net.minestom.server.network.packet.PacketReader; import net.minestom.server.network.packet.PacketWriter; +import net.minestom.server.potion.PotionType; import net.minestom.server.utils.buffer.BufferWrapper; +import net.minestom.server.utils.item.NbtReaderUtils; import java.util.ArrayList; import java.util.Map; +import java.util.Set; public class Utils { @@ -171,7 +174,7 @@ public class Utils { packet.writeByte((byte) 0x08); // Type id (string) packet.writeShortSizedString("id"); - packet.writeShortSizedString(enchantment.toMinecraftNamespaceId()); + packet.writeShortSizedString("minecraft:" + enchantment.name().toLowerCase()); } @@ -181,6 +184,20 @@ public class Utils { } // End enchantment + // Start potion + { + Set potionTypes = itemStack.getPotionTypes(); + if (!potionTypes.isEmpty()) { + for (PotionType potionType : potionTypes) { + packet.writeByte((byte) 0x08); // type id (string) + packet.writeShortSizedString("Potion"); + packet.writeShortSizedString("minecraft:" + potionType.name().toLowerCase()); + + } + } + } + // End potion + // Start hide flags /*{ int hideFlag = itemStack.getHideFlag(); diff --git a/src/main/java/net/minestom/server/utils/NbtReaderUtils.java b/src/main/java/net/minestom/server/utils/item/NbtReaderUtils.java similarity index 89% rename from src/main/java/net/minestom/server/utils/NbtReaderUtils.java rename to src/main/java/net/minestom/server/utils/item/NbtReaderUtils.java index a0beca546..073645009 100644 --- a/src/main/java/net/minestom/server/utils/NbtReaderUtils.java +++ b/src/main/java/net/minestom/server/utils/item/NbtReaderUtils.java @@ -1,10 +1,11 @@ -package net.minestom.server.utils; +package net.minestom.server.utils.item; import net.kyori.text.Component; import net.minestom.server.chat.Chat; import net.minestom.server.item.Enchantment; import net.minestom.server.item.ItemStack; import net.minestom.server.network.packet.PacketReader; +import net.minestom.server.potion.PotionType; import java.util.ArrayList; @@ -14,7 +15,7 @@ public class NbtReaderUtils { byte typeId = reader.readByte(); - //System.out.println("DEBUG TYPE: " + typeId); + System.out.println("DEBUG TYPE: " + typeId); switch (typeId) { case 0x00: // TAG_End // End of item NBT @@ -63,7 +64,17 @@ public class NbtReaderUtils { break; case 0x08: // TAG_String + String stringName = reader.readShortSizedString(); + if (stringName.equals("Potion")) { + String potionId = reader.readShortSizedString(); + potionId = potionId.replace("minecraft:", "").toUpperCase(); + PotionType potionType = PotionType.valueOf(potionId); + + item.addPotionType(potionType); + + readItemStackNBT(reader, item); + } break; case 0x09: // TAG_List