Jump to content

[1.10.2] [UN/SOLVED] Item NBT


Bektor

Recommended Posts

Hi,

 

I've got an item which should display an text in the tooltip. My problem is now, how can I get it to work

that each Item shows a different text (and that when the world get reloaded the same text applies to the same item as before).

 

Currently I've got this code here and it works fine, but at the moment where I'm crafting multiply items at the same time (like having a stack of all materials in the crafting table and then clicking shift) it does no longer work. :/

 

Since it been a very long time where I last dealed with such functionality item items, I don't even know if this is the best way to do it.

public class ItemCeuiScript extends Item
{

private Random random = new Random();

public ItemCeuiScript(String unlocalizedName, String registryName)
{
	this.setMaxStackSize(1);
	this.setUnlocalizedName(unlocalizedName);
	this.setRegistryName(new ResourceLocation(Reference.MODID, registryName));
}

@Override
public void onCreated(ItemStack stack, World worldIn, EntityPlayer playerIn) 
{
	int chapter = random.nextInt((8 - 1) + 1) + 1;

	// Create a new NBT Tag Compound if one doesn't already exist, or you will crash
	if(!stack.hasTagCompound())
		stack.setTagCompound(new NBTTagCompound());

	stack.getTagCompound().setString("lore", UtilsScCeui.getLine(chapter));
}

@Override
public void addInformation(ItemStack stack, EntityPlayer playerIn, List<String> tooltip, boolean advanced) 
{
	if(stack.hasTagCompound() && stack.getTagCompound().getString("lore") != null)
		tooltip.add(stack.getTagCompound().getString("lore"));
}
}

 

Thx in advance.

Bektor

Developer of Primeval Forest.

Link to comment
Share on other sites

  • First, why do you store the lore itself, if it seems to come from a constant source in your utility class? Store the index instead. That way you can also adapt the lore based on the client language instead of it being hardcoded.
  • Don't rely on
    onCreated

    . It will not work for things taken from the creative inventory and other mods that produce crafting results might not call it. Instead make a method

    int getLoreIndex(ItemStack)

    which first checks if the lore already exists and if so returns it. Otherwise it initializes the lore however you like and then returns it. Then whenever you need the lore (no exceptions!) you call this method.

Ok, I changed it now and it does still not work like I want it to be:

public class ItemCeuiScript extends Item
{

private Random random = new Random();

public ItemCeuiScript(String unlocalizedName, String registryName)
{
	this.setMaxStackSize(1);
	this.setUnlocalizedName(unlocalizedName);
	this.setCreativeTab(ModCreativeTab.cTab);
	this.setRegistryName(new ResourceLocation(Reference.MODID, registryName));
}

private int getLoreIndex(ItemStack stack)
{
	if(!stack.hasTagCompound())
		stack.setTagCompound(new NBTTagCompound());

	if(stack.getTagCompound().getString("lore") == null) 
	{
		int chapter = this.random.nextInt((8 - 1) + 1) + 1;
		stack.getTagCompound().setInteger("lore", UtilsScCeui.getIndexFromID(chapter));
	}

	return stack.getTagCompound().getInteger("lore");
}

@Override
public void addInformation(ItemStack stack, EntityPlayer playerIn, List<String> tooltip, boolean advanced) 
{
	tooltip.add(UtilsScCeui.getTypeFromID(getLoreIndex(stack)).getText());
}
}

 

Now it shows only one text, even when I create a thousand items.

Developer of Primeval Forest.

Link to comment
Share on other sites

	private int getLoreIndex(ItemStack stack)
{
	if(!stack.hasTagCompound())
		stack.setTagCompound(new NBTTagCompound());

	if(stack.getTagCompound().getString("lore") == null) 
	{
		int chapter = this.random.nextInt((8 - 1) + 1) + 1;
		stack.getTagCompound().setInteger("lore", UtilsScCeui.getIndexFromID(chapter));
	}

	return stack.getTagCompound().getInteger("lore");
}

 

You're still pulling the lore from the NBT.  Stop that.

This function should look like this:

 

private string getLoreIndex(ItemStack stack) {
    int loreID = stack.getTagCompound().getInteger("lore");
    return UtilsScCeui.getIndexFromID(loreID);
}

 

Along with the various "hasNBT" and "hasKey" checks.

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.

Link to comment
Share on other sites

private int getLoreIndex(ItemStack stack)

{

if(!stack.hasTagCompound())

stack.setTagCompound(new NBTTagCompound());

 

if(stack.getTagCompound().getString("lore") == null)

{

int chapter = this.random.nextInt((8 - 1) + 1) + 1;

stack.getTagCompound().setInteger("lore", UtilsScCeui.getIndexFromID(chapter));

}

 

return stack.getTagCompound().getInteger("lore");

}[/code]

 

You're still pulling the lore from the NBT.  Stop that.

This function should look like this:

 

private string getLoreIndex(ItemStack stack) {
    int loreID = stack.getTagCompound().getInteger("lore");
    return UtilsScCeui.getIndexFromID(loreID);
}

 

Along with the various "hasNBT" and "hasKey" checks.

Well, but then the item does not have the nbt tag lore, because it's never created.

And what do you mean by "pulling the lore from the NBT"?

Developer of Primeval Forest.

Link to comment
Share on other sites

Well, but then the item does not have the nbt tag lore, because it's never created.

And what do you mean by "pulling the lore from the NBT"?

 

Cough:

Along with the various "hasNBT" and "hasKey" checks.

 

I didn't type out the null checks because I figured you were intelligent enough to know what I'd meant.  Point is: don't store the lore string in the NBT.

 

Looking back over what you had, things actually may have been fine, you just have a weird "convert from index to chapter" method (honestly, what do that even do, add 1? Subtract 1? Return its input?).

 

Also:

this.random.nextInt((8 - 1) + 1) + 1;

 

"8 - 1, +1"  Do you mean "8" per chance?

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.

Link to comment
Share on other sites

 

-----

 

And if I don't store the lore String in the nbt tag, how should it work then? How should the Item remember which text to show while the second instance of the item has a complete different text to show? And how should I get the integer "lore" when I don't set it.

 

 

 

 

Well, the random thing isn't from me, a friend implemented this and I just didn't changed it because it is working. (never change a running system :P)

And what I am doing? I'm having an integer chapter which calculates a random number and then the getIndexFromID method get's the correct index at the given position, which basically means, it returns the index from the id. And thinking about it, the name might not be the best.

 

So, chapter calculates a number, some kind of id... Then I've got a list (made out of an enum) and the method getIndexFromID returns the ID or index of the object at that position or 0 when the calculated chapter thing returns a number like for example 20 when the array is 10 long.

Developer of Primeval Forest.

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.