Getting an ItemType from a string?

Hello!

I am working on a plugin that needs to open an inventory with some specific items in it. However, I want the user to be able to change the type of item the inventory shows in a config. Is it possible to change the itemtype by getting a string from a config?

So the config would look something like this

Test{
item = ItemTypes.DIAMOND
}

and I tried this code, but it didn’t work

node = loadConf().getNode(“Test”, “item”);
ItemStack badgeItem = ItemStack.builder().itemType((ItemType)node.getValue()).build();

Use this.

String itemString;
sponge.getRegistry().getType(ItemType.class, itemString()).get();

Note that it will return optional empty if the item type does not exist.

1 Like

That looks like it could work! Thanks alot!

1 Like

Just want to clarify for any future viewers. This works, but the config string needs to be changed to the actual minecraft ID, so e.g “minecraft:diamond” and not what I had written.