Posted March 30, 20214 yr I cant for the life of me find out why my ItemStack wont get updated properly in the Item::onItemUse method. The nbt data is working properly but it wont shrink the stack. Or rather it does shrink it in the local functions but somehow it does get reset afterwards. @Override public ActionResultType onItemUse(ItemUseContext context) { World world = context.getWorld(); if (!world.isRemote) { TileEntity te = world.getTileEntity(context.getPos()); ItemStack stack = context.getPlayer().getHeldItem(context.getHand()); if (te instanceof MarketTileEntity) { this.useOnMarket(stack, te); context.getPlayer().sendStatusMessage(new StringTextComponent("used on market"), false); } if (te instanceof ChestTileEntity || te instanceof BarrelTileEntity) { this.useOnContainer(stack, te); context.getPlayer().sendStatusMessage(new StringTextComponent("used on container"), false); } return ActionResultType.CONSUME; } return ActionResultType.SUCCESS; } private void useOnMarket(ItemStack stack, TileEntity tile) { CompoundNBT nbt = stack.getTag() != null ? stack.getTag() : new CompoundNBT(); BlockPos pos = tile.getPos(); // If no container data is stored, store market if (!nbt.getBoolean("containerInit")) { nbt.putInt("marketX", pos.getX()); nbt.putInt("marketY", pos.getY()); nbt.putInt("marketZ", pos.getZ()); nbt.putBoolean("marketInit", true); stack.setTag(nbt); } else { pos = new BlockPos(nbt.getInt("containerX"), nbt.getInt("containerY"), nbt.getInt("containerZ")); ((MarketTileEntity) tile).registerContainer(pos); nbt.putBoolean("marketInit", false); nbt.putBoolean("containerInit", false); stack.setTag(nbt); stack.shrink(1); } } private void useOnContainer(ItemStack stack, TileEntity tile) { CompoundNBT nbt = stack.getTag() != null ? stack.getTag() : new CompoundNBT(); BlockPos pos = tile.getPos(); // If no market data is stored, store container if (!nbt.getBoolean("marketInit")) { nbt.putInt("containerX", pos.getX()); nbt.putInt("containerY", pos.getY()); nbt.putInt("containerZ", pos.getZ()); nbt.putBoolean("containerInit", true); stack.setTag(nbt); } else { pos = new BlockPos(nbt.getInt("marketX"), nbt.getInt("marketY"), nbt.getInt("marketZ")); ((MarketTileEntity) tile.getWorld().getTileEntity(pos)).registerContainer(tile.getPos()); nbt.putBoolean("marketInit", false); nbt.putBoolean("containerInit", false); stack.setTag(nbt); stack.shrink(1); } }
March 30, 20214 yr Author Big oopsie, guess its been a long day it actually works perfectly fine and as intended
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.