Jump to content

[1.7.10] Saving Item Mode


Rohzek

Recommended Posts

Hello. I am trying to come up with a way to have an item that supports mode changing.

 

While the mode changing itself is fine, I want a way to save what mode it was in the last time the player used it. I tried sticking it in an NBTTagCompound which.. there doesn't seem to be a way to save in the standard Item based class so it only saves until Minecraft is restarted. I see some kind of saving method by passing that compound into itemStack.TagCompound(); but am unsure how to use that exactly. Outside of specific functions I don't seem to have access to the ItemStack... Would storing it there and pulling it out on load with the onUpdate function work? I assume all overridden functions get the same ItemStack passed into it? Or is there a better way of doing this or somewhere else to store my NBT Compound? (I original thought of trying to store this value in damage values?)

 

My class as it is now: http://pastebin.com/jdbjFiwF

Developing the Spiral Power Mod .

こんにちは!お元気ですか?

Link to comment
Share on other sites

Hello. I am trying to come up with a way to have an item that supports mode changing.

 

While the mode changing itself is fine, I want a way to save what mode it was in the last time the player used it. I tried sticking it in an NBTTagCompound which.. there doesn't seem to be a way to save in the standard Item based class so it only saves until Minecraft is restarted. I see some kind of saving method by passing that compound into itemStack.TagCompound(); but am unsure how to use that exactly. Outside of specific functions I don't seem to have access to the ItemStack... Would storing it there and pulling it out on load with the onUpdate function work? I assume all overridden functions get the same ItemStack passed into it? Or is there a better way of doing this or somewhere else to store my NBT Compound? (I original thought of trying to store this value in damage values?)

 

My class as it is now: http://pastebin.com/jdbjFiwF

 

Item

s are singletons, only one instance of the class exists for each type. If you want to store data for individual items, you need to use the

NBTTagCompound

of the

ItemStack

that you receive as an argument of various

Item

methods. If you don't have an

ItemStack

argument, you don't have access to the data.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Link to comment
Share on other sites

That's what I thought. Shame there isn't anywhere else.

So I've gotten something working by doing exactly what I mentioned in my first post: passing the NBT Compound to the ItemStack and checking for it in onUpdate which seems to start running well before visuals load in so it effectively does exactly what I need (setting textures on load). I can't help but feel though tying this into the onUpdate function seems a little much checking the boolean for loading every frame for the rest of time you hold the item... But I don't guess it would slow anything down. Is there a major downside to this?

 

 

 

// onUpdate seems to run while the game is loading to start simulating the world.. Thus the change happens before the player can see the item
@Override
public void onUpdate(ItemStack itemStack, World world, Entity entity, int x, boolean a)
{
	if(!hasLoaded)
	{
		retrieveMode(itemStack);
		hasLoaded = true;
	}

}

// We need to this every time we change the mode.
private void saveMode(ItemStack itemStack)
{
	dataStore.setInteger("changeNum", changeNum);
	itemStack.setTagCompound(dataStore);
}

// Gets the number out of our NBTCompound
private void retrieveMode(ItemStack itemStack)
{
	dataStore = itemStack.getTagCompound();
	changeNum = dataStore.getInteger("changeNum");
}

 

 

 

If this isn't a horrible atrocity I'll toss in a null check to make sure it doesn't try to load before there's data there and call it done?

If this is seriously bad I guess I'll have to try to set it to damage states which I imagine get saved with the item.

Developing the Spiral Power Mod .

こんにちは!お元気ですか?

Link to comment
Share on other sites

That won't work. Only one instance of the class exists, so any fields are shared between all stacks of that item type.

 

You need to store all data in the NBT, read from and write to the stack's tag compound directly rather than storing data in fields.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Link to comment
Share on other sites

This.... is true. And infact the way the class is currently set up it... it just changes all guns in the world at the same time... I... think I have to base the mode off damage states to make it work anyway... Yeah.. Thanks for pointing me in the right direction. :)

Developing the Spiral Power Mod .

こんにちは!お元気ですか?

Link to comment
Share on other sites

Apologies for a double post, but I'm not sure anyone would have checked if I just edited the last one, but I wonder.. assuming I found a way to keep the mode change out of the class and only passed it between functions in the ItemStack NBT... Is there any way I could use the number from the ItemStack NBT to change the texture drawn on a specific ItemStack? While damage states would solve my problem this time... I'm thinking if I had something like a pick that I wanted to change texture on right click and didn't have the damage states available.. How would that be done?

Developing the Spiral Power Mod .

こんにちは!お元気ですか?

Link to comment
Share on other sites

Metadata, it gets passed as a parameter to the getIIcon method(assuming you are in 1.7.10). You can control the metadata value anywhere you have access to the ItemStack (onItemRightClick) and in you getIIcon method you would return a different thing based on the metaData. You might have to override a lot of other methods like damaging an item, so that damage values are held in NBT instead of metadata.

Current Project: Armerger 

Planned mods: Light Drafter  | Ore Swords

Looking for help getting a mod off the ground? Coding  | Textures

Link to comment
Share on other sites

That is absolutely right. With the loss of item IDs (and the fact that the Item.class docs mention placing blocks with the item) it went invisible and it slipped my mind but.. Thing's like wool and dye are the same item with different textures based on metadata aren't they? That would work beautifully for not only my current issue but any further items with a similar idea. Thank you so much

Developing the Spiral Power Mod .

こんにちは!お元気ですか?

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.