From 0cea703b465daba3714011fc1fa7b86435581601 Mon Sep 17 00:00:00 2001 From: DoctorMacc Date: Tue, 7 Jul 2020 16:40:19 -0400 Subject: [PATCH] Save ItemEntry classes for items --- .../org/geysermc/connector/entity/Entity.java | 6 +-- .../entity/living/monster/PiglinEntity.java | 2 +- .../bedrock/BedrockInteractTranslator.java | 2 +- ...BedrockInventoryTransactionTranslator.java | 4 +- .../network/translators/item/ItemEntry.java | 2 + .../translators/item/ItemRegistry.java | 37 +++++++++---------- 6 files changed, 27 insertions(+), 26 deletions(-) diff --git a/connector/src/main/java/org/geysermc/connector/entity/Entity.java b/connector/src/main/java/org/geysermc/connector/entity/Entity.java index ffe13a50..1618d6af 100644 --- a/connector/src/main/java/org/geysermc/connector/entity/Entity.java +++ b/connector/src/main/java/org/geysermc/connector/entity/Entity.java @@ -284,11 +284,11 @@ public class Entity { // Shield code if (session.getPlayerEntity().getEntityId() == entityId && metadata.getFlags().getFlag(EntityFlag.SNEAKING)) { - if ((session.getInventory().getItemInHand() != null && session.getInventory().getItemInHand().getId() == ItemRegistry.SHIELD) || - (session.getInventoryCache().getPlayerInventory().getItem(45) != null && session.getInventoryCache().getPlayerInventory().getItem(45).getId() == ItemRegistry.SHIELD)) { + if ((session.getInventory().getItemInHand() != null && session.getInventory().getItemInHand().getId() == ItemRegistry.SHIELD.getJavaId()) || + (session.getInventoryCache().getPlayerInventory().getItem(45) != null && session.getInventoryCache().getPlayerInventory().getItem(45).getId() == ItemRegistry.SHIELD.getJavaId())) { ClientPlayerUseItemPacket useItemPacket; metadata.getFlags().setFlag(EntityFlag.BLOCKING, true); - if (session.getInventory().getItemInHand() != null && session.getInventory().getItemInHand().getId() == ItemRegistry.SHIELD) { + if (session.getInventory().getItemInHand() != null && session.getInventory().getItemInHand().getId() == ItemRegistry.SHIELD.getJavaId()) { useItemPacket = new ClientPlayerUseItemPacket(Hand.MAIN_HAND); } // Else we just assume it's the offhand, to simplify logic and to assure the packet gets sent diff --git a/connector/src/main/java/org/geysermc/connector/entity/living/monster/PiglinEntity.java b/connector/src/main/java/org/geysermc/connector/entity/living/monster/PiglinEntity.java index ae9319ef..83027e30 100644 --- a/connector/src/main/java/org/geysermc/connector/entity/living/monster/PiglinEntity.java +++ b/connector/src/main/java/org/geysermc/connector/entity/living/monster/PiglinEntity.java @@ -61,7 +61,7 @@ public class PiglinEntity extends MonsterEntity { @Override public void updateEquipment(GeyserSession session) { // Check if the Piglin is holding Gold and set the ADMIRING flag accordingly - metadata.getFlags().setFlag(EntityFlag.ADMIRING, offHand.getId() == ItemRegistry.GOLD); + metadata.getFlags().setFlag(EntityFlag.ADMIRING, offHand.getId() == ItemRegistry.GOLD.getBedrockId()); super.updateBedrockMetadata(session); super.updateEquipment(session); diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/bedrock/BedrockInteractTranslator.java b/connector/src/main/java/org/geysermc/connector/network/translators/bedrock/BedrockInteractTranslator.java index 16707327..7d03f983 100644 --- a/connector/src/main/java/org/geysermc/connector/network/translators/bedrock/BedrockInteractTranslator.java +++ b/connector/src/main/java/org/geysermc/connector/network/translators/bedrock/BedrockInteractTranslator.java @@ -60,7 +60,7 @@ public class BedrockInteractTranslator extends PacketTranslator switch (packet.getAction()) { case INTERACT: - if (session.getInventory().getItem(session.getInventory().getHeldItemSlot() + 36).getId() == ItemRegistry.SHIELD) { + if (session.getInventory().getItem(session.getInventory().getHeldItemSlot() + 36).getId() == ItemRegistry.SHIELD.getJavaId()) { break; } ClientPlayerInteractEntityPacket interactPacket = new ClientPlayerInteractEntityPacket((int) entity.getEntityId(), diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/bedrock/BedrockInventoryTransactionTranslator.java b/connector/src/main/java/org/geysermc/connector/network/translators/bedrock/BedrockInventoryTransactionTranslator.java index 25e1c54e..d4168709 100644 --- a/connector/src/main/java/org/geysermc/connector/network/translators/bedrock/BedrockInventoryTransactionTranslator.java +++ b/connector/src/main/java/org/geysermc/connector/network/translators/bedrock/BedrockInventoryTransactionTranslator.java @@ -99,7 +99,7 @@ public class BedrockInventoryTransactionTranslator extends PacketTranslator ITEM_ENTRIES = new Int2ObjectOpenHashMap<>(); // Shield ID, used in Entity.java - public static int SHIELD = 0; + public static ItemEntry SHIELD; // Boat ID, used in BedrockInventoryTransactionTranslator.java - public static int BOAT = 0; + public static ItemEntry BOAT; // Gold ID, used in PiglinEntity.java - public static int GOLD = 0; + public static ItemEntry GOLD; public static int BARRIER_INDEX = 0; @@ -85,20 +85,6 @@ public class ItemRegistry { for (JsonNode entry : itemEntries) { ITEMS.add(new StartGamePacket.ItemEntry(entry.get("name").textValue(), (short) entry.get("id").intValue())); - - switch (entry.get("name").textValue()) { - case "minecraft:shield": - SHIELD = entry.get("id").intValue(); - break; - case "minecraft:boat": - BOAT = entry.get("id").intValue(); - break; - case "minecraft:gold_ingot": - GOLD = entry.get("id").intValue(); - break; - default: - break; - } } stream = FileUtils.getResource("mappings/items.json"); @@ -139,8 +125,21 @@ public class ItemRegistry { entry.getValue().get("bedrock_data").intValue(), entry.getValue().get("is_block") != null && entry.getValue().get("is_block").booleanValue())); } - if (entry.getKey().equals("minecraft:barrier")) { - BARRIER_INDEX = itemIndex; + switch (entry.getKey()) { + case "minecraft:barrier": + BARRIER_INDEX = itemIndex; + break; + case "minecraft:oak_boat": + BOAT = ITEM_ENTRIES.get(itemIndex); + break; + case "minecraft:gold_ingot": + GOLD = ITEM_ENTRIES.get(itemIndex); + break; + case "minecraft:shield": + SHIELD = ITEM_ENTRIES.get(itemIndex); + break; + default: + break; } itemIndex++;