public class DemoBlock extends BlockWithEntity { [...] @Override protected ItemActionResult onUseWithItem(ItemStack stack, BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) { if (world.isClient) return ActionResult.SUCCESS; if (!(world.getBlockEntity(pos) instanceof DemoBlockEntity blockEntity)) { return ItemActionResult.PASS_TO_DEFAULT_BLOCK_INTERACTION; } if (!player.getStackInHand(hand).isEmpty()) { // Check what is the first open slot and put an item from the player's hand there if (blockEntity.getStack(0).isEmpty()) { // Put the stack the player is holding into the inventory blockEntity.setStack(0, player.getStackInHand(hand).copy()); // Remove the stack from the player's hand player.getStackInHand(hand).setCount(0); } else if (blockEntity.getStack(1).isEmpty()) { blockEntity.setStack(1, player.getStackInHand(hand).copy()); player.getStackInHand(hand).setCount(0); } else { // If the inventory is full we'll notify the player player.sendMessage(Text.literal("The inventory is full! The first slot holds ") .append(blockEntity.getStack(0).getName()) .append(" and the second slot holds ") .append(blockEntity.getStack(1).getName())); } } return ItemActionResult.SUCCESS; } }