Skip to content

Commit

Permalink
Fix shulker boxes resetting data and set all shulker boxes as valid
Browse files Browse the repository at this point in the history
  • Loading branch information
Camotoy committed Feb 26, 2021
1 parent e1db626 commit acbcf4b
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public void prepareInventory(InventoryTranslator translator, GeyserSession sessi
// 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;
}
}
Expand All @@ -98,7 +98,7 @@ public void prepareInventory(InventoryTranslator translator, GeyserSession sessi
session.sendUpstreamPacket(blockPacket);
inventory.setHolderPosition(position);

setCustomName(session, position, inventory);
setCustomName(session, position, inventory, defaultJavaBlockState);
}

/**
Expand All @@ -108,7 +108,7 @@ protected boolean isValidBlock(String[] javaBlockString) {
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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,17 @@ public AbstractBlockInventoryTranslator(int size, String javaBlockIdentifier, Co
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,16 @@
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) {
Expand Down

0 comments on commit acbcf4b

Please sign in to comment.