Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@
* An intermediary class made to allow easy access to work-in-progress NBT, such as lore and display.
*/
public final class BedrockItemBuilder {

public static final NbtMap EMPTY_ITEM = BedrockItemBuilder.createItemNbt("", 0, 0).build();

// All Bedrock-style
@Nullable
private String customName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,35 @@
import org.geysermc.geyser.item.parser.ItemStackParser;
import org.geysermc.geyser.level.block.type.BlockState;
import org.geysermc.geyser.session.GeyserSession;
import org.geysermc.geyser.translator.item.BedrockItemBuilder;
import org.geysermc.mcprotocollib.protocol.data.game.level.block.BlockEntityType;

import java.util.Comparator;
import java.util.Arrays;
import java.util.List;

@BlockEntity(type = BlockEntityType.SHELF)
public class ShelfBlockEntityTranslator extends BlockEntityTranslator {

private static final int SLOT_COUNT = 3;

@Override
public void translateTag(GeyserSession session, NbtMapBuilder bedrockNbt, NbtMap javaNbt, BlockState blockState) {
// We can't translate align_items_to_bottom, I think :(
bedrockNbt.putList("Items", NbtType.COMPOUND, javaNbt.getList("Items", NbtType.COMPOUND).stream()
.sorted(Comparator.comparingInt(stack -> stack.getByte("Slot")))
.map(stack -> ItemStackParser.javaItemStackToBedrock(session, stack).build())
.toList());

// Bedrock determines shelf item position by list index, so we must produce
// a dense list with empty items for unoccupied slots.
// Verified with BDS 1.26.0.2
NbtMap[] items = new NbtMap[SLOT_COUNT];
Arrays.fill(items, BedrockItemBuilder.EMPTY_ITEM);

List<NbtMap> javaItems = javaNbt.getList("Items", NbtType.COMPOUND);
for (NbtMap stack : javaItems) {
int slot = stack.getByte("Slot");
if (slot >= 0 && slot < SLOT_COUNT) {
items[slot] = ItemStackParser.javaItemStackToBedrock(session, stack).build();
}
}

bedrockNbt.putList("Items", NbtType.COMPOUND, Arrays.asList(items));
}
}
Loading