Jump to content

Saving a CompoundTag inside the root tag


Curlip

Recommended Posts

Ok so I've been working with NBT and some custom interfaces, my problem is that I can't figure out how to save a Compound to the root of an ItemStack.

 

Their is no setTagCompound() method and the closest I can find is merge(), anyway heres my code.

 

if(!stack.hasTagCompound()){
    stack.setTagCompound(new NBTTagCompound());
}

NBTTagCompound save = this.getCharger(stack).save();

for(Object key : save.getKeySet()){
    stack.getTagCompound().getCompoundTag("Charger").setTag((String) key, save.getTag((String) key));
}

 

This is in the onUpdate method, .save() is a method that returns an NBTTagCompound.

 

Also, is there a method I can override so I don't have to put this in onUpdate(), eg. onLoad, onSave.

 

- Curlip

Link to comment
Share on other sites

I can't figure out how to save a Compound to the root of an ItemStack.

 

stack.setTagCompound(new NBTTagCompound());

 

Like that.  Its magic.  You now have NBT data and can start storing things into it.

 

stack.getTagCompound().setWhatever("Key",value);

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

I can't figure out how to save a Compound to the root of an ItemStack.

 

stack.setTagCompound(new NBTTagCompound());

 

Like that.  Its magic.  You now have NBT data and can start storing things into it.

 

stack.getTagCompound().setWhatever("Key",value);

 

I need to save a second compound in the structure:

 

{

    Charger:{

        tags...

    }

}

Link to comment
Share on other sites

NBTTagCompound nbt = stack.getTagCompound()
if(nbt == null)
{
    nbt = new NBTTagCompound();
    stack.setTagCompound(nbt);
}

nbt.setCompoundTag("Tag1", new NBTTagCompound());
nbt.setCompoundTag("Tag2", new NBTTagCompound());

 

Will produce stack with:

NBT
>>Tag1
>>Tag2

 

Each tag is treated like NBTTagCompound, e.g you can save:

NBT
>>Tag1
>>>>String:"LOL"
>>>>Integer:5
>>Tag2
>>>>String:"LOL2"
>>>>Integer:10

 

I belive you just didn't fully understand NBTs structure - they are just maps that can contain objects - and those objects also can be maps (NBTTagCompounds)

 

EDIT: P.S: If you want direct heelp, post full code dammit! :D

1.7.10 is no longer supported by forge, you are on your own.

Link to comment
Share on other sites

It absolutely exists.  Vanilla does tags-within-tags for a ton of things.  For instance, chunk data is an NBT tag that contains the NBT tags of all the entities in that chunk.

Enchantments are stored as an NBTTagList, IIRC, as well.

 

So yes, it absolutely exists.  Whether or not the name has been de-SRGed, I don't know.

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

It's #setTagCompound... but you should have been able to find that out in < 1 second by just typing 'stack.set' in your IDE.

 

I have tried typing that, immediately after I typed stack.setTagCom the window disappears.

 

Does it have something to do with #merge(NbtTagCompound)

Link to comment
Share on other sites

What do you want with this merge method? Just... where did you get it from? Leave it alone. Merge is merge. Set is set. :D

 

Is there anything you do not understand as of now? What is the problem?

 

The proper method as of 1.8 is NBT#setTag(String key, NBTBase)

NBTBase is base class of all nbt data. You can compare it to a simple java Object.class.

Now, this NBTBase extends to "generic" data - which are integers, Strings, double, whatever - which can be used with e.g:

NBT#setDouble(String key, double value).

It is either that or it extends to a NBTTagCompound - which translated to "java" is literally a MAP.

 

NBTTagCompound can contain any NBTBase or even next sub-maps - NBTTagCompounds.

 

To set map inside map you can use NBT#setTag(String key, NBTBase subMap).

 

Why did I made mistake in my last post? Because this:

NBT#getCompoundTag(String key)

I though the setter has similar name.

 

Anyway, what many people miss is the fact that e.g:

NBT$setString(String key, String value)

is actually a wrapper method to something like this:

NBT#setTag(String key, new NBTTagString(value));

 

Once you experiment with this you will see that it's VERY obvious in its design.

 

Post full code if you need assistance.

1.7.10 is no longer supported by forge, you are on your own.

Link to comment
Share on other sites

I always assumed setTag was private.

 

That would be dumb and would violate the whole point of a public getter/setter wrapped around a private object.

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

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.