How do I make a simple GUI?

Hello!

I wanted to create a simple GUI for my plugin which basically gets items from a config and displays them on a menu opened with a command. Is there an “easy” way to do this because I couldn’t really find any guides online and I have absolultely no idea on how to do this.

Thank you in advance!

Do you mean like an inventory based GUI? Or a chat based GUI?

Inventory would be awesome

For any custom inventory. Take a look at Inventory.Builder and ItemStack.Builder.

Also how do you mean easy? Do you mean for the user or for you developing it?

1 Like

Thank you for the tips! Also since I am quite new to sponge development I was looking for the easiest way to do this. Don’t wanna overcomplicate things :slight_smile:

I would say a simplistic way of doing that would be to store the items as ItemStackSnapshots in the config, this means when reading the Item it comes out as a ItemStackSnapshot and the user can change the data they wish.

From there when the command is ran. Your plugin gains all the itemStackSnapshots from the config file, then creates a inventory, puts all the itemstacks into the inventory and then send the inventory over.

The issue is when there are more then 64 items in the config as the typical inventory doesnt really support more then 64 items (i mean you can put more in, just doesnt look right). The way around this would be to have sub sections, such as you first have a inventory that allows you to click on a wooden plank and that would open a new inventory that has all the items that are wood related. Another way around would be “pages” where you display 55 items and then have a item to “go to next page” and “go to previous page”.

As this is your first plugin, i would go with just putting items in the inventory first.

Oh it definetely won’t be more than 64 items, I will try to look into this. Thank you again-

If you need any help, im happy to answer any questions about sponge or code :slight_smile:

Aye, right off the bat. How do I go about just making the command open the inventory menu?

Hehe. Fair enough. Here is a great tutorial on Sponge commands.

https://docs.spongepowered.org/stable/en/plugin/commands/creating.html

Ah, maybe my question was a bit confusing. I know how to create a command, but how do I make that command open the inventory menu :sweat_smile:

First you need to create the inventory to open, once you have that, grab the player who sent the command (or the target player if your doing it that way) and do the following.

Inventory inv; //your custom inventory
Player player; //player who sent the command
player.openInventory(inv); //sends the inventory to the player

Ah, I see. Thank you!
And if you feel like you have time would you mind explaining how to add the items into that too? Basically I have a config file with the UUID’s of all the players who have joined and that will contain a node saying which items to display in this menu.

1 Like

Sure. So im going to assume you have a Collection or Array of ItemStackSnapshots.

So first of all, you need to build the Inventory so you have a empty inventory. After that you need to insert the items into the slots. I would do it like this.

Inventory inv;
List<ItemStackSnapshot> items;
for(int A = 0; A < items.size(); A++){
    Slot slot = inv.query(QueryOperationType.INVENTORY_PROPERTY.of(SlotIndex.of(A)));
    slot.set(items.get(A).createStack());
}

That personally is my way, however i do know that some developers prefer to use the offer function that puts the item in the next best slot in the inventory.

for(ItemStackSnapshot item : items){
    inv.offer(item.createStack());
}

Thanks alot! I really appreciate the help. :heart:

1 Like

You also might want to have a look at TeslaPowered’s inv lib.

Will check it out! Thanks!