Lefan
Lefan

Reputation: 11

FABRIC MODDING - BlockEntityRenderer render item in blockentity inventory

Minecraft Version 1.17.1

I'm trying to create a block similar to a pedestal. It consists of a blockentity with a 1 slot inventory and it'll render whatever is in its inventory on top of the block. I've already tested the 1 slot inventory and made sure it works but I just can't seem to get the blockentityrenderer to get the contents of its inventory :(

@Override
public void render(SwapperBlockEntity entity, float tickDelta, MatrixStack matrices, VertexConsumerProvider vertexConsumers, int light, int overlay) {
    matrices.push();

    ItemStack stack = entity.getStack(0); //literally doesn't work like what???

    // Calculate the current offset in the y value
    double offset = Math.sin((entity.getWorld().getTime() + tickDelta) / 8.0) / 4.0;
    // Move the item
    matrices.translate(0.5, 0.8 + offset, 0.5);

    // Rotate the item
    matrices.multiply(Vec3f.POSITIVE_Y.getDegreesQuaternion((entity.getWorld().getTime() + tickDelta) * 4));

    MinecraftClient.getInstance().getItemRenderer().renderItem(stack, ModelTransformation.Mode.GROUND, light, overlay, matrices, vertexConsumers, 1);

    // Mandatory call after GL calls
    matrices.pop();
}

Above is the render function that gets called every frame. I am trying to grab the ItemStack in slot index 0 by using ItemStack stack = entity.getStack(0); which results in nothing. I am sure the render code works as replacing the above with ItemStack stack = new ItemStack(Items.JUKEBOX, 1); does work.

The only issue is that entity.getStack(0); will return an empty stack even though it has other things in it. There are no errors.

public class SwapperBlockEntity extends ModBlockEntity implements ImplementedInventory {
    private final DefaultedList<ItemStack> items = DefaultedList.ofSize(1, ItemStack.EMPTY);

    public SwapperBlockEntity(BlockPos pos, BlockState state) {
        super(ModBlockEntities.SWAPPER_BLOCK_ENTITY, pos, state);
    }

    @Override
    public void readNbt(NbtCompound nbt){
        super.readNbt(nbt);
        Inventories.readNbt(nbt, items);
    }

    @Override
    public NbtCompound writeNbt(NbtCompound nbt){
        Inventories.writeNbt(nbt, items);
        return super.writeNbt(nbt);
    }

    @Override
    public DefaultedList<ItemStack> getItems() {
        return items;
    }
}

JUKEBOX Render Demo

Inventory Demo

Note: I'm using the default "ImplementedInventory.java" as provided by the fabric wiki

Any help would be appreciated! Thanks.

Upvotes: 0

Views: 1159

Answers (1)

Killar. exe
Killar. exe

Reputation: 46

In Forge I have the same issue. But I find that the block entity in renderer is not updated. The only way to update is to reload the chunk were the block entity is or reload the world.

Upvotes: 0

Related Questions