Jump to content

Recommended Posts

Posted (edited)

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
Posted

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

 

Posted
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?

Posted
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?

Posted

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

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