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

So my problem seems pretty simple:

I want to damage an ItemStack, even when the Player is in Creative Mode

 

So far, i've used

ItemStack#setItemDamage

ItemStack#damageItem

Item#setDamage

 

but nothing has worked. I've passed null as EntityLivingBase in ItemStack#damageItem, results in a NullPointerException. I have even passed a newly created EntityPig as argument, it still somehow did the creative mode check and didn't damage the item...

So, what is the solution here? Events? Nbt? Or am I just simply overlooking something major?

Edited by Tschipp
Grammar

  • Author
12 minutes ago, diesieben07 said:

See my reply in the other thread:

 

No, that sadly also doesn't do the trick

 

  • Author

Beware, it's a mess:

public static void addHeat(ItemStack stack, int heat)
	{
		stack.getItem().setDamage(stack, stack.getItemDamage() - heat);
		//stack.damageItem(-heat, new EntityPig(null));

	}

	public static void removeHeat(ItemStack stack, int heat)
	{
		stack.getItem().setDamage(stack, stack.getItemDamage() + heat);
		//stack.damageItem(heat, new EntityPig(null));
	}



	@Override
	public ActionResult<ItemStack> onItemRightClick(World world, EntityPlayer player, EnumHand hand)
	{
		super.onItemRightClick(world, player, hand);
		ItemStack stack = player.getHeldItem(hand);
		RayTraceResult ray = this.rayTrace(world, player, true);
		ItemStack stack2 = stack.copy();

		if(ray != null && ray.getBlockPos() != null)
		{
			BlockPos pos = ray.getBlockPos();
			if(world.getBlockState(pos).getBlock() == Blocks.LAVA)
			{
				addHeat(stack2, 10);
				world.setBlockToAir(ray.getBlockPos());
				return new ActionResult(EnumActionResult.SUCCESS, stack2);


			}
		}

		return new ActionResult(EnumActionResult.PASS, stack);


	}

	@Override
	public EnumActionResult onItemUse(EntityPlayer player, World world, BlockPos pos, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ)
	{
		ItemStack stack = player.getHeldItem(hand);
		IBlockState state = world.getBlockState(pos);
		Block block = state.getBlock();
		int meta = block.getMetaFromState(state);
		Map<ItemStack, ItemStack> smelting = FurnaceRecipes.instance().getSmeltingList();
		ItemStack blockstack = new ItemStack(block, 1, meta);
		ItemStack stack2 = stack.copy();
		if(stack.getItemDamage() < 1000 && !world.isRemote)
		{
			if(FurnaceRecipes.instance().getSmeltingResult(blockstack) != null)
			{
				ItemStack output = FurnaceRecipes.instance().getSmeltingResult(blockstack);
				Block outputblock = Block.getBlockFromItem(output.getItem());
				int outputmeta = output.getMetadata();
				if(outputblock != Blocks.AIR || !FIConfig.heatTalismanCreatesItems)
				{
					IBlockState outputstate = outputblock.getStateFromMeta(outputmeta);
					world.setBlockState(pos, outputstate);
					removeHeat(stack2, 1);
					((WorldServer)world).spawnParticle(EnumParticleTypes.SMOKE_LARGE, (double)pos.getX() + 0.5D, (double)pos.getY() + 0.25D, (double)pos.getZ() + 0.5D, 8, 0.5D, 0.25D, 0.5D, 0.0D, new int[0]);
					world.playSound((EntityPlayer)null, player.posX, player.posY, player.posZ, SoundEvents.BLOCK_FIRE_EXTINGUISH, SoundCategory.BLOCKS, 0.3F, 0.8F / (itemRand.nextFloat() * 0.1F + 0.8F));
					player.setHeldItem(hand, stack2);
					return EnumActionResult.SUCCESS;
				}
				else if(!output.isEmpty())
				{
					removeHeat(stack2, 1);
					output.setCount(1);
					world.spawnEntity(new EntityItem(world, pos.getX() + 0.5, pos.getY(), pos.getZ() + 0.5, output));
					world.setBlockToAir(pos);
					((WorldServer)world).spawnParticle(EnumParticleTypes.SMOKE_LARGE, (double)pos.getX() + 0.5D, (double)pos.getY() + 0.25D, (double)pos.getZ() + 0.5D, 8, 0.25D, 0.25D, 0.25D, 0.0D, new int[0]);
					world.playSound((EntityPlayer)null, player.posX, player.posY, player.posZ, SoundEvents.BLOCK_FIRE_EXTINGUISH, SoundCategory.BLOCKS, 0.3F, 0.8F / (itemRand.nextFloat() * 0.1F + 0.8F));
					player.setHeldItem(hand, stack2);
					return EnumActionResult.SUCCESS;

				}

			}

		}
		return EnumActionResult.FAIL;
	}

 

  • Author
3 minutes ago, diesieben07 said:
  • Why do you call super.onItemRightClick but then completely ignore the result?

I implement another class made by me, it's just some debug stuff.

 

4 minutes ago, diesieben07 said:
  • Never ever call Block::getMetaFromState (or Block::getStateFromMeta).

What should I use instead?

  • Author
2 minutes ago, diesieben07 said:

Depends on what you want to achieve. As far as I can tell from your code you are trying to smelt a block in the world, right? The most accurate ItemStack representation of a block you can get is by calling getPickBlock on the Block. And then to place it back down you'd have to simulate onItemUse on the Item, but the block might smelt into an item that does not represent a block (e.g. Iron Ore into Iron), you would have to handle that.

Ok, I'll try it. But out of sheer why should I not use these two methods? I see that Block::getStateFromMeta is deprecated, but why?

(Note: block-meta-4 <==> item-meta-4 only by coincidence (ease of programmer conversion), but there does exist one usage in vanilla code that assumes that they are exactly the same, and technically a bug from the perspective of the Forge team)

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

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.