How to set NBT to item?

Is there way to set NBT to item?

I tried this:

ItemStack item = ItemStack.of(ItemTypes.SPAWN_EGG, 1); DataContainer container = item.toContainer(); container.set(DataQuery.of("UnsafeData", "EntityTag", "id"), "minecraft:creeper"); item.setRawData(container); player.setItemInHand(HandTypes.MAIN_HAND, item); item.toContainer().getKeys(true).forEach(dataQuery -> System.out.println(Arrays.toString(dataQuery.getParts().toArray()) + " - " + item.toContainer().get(dataQuery).get()));
But I get default spawn egg, and in console I see this (no my NBT):

You need to access the UnsafeData portion of the container and write values to it. SpawnEggs in newer versions contain the mob to be spawned in a special object inside of UnsafeData.

Can I see example of code?

Per chance, may I ask why you want to modify NBT? What feature are you trying to get at? As having written a majority of the Data API myself, I’d like to know what it’s not able to do for you that you need access to the NBT compound for.

I dont so much want to modify NBT, but I dont know how I can allow to users create ItemStacks with NBT tags (like spawn egg or mod items) from config?

Ah. So your wanting to create your own custom keys so then you can interact with modded items like you do with vanilla items in normal sponge?

From there parse them into JSON and parse from JSON so you can save and read from config?

You can do a lot of those things with EntityArchetypes and using the keys to set them onto the items. I’d write an example of how to do it and how it’d look like in a config, will probably get around to it over the next couple of days.

1 Like

No, I only want to allow users to “send to my plugin” any item (using config), with any NBTs (and maybe any other Data if possible?) to use it later everywhere it needed.

1 Like

Using EntityArchetypes with items?
Okay, I will wait “couple of days” for examples)

Oh, items are easy.

Not Java but should be pretty clear nonetheless.

1 Like

Here’s how I do it directly from the config.

if(!itemRoot.getNode("nbt").isVirtual()){
    //nbt overrrides
    LinkedHashMap items = (LinkedHashMap) itemRoot.getNode("nbt").getValue();
    if(item.toContainer().get(DataQuery.of("UnsafeData")).isPresent()) {
        BiMap real = ((BiMap) item.toContainer().getMap(DataQuery.of("UnsafeData")).get());
        items.putAll(real);
    }
    //System.out.println(item.toContainer().get(DataQuery.of("UnsafeData")).get().getClass());
    item = ItemStack.builder()
            .fromContainer(item.toContainer().set(DataQuery.of("UnsafeData"),items))
            .build();
}
2 Likes

Can I got examples, how it must looks in config?
I tried many variants, but always got exception “ArrayList can not be cast to LinkedHashMap”.

I don`t undestand Kotlin =(

Well, don’t use arrays then. Use maps with keys and values.

I can`t find syntax of maps in HOCON…
I using:

NBT: { "EntityTag.id" : "minecraft:creeper" }

I mean, afaik that’s not valid HOCON even.
Really, it would be EntityTag{id=“gr”} or something.
What you’re doing is like, literally JSON.

NBT { EntityTag{id="gr"} }
Some result…

All JSON is also HOCON.

1 Like

Which is gross and annoying.
@GWM So did that not work?