Checking item in hand against a certain item

I am trying to check an item from a certain mod and all the resources I found are outdated and not working. I have been trying to use the code to get the item from the mod but my IDE is throwing an error at the .getItemBuilder() part.

    game.getRegistry().getItemBuilder().itemType(game.getRegistry().getType(ItemType.class,             itemIDName).get()).build();

As well I am trying to get the item on right click in the persons main hand with the following code.

        if (p.getItemInHand(HandTypes.MAIN_HAND).get().getItem().getName().equalsIgnoreCase("Animal Net")) 

But it is throwing an error in the IDE about the API since 1.6

Any solutions?

It would be helpful to paste what errors you’re getting so we’d be able to better help you. As for getting an item from mod, here’s how I’d do it.

String itemID = "modid:moditemid"
if (Sponge.getRegistry().getType(ItemType.class, itemID).isPresent()) {
ItemType modItemType = Sponge.getRegistry().getType(ItemType.class, itemID).get();
ItemStack modItemStack = ItemStack.of(modItemType, 1);
}

I’m not sure about the second one, it looks to be correct code but without the error I don’t have much to go on.
EDIT: Oh I think I might know what the problem might be, is your IDE set to use JDK 1.8? If not that’s probably where you’re going wrong, Sponge is built against Java 8 so your project needs to be as well.

1 Like

Yep that was my problem with the IDE not using JDK 1.8! Thank you! :slight_smile:

Also, if you’re able to get the mod item type via having it as a dependency, that’s even better - you can just cast the mod’s item type to ItemType.