Creating Custom Inventories, HELP D:

Hey there,

I’m in the process of switching over to Sponge development from a long time on Spigot/Bukkit. I’m trying to figure out how to create a custom inventory with Sponge (to use as a menu), but all the threads I’m seeing are from over a year ago and show something like “CustomInventory” which, I think, doesn’t exist anymore. I’ve gone through the javadocs but wasn’t able to piece together what to do.

Any help would be greatly appreciated!

Thanks :smiley:

1 Like

Custom Inventories are not implemented yet

Oh really? Dang. Yeah, I was going through the Sponge Docs and that seemed to be the only “main” thing missing. Well I guess I’ll have to wait on that or get really creative and find a work-around

Pretty sure they’re implemented.
https://github.com/SpongePowered/SpongeCommon/commit/b00315c559ecaf232fc5d64568f210126e02456a

2 Likes

Did they? I’m not too versed with GitHub, but CustomInventory has a red dash next to it, which I’d assume means removed. haha. Maybe I’m just meant to implement Inventory and then pass my new class to players to open…?

It was implemented for API 5.0.0;
Inventory.builder().property("title", new InventoryTitle(Text.of("Custom inventory"))).property("inventorydimension", new InventoryDimension(9, 6)).build(TestPlugin.getPlugin());

Will create double chest, I don’t really like the way how you need to use properties to set the title & size but oh well thats how it works, but the properties are really useful if you need something extra for your plugin to know afterwards

Oh awesome. I’ll give that a shot. And could you elaborate more on what you mean by “something extra to know afterwards”? Thanks! :smiley:

I meant that you can add different (custom) properties to the custom inventorys to help out making stuff, example you could add property “Identifiable” and figure out if player is in that inventory

Will be great add some Custom Inventory tutorial to Sponge CookBook: GitHub - SpongePowered/Cookbook: SpongeAPI munchy crunchy delicious recipes

Here’s the code I’m using to create a custom inventory. There’s two problems that happen. The first is that it’s opening the inventory that looks like a chest inventory on top of a player’s, rather than a double chest inventory. The second is that the inventory name isn’t what I set here. Any ideas? Thanks! :smiley:

Try this:

Inventory.builder().of(InventoryArchetypes.DOUBLE_CHEST)
        .property(InventoryTitle.PROPERTY_NAME, InventoryTitle.of(Text.of("Quests: Page " + (i + 1))))
        .build(QuestAPI.getInstance());

Perfect, that worked! Now we just need to figure out the dimension thing. Would you happen to have any idea about that? Also, thanks for the help!

Oh wait, I just say the first line of code… I didn’t even read that at first. haha. Thanks again!

Okay, so I just tried that, and although the dimensions are correct, it still shows it as a “connected” inventory. So it shows the chest inventory on top and the player’s on bottom. Would you happen to know how to “separate” those?

No, I don’t, I’m not sure it’s possible, sorry.

If you need to change the dimensions, you can add the following property method to the builder (this is 9x4):

.property(InventoryDimension.PROPERTY_NAM, new InventoryDimension(9, 4))

Yep, that’s a misspelling of “PROPERTY_NAME” - but it will make sure that the property name is correct.

Lol. Alright, I appreciate it. Last thing, do you know how to get the slot number from a clicked inventory? I see that I can get the Slot through SlotTransactions, however, I can’t seem to find out how to see what slot number that is in the inventory without getting an iterable of the Slots, then just slotNumber++ in a loop until I find it. Surely there’s a more ‘clean’ way than that

And possibly how to get the player that clicked the item? Sorry, this inventory system is soooooooo different than Spigot’s .-. haha

Yup it’s a hundred times better.

  1. Slots don’t have numbers. Why do you need slot numbers?
  2. This should be in the Cause for the event, e.g. @Listener public void onClick(ClickInventoryEvent.Primary event, @First Player p) {

If you don’t mind, could you explain what you just showed me? The .Primary, what’s that do?

Events extend other events, such as DestructEntityEvent.Death extending TargetEntityEvent. However, for specific variations of an event, such as ClickInventoryEvent which has many variations, the sub-event is a static interface inside the original event. Have a read of the javadocs for more information.
So, for instance, ClickInventoryEvent.Primary represents the primary click (left-click by default). Extending that is ClickInventoryEvent.Double which refers to a double-click. ClickInventoryEvent.NumberPress happens when, rather than an actual click, a number is pressed while hovering over a slot, which swaps it with that hotbar slot. All of these are different types of ClickInventoryEvents, so they are static inner interfaces of ClickInventoryEvent.