From d07404406ba7d6cc54c9c9d22cf6f8903146a60d Mon Sep 17 00:00:00 2001 From: AJ Ferguson Date: Sun, 1 Dec 2019 00:26:11 -0900 Subject: [PATCH] Cleanup and fixes --- ...BedrockInventoryTransactionTranslator.java | 38 +++++++++++------ .../inventory/AnvilInventoryTranslator.java | 42 ++++++------------- .../inventory/BlockInventoryTranslator.java | 4 +- .../BrewingStandInventoryTranslator.java | 34 ++++++--------- .../ContainerInventoryTranslator.java | 6 +-- .../CraftingTableInventoryTranslator.java | 20 ++++----- .../inventory/FurnaceInventoryTranslator.java | 7 ++-- .../inventory/InventoryTranslator.java | 2 +- .../inventory/PlayerInventoryTranslator.java | 10 ++--- .../translators/inventory/SlotType.java | 32 ++++++++++++++ 10 files changed, 108 insertions(+), 87 deletions(-) create mode 100644 connector/src/main/java/org/geysermc/connector/network/translators/inventory/SlotType.java 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 06343110..1fd0500c 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 @@ -55,6 +55,7 @@ import org.geysermc.connector.network.session.GeyserSession; import org.geysermc.connector.network.translators.PacketTranslator; import org.geysermc.connector.network.translators.TranslatorsInit; import org.geysermc.connector.network.translators.inventory.InventoryTranslator; +import org.geysermc.connector.network.translators.inventory.SlotType; import java.util.*; @@ -237,8 +238,19 @@ public class BedrockInventoryTransactionTranslator extends PacketTranslator plan = new ArrayList<>(); - ItemStack translatedCursor = TranslatorsInit.getItemTranslator().translateToJava(cursorAction.getFromItem()); - boolean refresh = !Objects.equals(session.getInventory().getCursor(), translatedCursor.getId() == 0 ? null : translatedCursor); //refresh slot if there is a cursor mismatch + ItemStack translatedCursor = cursorAction.getFromItem().isValid() ? + TranslatorsInit.getItemTranslator().translateToJava(cursorAction.getFromItem()) : null; + ItemStack currentCursor = session.getInventory().getCursor(); + boolean refresh = false; + if (currentCursor != null) { + if (translatedCursor != null) { + refresh = !(currentCursor.getId() == translatedCursor.getId() && + currentCursor.getAmount() == translatedCursor.getAmount()); + } else { + refresh = true; + } + } + int javaSlot = translator.bedrockSlotToJava(containerAction); if (cursorAction.getFromItem().equals(containerAction.getToItem()) && containerAction.getFromItem().equals(cursorAction.getToItem()) && @@ -258,7 +270,7 @@ public class BedrockInventoryTransactionTranslator extends PacketTranslator= 9) { - return slotnum + this.size - 9; - } else { - return slotnum + this.size + 27; - } - } else { - if (action.getSource().getContainerId() == ContainerId.CURSOR) { - switch (slotnum) { - case 1: - return 0; - case 2: - return 1; - case 50: - return 2; - } + if (action.getSource().getContainerId() == ContainerId.CURSOR) { + switch (action.getSlot()) { + case 1: + return 0; + case 2: + return 1; + case 50: + return 2; } } - return slotnum; + return super.bedrockSlotToJava(action); } @Override - public boolean isOutputSlot(int slot) { - return slot == 2; + public SlotType getSlotType(int javaSlot) { + if (javaSlot == 2) + return SlotType.OUTPUT; + return SlotType.NORMAL; } } diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/inventory/BlockInventoryTranslator.java b/connector/src/main/java/org/geysermc/connector/network/translators/inventory/BlockInventoryTranslator.java index 065d63dc..0112dc9e 100644 --- a/connector/src/main/java/org/geysermc/connector/network/translators/inventory/BlockInventoryTranslator.java +++ b/connector/src/main/java/org/geysermc/connector/network/translators/inventory/BlockInventoryTranslator.java @@ -38,8 +38,8 @@ import org.geysermc.connector.network.translators.block.BlockEntry; import org.geysermc.connector.world.GlobalBlockPalette; public class BlockInventoryTranslator extends ContainerInventoryTranslator { - protected final int blockId; - protected final ContainerType containerType; + final int blockId; + private final ContainerType containerType; public BlockInventoryTranslator(int size, int blockId, ContainerType containerType) { super(size); diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/inventory/BrewingStandInventoryTranslator.java b/connector/src/main/java/org/geysermc/connector/network/translators/inventory/BrewingStandInventoryTranslator.java index 92eb4bc8..e6089cb8 100644 --- a/connector/src/main/java/org/geysermc/connector/network/translators/inventory/BrewingStandInventoryTranslator.java +++ b/connector/src/main/java/org/geysermc/connector/network/translators/inventory/BrewingStandInventoryTranslator.java @@ -25,7 +25,6 @@ package org.geysermc.connector.network.translators.inventory; -import com.nukkitx.protocol.bedrock.data.ContainerId; import com.nukkitx.protocol.bedrock.data.ContainerType; import com.nukkitx.protocol.bedrock.data.InventoryAction; import com.nukkitx.protocol.bedrock.packet.ContainerSetDataPacket; @@ -58,27 +57,18 @@ public class BrewingStandInventoryTranslator extends BlockInventoryTranslator { @Override public int bedrockSlotToJava(InventoryAction action) { - int slotnum = action.getSlot(); - if (action.getSource().getContainerId() == ContainerId.INVENTORY) { - //hotbar - if (slotnum >= 9) { - return slotnum + this.size - 9; - } else { - return slotnum + this.size + 27; - } - } else { - switch (slotnum) { - case 0: - return 3; - case 1: - return 0; - case 2: - return 1; - case 3: - return 2; - default: - return slotnum; - } + int slotnum = super.bedrockSlotToJava(action); + switch (slotnum) { + case 0: + return 3; + case 1: + return 0; + case 2: + return 1; + case 3: + return 2; + default: + return slotnum; } } diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/inventory/ContainerInventoryTranslator.java b/connector/src/main/java/org/geysermc/connector/network/translators/inventory/ContainerInventoryTranslator.java index 22d73b9b..8f66675b 100644 --- a/connector/src/main/java/org/geysermc/connector/network/translators/inventory/ContainerInventoryTranslator.java +++ b/connector/src/main/java/org/geysermc/connector/network/translators/inventory/ContainerInventoryTranslator.java @@ -35,7 +35,7 @@ import org.geysermc.connector.network.session.GeyserSession; import org.geysermc.connector.network.translators.TranslatorsInit; public abstract class ContainerInventoryTranslator extends InventoryTranslator { - public ContainerInventoryTranslator(int size) { + ContainerInventoryTranslator(int size) { super(size); } @@ -97,7 +97,7 @@ public abstract class ContainerInventoryTranslator extends InventoryTranslator { } @Override - public boolean isOutputSlot(int slot) { - return false; + public SlotType getSlotType(int javaSlot) { + return SlotType.NORMAL; } } diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/inventory/CraftingTableInventoryTranslator.java b/connector/src/main/java/org/geysermc/connector/network/translators/inventory/CraftingTableInventoryTranslator.java index 82b4f922..28ad0cb2 100644 --- a/connector/src/main/java/org/geysermc/connector/network/translators/inventory/CraftingTableInventoryTranslator.java +++ b/connector/src/main/java/org/geysermc/connector/network/translators/inventory/CraftingTableInventoryTranslator.java @@ -59,21 +59,21 @@ public class CraftingTableInventoryTranslator extends ContainerInventoryTranslat @Override public int bedrockSlotToJava(InventoryAction action) { - int slotnum = action.getSlot(); - if (action.getSource().getContainerId() == ContainerId.INVENTORY) { - //hotbar - if (slotnum >= 9) { - return slotnum + this.size - 9; - } else { - return slotnum + this.size + 27; - } - } else { + if (action.getSource().getContainerId() == ContainerId.CURSOR) { + int slotnum = action.getSlot(); if (slotnum >= 32 && 42 >= slotnum) { return slotnum - 31; } else if (slotnum == 50) { return 0; } - return slotnum; } + return super.bedrockSlotToJava(action); + } + + @Override + public SlotType getSlotType(int javaSlot) { + if (javaSlot == 0) + return SlotType.OUTPUT; + return SlotType.NORMAL; } } diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/inventory/FurnaceInventoryTranslator.java b/connector/src/main/java/org/geysermc/connector/network/translators/inventory/FurnaceInventoryTranslator.java index 9c274c87..95d78562 100644 --- a/connector/src/main/java/org/geysermc/connector/network/translators/inventory/FurnaceInventoryTranslator.java +++ b/connector/src/main/java/org/geysermc/connector/network/translators/inventory/FurnaceInventoryTranslator.java @@ -26,7 +26,6 @@ package org.geysermc.connector.network.translators.inventory; import com.nukkitx.protocol.bedrock.data.ContainerType; -import com.nukkitx.protocol.bedrock.data.InventoryAction; import com.nukkitx.protocol.bedrock.packet.ContainerSetDataPacket; import org.geysermc.connector.inventory.Inventory; import org.geysermc.connector.network.session.GeyserSession; @@ -59,7 +58,9 @@ public class FurnaceInventoryTranslator extends BlockInventoryTranslator { } @Override - public boolean isOutputSlot(int slot) { - return slot == 2; + public SlotType getSlotType(int javaSlot) { + if (javaSlot == 2) + return SlotType.FURNACE_OUTPUT; + return SlotType.NORMAL; } } 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 edc7f495..0aa4df0a 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 @@ -44,5 +44,5 @@ public abstract class InventoryTranslator { public abstract void updateSlot(GeyserSession session, Inventory inventory, int slot); public abstract int bedrockSlotToJava(InventoryAction action); public abstract int javaSlotToBedrock(int slot); - public abstract boolean isOutputSlot(int slot); + public abstract SlotType getSlotType(int javaSlot); } diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/inventory/PlayerInventoryTranslator.java b/connector/src/main/java/org/geysermc/connector/network/translators/inventory/PlayerInventoryTranslator.java index 773c4dca..6ba25bc5 100644 --- a/connector/src/main/java/org/geysermc/connector/network/translators/inventory/PlayerInventoryTranslator.java +++ b/connector/src/main/java/org/geysermc/connector/network/translators/inventory/PlayerInventoryTranslator.java @@ -119,15 +119,13 @@ public class PlayerInventoryTranslator extends InventoryTranslator { break; case ContainerId.OFFHAND: return 45; - case ContainerId.CRAFTING_ADD_INGREDIENT: - case ContainerId.CRAFTING_REMOVE_INGREDIENT: - return slotnum + 1; case ContainerId.CURSOR: if (slotnum >= 28 && 31 >= slotnum) { return slotnum - 27; } else if (slotnum == 50) { return 0; } + break; } return slotnum; } @@ -138,8 +136,10 @@ public class PlayerInventoryTranslator extends InventoryTranslator { } @Override - public boolean isOutputSlot(int slot) { - return false; + public SlotType getSlotType(int javaSlot) { + if (javaSlot == 0) + return SlotType.OUTPUT; + return SlotType.NORMAL; } @Override diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/inventory/SlotType.java b/connector/src/main/java/org/geysermc/connector/network/translators/inventory/SlotType.java new file mode 100644 index 00000000..b5657af4 --- /dev/null +++ b/connector/src/main/java/org/geysermc/connector/network/translators/inventory/SlotType.java @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2019 GeyserMC. http://geysermc.org + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + * @author GeyserMC + * @link https://github.com/GeyserMC/Geyser + */ + +package org.geysermc.connector.network.translators.inventory; + +public enum SlotType { + NORMAL, + OUTPUT, + FURNACE_OUTPUT +}