From eaf57550e5f9227443a62f1aef471fba0408e46f Mon Sep 17 00:00:00 2001 From: RednedEpic Date: Mon, 5 Aug 2019 15:16:45 -0500 Subject: [PATCH] Implement support for changing held item --- .../connector/inventory/Inventory.java | 22 ++++++--- .../connector/inventory/PlayerInventory.java | 47 ++++++++++++++++++ .../network/UpstreamPacketHandler.java | 4 ++ .../network/session/GeyserSession.java | 10 +++- .../network/session/cache/InventoryCache.java | 3 -- .../network/translators/TranslatorsInit.java | 3 ++ .../BedrockCommandRequestTranslator.java | 1 - .../BedrockMobEquipmentTranslator.java | 49 +++++++++++++++++++ 8 files changed, 126 insertions(+), 13 deletions(-) create mode 100644 connector/src/main/java/org/geysermc/connector/inventory/PlayerInventory.java create mode 100644 connector/src/main/java/org/geysermc/connector/network/translators/bedrock/BedrockMobEquipmentTranslator.java 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 ed988dcc..801f670c 100644 --- a/connector/src/main/java/org/geysermc/connector/inventory/Inventory.java +++ b/connector/src/main/java/org/geysermc/connector/inventory/Inventory.java @@ -33,32 +33,40 @@ import lombok.Setter; public class Inventory { @Getter - private int id; + protected int id; @Getter @Setter - private boolean open; + protected boolean open; @Getter - private WindowType windowType; + protected WindowType windowType; @Getter - private int size; + protected int size; @Getter @Setter - private String title; + protected String title; @Getter @Setter - private ItemStack[] items; + protected ItemStack[] items; public Inventory(int id, WindowType windowType, int size) { + this("Inventory", id, windowType, size); + } + + public Inventory(String title, int id, WindowType windowType, int size) { + this.title = title; this.id = id; this.windowType = windowType; this.size = size; - this.title = "Inventory"; this.items = new ItemStack[size]; } + + public ItemStack getItem(int slot) { + return items[slot]; + } } diff --git a/connector/src/main/java/org/geysermc/connector/inventory/PlayerInventory.java b/connector/src/main/java/org/geysermc/connector/inventory/PlayerInventory.java new file mode 100644 index 00000000..424570b9 --- /dev/null +++ b/connector/src/main/java/org/geysermc/connector/inventory/PlayerInventory.java @@ -0,0 +1,47 @@ +/* + * 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.inventory; + +import com.github.steveice10.mc.protocol.data.game.entity.metadata.ItemStack; +import lombok.Getter; +import lombok.Setter; + +public class PlayerInventory extends Inventory { + + @Getter + @Setter + private int heldItemSlot; + + public PlayerInventory() { + super(0, null, 45); + + heldItemSlot = 0; + } + + public ItemStack getItemInHand() { + return items[heldItemSlot]; + } +} diff --git a/connector/src/main/java/org/geysermc/connector/network/UpstreamPacketHandler.java b/connector/src/main/java/org/geysermc/connector/network/UpstreamPacketHandler.java index 7a0f4616..8b4c188c 100644 --- a/connector/src/main/java/org/geysermc/connector/network/UpstreamPacketHandler.java +++ b/connector/src/main/java/org/geysermc/connector/network/UpstreamPacketHandler.java @@ -139,4 +139,8 @@ public class UpstreamPacketHandler extends LoggingPacketHandler { return translateAndDefault(packet); } + @Override + public boolean handle(MobEquipmentPacket packet) { + return translateAndDefault(packet); + } } \ No newline at end of file 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 3d514016..3333d650 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 @@ -54,6 +54,7 @@ import org.geysermc.api.window.FormWindow; import org.geysermc.connector.GeyserConnector; import org.geysermc.connector.entity.PlayerEntity; import org.geysermc.connector.entity.type.EntityType; +import org.geysermc.connector.inventory.PlayerInventory; import org.geysermc.connector.network.session.cache.DataCache; import org.geysermc.connector.network.session.cache.EntityCache; import org.geysermc.connector.network.session.cache.InventoryCache; @@ -76,6 +77,8 @@ public class GeyserSession implements PlayerSession, Player { private AuthData authenticationData; private PlayerEntity playerEntity; + private PlayerInventory inventory; + private EntityCache entityCache; private InventoryCache inventoryCache; private WindowCache windowCache; @@ -94,17 +97,20 @@ public class GeyserSession implements PlayerSession, Player { this.connector = connector; this.upstream = bedrockServerSession; - this.playerEntity = new PlayerEntity(UUID.randomUUID(), 1, 1, EntityType.PLAYER, new Vector3f(0, 0, 0), new Vector3f(0, 0, 0), new Vector3f(0, 0, 0)); - this.entityCache = new EntityCache(this); this.inventoryCache = new InventoryCache(this); this.windowCache = new WindowCache(this); this.scoreboardCache = new ScoreboardCache(this); + this.playerEntity = new PlayerEntity(UUID.randomUUID(), 1, 1, EntityType.PLAYER, new Vector3f(0, 0, 0), new Vector3f(0, 0, 0), new Vector3f(0, 0, 0)); + this.inventory = new PlayerInventory(); + this.javaPacketCache = new DataCache(); this.spawned = false; this.loggedIn = false; + + this.inventoryCache.getInventories().put(0, inventory); } public void connect(RemoteServer remoteServer) { diff --git a/connector/src/main/java/org/geysermc/connector/network/session/cache/InventoryCache.java b/connector/src/main/java/org/geysermc/connector/network/session/cache/InventoryCache.java index b6c8b501..4eea1de2 100644 --- a/connector/src/main/java/org/geysermc/connector/network/session/cache/InventoryCache.java +++ b/connector/src/main/java/org/geysermc/connector/network/session/cache/InventoryCache.java @@ -52,9 +52,6 @@ public class InventoryCache { public InventoryCache(GeyserSession session) { this.session = session; - - // This is the player's inventory - inventories.put(0, new Inventory(0, null, 45)); } public Inventory getPlayerInventory() { diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/TranslatorsInit.java b/connector/src/main/java/org/geysermc/connector/network/translators/TranslatorsInit.java index cacdf090..0edcd59d 100644 --- a/connector/src/main/java/org/geysermc/connector/network/translators/TranslatorsInit.java +++ b/connector/src/main/java/org/geysermc/connector/network/translators/TranslatorsInit.java @@ -59,10 +59,12 @@ import com.nukkitx.nbt.stream.NBTOutputStream; import com.nukkitx.nbt.tag.CompoundTag; import com.nukkitx.protocol.bedrock.packet.AnimatePacket; import com.nukkitx.protocol.bedrock.packet.CommandRequestPacket; +import com.nukkitx.protocol.bedrock.packet.MobEquipmentPacket; import com.nukkitx.protocol.bedrock.packet.TextPacket; import lombok.Getter; import org.geysermc.connector.network.translators.bedrock.BedrockAnimateTranslator; import org.geysermc.connector.network.translators.bedrock.BedrockCommandRequestTranslator; +import org.geysermc.connector.network.translators.bedrock.BedrockMobEquipmentTranslator; import org.geysermc.connector.network.translators.bedrock.BedrockTextTranslator; import org.geysermc.connector.network.translators.inventory.GenericInventoryTranslator; import org.geysermc.connector.network.translators.inventory.InventoryTranslator; @@ -161,6 +163,7 @@ public class TranslatorsInit { Registry.registerBedrock(AnimatePacket.class, new BedrockAnimateTranslator()); Registry.registerBedrock(CommandRequestPacket.class, new BedrockCommandRequestTranslator()); Registry.registerBedrock(TextPacket.class, new BedrockTextTranslator()); + Registry.registerBedrock(MobEquipmentPacket.class, new BedrockMobEquipmentTranslator()); itemTranslator = new ItemTranslator(); diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/bedrock/BedrockCommandRequestTranslator.java b/connector/src/main/java/org/geysermc/connector/network/translators/bedrock/BedrockCommandRequestTranslator.java index c453c31a..cc2ab4d0 100644 --- a/connector/src/main/java/org/geysermc/connector/network/translators/bedrock/BedrockCommandRequestTranslator.java +++ b/connector/src/main/java/org/geysermc/connector/network/translators/bedrock/BedrockCommandRequestTranslator.java @@ -23,7 +23,6 @@ * @link https://github.com/GeyserMC/Geyser */ - package org.geysermc.connector.network.translators.bedrock; import com.github.steveice10.mc.protocol.packet.ingame.client.ClientChatPacket; diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/bedrock/BedrockMobEquipmentTranslator.java b/connector/src/main/java/org/geysermc/connector/network/translators/bedrock/BedrockMobEquipmentTranslator.java new file mode 100644 index 00000000..7ae5a4ee --- /dev/null +++ b/connector/src/main/java/org/geysermc/connector/network/translators/bedrock/BedrockMobEquipmentTranslator.java @@ -0,0 +1,49 @@ +/* + * 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.bedrock; + +import com.github.steveice10.mc.protocol.packet.ingame.client.player.ClientPlayerChangeHeldItemPacket; +import com.nukkitx.protocol.bedrock.data.ContainerId; +import com.nukkitx.protocol.bedrock.packet.MobEquipmentPacket; +import org.geysermc.connector.network.session.GeyserSession; +import org.geysermc.connector.network.translators.PacketTranslator; + +public class BedrockMobEquipmentTranslator extends PacketTranslator { + + @Override + public void translate(MobEquipmentPacket packet, GeyserSession session) { + if (packet.getHotbarSlot() > 8) + return; + + if (packet.getContainerId() != ContainerId.INVENTORY) + return; + + session.getInventory().setHeldItemSlot(packet.getHotbarSlot()); + + ClientPlayerChangeHeldItemPacket changeHeldItemPacket = new ClientPlayerChangeHeldItemPacket(packet.getHotbarSlot()); + session.getDownstream().getSession().send(changeHeldItemPacket); + } +}