Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

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

 

  • Author

Big oopsie, guess its been a long day :D 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.

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.