Jump to content

1.16 onItemUse() is not updating the itemstack


Titmar

Recommended Posts

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);
		}
	}

 

Link to comment
Share on other sites

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.