Posted March 14, 20223 yr When you right-click, the container and nests will open normally. But when he wants to put items in his backpack, the items start to duplicate. What did I do wrong? public class BackpackItem extends Item implements MenuProvider, ICapabilityProvider { private final ItemStackHandler itemStackHandler = new ItemStackHandler(15); private final LazyOptional<IItemHandler> lazyOptional = LazyOptional.of(() -> itemStackHandler); public BackpackItem(Properties pProperties) { super(pProperties); } @Override public InteractionResultHolder<ItemStack> use(Level pLevel, Player pPlayer, InteractionHand hand) { if(!pLevel.isClientSide()){ if(hand == InteractionHand.MAIN_HAND){ NetworkHooks.openGui(((ServerPlayer) pPlayer), this); } } return InteractionResultHolder.success(pPlayer.getItemInHand(hand)); } @Override public Component getDisplayName() { return new TextComponent("Backpack"); } @Nullable @Override public AbstractContainerMenu createMenu(int pContainerId, Inventory pInventory, Player pPlayer) { return new BackpackMenu(pContainerId, pInventory, pPlayer.getMainHandItem()); } @NotNull @Override public <T> LazyOptional<T> getCapability(@NotNull Capability<T> cap, @Nullable Direction side) { if(cap == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY){ return lazyOptional.cast(); } return null; } public class BackpackMenu extends AbstractContainerMenu { private final ItemStack backpack; private final Level level; public BackpackMenu(int id, Inventory inventory, FriendlyByteBuf buf){ this(id, inventory, inventory.player.getMainHandItem()); } public BackpackMenu(int pContainerId, Inventory inventory, ItemStack item) { super(ModMenuType.BACKPACK.get(), pContainerId); this.level = inventory.player.level; this.backpack = item; BackpackItem item1 = (BackpackItem) this.backpack.getItem(); item1.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY).ifPresent(handler -> { for(int i = 0; i < 3; ++i) { for(int j = 0; j < 5; ++j) { this.addSlot(new SlotItemHandler(handler, j + i * 3, 44 + j * 18, 16 + i * 18)); } } }); this.addPlayerHotbar(inventory); this.addPlayerInventory(inventory); } @Override public boolean stillValid(Player pPlayer) { return pPlayer.getMainHandItem().is(this.backpack.getItem()); } private void addPlayerInventory(Inventory inv){ for(int i = 0; i < 3; ++i) { for(int j = 0; j < 9; ++j) { this.addSlot(new Slot(inv, j + i * 9 + 9, 8 + j * 18, 84 + i * 18)); } } } private void addPlayerHotbar(Inventory inv){ for(int k = 0; k < 9; ++k) { this.addSlot(new Slot(inv, k, 8 + k * 18, 142)); } } @Override public ItemStack quickMoveStack(Player pPlayer, int pIndex) { ItemStack toReturn = ItemStack.EMPTY; Slot slot = this.getSlot(pIndex); if(slot != null && slot.hasItem()){ ItemStack stack = slot.getItem(); toReturn = stack.copy(); if(pIndex < 15){ if(!this.moveItemStackTo(stack, 15, this.slots.size(), true)){ return ItemStack.EMPTY; } }else if(!this.moveItemStackTo(stack, 0, 15, false)){ return ItemStack.EMPTY; } if(!stack.isEmpty()){ slot.set(ItemStack.EMPTY); }else{ slot.setChanged(); } } return toReturn; } }
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.