[Solved] Possible to add mod-items to a Player's inventory?

I do apologise if this has been resolved in the newer versions, the only thread I could find was from 2015 where the solution given is no longer available.

As the title suggests, I’m looking to give players an item from a mod upon typing a certain command, however going through the typical route of getting a players inventory and calling “offer”, this only allows for me to add vanilla items.

It’s very possible I could be missing something obvious as I’m only now getting back in to Sponge development, so lord knows a fair bit of progress has been made.

If it’s at all possible for someone to point me in the right direction, it’d be much appreciated, thank you :slight_smile:

There is no distinction between vanilla items and mod items. You’d just make an ItemStack like normal. Any mod item types can be cast to ItemType.

1 Like

Well overall the method I’ve had to take isn’t as clean as it was first made out to be (instead of using your standard ItemStack builder, I need to get the game, then the registry, etc).

I will mark this as solved seeing as far as I’m concerned, I’m getting the result I’m after.

For future reference to other developers, this will allow you to give items based off ID’s:

"player.getInventory().offer(ItemStack.of(Sponge.getGame().getRegistry().getType(ItemType.class, "stone").get(), 1));

But you don’t need to do that. Yes, that’s how one would do that with the registry, but if you can access mod item type fields, you can cast them to ItemType.

Also, Sponge.getGame() is rather useless since every method on Game is also a static method on Sponge.

1 Like