Hello. I am making inventory menu for my plugin and I need to set an ItemStack in a specific slot in my inventory, which is created via Inventory.builder().
Currently I tried that code, but both ways do not work:
inventory.query(new SlotPos(2,2)).set(itemStack);
inventory.query(QueryOperationTypes.INVENTORY_PROPERTY.of(new SlotPos(2, 2))).offer(itemStack);
Please, drop a tip how to do this.
Thanks in advance.
new SlotPos
does not do what you think it does. If you trace through it in code, there is an additional Operator
parameter of the constructor that is initialized to Operator.DELEGATE
. In actuality, what you want is Operator.EQUALS
which is given by SlotPos.of
. The same system holds true for SlotIndex
and a couple other inventory properties.
Also, just to note the difference between the two - the first one uses the deprecated query method, so it’s recommended to use the QueryOperationTypes.INVENTORY_PROPERTY
form you have below. However, set
and offer
do very different things - for the purposes of a menu, you should be using set
as the slot should be empty anyway. Using offer
is more for trying to insert an item into an inventory when you don’t know what’s there (like giving an item to a player) and could potentially fail if the slot has an item in it.
If you’re creating an inventory menu, have you considered using an external API? The Inventory Library in TeslaLibs is designed to allow you to quickly create GUI views, handle click actions, and even has built-in support for more advanced features like animations and menu paging. It’s one of the more versatile libraries available, so if it’s something you’d be interested in using I’d be more than happy to help you out with it.
1 Like
Thanks for reply, it helped.
I used both methods, including deprecated one to test if any of them really work.
If you like, you can also use HuskyUI, which does a similar thing to TeslaLibs’ Inventory library, just in a different way. Check it out here: 📋 HuskyUI [v0.5.1] [API 5-7] - A simple, fast UI system for plugins - Now with Java Docs!
HuskyUI uses a more traditional approach to inventories rather than the Sponge DataAPI approach.