[Solved] Set item variant when creating item stack

Hello, I am looking for a way to create an item stack (that will be given to a player) with a certain variant.For example spawning a wither skull which has item id minecraft:skull and the variant id 1 compared to a skeleton skull which has the same item id but variant id 0.
I tried setting the item id to minecraft:skull:1 but it will return a null because it can not find such an item in the game.
I can’t figure this out. Any help is appreciated.

My code:

//Generating item type
ItemType myItemType = null;
	    		if (Sponge.getRegistry().getType(ItemType.class, itemID).isPresent()) {
	    			myItemType = Sponge.getRegistry().getType(ItemType.class, itemID).get();
	    		}
//Generating item stack
ItemStack myItems= ItemStack.builder().itemType(modItemType).quantity(ammount).build();
//Give the item stack
InventoryUtils.addItem((PlayerInventory) player.getInventory(), myItems, ammount);

You could try this

String itemID;
int amount;
String splitID = itemID.split(":");
int dataValue = Integer.parseInt(splitID[2]);
String parsedID = splitID[0] + ":" + splitID[1];
Optional<ItemType> opType = Sponge.getRegistry().getType(ItemType.class, parsedID);
if(opType.isPresent()){
    ItemStack item = ItemStack.builder().itemType(opType.get()).quantity(amount).build();
    switch(dataValue){
        case 0: item.offer(Key.SKULL_TYPE, SkullTypes.CREEPER); break;
        case 1: item.offer(Key.SKULL_TYPE, SkullTypes.WITHER_SKELETON); break;
        //ETC
    }
}

The code is from memory and not tested, if it does not compile then consider it a way of going about it.

SpongePowered does not support data values due to the fact Minecraft is attempting to remove them. This has resulted to some really strange work arounds.

I advise you look at this thread where @gabizou (a sponge leader) talks about getting varients

There are no ID numbers in Sponge. Any extra item data should be done via Keys.

ItemStack stack = ItemStack.builder().itemType(ItemTypes.SKULL).add(Keys.SKULL_TYPE, SkullTypes.WITHER_SKELETON).build();

If you need a dynamic way to obtain this, such as command arguments, remember that Keys are also catalog types, as are any of these variants, so they can be retrieved from the registry as well.

I am unfamiliar with “catalog types” and “the registry” in regards to Sponge. What do you mean with this and where can I find more information about it?

@VapidLinus
Catalogue types are interfaces within sponge that have multiple values within minecraft, for example, BlockType, ItemType, etc are all catalogue type because of there being multiple values of them

As for the regersty. Its a place where you can get catalogue types from. All you need to do is

Sponge.getRegersty(); 

From there just take a look what it can offer.

I would also recommend taking a look though the spongeDocs. This will help you understand sponge

https://docs.spongepowered.org

1 Like

I think you may have mis-spelled “registry” …

… but at least you’re consistent :wink:

4 Likes

To refine @MoseMister 's explanation, a CatalogType is sort of a not-quite-enum. There are a finite (usually) number of instances of each catalog type, which are stored in the registry and can be looked up by their IDs. A lot of things are catalogued, such as item types, block types, entity types, but also some stranger things like block states, currencies, and data keys. There are also functions for auto retrieving them through APIs. For instance, one can use GenericArguments.catalogType in commands, or retrieve any catalogued type by class from a ConfigurationNode or DataView.

1 Like