From e2d46c3d490f62d0c5b6a8518e0f962dc7505c77 Mon Sep 17 00:00:00 2001 From: AJ Ferguson Date: Mon, 18 May 2020 21:15:29 -0800 Subject: [PATCH] Work on villager trading --- .../network/session/GeyserSession.java | 6 -- .../bedrock/BedrockEntityEventTranslator.java | 22 ++++++ .../MerchantInventoryTranslator.java | 75 +++---------------- .../java/world/JavaTradeListTranslator.java | 54 +++++++++---- 4 files changed, 72 insertions(+), 85 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 ea34bef6..1bf506e7 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 @@ -156,12 +156,6 @@ public class GeyserSession implements CommandSender { @Setter private Vector3i lastInteractionPosition; - @Setter - private ItemStack firstTradeSlot; - - @Setter - private ItemStack secondTradeSlot; - @Setter private boolean switchingDimension = false; private boolean manyDimPackets = false; diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/bedrock/BedrockEntityEventTranslator.java b/connector/src/main/java/org/geysermc/connector/network/translators/bedrock/BedrockEntityEventTranslator.java index f97dc3c6..c7d1ae67 100644 --- a/connector/src/main/java/org/geysermc/connector/network/translators/bedrock/BedrockEntityEventTranslator.java +++ b/connector/src/main/java/org/geysermc/connector/network/translators/bedrock/BedrockEntityEventTranslator.java @@ -26,7 +26,12 @@ package org.geysermc.connector.network.translators.bedrock; +import com.github.steveice10.mc.protocol.data.game.window.VillagerTrade; +import com.github.steveice10.mc.protocol.data.game.window.WindowType; +import com.github.steveice10.mc.protocol.packet.ingame.client.window.ClientSelectTradePacket; import com.nukkitx.protocol.bedrock.packet.EntityEventPacket; +import org.geysermc.connector.entity.living.merchant.VillagerEntity; +import org.geysermc.connector.inventory.Inventory; import org.geysermc.connector.network.session.GeyserSession; import org.geysermc.connector.network.translators.PacketTranslator; import org.geysermc.connector.network.translators.Translator; @@ -41,6 +46,23 @@ public class BedrockEntityEventTranslator extends PacketTranslator 0 && packet.getData() < villager.getVillagerTrades().length) { + VillagerTrade trade = villager.getVillagerTrades()[packet.getData()]; + openInventory.setItem(2, trade.getOutput()); + } + } + return; } session.getConnector().getLogger().debug("Did not translate incoming EntityEventPacket: " + packet.toString()); } diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/inventory/MerchantInventoryTranslator.java b/connector/src/main/java/org/geysermc/connector/network/translators/inventory/MerchantInventoryTranslator.java index 5d1ce122..7e3f24bd 100644 --- a/connector/src/main/java/org/geysermc/connector/network/translators/inventory/MerchantInventoryTranslator.java +++ b/connector/src/main/java/org/geysermc/connector/network/translators/inventory/MerchantInventoryTranslator.java @@ -26,18 +26,11 @@ package org.geysermc.connector.network.translators.inventory; -import com.github.steveice10.mc.protocol.data.game.entity.metadata.ItemStack; -import com.github.steveice10.mc.protocol.data.game.window.ClickItemParam; -import com.github.steveice10.mc.protocol.data.game.window.VillagerTrade; -import com.github.steveice10.mc.protocol.data.game.window.WindowAction; -import com.github.steveice10.mc.protocol.packet.ingame.client.window.ClientSelectTradePacket; -import com.github.steveice10.mc.protocol.packet.ingame.client.window.ClientWindowActionPacket; import com.nukkitx.protocol.bedrock.data.ContainerId; import com.nukkitx.protocol.bedrock.data.InventoryActionData; -import org.geysermc.connector.entity.living.merchant.VillagerEntity; +import com.nukkitx.protocol.bedrock.data.InventorySource; import org.geysermc.connector.inventory.Inventory; import org.geysermc.connector.network.session.GeyserSession; -import org.geysermc.connector.network.translators.Translators; import org.geysermc.connector.network.translators.inventory.updater.CursorInventoryUpdater; import org.geysermc.connector.network.translators.inventory.updater.InventoryUpdater; @@ -80,6 +73,14 @@ public class MerchantInventoryTranslator extends BaseInventoryTranslator { return super.bedrockSlotToJava(action); } + @Override + public SlotType getSlotType(int javaSlot) { + if (javaSlot == 2) { + return SlotType.OUTPUT; + } + return SlotType.NORMAL; + } + @Override public void prepareInventory(GeyserSession session, Inventory inventory) { @@ -93,8 +94,6 @@ public class MerchantInventoryTranslator extends BaseInventoryTranslator { @Override public void closeInventory(GeyserSession session, Inventory inventory) { session.setLastInteractedVillagerEid(-1); - session.setFirstTradeSlot(null); - session.setSecondTradeSlot(null); } @Override @@ -109,60 +108,10 @@ public class MerchantInventoryTranslator extends BaseInventoryTranslator { @Override public void translateActions(GeyserSession session, Inventory inventory, List actions) { - InventoryActionData result = null; - - VillagerEntity villager = (VillagerEntity) session.getEntityCache().getEntityByGeyserId(session.getLastInteractedVillagerEid()); - if (villager == null) { - session.getConnector().getLogger().debug("Could not find villager with entity id: " + session.getLastInteractedVillagerEid()); - return; - } - - // We need to store the trade slot data in the session itself as data - // needs to persist beyond this translateActions method since the client - // sends multiple packets for this - for (InventoryActionData data : actions) { - if (data.getSlot() == 4 && session.getFirstTradeSlot() == null && data.getSource().getContainerId() == ContainerId.CURSOR) { - session.setFirstTradeSlot(Translators.getItemTranslator().translateToJava(session, data.getToItem())); + for (InventoryActionData action : actions) { + if (action.getSource().getType() == InventorySource.Type.NON_IMPLEMENTED_TODO) { + return; } - - if (data.getSlot() == 5 && session.getSecondTradeSlot() == null && data.getToItem() != null && data.getSource().getContainerId() == ContainerId.CURSOR) { - session.setSecondTradeSlot(Translators.getItemTranslator().translateToJava(session, data.getToItem())); - } - if (data.getSlot() == 50 && result == null) { - result = data; - } - } - - if (result == null || session.getFirstTradeSlot() == null) { - super.translateActions(session, inventory, actions); - return; - } - - ItemStack resultSlot = Translators.getItemTranslator().translateToJava(session, result.getToItem()); - for (int i = 0; i < villager.getVillagerTrades().length; i++) { - VillagerTrade trade = villager.getVillagerTrades()[i]; - if (!Translators.getItemTranslator().equals(session.getFirstTradeSlot(), trade.getFirstInput(), true, true, false) || !Translators.getItemTranslator().equals(resultSlot, trade.getOutput(), true, false, false)) { - continue; - } - - if (session.getSecondTradeSlot() != null && trade.getSecondInput() != null && !Translators.getItemTranslator().equals(session.getSecondTradeSlot(), trade.getSecondInput(), true, false, false)) { - continue; - } - - ClientSelectTradePacket selectTradePacket = new ClientSelectTradePacket(i); - session.sendDownstreamPacket(selectTradePacket); - - ClientWindowActionPacket tradeAction = new ClientWindowActionPacket( - inventory.getId(), - inventory.getTransactionId().getAndIncrement(), - this.bedrockSlotToJava(result), - null, - WindowAction.CLICK_ITEM, - ClickItemParam.LEFT_CLICK - ); - session.sendDownstreamPacket(tradeAction); - break; - } super.translateActions(session, inventory, actions); 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 b156ef83..7ba20d1a 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 @@ -56,12 +56,13 @@ public class JavaTradeListTranslator extends PacketTranslator