-
Notifications
You must be signed in to change notification settings - Fork 2
Modifying the Menu
FlameyosFlow edited this page Sep 26, 2023
·
2 revisions
This example assumes you have menu
as class T extends BaseMenu
.
This is how you add items in ANY menu:
MenuItem item = ItemBuilder.of(Material.STONE).buildItem();
ItemStack itStack = ItemBuilder.of(Material.STONE).build();
// MenuItem
menu.addItem(item);
// ItemStack (will be converted to MenuItem internally)
menu.addItem(itStack);
This is how you set items in ANY menu:
MenuItem item = ItemBuilder.of(Material.STONE).buildItem();
ItemStack itStack = ItemBuilder.of(Material.STONE).build();
// using a normal int slot
menu.setItem(0, item);
// using Slot (row + col access)
menu.setItem(new Slot(1, 5), item);
menu.setItem(new Slot(5) /* slot 5 */, item);
// ItemStack
// using a normal int slot
menu.setItem(0, itStack); // converts to MenuIte
// using Slot (row + col access)
menu.setItem(new Slot(1, 5), itStack); // converts to MenuItem
menu.setItem(new Slot(5) /* slot 5 */, itStack); // converts to MenuItem
This is how you remove items in ANY menu:
MenuItem item = menu.getItem(0);
// O(1) removal; constant-time.
// slots
menu.removeItem(0);
// or
menu.removeItem(new Slot(1, 1));
// moving on to search based removal; O(n) and not O(1); searches through the entire inventory with max size of 54 (takes in how big the inventory really is)
menu.removeItem(item);
menu.removeItem(item.getItemStack()); // checks for MenuItem#getItemStack internally
Recap:
the class will offer much more flexibility and more methods and functionality to deal with.
it can also make certain tasks easier, whether it's external or internal (e.g. internal: MenuIterator like checking validation of a Slot for "hasNext" which can improve performance)
Want to have a Menu that can't be modified?
If you have a Menu Object you can use this:
IMenu menu = ImmutableMenu.copyOf(menu);
else:
IMenu menu = ImmutableMenu.of(new MenuData(...)); // MenuData includes List<MenuItem> so add the items like that
// after menu initialization an "UnsupportedOperationException" is thrown when attempting to make modifications to the menu.
// works like most ImmutableList and stuff like that in Guava