ItemStack from a mod

How can I make a new ItemStack from an item of a mod?
Thanks for your time

First of all you need to know the ItemType id of the modded item.

String itemTypeId = "<modID>:<itemname>";

From there you can get the ItemType of the modded Item.

Optional<ItemType> opItem = Sponge.getRegistery(ItemType.class, itemTypeId);

Next thing to do is to create the ItemStack from the item type. There are two ways of doing this, from either the ItemStack.builder() or the way I prefer.

ItemStackSnapshot itemSnapshot = itemType.getTemplate();
ItemStack item = itemSnapshot.createStack();

Dont know why I prefer this way, just do. This is all done from memory so the syntax maybe slightly wrong

Thank you MoseMister, you´ve really helped me a lot these days, you´re a hero :smiley:
Can I ask just one more thing? can you please tell me a way to add the subid (or item variant, I don´t know how to say it) to the item?

This is a little complex. Because Minecraft started phasing the id and sub id (number values) out back in 1.8 (i believe) so Sponge never added support for it but instead opted for the Item Varent/Properties.

These are stored in sponge as essentially Key/Value pair. And you can get the supported Keys from any ItemStack or ItemStackSnapshot by calling the

item.keys()

As I said just above you cannot simply just get the Item from the id and sub id. So you need to use the key value pair. So it really depends on what makes that Item special in the key value pairs.

Sorry I can not be any mors help then that without knowing the mod you are using.

Sponge.getRegistry().getType(ItemType.class, itemTypeId);

That won’t work. Data value keys are defined per item type, and mods don’t exactly have their own.

1 Like

Sponge does not expose the data value directly. Dunno why, but it doesn’t. If you want to change the data value, you need essentially to convert it to DataContainer, set UnsafeDamage, and convert it back.

Alternatively, if your item is a block, you can use a BlockState (for example minecraft:wool[color=red]) instead of an ItemType, and build from the BlockState. F3 would show you the block state variables.

How can I do thefirst option? I saw the “toContainer()” in the item, but how can I get the UnsafeDamage from it, and then converting it back to an ItemStackSnapshot?

DataView allows you to basically manipulate raw NBT like data. You can read some more about DataContaienrs and how they’re used on the docs. Likewise, you can somewhat see how the ItemStack.Builder actually re-builds an Itemstack from these DataViews.

In short though, it’s something along the lines of this:

public ItemStack setDamageValue(ItemStack stack, int damage) {
  return ItemStack.builder()
    .fromContainer(stack.toContainer().set(DataQuery.of("UnsafeDamage"), damage))
    .build();
}

Note that this re-creates the ItemStack, it doesn’t mutate it in that way.

Because, damage values are unrepresentable as an API. They’re just magic numbers, compared to something clearly identifiable, and we wrote that as keys/values/catalog types/etc. Each major MC version since 1.9, Mojang has been teasing us with getting rid of the damage values, and finally, they have in 1.13 (meaning the unsafe damage, which has been labeled as unsafe since it’s creation, is not going to be usable in API 8 and newer).