Jump to content

Recommended Posts

Posted

Hello everyone:

I am having problems with adding and getting NBT tags off of a custom item.

When a BlockOre drops this item, a HarvestDropsEvent is fired:

@SubscribeEvent
public void GetCorrectOreDrops(HarvestDropsEvent event)
{
	if (event.block instanceof BlockOre)
	{

		//STOP NOW if Block is being silk touched
		if (event.isSilkTouching)
		{
			event.drops.clear();
			event.drops.add(Stack.S(event.block));
		}

		int nuggets = (event.world.rand.nextInt(16) + event.fortuneLevel * event.world.rand.nextInt(5) + 1);

		ItemStack stack = event.drops.get(0);
		stack = ORef.lumpOre.addOre((BlockOre) event.block, nuggets, stack);
		event.drops.set(0, stack);
		event.dropChance = 1.0F;
	}
}

 

This refers to a NBT adding script in the item (addOre):

public ItemStack addOre(BlockOre ore, int nuggets, ItemStack stack)
{
	NBTTagList taglist = (stack.stackTagCompound != null && stack.stackTagCompound.hasKey("Ores")) ? (NBTTagList) stack.stackTagCompound.getTag("BiggerDim") : new NBTTagList();
	NBTTagCompound tag = new NBTTagCompound();

	tag.setInteger("nuggets", nuggets);
	tag.setInteger("ore", ore.getIdFromBlock(ore));
	taglist.appendTag(tag);

	System.out.println(tag.getInteger("ore"));

	stack.stackTagCompound.setTag("Ores", taglist);

	return stack;
}

 

Whenever I harvest the block, the game crashes. What am I missing?

 

 

Posted

This helped me understand NBT greatly "

 

But the issue is you are forgetting to check to see if the itemstack has a NBTTagCompound

 

if (itemStack.stackTagCompound == null) {
		itemStack.setTagCompound(new NBTTagCompound());
	}

 

you are probably getting a crash of something being null right? Watch the video and all will be revealed to you. (also the posted code snippet is how you make sure not to get a crash trying to add to a null object)

Posted

The actual crash comes from a ticking memory connection with a whole bunch of forge/minecraft code, but it is caused by a null pointer in

stack.stackTagCompound.setTag("Ores", taglist);

in the second code bit.

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.