MinecraftScripterAI
Discord ↗
Link Copied to Clipboard
×
"Add an item with a two-slot inventory that can only contain diamonds"
/* This mod creates an item with a two-slot inventory that can only contain diamonds. */ public class DiamondContainerItem extends Item { public DiamondContainerItem(Properties properties) { super(properties); } @Override public ActionResult<ItemStack> use(World world, PlayerEntity player, Hand hand) { if (!world.isClientSide) { NetworkHooks.openGui((ServerPlayerEntity) player, new SimpleNamedContainerProvider((id, inventory, playerEntity) -> { return new DiamondContainer(id, inventory); }, new TranslationTextComponent("container.diamond_container"))); } return ActionResult.success(player.getItemInHand(hand)); } } public class DiamondContainer extends Container { private final IInventory inventory; public DiamondContainer(int id, PlayerInventory playerInventory) { super(ContainerType.GENERIC_9x2, id); this.inventory = new Inventory(2); for (int i = 0; i < 2; i++) { this.addSlot(new Slot(this.inventory, i, 8 + i * 18, 18) { @Override public boolean mayPlace(ItemStack stack) { return stack.getItem() == Items.DIAMOND; } }); } } @Override public boolean stillValid(PlayerEntity player) { return true; } } /* Note: You will need to register the item and container type in your mod's registry events. */