From acbcf4baf32f3cf21730cbe3aa1270d88ae2d25b Mon Sep 17 00:00:00 2001 From: Camotoy <20743703+Camotoy@users.noreply.github.com> Date: Thu, 25 Feb 2021 21:51:50 -0500 Subject: [PATCH] Fix shulker boxes resetting data and set all shulker boxes as valid --- .../holder/BlockInventoryHolder.java | 6 ++-- .../AbstractBlockInventoryTranslator.java | 11 +++++++ .../ShulkerInventoryTranslator.java | 33 ++++++++++++++++++- .../ShulkerBoxBlockEntityTranslator.java | 8 ++++- 4 files changed, 53 insertions(+), 5 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 3186a85e9..8ea24053a 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 @@ -82,7 +82,7 @@ public class BlockInventoryHolder extends InventoryHolder { // We can safely use this block inventory.setHolderPosition(session.getLastInteractionBlockPosition()); ((Container) inventory).setUsingRealBlock(true, javaBlockString[0]); - setCustomName(session, session.getLastInteractionBlockPosition(), inventory); + setCustomName(session, session.getLastInteractionBlockPosition(), inventory, javaBlockId); return; } } @@ -98,7 +98,7 @@ public class BlockInventoryHolder extends InventoryHolder { session.sendUpstreamPacket(blockPacket); inventory.setHolderPosition(position); - setCustomName(session, position, inventory); + setCustomName(session, position, inventory, defaultJavaBlockState); } /** @@ -108,7 +108,7 @@ public class BlockInventoryHolder extends InventoryHolder { return this.validBlocks.contains(javaBlockString[0]); } - protected void setCustomName(GeyserSession session, Vector3i position, Inventory inventory) { + protected void setCustomName(GeyserSession session, Vector3i position, Inventory inventory, int javaBlockState) { NbtMap tag = NbtMap.builder() .putInt("x", position.getX()) .putInt("y", position.getY()) diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/inventory/translators/AbstractBlockInventoryTranslator.java b/connector/src/main/java/org/geysermc/connector/network/translators/inventory/translators/AbstractBlockInventoryTranslator.java index 8664dc192..49caef13b 100644 --- a/connector/src/main/java/org/geysermc/connector/network/translators/inventory/translators/AbstractBlockInventoryTranslator.java +++ b/connector/src/main/java/org/geysermc/connector/network/translators/inventory/translators/AbstractBlockInventoryTranslator.java @@ -53,6 +53,17 @@ public abstract class AbstractBlockInventoryTranslator extends BaseInventoryTran this.updater = updater; } + /** + * @param size the amount of slots that the inventory adds alongside the base inventory slots + * @param holder the custom block holder + * @param updater updater + */ + public AbstractBlockInventoryTranslator(int size, InventoryHolder holder, InventoryUpdater updater) { + super(size); + this.holder = holder; + this.updater = updater; + } + @Override public void prepareInventory(GeyserSession session, Inventory inventory) { holder.prepareInventory(this, session, inventory); diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/inventory/translators/ShulkerInventoryTranslator.java b/connector/src/main/java/org/geysermc/connector/network/translators/inventory/translators/ShulkerInventoryTranslator.java index 43584df41..76d1cb1cf 100644 --- a/connector/src/main/java/org/geysermc/connector/network/translators/inventory/translators/ShulkerInventoryTranslator.java +++ b/connector/src/main/java/org/geysermc/connector/network/translators/inventory/translators/ShulkerInventoryTranslator.java @@ -25,14 +25,45 @@ package org.geysermc.connector.network.translators.inventory.translators; +import com.nukkitx.math.vector.Vector3i; +import com.nukkitx.nbt.NbtMap; +import com.nukkitx.nbt.NbtMapBuilder; import com.nukkitx.protocol.bedrock.data.inventory.ContainerSlotType; import com.nukkitx.protocol.bedrock.data.inventory.ContainerType; +import com.nukkitx.protocol.bedrock.packet.BlockEntityDataPacket; +import org.geysermc.connector.inventory.Inventory; +import org.geysermc.connector.network.session.GeyserSession; import org.geysermc.connector.network.translators.inventory.BedrockContainerSlot; +import org.geysermc.connector.network.translators.inventory.holder.BlockInventoryHolder; import org.geysermc.connector.network.translators.inventory.updater.ContainerInventoryUpdater; +import org.geysermc.connector.network.translators.world.block.entity.BlockEntityTranslator; public class ShulkerInventoryTranslator extends AbstractBlockInventoryTranslator { public ShulkerInventoryTranslator() { - super(27, "minecraft:shulker_box[facing=north]", ContainerType.CONTAINER, ContainerInventoryUpdater.INSTANCE); + super(27, new BlockInventoryHolder("minecraft:shulker_box[facing=north]", ContainerType.CONTAINER) { + private final BlockEntityTranslator shulkerBoxTranslator = BlockEntityTranslator.BLOCK_ENTITY_TRANSLATORS.get("ShulkerBox"); + + @Override + protected boolean isValidBlock(String[] javaBlockString) { + return javaBlockString[0].contains("shulker_box"); + } + + @Override + protected void setCustomName(GeyserSession session, Vector3i position, Inventory inventory, int javaBlockState) { + NbtMapBuilder tag = NbtMap.builder() + .putInt("x", position.getX()) + .putInt("y", position.getY()) + .putInt("z", position.getZ()) + .putString("CustomName", inventory.getTitle()); + // Don't reset facing property + shulkerBoxTranslator.translateTag(tag, null, javaBlockState); + + BlockEntityDataPacket dataPacket = new BlockEntityDataPacket(); + dataPacket.setData(tag.build()); + dataPacket.setBlockPosition(position); + session.sendUpstreamPacket(dataPacket); + } + }, ContainerInventoryUpdater.INSTANCE); } @Override diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/world/block/entity/ShulkerBoxBlockEntityTranslator.java b/connector/src/main/java/org/geysermc/connector/network/translators/world/block/entity/ShulkerBoxBlockEntityTranslator.java index c09d2f99d..04d58fcce 100644 --- a/connector/src/main/java/org/geysermc/connector/network/translators/world/block/entity/ShulkerBoxBlockEntityTranslator.java +++ b/connector/src/main/java/org/geysermc/connector/network/translators/world/block/entity/ShulkerBoxBlockEntityTranslator.java @@ -29,10 +29,16 @@ import com.github.steveice10.opennbt.tag.builtin.CompoundTag; import com.nukkitx.nbt.NbtMapBuilder; import org.geysermc.connector.network.translators.world.block.BlockStateValues; +import javax.annotation.Nullable; + @BlockEntity(name = "ShulkerBox") public class ShulkerBoxBlockEntityTranslator extends BlockEntityTranslator { + /** + * Also used in {@link org.geysermc.connector.network.translators.inventory.translators.ShulkerInventoryTranslator} + * where {@code tag} is passed as null. + */ @Override - public void translateTag(NbtMapBuilder builder, CompoundTag tag, int blockState) { + public void translateTag(NbtMapBuilder builder, @Nullable CompoundTag tag, int blockState) { byte direction = BlockStateValues.getShulkerBoxDirection(blockState); // Just in case... if (direction == -1) {