From 3c1a40c56af6a708b48c8077a90379176b6f876d Mon Sep 17 00:00:00 2001 From: Camotoy <20743703+Camotoy@users.noreply.github.com> Date: Tue, 29 Dec 2020 19:59:22 -0500 Subject: [PATCH] Better net ID handling --- .../org/geysermc/connector/entity/Entity.java | 6 +-- .../connector/inventory/Container.java | 7 ++-- .../connector/inventory/GeyserItemStack.java | 37 +++++++++++-------- .../connector/inventory/Inventory.java | 13 ++++++- ...BedrockInventoryTransactionTranslator.java | 2 +- .../entity/BedrockEntityEventTranslator.java | 2 +- .../player/BedrockInteractTranslator.java | 2 +- .../inventory/InventoryTranslator.java | 36 +++++++++--------- .../inventory/click/ClickPlan.java | 2 +- .../translators/LoomInventoryTranslator.java | 2 +- .../StonecutterInventoryTranslator.java | 6 +-- .../java/window/JavaSetSlotTranslator.java | 10 +---- .../window/JavaWindowItemsTranslator.java | 8 +--- .../connector/utils/InventoryUtils.java | 2 +- 14 files changed, 68 insertions(+), 67 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 68b2b266..921d5e14 100644 --- a/connector/src/main/java/org/geysermc/connector/entity/Entity.java +++ b/connector/src/main/java/org/geysermc/connector/entity/Entity.java @@ -286,11 +286,11 @@ public class Entity { // Shield code if (session.getPlayerEntity().getEntityId() == entityId && metadata.getFlags().getFlag(EntityFlag.SNEAKING)) { PlayerInventory playerInv = session.getPlayerInventory(); - if ((playerInv.getItemInHand().getId() == ItemRegistry.SHIELD.getJavaId()) || - (playerInv.getOffhand().getId() == ItemRegistry.SHIELD.getJavaId())) { + if ((playerInv.getItemInHand().getJavaId() == ItemRegistry.SHIELD.getJavaId()) || + (playerInv.getOffhand().getJavaId() == ItemRegistry.SHIELD.getJavaId())) { ClientPlayerUseItemPacket useItemPacket; metadata.getFlags().setFlag(EntityFlag.BLOCKING, true); - if (playerInv.getItemInHand().getId() == ItemRegistry.SHIELD.getJavaId()) { + if (playerInv.getItemInHand().getJavaId() == 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/inventory/Container.java b/connector/src/main/java/org/geysermc/connector/inventory/Container.java index eb1d9c3d..9768520b 100644 --- a/connector/src/main/java/org/geysermc/connector/inventory/Container.java +++ b/connector/src/main/java/org/geysermc/connector/inventory/Container.java @@ -27,6 +27,7 @@ package org.geysermc.connector.inventory; import lombok.Getter; import lombok.NonNull; +import org.geysermc.connector.network.session.GeyserSession; import org.geysermc.connector.network.translators.inventory.InventoryTranslator; /** @@ -53,11 +54,11 @@ public class Container extends Inventory { } @Override - public void setItem(int slot, @NonNull GeyserItemStack item) { + public void setItem(int slot, @NonNull GeyserItemStack newItem, GeyserSession session) { if (slot < this.size) { - super.setItem(slot, item); + super.setItem(slot, newItem, session); } else { - playerInventory.setItem(slot - this.size + InventoryTranslator.PLAYER_INVENTORY_OFFSET, item); + playerInventory.setItem(slot - this.size + InventoryTranslator.PLAYER_INVENTORY_OFFSET, newItem, session); } } diff --git a/connector/src/main/java/org/geysermc/connector/inventory/GeyserItemStack.java b/connector/src/main/java/org/geysermc/connector/inventory/GeyserItemStack.java index c935fcdb..46cf8529 100644 --- a/connector/src/main/java/org/geysermc/connector/inventory/GeyserItemStack.java +++ b/connector/src/main/java/org/geysermc/connector/inventory/GeyserItemStack.java @@ -39,32 +39,34 @@ import org.geysermc.connector.network.translators.item.ItemTranslator; public class GeyserItemStack { public static final GeyserItemStack EMPTY = new GeyserItemStack(0, 0, null); - private final int id; + private final int javaId; private int amount; private CompoundTag nbt; private int netId; + private boolean netIdWasUpdated; - public GeyserItemStack(int id) { - this(id, 1); + public GeyserItemStack(int javaId) { + this(javaId, 1); } - public GeyserItemStack(int id, int amount) { - this(id, amount, null); + public GeyserItemStack(int javaId, int amount) { + this(javaId, amount, null); } - public GeyserItemStack(int id, int amount, CompoundTag nbt) { - this(id, amount, nbt, 1); + public GeyserItemStack(int javaId, int amount, CompoundTag nbt) { + this(javaId, amount, nbt, 1); } - public GeyserItemStack(int id, int amount, CompoundTag nbt, int netId) { - this.id = id; + public GeyserItemStack(int javaId, int amount, CompoundTag nbt, int netId) { + this.javaId = javaId; this.amount = amount; this.nbt = nbt; this.netId = netId; + this.netIdWasUpdated = !this.isEmpty(); } - public int getId() { - return isEmpty() ? 0 : id; + public int getJavaId() { + return isEmpty() ? 0 : javaId; } public int getAmount() { @@ -75,6 +77,11 @@ public class GeyserItemStack { return isEmpty() ? null : nbt; } + public void setNetId(int netId) { + this.netId = netId; + this.netIdWasUpdated = true; + } + public int getNetId() { return isEmpty() ? 0 : netId; } @@ -96,7 +103,7 @@ public class GeyserItemStack { } public ItemStack getItemStack() { - return isEmpty() ? null : new ItemStack(id, amount, nbt); + return isEmpty() ? null : new ItemStack(javaId, amount, nbt); } public ItemData getItemData(GeyserSession session) { @@ -106,11 +113,11 @@ public class GeyserItemStack { } public ItemEntry getItemEntry() { - return ItemRegistry.ITEM_ENTRIES.get(getId()); + return ItemRegistry.ITEM_ENTRIES.get(getJavaId()); } public boolean isEmpty() { - return amount <= 0 || id == 0; + return amount <= 0 || javaId == 0; } public GeyserItemStack copy() { @@ -118,6 +125,6 @@ public class GeyserItemStack { } public GeyserItemStack copy(int newAmount) { - return isEmpty() ? EMPTY : new GeyserItemStack(id, newAmount, nbt == null ? null : nbt.clone(), netId); + return isEmpty() ? EMPTY : new GeyserItemStack(javaId, newAmount, nbt == null ? null : nbt.clone(), netId); } } diff --git a/connector/src/main/java/org/geysermc/connector/inventory/Inventory.java b/connector/src/main/java/org/geysermc/connector/inventory/Inventory.java index 14d80c38..53d0905c 100644 --- a/connector/src/main/java/org/geysermc/connector/inventory/Inventory.java +++ b/connector/src/main/java/org/geysermc/connector/inventory/Inventory.java @@ -29,6 +29,7 @@ import com.nukkitx.math.vector.Vector3i; import lombok.Getter; import lombok.NonNull; import lombok.Setter; +import org.geysermc.connector.network.session.GeyserSession; import java.util.Arrays; @@ -76,8 +77,16 @@ public class Inventory { return items[slot]; } - public void setItem(int slot, @NonNull GeyserItemStack item) { - items[slot] = item; + public void setItem(int slot, @NonNull GeyserItemStack newItem, GeyserSession session) { + GeyserItemStack oldItem = items[slot]; + if (!newItem.isEmpty()) { + if (newItem.getItemData(session).equals(oldItem.getItemData(session), false, false, false)) { + newItem.setNetId(oldItem.getNetId()); + } else { + newItem.setNetId(session.getNextItemNetId()); + } + } + items[slot] = newItem; } public short getNextTransactionId() { 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 54221b4a..9e71455c 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 @@ -186,7 +186,7 @@ public class BedrockInventoryTransactionTranslator extends PacketTranslator= 0 && packet.getData() < trades.length) { VillagerTrade trade = merchantInventory.getVillagerTrades()[packet.getData()]; - openInventory.setItem(2, GeyserItemStack.from(trade.getOutput())); + openInventory.setItem(2, GeyserItemStack.from(trade.getOutput()), session); villager.getMetadata().put(EntityData.TRADE_XP, trade.getXp() + villager.getMetadata().getInt(EntityData.TRADE_XP)); villager.updateBedrockMetadata(session); } diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/bedrock/entity/player/BedrockInteractTranslator.java b/connector/src/main/java/org/geysermc/connector/network/translators/bedrock/entity/player/BedrockInteractTranslator.java index 86f30c2f..0c694fb9 100644 --- a/connector/src/main/java/org/geysermc/connector/network/translators/bedrock/entity/player/BedrockInteractTranslator.java +++ b/connector/src/main/java/org/geysermc/connector/network/translators/bedrock/entity/player/BedrockInteractTranslator.java @@ -100,7 +100,7 @@ public class BedrockInteractTranslator extends PacketTranslator switch (packet.getAction()) { case INTERACT: - if (session.getPlayerInventory().getItemInHand().getId() == ItemRegistry.SHIELD.getJavaId()) { + if (session.getPlayerInventory().getItemInHand().getJavaId() == ItemRegistry.SHIELD.getJavaId()) { break; } ClientPlayerInteractEntityPacket interactPacket = new ClientPlayerInteractEntityPacket((int) entity.getEntityId(), diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/inventory/InventoryTranslator.java b/connector/src/main/java/org/geysermc/connector/network/translators/inventory/InventoryTranslator.java index ef052409..7e626955 100644 --- a/connector/src/main/java/org/geysermc/connector/network/translators/inventory/InventoryTranslator.java +++ b/connector/src/main/java/org/geysermc/connector/network/translators/inventory/InventoryTranslator.java @@ -213,7 +213,7 @@ public abstract class InventoryTranslator { GeyserItemStack newItem = sourceItem.copy(); if (sourceIsCursor) { GeyserItemStack destItem = inventory.getItem(destSlot); - if (destItem.getId() == sourceItem.getId()) { + if (destItem.getJavaId() == sourceItem.getJavaId()) { // Combining items int itemsLeftOver = destItem.getAmount() + transferAction.getCount(); if (itemsLeftOver > MAX_ITEM_STACK_SIZE) { @@ -235,7 +235,7 @@ public abstract class InventoryTranslator { } } else { // Delete the source since we're moving it - inventory.setItem(sourceSlot, GeyserItemStack.EMPTY); + inventory.setItem(sourceSlot, GeyserItemStack.EMPTY, session); ClientCreativeInventoryActionPacket creativeActionPacket = new ClientCreativeInventoryActionPacket( sourceSlot, new ItemStack(0) @@ -252,13 +252,13 @@ public abstract class InventoryTranslator { if (sourceIsCursor) { session.getPlayerInventory().setCursor(GeyserItemStack.EMPTY); } else { - inventory.setItem(sourceSlot, GeyserItemStack.EMPTY); + inventory.setItem(sourceSlot, GeyserItemStack.EMPTY, session); } } if (destIsCursor) { session.getPlayerInventory().setCursor(newItem); } else { - inventory.setItem(destSlot, newItem); + inventory.setItem(destSlot, newItem, session); } GeyserItemStack itemToUpdate = destIsCursor ? sourceItem : newItem; // The Java server doesn't care about what's in the mouse in creative mode, so we just need to track @@ -386,7 +386,7 @@ public abstract class InventoryTranslator { ); System.out.println(creativeActionPacket); session.sendDownstreamPacket(creativeActionPacket); - inventory.setItem(sourceSlot, oldDestinationItem); + inventory.setItem(sourceSlot, oldDestinationItem, session); } if (isCursor(swapAction.getDestination())) { session.getPlayerInventory().setCursor(oldSourceItem); @@ -397,7 +397,7 @@ public abstract class InventoryTranslator { ); System.out.println(creativeActionPacket); session.sendDownstreamPacket(creativeActionPacket); - inventory.setItem(destSlot, oldSourceItem); + inventory.setItem(destSlot, oldSourceItem, session); } } else if (isCursor(swapAction.getSource()) && isCursor(swapAction.getDestination())) { //??? @@ -499,7 +499,7 @@ public abstract class InventoryTranslator { ); session.sendDownstreamPacket(destroyItemPacket); System.out.println(destroyItemPacket); - inventory.setItem(javaSlot, GeyserItemStack.EMPTY); + inventory.setItem(javaSlot, GeyserItemStack.EMPTY, session); affectedSlots.add(javaSlot); } else { // Just sync up the item on our end, since the server doesn't care what's in our cursor @@ -522,7 +522,7 @@ public abstract class InventoryTranslator { GeyserItemStack item = inventory.getItem(sourceSlot); item.setAmount(item.getAmount() - consumeData.getCount()); if (item.isEmpty()) { - inventory.setItem(sourceSlot, GeyserItemStack.EMPTY); + inventory.setItem(sourceSlot, GeyserItemStack.EMPTY, session); } affectedSlots.add(sourceSlot); } @@ -718,16 +718,16 @@ public abstract class InventoryTranslator { session.getPlayerInventory().setCursor(GeyserItemStack.from(javaCreativeItem, session.getNextItemNetId())); return acceptRequest(request, Collections.singletonList( new ItemStackResponsePacket.ContainerEntry(ContainerSlotType.CURSOR, - Collections.singletonList(makeItemEntry(session, 0, session.getPlayerInventory().getCursor()))))); + Collections.singletonList(makeItemEntry(0, session.getPlayerInventory().getCursor()))))); } else { int javaSlot = bedrockSlotToJava(transferAction.getDestination()); GeyserItemStack existingItem = inventory.getItem(javaSlot); - if (existingItem.getId() == javaCreativeItem.getId()) { + if (existingItem.getJavaId() == javaCreativeItem.getId()) { // Adding more to an existing item existingItem.setAmount(existingItem.getAmount() + transferAction.getCount()); javaCreativeItem = existingItem.getItemStack(); } else { - inventory.setItem(javaSlot, GeyserItemStack.from(javaCreativeItem, session.getNextItemNetId())); + inventory.setItem(javaSlot, GeyserItemStack.from(javaCreativeItem, session.getNextItemNetId()), session); } ClientCreativeInventoryActionPacket creativeActionPacket = new ClientCreativeInventoryActionPacket( javaSlot, @@ -765,8 +765,8 @@ public abstract class InventoryTranslator { public boolean checkNetId(GeyserSession session, Inventory inventory, StackRequestSlotInfoData slotInfoData) { if (slotInfoData.getStackNetworkId() < 0) return true; - if (slotInfoData.getContainer() == ContainerSlotType.CURSOR) //TODO: temporary - return true; +// if (slotInfoData.getContainer() == ContainerSlotType.CURSOR) //TODO: temporary +// return true; GeyserItemStack currentItem = isCursor(slotInfoData) ? session.getPlayerInventory().getCursor() : inventory.getItem(bedrockSlotToJava(slotInfoData)); return currentItem.getNetId() == slotInfoData.getStackNetworkId(); @@ -826,7 +826,7 @@ public abstract class InventoryTranslator { for (int slot : affectedSlots) { BedrockContainerSlot bedrockSlot = javaSlotToBedrockContainer(slot); List list = containerMap.computeIfAbsent(bedrockSlot.getContainer(), k -> new ArrayList<>()); - list.add(makeItemEntry(session, bedrockSlot.getSlot(), inventory.getItem(slot))); + list.add(makeItemEntry(bedrockSlot.getSlot(), inventory.getItem(slot))); } List containerEntries = new ArrayList<>(); @@ -834,18 +834,16 @@ public abstract class InventoryTranslator { containerEntries.add(new ItemStackResponsePacket.ContainerEntry(entry.getKey(), entry.getValue())); } - ItemStackResponsePacket.ItemEntry cursorEntry = makeItemEntry(session, 0, session.getPlayerInventory().getCursor()); + ItemStackResponsePacket.ItemEntry cursorEntry = makeItemEntry(0, session.getPlayerInventory().getCursor()); containerEntries.add(new ItemStackResponsePacket.ContainerEntry(ContainerSlotType.CURSOR, Collections.singletonList(cursorEntry))); return containerEntries; } - public static ItemStackResponsePacket.ItemEntry makeItemEntry(GeyserSession session, int bedrockSlot, GeyserItemStack itemStack) { + public static ItemStackResponsePacket.ItemEntry makeItemEntry(int bedrockSlot, GeyserItemStack itemStack) { ItemStackResponsePacket.ItemEntry itemEntry; if (!itemStack.isEmpty()) { - int newNetId = session.getNextItemNetId(); - itemStack.setNetId(newNetId); - itemEntry = new ItemStackResponsePacket.ItemEntry((byte) bedrockSlot, (byte) bedrockSlot, (byte) itemStack.getAmount(), newNetId, ""); + itemEntry = new ItemStackResponsePacket.ItemEntry((byte) bedrockSlot, (byte) bedrockSlot, (byte) itemStack.getAmount(), itemStack.getNetId(), ""); } else { itemEntry = new ItemStackResponsePacket.ItemEntry((byte) bedrockSlot, (byte) bedrockSlot, (byte) 0, 0, ""); } diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/inventory/click/ClickPlan.java b/connector/src/main/java/org/geysermc/connector/network/translators/inventory/click/ClickPlan.java index 4e5c2855..5bfe197e 100644 --- a/connector/src/main/java/org/geysermc/connector/network/translators/inventory/click/ClickPlan.java +++ b/connector/src/main/java/org/geysermc/connector/network/translators/inventory/click/ClickPlan.java @@ -127,7 +127,7 @@ public class ClickPlan { if (simulating) { simulatedItems.put(slot, item); } else { - inventory.setItem(slot, item); + inventory.setItem(slot, item, session); } } diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/inventory/translators/LoomInventoryTranslator.java b/connector/src/main/java/org/geysermc/connector/network/translators/inventory/translators/LoomInventoryTranslator.java index 59dd7d74..7fbca6e9 100644 --- a/connector/src/main/java/org/geysermc/connector/network/translators/inventory/translators/LoomInventoryTranslator.java +++ b/connector/src/main/java/org/geysermc/connector/network/translators/inventory/translators/LoomInventoryTranslator.java @@ -166,7 +166,7 @@ public class LoomInventoryTranslator extends AbstractBlockInventoryTranslator { inputCopy.getNbt().put(blockEntityTag); } // Set the new item as the output - inventory.setItem(3, inputCopy); + inventory.setItem(3, inputCopy, session); return translateRequest(session, inventory, request); } diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/inventory/translators/StonecutterInventoryTranslator.java b/connector/src/main/java/org/geysermc/connector/network/translators/inventory/translators/StonecutterInventoryTranslator.java index 16aefc74..4ffe87eb 100644 --- a/connector/src/main/java/org/geysermc/connector/network/translators/inventory/translators/StonecutterInventoryTranslator.java +++ b/connector/src/main/java/org/geysermc/connector/network/translators/inventory/translators/StonecutterInventoryTranslator.java @@ -64,7 +64,7 @@ public class StonecutterInventoryTranslator extends AbstractBlockInventoryTransl } CraftResultsDeprecatedStackRequestActionData craftData = (CraftResultsDeprecatedStackRequestActionData) data; // Get the ID of the item we are cutting - int id = inventory.getItem(0).getId(); + int id = inventory.getItem(0).getJavaId(); // Look up all possible options of cutting from this ID IntList results = session.getStonecutterRecipes().get(id); if (results == null) { @@ -77,9 +77,9 @@ public class StonecutterInventoryTranslator extends AbstractBlockInventoryTransl ClientClickWindowButtonPacket packet = new ClientClickWindowButtonPacket(inventory.getId(), results.indexOf(javaOutput.getId())); System.out.println(packet.toString()); session.sendDownstreamPacket(packet); - if (inventory.getItem(1).getId() != javaOutput.getId()) { + if (inventory.getItem(1).getJavaId() != javaOutput.getId()) { // We don't know there is an output here, so we tell ourselves that there is - inventory.setItem(1, GeyserItemStack.from(javaOutput, session.getNextItemNetId())); + inventory.setItem(1, GeyserItemStack.from(javaOutput, session.getNextItemNetId()), session); } return translateRequest(session, inventory, request); } diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/java/window/JavaSetSlotTranslator.java b/connector/src/main/java/org/geysermc/connector/network/translators/java/window/JavaSetSlotTranslator.java index 5d311fe3..94c57271 100644 --- a/connector/src/main/java/org/geysermc/connector/network/translators/java/window/JavaSetSlotTranslator.java +++ b/connector/src/main/java/org/geysermc/connector/network/translators/java/window/JavaSetSlotTranslator.java @@ -62,15 +62,7 @@ public class JavaSetSlotTranslator extends PacketTranslator InventoryTranslator translator = session.getInventoryTranslator(); if (translator != null) { GeyserItemStack newItem = GeyserItemStack.from(packet.getItem()); - GeyserItemStack oldItem = inventory.getItem(packet.getSlot()); - if (newItem.getItemData(session).equals(oldItem.getItemData(session), false, false, false)) { - newItem.setNetId(oldItem.getNetId()); - System.out.println("OLD: " + newItem.getNetId()); - } else { - newItem.setNetId(session.getNextItemNetId()); - System.out.println("NEW: " + newItem.getNetId()); - } - inventory.setItem(packet.getSlot(), newItem); + inventory.setItem(packet.getSlot(), newItem, session); translator.updateSlot(session, inventory, packet.getSlot()); } }); diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/java/window/JavaWindowItemsTranslator.java b/connector/src/main/java/org/geysermc/connector/network/translators/java/window/JavaWindowItemsTranslator.java index b87f1de8..c8afd1c5 100644 --- a/connector/src/main/java/org/geysermc/connector/network/translators/java/window/JavaWindowItemsTranslator.java +++ b/connector/src/main/java/org/geysermc/connector/network/translators/java/window/JavaWindowItemsTranslator.java @@ -46,13 +46,7 @@ public class JavaWindowItemsTranslator extends PacketTranslator