From 44e9dba7598ee96c77f7a9a1b94e55d9be185dec Mon Sep 17 00:00:00 2001 From: Camotoy <20743703+Camotoy@users.noreply.github.com> Date: Mon, 8 Mar 2021 14:17:31 -0500 Subject: [PATCH] Reject requests that are unavailable spaces; other things --- .../network/session/GeyserSession.java | 2 -- .../inventory/InventoryTranslator.java | 32 ++++++++++++------- .../CartographyInventoryTranslator.java | 3 +- .../translators/LoomInventoryTranslator.java | 3 +- .../MerchantInventoryTranslator.java | 5 ++- .../chest/ChestInventoryTranslator.java | 10 ++++++ .../window/JavaOpenHorseWindowTranslator.java | 2 +- 7 files changed, 40 insertions(+), 17 deletions(-) diff --git a/connector/src/main/java/org/geysermc/connector/network/session/GeyserSession.java b/connector/src/main/java/org/geysermc/connector/network/session/GeyserSession.java index 68f6297e1..cf4ad8095 100644 --- a/connector/src/main/java/org/geysermc/connector/network/session/GeyserSession.java +++ b/connector/src/main/java/org/geysermc/connector/network/session/GeyserSession.java @@ -418,8 +418,6 @@ public class GeyserSession implements CommandSender { this.spawned = false; this.loggedIn = false; - connector.getPlayers().forEach(player -> this.emotes.addAll(player.getEmotes())); - // Make a copy to prevent ConcurrentModificationException final List tmpPlayers = new ArrayList<>(connector.getPlayers()); tmpPlayers.forEach(player -> this.emotes.addAll(player.getEmotes())); 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 bb0a033ef..1eef679f5 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 @@ -121,7 +121,8 @@ public abstract class InventoryTranslator { * * @return true if this transfer should be rejected */ - public boolean shouldRejectItemPlace(GeyserSession session, Inventory inventory, int javaSourceSlot, int javaDestinationSlot) { + public boolean shouldRejectItemPlace(GeyserSession session, Inventory inventory, ContainerSlotType bedrockSourceContainer, + int javaSourceSlot, ContainerSlotType bedrockDestinationContainer, int javaDestinationSlot) { return false; } @@ -201,8 +202,9 @@ public abstract class InventoryTranslator { int sourceSlot = bedrockSlotToJava(transferAction.getSource()); int destSlot = bedrockSlotToJava(transferAction.getDestination()); - if (shouldRejectItemPlace(session, inventory, isCursor(transferAction.getSource()) ? -1 : sourceSlot, - isCursor(transferAction.getDestination()) ? -1 : destSlot)) { + if (shouldRejectItemPlace(session, inventory, transferAction.getSource().getContainer(), + isCursor(transferAction.getSource()) ? -1 : sourceSlot, + transferAction.getDestination().getContainer(), isCursor(transferAction.getDestination()) ? -1 : destSlot)) { // This item would not be here in Java return rejectRequest(request, false); } @@ -273,7 +275,7 @@ public abstract class InventoryTranslator { } break; } - case SWAP: { //TODO + case SWAP: { SwapStackRequestActionData swapAction = (SwapStackRequestActionData) action; if (!(checkNetId(session, inventory, swapAction.getSource()) && checkNetId(session, inventory, swapAction.getDestination()))) { session.getConnector().getLogger().error("DEBUG: About to reject SWAP request made by " + session.getName()); @@ -284,23 +286,31 @@ public abstract class InventoryTranslator { return rejectRequest(request); } - if (isCursor(swapAction.getSource()) && isCursor(swapAction.getDestination())) { //??? + int sourceSlot = bedrockSlotToJava(swapAction.getSource()); + int destSlot = bedrockSlotToJava(swapAction.getDestination()); + boolean isSourceCursor = isCursor(swapAction.getSource()); + boolean isDestCursor = isCursor(swapAction.getDestination()); + + if (shouldRejectItemPlace(session, inventory, swapAction.getSource().getContainer(), + isSourceCursor ? -1 : sourceSlot, + swapAction.getDestination().getContainer(), isDestCursor ? -1 : destSlot)) { + // This item would not be here in Java + return rejectRequest(request, false); + } + + if (isSourceCursor && isDestCursor) { //??? return rejectRequest(request); - } else if (isCursor(swapAction.getSource())) { //swap cursor - int destSlot = bedrockSlotToJava(swapAction.getDestination()); + } else if (isSourceCursor) { //swap cursor if (InventoryUtils.canStack(cursor, plan.getItem(destSlot))) { //TODO: cannot simply swap if cursor stacks with slot (temp slot) return rejectRequest(request); } plan.add(Click.LEFT, destSlot); - } else if (isCursor(swapAction.getDestination())) { //swap cursor - int sourceSlot = bedrockSlotToJava(swapAction.getSource()); + } else if (isDestCursor) { //swap cursor if (InventoryUtils.canStack(cursor, plan.getItem(sourceSlot))) { //TODO return rejectRequest(request); } plan.add(Click.LEFT, sourceSlot); } else { - int sourceSlot = bedrockSlotToJava(swapAction.getSource()); - int destSlot = bedrockSlotToJava(swapAction.getDestination()); if (!cursor.isEmpty()) { //TODO: (temp slot) return rejectRequest(request); } 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 2038a5ba0..319d9ec0a 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 @@ -43,7 +43,8 @@ public class CartographyInventoryTranslator extends AbstractBlockInventoryTransl } @Override - public boolean shouldRejectItemPlace(GeyserSession session, Inventory inventory, int javaSourceSlot, int javaDestinationSlot) { + public boolean shouldRejectItemPlace(GeyserSession session, Inventory inventory, ContainerSlotType bedrockSourceContainer, + int javaSourceSlot, ContainerSlotType bedrockDestinationContainer, int javaDestinationSlot) { if (javaDestinationSlot == 0) { // Bedrock Edition can use paper in slot 0 GeyserItemStack itemStack = javaSourceSlot == -1 ? session.getPlayerInventory().getCursor() : inventory.getItem(javaSourceSlot); 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 3209a161e..befe9ff30 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 @@ -100,7 +100,8 @@ public class LoomInventoryTranslator extends AbstractBlockInventoryTranslator { } @Override - public boolean shouldRejectItemPlace(GeyserSession session, Inventory inventory, int javaSourceSlot, int javaDestinationSlot) { + public boolean shouldRejectItemPlace(GeyserSession session, Inventory inventory, ContainerSlotType bedrockSourceContainer, + int javaSourceSlot, ContainerSlotType bedrockDestinationContainer, int javaDestinationSlot) { if (javaDestinationSlot != 1) { return false; } diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/inventory/translators/MerchantInventoryTranslator.java b/connector/src/main/java/org/geysermc/connector/network/translators/inventory/translators/MerchantInventoryTranslator.java index 34feb40d9..736568868 100644 --- a/connector/src/main/java/org/geysermc/connector/network/translators/inventory/translators/MerchantInventoryTranslator.java +++ b/connector/src/main/java/org/geysermc/connector/network/translators/inventory/translators/MerchantInventoryTranslator.java @@ -30,7 +30,9 @@ import com.nukkitx.math.vector.Vector3f; import com.nukkitx.protocol.bedrock.data.entity.EntityData; import com.nukkitx.protocol.bedrock.data.entity.EntityDataMap; import com.nukkitx.protocol.bedrock.data.entity.EntityLinkData; -import com.nukkitx.protocol.bedrock.data.inventory.*; +import com.nukkitx.protocol.bedrock.data.inventory.ContainerSlotType; +import com.nukkitx.protocol.bedrock.data.inventory.ItemStackRequest; +import com.nukkitx.protocol.bedrock.data.inventory.StackRequestSlotInfoData; import com.nukkitx.protocol.bedrock.packet.ItemStackResponsePacket; import com.nukkitx.protocol.bedrock.packet.SetEntityLinkPacket; import org.geysermc.connector.entity.Entity; @@ -128,6 +130,7 @@ public class MerchantInventoryTranslator extends BaseInventoryTranslator { @Override public void openInventory(GeyserSession session, Inventory inventory) { //Handled in JavaTradeListTranslator + //TODO: send a blank inventory here in case the villager doesn't send a TradeList packet } @Override diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/inventory/translators/chest/ChestInventoryTranslator.java b/connector/src/main/java/org/geysermc/connector/network/translators/inventory/translators/chest/ChestInventoryTranslator.java index 89366e2c0..d54419b82 100644 --- a/connector/src/main/java/org/geysermc/connector/network/translators/inventory/translators/chest/ChestInventoryTranslator.java +++ b/connector/src/main/java/org/geysermc/connector/network/translators/inventory/translators/chest/ChestInventoryTranslator.java @@ -41,6 +41,16 @@ public abstract class ChestInventoryTranslator extends BaseInventoryTranslator { this.updater = new ChestInventoryUpdater(paddedSize); } + @Override + public boolean shouldRejectItemPlace(GeyserSession session, Inventory inventory, ContainerSlotType bedrockSourceContainer, + int javaSourceSlot, ContainerSlotType bedrockDestinationContainer, int javaDestinationSlot) { + // Reject any item placements that occur in the unusable inventory space + if (bedrockSourceContainer == ContainerSlotType.CONTAINER && javaSourceSlot >= this.size) { + return true; + } + return bedrockDestinationContainer == ContainerSlotType.CONTAINER && javaDestinationSlot >= this.size; + } + @Override public void updateInventory(GeyserSession session, Inventory inventory) { updater.updateInventory(this, session, inventory); diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/java/window/JavaOpenHorseWindowTranslator.java b/connector/src/main/java/org/geysermc/connector/network/translators/java/window/JavaOpenHorseWindowTranslator.java index 8952ddcb3..5016b6150 100644 --- a/connector/src/main/java/org/geysermc/connector/network/translators/java/window/JavaOpenHorseWindowTranslator.java +++ b/connector/src/main/java/org/geysermc/connector/network/translators/java/window/JavaOpenHorseWindowTranslator.java @@ -61,7 +61,7 @@ public class JavaOpenHorseWindowTranslator extends PacketTranslator acceptedArmors = new ArrayList<>(); + List acceptedArmors = new ArrayList<>(4); for (String identifier : acceptedHorseArmorIdentifiers) { NbtMapBuilder acceptedItemBuilder = NbtMap.builder() .putShort("Aux", Short.MAX_VALUE)