From b57109ddf7dabe77ca97f36de48d7266eaec08a1 Mon Sep 17 00:00:00 2001 From: Kevin Ludwig Date: Mon, 12 Sep 2022 12:23:36 +0200 Subject: [PATCH] Revert use entities for single chest inventories --- .../chest/SingleChestInventoryTranslator.java | 95 ++++--------------- 1 file changed, 20 insertions(+), 75 deletions(-) diff --git a/core/src/main/java/org/geysermc/geyser/translator/inventory/chest/SingleChestInventoryTranslator.java b/core/src/main/java/org/geysermc/geyser/translator/inventory/chest/SingleChestInventoryTranslator.java index 3d3b4e6bd..ae914ed8c 100644 --- a/core/src/main/java/org/geysermc/geyser/translator/inventory/chest/SingleChestInventoryTranslator.java +++ b/core/src/main/java/org/geysermc/geyser/translator/inventory/chest/SingleChestInventoryTranslator.java @@ -25,99 +25,44 @@ package org.geysermc.geyser.translator.inventory.chest; -import com.nukkitx.math.vector.Vector3f; -import com.nukkitx.nbt.NbtMap; -import com.nukkitx.protocol.bedrock.data.entity.EntityData; -import com.nukkitx.protocol.bedrock.data.entity.EntityFlag; -import com.nukkitx.protocol.bedrock.data.entity.EntityFlags; import com.nukkitx.protocol.bedrock.data.inventory.ContainerType; -import com.nukkitx.protocol.bedrock.packet.AddEntityPacket; -import com.nukkitx.protocol.bedrock.packet.BlockEntityDataPacket; -import com.nukkitx.protocol.bedrock.packet.ContainerClosePacket; -import com.nukkitx.protocol.bedrock.packet.ContainerOpenPacket; -import com.nukkitx.protocol.bedrock.packet.RemoveEntityPacket; -import org.geysermc.geyser.inventory.Container; import org.geysermc.geyser.inventory.Inventory; -import org.geysermc.geyser.registry.BlockRegistries; +import org.geysermc.geyser.inventory.holder.BlockInventoryHolder; +import org.geysermc.geyser.inventory.holder.InventoryHolder; import org.geysermc.geyser.session.GeyserSession; public class SingleChestInventoryTranslator extends ChestInventoryTranslator { + private final InventoryHolder holder; + public SingleChestInventoryTranslator(int size) { super(size, 27); + this.holder = new BlockInventoryHolder("minecraft:chest[facing=north,type=single,waterlogged=false]", ContainerType.CONTAINER, + "minecraft:ender_chest", "minecraft:trapped_chest") { + @Override + protected boolean isValidBlock(String[] javaBlockString) { + if (javaBlockString[0].equals("minecraft:ender_chest")) { + // Can't have double ender chests + return true; + } + + // Add provision to ensure this isn't a double chest + return super.isValidBlock(javaBlockString) && (javaBlockString.length > 1 && javaBlockString[1].contains("type=single")); + } + }; } @Override public boolean prepareInventory(GeyserSession session, Inventory inventory) { - // See BlockInventoryHolder - same concept there except we're also dealing with a specific block state - if (session.getLastInteractionPlayerPosition().equals(session.getPlayerEntity().getPosition())) { - int javaBlockId = session.getGeyser().getWorldManager().getBlockAt(session, session.getLastInteractionBlockPosition()); - String[] javaBlockString = BlockRegistries.JAVA_IDENTIFIERS.get().getOrDefault(javaBlockId, "minecraft:air").split("\\["); - if (javaBlockString[0].equals("minecraft:ender_chest") || javaBlockString.length > 1 && (javaBlockString[0].equals("minecraft:chest") || javaBlockString[0].equals("minecraft:trapped_chest")) - && javaBlockString[1].contains("type=single")) { - inventory.setHolderPosition(session.getLastInteractionBlockPosition()); - ((Container) inventory).setUsingRealBlock(true, javaBlockString[0]); - - NbtMap tag = NbtMap.builder() - .putInt("x", session.getLastInteractionBlockPosition().getX()) - .putInt("y", session.getLastInteractionBlockPosition().getY()) - .putInt("z", session.getLastInteractionBlockPosition().getZ()) - .putString("CustomName", inventory.getTitle()).build(); - BlockEntityDataPacket dataPacket = new BlockEntityDataPacket(); - dataPacket.setData(tag); - dataPacket.setBlockPosition(session.getLastInteractionBlockPosition()); - session.sendUpstreamPacket(dataPacket); - - return true; - } - } - - long entityId = session.getEntityCache().getNextEntityId().incrementAndGet(); - AddEntityPacket addEntityPacket = new AddEntityPacket(); - addEntityPacket.setUniqueEntityId(entityId); - addEntityPacket.setRuntimeEntityId(entityId); - addEntityPacket.setIdentifier("minecraft:creeper"); - addEntityPacket.setPosition(session.getPlayerEntity().getPosition()); - addEntityPacket.setMotion(Vector3f.ZERO); - addEntityPacket.setRotation(Vector3f.ZERO); - EntityFlags entityFlags = new EntityFlags(); - entityFlags.setFlag(EntityFlag.INVISIBLE, true); - addEntityPacket.getMetadata() - .putFlags(entityFlags) - .putFloat(EntityData.SCALE, 0F) - .putFloat(EntityData.BOUNDING_BOX_WIDTH, 0F) - .putFloat(EntityData.BOUNDING_BOX_HEIGHT, 0F) - .putString(EntityData.NAMETAG, inventory.getTitle()) - .putInt(EntityData.CONTAINER_BASE_SIZE, inventory.getSize()); - session.sendUpstreamPacket(addEntityPacket); - inventory.setHolderId(entityId); - - return true; + return holder.prepareInventory(this, session, inventory); } @Override public void openInventory(GeyserSession session, Inventory inventory) { - ContainerOpenPacket containerOpenPacket = new ContainerOpenPacket(); - containerOpenPacket.setId((byte) inventory.getBedrockId()); - containerOpenPacket.setType(ContainerType.CONTAINER); - containerOpenPacket.setBlockPosition(inventory.getHolderPosition()); - containerOpenPacket.setUniqueEntityId(inventory.getHolderId()); - session.sendUpstreamPacket(containerOpenPacket); + holder.openInventory(this, session, inventory); } @Override public void closeInventory(GeyserSession session, Inventory inventory) { - if (((Container) inventory).isUsingRealBlock()) { - // No need to reset a block since we didn't change any blocks - // But send a container close packet because we aren't destroying the original. - ContainerClosePacket packet = new ContainerClosePacket(); - packet.setId((byte) inventory.getBedrockId()); - packet.setUnknownBool0(true); //TODO needs to be changed in Protocol to "server-side" or something - session.sendUpstreamPacket(packet); - return; - } - - RemoveEntityPacket removeEntityPacket = new RemoveEntityPacket(); - removeEntityPacket.setUniqueEntityId(inventory.getHolderId()); - session.sendUpstreamPacket(removeEntityPacket); + holder.closeInventory(this, session, inventory); } }