[Solved] Help with using Materials

So I know how Bukkit has handled their materials but I haven’t yet figured out how Sponge has, I’ve gone through the java docs and found some stuff about Items and ItemStacks but I’m not quite sure if thats what I’m looking for. Im currently working on porting over a Bukkit plugin and I need to get the ID and Meta of a block and i have the whole method the way it was done with Bukkit but there is no Material class like Bukkit. The whole method is below, if anyone could provide some suggestions on what to do or just simply provide the Class I’m supposed to use because I feel like I’m being really stupid and just skipping over it in the Java Docs.

 public static BlockType loadFromConfig(String input)
    if (input == null)
        return null;
    int blockId;
    Set<Short> meta = new HashSet<Short>();
    input = RegexHelper.trimWhitespace(input);
    String[] splitted = seperators.split(input);
    if (splitted.length == 0)
        return null;
    for (int i = 1; i < splitted.length; i++) //first value is blockId
    //BLOCK ID
    String blockIdString = splitted[0];
    //The Material below is the one I'm having trouble with.
    Material material = Material.matchMaterial(blockIdString);
    if (material == null) //Not found in material enum
        // try as a number (blockId)
        String tempId = RegexHelper.stripNumber(blockIdString);
        if (!tempId.isEmpty())
            material = Material.getMaterial(tempId);
        // still fail -> try as enum again but strip numbers
        if (material == null)
            material = Material.matchMaterial(RegexHelper.stripEnum(blockIdString));
    if (material != null)
        blockId = material.getId();
    else //mod item or -1 if not valid
        blockId = RegexHelper.parseNumber(blockIdString, -1);
    return new BlockType(blockId, meta);

The equivalent statement in Sponge to get a ItemType is:

GameRegistry reg = game.getRegistry(); // Obtained via event or injected
Optional<ItemType> type = reg.getType(ItemType.class, material); // Returns an Optional<ItemType>

From this you can get the ItemType (or ItemBlock if you want to be more specific) to return.

Note that this ItemType is the parent type, so items such as birch logs or red wool will need to be handled slightly differently.

@ZephireNZ How would i get the item’s meta then?

There’s no such concept of “metadata” in blocks and items in SpongeAPI. Instead, ItemStacks and BlockStates are DataHolders. You use them just like with any other data holder, item.getData(SomeData.class)
You may be interested in this:

And in not too long now it will be upgraded with the ‘values API’

1 Like