How too set an Itemstack or ItemType variant

I am trying too create a shop system for my server. The issue is the mods that my server has a lot of the items that are included in the mods use the same ItemID for multiple Items so I am trying too get the variant of that item so that I can get the specific item I am looking for.

Hi, sorry for the late reply.

In minecraft the id values were set to deprecated around minecraft 1.8, so mods should have followed and used the replacement, it seems only Sponge understood this. Sponge doesnt support anything that is deprecated within minecraft, which includes the id system.

Therefore for Vannila items (and mods that support the newer way) you use keys to set the “id” of the item. So for example if I wanted to get charcoal I would do the following.

ItemStackSnapshot snapshot = ItemTypes.COAL.getTemplate().with(Keys.COAL_TYPE, CoalTypes.CHARCOAL).get();

However because most mods didnt go this route and sponge does support the route they went, it means that developers have done things that Sponge doesnt support, meaning that it may cause issues down the line.

Here is one I just tried doing that works, it uses Sponge code only so hopefully there isnt too many issues down the line. The example is once again for charcoal, but you can modify it for your own use.

DataContainer cont = ItemTypes.COAL.getTemplate().toContainer();
cont.set(DataQuery.of("UnsafeDamage"), 1); //the 1 is the sub id
ItemStack stack = ItemStack.builder().fromContainer(cont).build();

This method relies on the fact you have the ItemType of the item. This is easy to get for modded and none modded items and blocks.

Sponge.getRegistry().getType(ItemType.class, "minecraft:coal");

If you plan on getting a BlockState with a paticular id, that is much simpler and less hacky. You do the way you got the custom item, however replace ItemType with BlockState and replace minecraft:coal with the character id of the block (example minecraft:stone[stone=granite])

Hope that helps

Thank you this seems too have solved my issue! My server shops are now working!

1 Like