Set player's held item slot

Hi,

I’m currently trying to set player’s item held slot. With Bukkit/Spigot, it was player.getInventory().setHeldItemSlot(i);
Is there an equivalent with Sponge ?

Thanks

Yep. So for some reason the players inventory is not casted to a PlayerInventory automatically, but it is always genrenteed (I believe)

PlayerInventory inventory = (PlayerInventory)player.getInventory();

After that you need to talk to the hotbar section of the players inventory

Hotbar bar = inventory.getHotbar();

After that simply set the selected index with the slot you want.

bar.setSelectedIndex(2);
1 Like

You can also do

player.setItemInHand(HandTypes.MAIN_HAND, stack);

Yes, but this is setting the item in the player’s hand and not in a specific slot :stuck_out_tongue:

Ah yeah, I misunderstood what you were meaning by setting the held item slot, since held item could be the item in hand/off-hand :wink: