MinecraftScripterAI
Discord ↗
Link Copied to Clipboard
×
"Show me how to make an item that holds an inventory like a backpack"
/* To create an item that holds an inventory like a backpack, you'll need to create a custom Item class and a custom Container class. Here's a basic example: */ // Custom Item class public class BackpackItem extends Item { public BackpackItem(Properties properties) { super(properties); } // This method is called when the item is right-clicked @Override public ActionResult<ItemStack> use(World world, PlayerEntity player, Hand hand) { if (!world.isClientSide) { // Open the custom container on the server side NetworkHooks.openGui((ServerPlayerEntity) player, new SimpleNamedContainerProvider( (id, playerInventory, playerEntity) -> new BackpackContainer(id, playerInventory), new StringTextComponent("Backpack"))); } return ActionResult.success(player.getItemInHand(hand)); } } // Custom Container class public class BackpackContainer extends Container { private static final int INVENTORY_SIZE = 27; // Size of the backpack inventory public BackpackContainer(int id, PlayerInventory playerInventory) { super(null, id); // Add slots for the backpack inventory for (int i = 0; i < INVENTORY_SIZE; i++) { this.addSlot(new Slot(new Inventory(INVENTORY_SIZE), i, 8 + i % 9 * 18, 18 + i / 9 * 18)); } // Add slots for the player's inventory for (int i = 0; i < 3; ++i) { for (int j = 0; j < 9; ++j) { this.addSlot(new Slot(playerInventory, j + i * 9 + 9, 8 + j * 18, 86 + i * 18)); } } for (int i = 0; i < 9; ++i) { this.addSlot(new Slot(playerInventory, i, 8 + i * 18, 144)); } } @Override public boolean stillValid(PlayerEntity player) { return true; } } /* You'll also need to register your item and container in your mod's registry event classes. */