From 23ab69702d56f97358129f305fdb44882506ad91 Mon Sep 17 00:00:00 2001 From: Camotoy <20743703+Camotoy@users.noreply.github.com> Date: Fri, 12 Mar 2021 17:07:29 -0500 Subject: [PATCH] More review fixes and make beacon more reliable --- .../holder/BlockInventoryHolder.java | 12 +++++++- .../BeaconInventoryTranslator.java | 29 ++++++++++++++++++- .../CartographyInventoryTranslator.java | 16 +++++----- .../java/world/JavaTradeListTranslator.java | 2 ++ 4 files changed, 49 insertions(+), 10 deletions(-) diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/inventory/holder/BlockInventoryHolder.java b/connector/src/main/java/org/geysermc/connector/network/translators/inventory/holder/BlockInventoryHolder.java index e7bfd90f0..b7f67879b 100644 --- a/connector/src/main/java/org/geysermc/connector/network/translators/inventory/holder/BlockInventoryHolder.java +++ b/connector/src/main/java/org/geysermc/connector/network/translators/inventory/holder/BlockInventoryHolder.java @@ -74,7 +74,7 @@ public class BlockInventoryHolder extends InventoryHolder { // Check to see if there is an existing block we can use that the player just selected. // First, verify that the player's position has not changed, so we don't try to select a block wildly out of range. // (This could be a virtual inventory that the player is opening) - if (session.getLastInteractionPlayerPosition().equals(session.getPlayerEntity().getPosition())) { + if (checkInteractionPosition(session)) { // Then, check to see if the interacted block is valid for this inventory by ensuring the block state identifier is valid int javaBlockId = session.getConnector().getWorldManager().getBlockAt(session, session.getLastInteractionBlockPosition()); String[] javaBlockString = BlockTranslator.getJavaIdBlockMap().inverse().getOrDefault(javaBlockId, "minecraft:air").split("\\["); @@ -101,6 +101,16 @@ public class BlockInventoryHolder extends InventoryHolder { setCustomName(session, position, inventory, defaultJavaBlockState); } + /** + * Will be overwritten in the beacon inventory translator to remove the check, since virtual inventories can't exist. + * + * @return if the player's last interaction position and current position match. Used to ensure that we don't select + * a block to hold the inventory that's wildly out of range. + */ + protected boolean checkInteractionPosition(GeyserSession session) { + return session.getLastInteractionPlayerPosition().equals(session.getPlayerEntity().getPosition()); + } + /** * @return true if this Java block ID can be used for player inventory. */ diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/inventory/translators/BeaconInventoryTranslator.java b/connector/src/main/java/org/geysermc/connector/network/translators/inventory/translators/BeaconInventoryTranslator.java index 46c09b66b..5af921f2d 100644 --- a/connector/src/main/java/org/geysermc/connector/network/translators/inventory/translators/BeaconInventoryTranslator.java +++ b/connector/src/main/java/org/geysermc/connector/network/translators/inventory/translators/BeaconInventoryTranslator.java @@ -44,13 +44,40 @@ import org.geysermc.connector.inventory.Inventory; import org.geysermc.connector.inventory.PlayerInventory; import org.geysermc.connector.network.session.GeyserSession; import org.geysermc.connector.network.translators.inventory.BedrockContainerSlot; +import org.geysermc.connector.network.translators.inventory.InventoryTranslator; +import org.geysermc.connector.network.translators.inventory.holder.BlockInventoryHolder; import org.geysermc.connector.network.translators.inventory.updater.UIInventoryUpdater; +import org.geysermc.connector.utils.InventoryUtils; import java.util.Collections; public class BeaconInventoryTranslator extends AbstractBlockInventoryTranslator { public BeaconInventoryTranslator() { - super(1, "minecraft:beacon", ContainerType.BEACON, UIInventoryUpdater.INSTANCE); + super(1, new BlockInventoryHolder("minecraft:beacon", ContainerType.BEACON) { + @Override + public void prepareInventory(InventoryTranslator translator, GeyserSession session, Inventory inventory) { + if (!session.getConnector().getConfig().isCacheChunks()) { + // Beacons cannot work without knowing their physical location + return; + } + super.prepareInventory(translator, session, inventory); + } + + @Override + protected boolean checkInteractionPosition(GeyserSession session) { + // Since we can't fall back to a virtual inventory, let's make opening one easier + return true; + } + + @Override + public void openInventory(InventoryTranslator translator, GeyserSession session, Inventory inventory) { + if (!session.getConnector().getConfig().isCacheChunks() || !((BeaconContainer) inventory).isUsingRealBlock()) { + InventoryUtils.closeInventory(session, inventory.getId(), false); + return; + } + super.openInventory(translator, session, inventory); + } + }, UIInventoryUpdater.INSTANCE); } @Override diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/inventory/translators/CartographyInventoryTranslator.java b/connector/src/main/java/org/geysermc/connector/network/translators/inventory/translators/CartographyInventoryTranslator.java index f0f8b6d84..a3b50dace 100644 --- a/connector/src/main/java/org/geysermc/connector/network/translators/inventory/translators/CartographyInventoryTranslator.java +++ b/connector/src/main/java/org/geysermc/connector/network/translators/inventory/translators/CartographyInventoryTranslator.java @@ -59,14 +59,14 @@ public class CartographyInventoryTranslator extends AbstractBlockInventoryTransl @Override public int bedrockSlotToJava(StackRequestSlotInfoData slotInfoData) { - if (slotInfoData.getContainer() == ContainerSlotType.CARTOGRAPHY_INPUT) { - return 0; - } - if (slotInfoData.getContainer() == ContainerSlotType.CARTOGRAPHY_ADDITIONAL) { - return 1; - } - if (slotInfoData.getContainer() == ContainerSlotType.CARTOGRAPHY_RESULT || slotInfoData.getContainer() == ContainerSlotType.CREATIVE_OUTPUT) { - return 2; + switch (slotInfoData.getContainer()) { + case CARTOGRAPHY_INPUT: + return 0; + case CARTOGRAPHY_ADDITIONAL: + return 1; + case CARTOGRAPHY_RESULT: + case CREATIVE_OUTPUT: + return 2; } return super.bedrockSlotToJava(slotInfoData); } diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/java/world/JavaTradeListTranslator.java b/connector/src/main/java/org/geysermc/connector/network/translators/java/world/JavaTradeListTranslator.java index df8339079..d31dbb617 100644 --- a/connector/src/main/java/org/geysermc/connector/network/translators/java/world/JavaTradeListTranslator.java +++ b/connector/src/main/java/org/geysermc/connector/network/translators/java/world/JavaTradeListTranslator.java @@ -58,6 +58,7 @@ public class JavaTradeListTranslator extends PacketTranslator