Initial commit

This commit is contained in:
BuildTools 2021-01-21 22:03:07 +01:00
commit ab4bb73cc1
18 changed files with 1592 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
# Project exclude paths
/target/

24
pom.xml Normal file
View File

@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<groupId>nl.iobyte</groupId>
<artifactId>menuapi</artifactId>
<version>1.0</version>
<dependencies>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.12.2-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,56 @@
package nl.iobyte.menuapi;
import nl.iobyte.menuapi.interfaces.IActionHandler;
import org.bukkit.Bukkit;
import org.bukkit.plugin.Plugin;
import java.util.HashMap;
public class MenuAPI {
private static boolean registered = false;
private static final HashMap<String, IActionHandler> actionHandlers = new HashMap<>();
/**
* Add ActionHandler
* @param handler IActionHandler
*/
public static void addActionHandler(IActionHandler handler) {
actionHandlers.put(handler.getID(), handler);
}
/**
* Get IActionHandler by ID
* @param id String
* @return IActionHandler
*/
public static IActionHandler getActionHandler(String id) {
return actionHandlers.get(id);
}
/**
* Has IActionHandler with specific ID
* @param id String
* @return Boolean
*/
public static boolean hasActionHandler(String id) {
return actionHandlers.containsKey(id);
}
/**
* Remove IActionHandler by ID
* @param id String
* @return IActionHandler
*/
public static IActionHandler removeActionHandler(String id) {
return actionHandlers.remove(id);
}
public static void register(Plugin plugin) {
if(registered || plugin == null)
return;
Bukkit.getPluginManager().registerEvents(new MenuListener(), plugin);
registered = true;
}
}

View File

@ -0,0 +1,100 @@
package nl.iobyte.menuapi;
import nl.iobyte.menuapi.interfaces.IMenu;
import nl.iobyte.menuapi.action.InteractAction;
import nl.iobyte.menuapi.multi.MenuPage;
import nl.iobyte.menuapi.multi.MultiMenu;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.*;
import org.bukkit.inventory.Inventory;
public class MenuListener implements Listener {
@EventHandler
public void onClick(InventoryClickEvent e) {
if(e.getClickedInventory() == null)
return;
Inventory inventory = e.getClickedInventory();
if(!(inventory.getHolder() instanceof IMenu))
return;
if(e.isCancelled())
return;
Player player = (Player) e.getWhoClicked();
int slot = e.getRawSlot();
ClickType type = e.getClick();
IMenu menu = (IMenu) inventory.getHolder();
e.setCancelled(menu.execute(player, slot, type));
}
@EventHandler
public void onOpen(InventoryOpenEvent e) {
Player player = (Player) e.getPlayer();
Inventory inventory = e.getInventory();
if(!(inventory.getHolder() instanceof IMenu))
return;
if(!(inventory.getHolder() instanceof MenuPage)) {
IMenu menu = (IMenu) inventory.getHolder();
InteractAction open = menu.getOpenAction();
if (open == null)
return;
open.execute(player);
e.setCancelled(open.doCancel());
} else {
MenuPage page = (MenuPage) inventory.getHolder();
MultiMenu menu = page.getParent();
if(!menu.isActivePage(player.getUniqueId(), page))
return;
InteractAction open = menu.getOpenAction();
if (open == null)
return;
open.execute(player);
e.setCancelled(open.doCancel());
}
}
@EventHandler
public void onClose(InventoryCloseEvent e) {
Player player = (Player) e.getPlayer();
Inventory inventory = e.getInventory();
if(!(inventory.getHolder() instanceof IMenu))
return;
if(!(inventory.getHolder() instanceof MenuPage)) {
IMenu menu = (IMenu) inventory.getHolder();
InteractAction close = menu.getCloseAction();
if(close == null)
return;
close.execute(player);
if(close.doCancel())
menu.open(player);
} else {
MenuPage page = (MenuPage) inventory.getHolder();
MultiMenu menu = page.getParent();
if(!menu.isActivePage(player.getUniqueId(), page))
return;
InteractAction close = menu.getCloseAction();
if (close == null)
return;
close.execute(player);
if(close.doCancel()) {
page.open(player);
} else {
menu.close(player);
}
}
}
}

View File

@ -0,0 +1,23 @@
package nl.iobyte.menuapi.action;
import nl.iobyte.menuapi.MenuAPI;
import nl.iobyte.menuapi.interfaces.IActionHandler;
import org.bukkit.entity.Player;
public class HandlerAction extends MenuAction {
private final String handler;
public HandlerAction(String handler) {
this.handler = handler;
}
public void execute(Player player) {
IActionHandler handler = MenuAPI.getActionHandler(this.handler);
if(handler == null)
return;
handler.execute(player);
}
}

View File

@ -0,0 +1,18 @@
package nl.iobyte.menuapi.action;
public abstract class InteractAction extends MenuAction {
/**
* Set if should cancel
* @param cancel boolean
* @return InteractAction
*/
public abstract InteractAction setCancel(boolean cancel);
/**
* Check if should cancel
* @return boolean
*/
public abstract boolean doCancel();
}

View File

@ -0,0 +1,13 @@
package nl.iobyte.menuapi.action;
import org.bukkit.entity.Player;
public abstract class MenuAction {
/**
* Execute Action for Player
* @param player Player instance
*/
public abstract void execute(Player player);
}

View File

@ -0,0 +1,274 @@
package nl.iobyte.menuapi.basic;
import nl.iobyte.menuapi.action.InteractAction;
import nl.iobyte.menuapi.action.MenuAction;
import nl.iobyte.menuapi.interfaces.IMenu;
import nl.iobyte.menuapi.item.Color;
import nl.iobyte.menuapi.item.MenuItem;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
public class Menu implements IMenu {
private String title;
private int size;
private final HashMap<Integer, MenuItem> items = new HashMap<>();
private InteractAction open, close;
private Inventory inventory;
private boolean locked = true;
public Menu(String title, int size) {
this.title = title;
this.size = size;
}
public Menu(String title, int size, boolean locked) {
this.title = title;
this.size = size;
this.locked = locked;
}
/**
* Return inventory size
* @return int
*/
public int getSize() {
return size;
}
/**
* Set size
* @param size int
* @return IMenu
*/
public IMenu setSize(int size) {
this.size = size;
return this;
}
/**
* Get item
* @param slot int
* @return MenuItem
*/
public MenuItem getItem(int slot) {
if(!items.containsKey(slot))
return null;
return items.get(slot);
}
/**
* Get menu items
* @return MenuItem
*/
public HashMap<Integer, MenuItem> getItems() {
return items;
}
/**
* Set item to slot
* @param slot int
* @param item MenuItem
* @return IMenu
*/
public IMenu setItem(int slot, MenuItem item) {
if(slot < 0 || slot >= size)
return this;
items.put(slot, item);
return this;
}
/**
* Set item to slot
* @param slot int
* @param item ItemStack
* @return IMenu
*/
public IMenu setItem(int slot, ItemStack item) {
MenuItem mi = getItem(slot);
if(mi == null)
return this;
mi.setItem(item);
return this;
}
public IMenu setItems(HashMap<Integer, MenuItem> items) {
items.replaceAll((k, v) -> v.clone());
return this;
}
/**
* Update item
* @param slot int
* @return IMenu
*/
public IMenu updateItem(int slot) {
MenuItem item = getItem(slot);
if(item == null)
return this;
inventory.setItem(slot, item.getItem());
return this;
}
/**
* Get action on open
* @return InteractAction
*/
public InteractAction getOpenAction() {
return open;
}
/**
* Set action on open
* @param action InteractAction
* @return IMenu
*/
public IMenu setOpenAction(InteractAction action) {
open = action;
return this;
}
/**
* Get action on close
* @return InteractAction
*/
public InteractAction getCloseAction() {
return close;
}
/**
* Set action on close
* @param action InteractAction
* @return IMenu
*/
public IMenu setCloseAction(InteractAction action) {
close = action;
return this;
}
/**
* Get menu title
* @return String
*/
public String getTitle() {
return title;
}
/**
* Set menu title
* @param title String
* @return IMenu
*/
public IMenu setTitle(String title) {
this.title = title;
return this;
}
/**
* Get if Menu is locked
* @return Boolean
*/
public boolean isLocked() {
return locked;
}
/**
* Set if menu is locked
* @param locked Boolean
* @return IMenu
*/
public IMenu setLocked(boolean locked) {
this.locked = locked;
return this;
}
/**
* Build Menu
* @return IMenu
*/
public IMenu build() {
inventory = Bukkit.createInventory(this, toSize(size, 9), Color.parse(title));
for(Map.Entry<Integer, MenuItem> entrySet : items.entrySet())
if(entrySet.getKey() < size)
inventory.setItem(entrySet.getKey(), entrySet.getValue().getItem());
return this;
}
/**
* Number to size
* @param i int
* @param size int
* @return int
*/
protected static int toSize(int i, int size) {
int r = i % size;
if(r > 0)
i += size - r;
return i;
}
/**
* Get inventory
* @return Inventory
*/
public Inventory getInventory() {
return inventory;
}
/**
* Execute Click
* @param player Player
* @param slot int
* @param type ClickType
* @return cancel
*/
public boolean execute(Player player, int slot, ClickType type) {
if(player == null || type == null)
return locked;
if(slot < 0 || slot >= size)
return locked;
MenuItem item = getItem(slot);
if(item == null)
return locked;
ArrayList<MenuAction> actions = item.getActions(type);
if(actions != null && !actions.isEmpty())
for(MenuAction action : actions)
action.execute(player);
return item.doCancel();
}
/**
* Open menu for player
* @param player Player
*/
public void open(Player player) {
if(inventory == null)
return;
player.openInventory(inventory);
}
@Override
public Menu clone() {
Menu menu = new Menu(title, size);
menu.setItems(items);
return menu;
}
}

View File

@ -0,0 +1,28 @@
package nl.iobyte.menuapi.enums;
import org.bukkit.event.inventory.ClickType;
import java.util.Arrays;
import java.util.List;
public enum Types {
LEFT(ClickType.LEFT, ClickType.SHIFT_LEFT),
RIGHT(ClickType.RIGHT, ClickType.SHIFT_RIGHT),
NORMAL(ClickType.LEFT, ClickType.RIGHT),
SHIFT(ClickType.SHIFT_LEFT, ClickType.SHIFT_RIGHT),
WINDOW_BORDER(ClickType.WINDOW_BORDER_LEFT, ClickType.WINDOW_BORDER_RIGHT);
private final ClickType[] types;
Types(ClickType... types) {
this.types = types;
}
public ClickType[] getTypes() {
return types;
}
public List<ClickType> getTypesList() {
return Arrays.asList(types);
}
}

View File

@ -0,0 +1,27 @@
package nl.iobyte.menuapi.interfaces;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.ClickType;
import java.util.List;
public interface IActionHandler {
/**
* Get ID of the handler
* @return String
*/
String getID();
/**
* Get ClickType's of handler
* @return List<ClickType>
*/
List<ClickType> getClickTypes();
/**
* Action to perform on click
* @param player Player
*/
void execute(Player player);
}

View File

@ -0,0 +1,130 @@
package nl.iobyte.menuapi.interfaces;
import nl.iobyte.menuapi.action.InteractAction;
import nl.iobyte.menuapi.item.MenuItem;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack;
import java.util.HashMap;
public interface IMenu extends InventoryHolder {
/**
* Get item
* @param slot int
* @return MenuItem
*/
MenuItem getItem(int slot);
/**
* Get menu items
* @return MenuItem
*/
HashMap<Integer, MenuItem> getItems();
/**
* Set item to slot
* @param slot int
* @param item MenuItem
* @return IMenu
*/
IMenu setItem(int slot, MenuItem item);
/**
* Set item to slot
* @param slot int
* @param item ItemStack
* @return IMenu
*/
IMenu setItem(int slot, ItemStack item);
/**
* Set menu items
* @param items MenuItems
* @return IMenu
*/
IMenu setItems(HashMap<Integer, MenuItem> items);
/**
* Update item
* @param slot int
* @return IMenu
*/
IMenu updateItem(int slot);
/**
* Get action on open
* @return InteractAction
*/
InteractAction getOpenAction();
/**
* Set action on open
* @param action InteractAction
* @return IMenu
*/
IMenu setOpenAction(InteractAction action);
/**
* Get action on close
* @return InteractAction
*/
InteractAction getCloseAction();
/**
* Set action on close
* @param action InteractAction
* @return IMenu
*/
IMenu setCloseAction(InteractAction action);
/**
* Get menu title
* @return String
*/
String getTitle();
/**
* Set menu title
* @param title String
* @return IMenu
*/
IMenu setTitle(String title);
/**
* Build Menu
* @return IMenu
*/
IMenu build();
/**
* Get inventory
* @return Inventory
*/
Inventory getInventory();
/**
* Execute Click
* @param player Player
* @param slot int
* @param type ClickType
* @return cancel
*/
boolean execute(Player player, int slot, ClickType type);
/**
* Open menu for player
* @param player Player
*/
void open(Player player);
/**
* Clone Menu
* @return IMenu
*/
IMenu clone();
}

View File

@ -0,0 +1,9 @@
package nl.iobyte.menuapi.interfaces;
import org.bukkit.entity.Player;
public interface LoadAction {
void run(IMenu menu, Player player);
}

View File

@ -0,0 +1,14 @@
package nl.iobyte.menuapi.item;
import org.bukkit.ChatColor;
public class Color {
public static String parse(String str) {
if(str == null || str.isEmpty())
return str;
return ChatColor.translateAlternateColorCodes('&', str);
}
}

View File

@ -0,0 +1,138 @@
package nl.iobyte.menuapi.item;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
public class ItemBuilder {
private final ItemStack item;
public ItemBuilder(ItemStack item) {
this.item = item;
}
public ItemBuilder(Material material) {
item = new ItemStack(material, 1);
}
public ItemBuilder(Material material, int amount) {
item = new ItemStack(material, amount);
}
public ItemBuilder(Material material, int amount, short data) {
item = new ItemStack(material, amount, data);
}
public ItemStack getItem() {
return item;
}
public ItemBuilder setType(Material material) {
if(material == null)
return this;
item.setType(material);
return this;
}
public ItemBuilder setUnbreakable(boolean b) {
ItemMeta meta = item.getItemMeta();
meta.setUnbreakable(b);
item.setItemMeta(meta);
return this;
}
public ItemBuilder setName(String name) {
if(name == null)
return this;
ItemMeta meta = item.getItemMeta();
meta.setDisplayName(Color.parse(name));
this.item.setItemMeta(meta);
return this;
}
public ItemBuilder setLore(List<String> lore) {
if(lore == null)
return this;
ItemMeta meta = item.getItemMeta();
for(String string : lore)
lore.set(lore.indexOf(string), Color.parse(string));
meta.setLore(lore);
item.setItemMeta(meta);
return this;
}
public ItemBuilder setLore(String lore) {
List<String> list = new ArrayList<>();
list.add(lore);
setLore(list);
return this;
}
public ItemBuilder addLore(String lore) {
List<String> list = item.getItemMeta().getLore();
list.add(lore);
setLore(list);
return this;
}
public ItemBuilder setEnchantment(HashMap<Enchantment, Integer> enchantments) {
ItemMeta meta = item.getItemMeta();
if(!meta.getEnchants().isEmpty())
meta.getEnchants().clear();
for(Map.Entry<Enchantment, Integer> entry : enchantments.entrySet())
meta.addEnchant(entry.getKey(), entry.getValue(), true);
item.setItemMeta(meta);
return this;
}
public ItemBuilder setEnchantment(Enchantment enchantment, int i) {
ItemMeta meta = item.getItemMeta();
if (!meta.getEnchants().isEmpty())
meta.getEnchants().clear();
meta.addEnchant(enchantment, i, true);
item.setItemMeta(meta);
return this;
}
public ItemBuilder addEnchantment(Enchantment enchantment, int i) {
ItemMeta meta = item.getItemMeta();
meta.addEnchant(enchantment, i, true);
item.setItemMeta(meta);
return this;
}
public ItemBuilder addFlag(ItemFlag flag) {
ItemMeta meta = item.getItemMeta();
meta.addItemFlags(flag);
item.setItemMeta(meta);
return this;
}
public ItemBuilder addFlags(ItemFlag... flags) {
ItemMeta meta = item.getItemMeta();
meta.addItemFlags(flags);
item.setItemMeta(meta);
return this;
}
public ItemBuilder clone() {
return new ItemBuilder(item.clone());
}
}

View File

@ -0,0 +1,150 @@
package nl.iobyte.menuapi.item;
import nl.iobyte.menuapi.action.HandlerAction;
import nl.iobyte.menuapi.interfaces.IActionHandler;
import nl.iobyte.menuapi.action.MenuAction;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.inventory.ItemStack;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
public class MenuItem {
private ItemStack item;
private boolean cancel;
private HashMap<ClickType, ArrayList<MenuAction>> actions = new HashMap<>();
public MenuItem(ItemStack item, boolean cancel) {
this.item = item;
this.cancel = cancel;
}
/**
* Get item
* @return ItemStack instance
*/
public ItemStack getItem() {
return item;
}
/**
* Set item
* @param item ItemStack instance
* @return MenuItem instance
*/
public MenuItem setItem(ItemStack item) {
this.item = item;
return this;
}
/**
* Get Actions for ClickType
* @param type Type of Click
* @return Array of MenuActions
*/
public ArrayList<MenuAction> getActions(ClickType type) {
if(type == null)
return null;
if(!actions.containsKey(type))
return null;
return actions.get(type);
}
/**
* Add actions to MenuItem
* @param type ClickType
* @param actions Array of MenuActions
* @return MenuItem Instance
*/
public MenuItem addActions(ClickType type, MenuAction... actions) {
if(type == null || actions == null)
return this;
ArrayList<MenuAction> array = new ArrayList<>();
if(this.actions.containsKey(type)) {
array = this.actions.get(type);
} else {
this.actions.put(type, array);
}
array.addAll(Arrays.asList(actions));
return this;
}
/**
* Add actions to MenuItem
* @param types List of ClickType
* @param actions Array of MenuActions
* @return MenuItem Instance
*/
public MenuItem addActions(List<ClickType> types, MenuAction... actions) {
for(ClickType type : types)
addActions(type, actions);
return this;
}
/**
* Add actions to MenuItem for any type of Click
* @param actions Array of MenuActions
* @return MenuItem Instance
*/
public MenuItem addActions(MenuAction... actions) {
for(ClickType type : ClickType.values()) {
addActions(type, actions);
addActions(type, actions);
}
return this;
}
/**
* Add actions to MenuItem
* @param handlers IActionHandler[]
* @return MenuItem Instance
*/
public MenuItem addActions(IActionHandler... handlers) {
for(IActionHandler handler : handlers)
addActions(handler.getClickTypes(), new MenuAction() {
public void execute(Player player) {
handler.execute(player);
}
});
return this;
}
/**
* Get if should do cancel
* @return boolean
*/
public boolean doCancel() {
return cancel;
}
/**
* Set if should do cancel
* @param cancel boolean
* @return MenuItem Instance
*/
public MenuItem setCancel(boolean cancel) {
this.cancel = cancel;
return this;
}
/**
* Clone menu item
* @return MenuItem
*/
public MenuItem clone() {
MenuItem mi = new MenuItem(item.clone(), cancel);
mi.actions = actions;
return mi;
}
}

View File

@ -0,0 +1,96 @@
package nl.iobyte.menuapi.map;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class WeakConcurrentHashMap<K, V> extends ConcurrentHashMap<K, V> {
private Map<K, Long> timeMap = new ConcurrentHashMap<>();
private long expiryInMillis;
private boolean mapAlive = true;
public WeakConcurrentHashMap() {
this.expiryInMillis = 10 * 1000;
initialize();
}
public WeakConcurrentHashMap(long expiryInMillis) {
this.expiryInMillis = expiryInMillis;
initialize();
}
void initialize() {
new CleanerThread().start();
}
@Override
public V put(K key, V value) {
if (!mapAlive)
throw new IllegalStateException("WeakConcurrent Hashmap is no more alive.. Try creating a new one.");
Date date = new Date();
timeMap.put(key, date.getTime());
return super.put(key, value);
}
@Override
@SuppressWarnings("unchecked")
public V get(Object key) {
if (!mapAlive)
throw new IllegalStateException("WeakConcurrent Hashmap is no more alive.. Try creating a new one.");
Date date = new Date();
timeMap.put((K) key, date.getTime());
return super.get(key);
}
@Override
public void putAll(Map<? extends K, ? extends V> m) {
if (!mapAlive)
throw new IllegalStateException("WeakConcurrent Hashmap is no more alive.. Try creating a new one.");
for (K key : m.keySet())
put(key, m.get(key));
}
@Override
public V putIfAbsent(K key, V value) {
if (!mapAlive)
throw new IllegalStateException("WeakConcurrent Hashmap is no more alive.. Try creating a new one.");
return !containsKey(key) ? put(key, value) : get(key);
}
public void quitMap() {
mapAlive = false;
}
public boolean isAlive() {
return mapAlive;
}
class CleanerThread extends Thread {
@Override
public void run() {
while (mapAlive) {
cleanMap();
try {
Thread.sleep(expiryInMillis / 2);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private void cleanMap() {
long currentTime = new Date().getTime();
for (K key : timeMap.keySet())
if (currentTime > (timeMap.get(key) + expiryInMillis))
timeMap.remove(key);
}
}
}

View File

@ -0,0 +1,23 @@
package nl.iobyte.menuapi.multi;
import nl.iobyte.menuapi.basic.Menu;
public class MenuPage extends Menu {
private final MultiMenu menu;
public MenuPage(String title, int size, MultiMenu menu) {
super(title, size);
this.menu = menu;
}
public MenuPage(String title, int size, boolean locked, MultiMenu menu) {
super(title, size, locked);
this.menu = menu;
}
public MultiMenu getParent() {
return menu;
}
}

View File

@ -0,0 +1,467 @@
package nl.iobyte.menuapi.multi;
import nl.iobyte.menuapi.action.InteractAction;
import nl.iobyte.menuapi.interfaces.IMenu;
import nl.iobyte.menuapi.item.MenuItem;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
public class MultiMenu implements IMenu {
private String title;
private ArrayList<Integer> page_sizes;
private InteractAction open, close;
private boolean locked = true;
private final HashMap<Integer, MenuPage> pages = new HashMap<>();
private final HashMap<UUID, MenuPage> page_data = new HashMap<>();
public MultiMenu(String title, ArrayList<Integer> page_sizes) {
this.title = title;
this.page_sizes = page_sizes;
buildPages();
}
public MultiMenu(String title, ArrayList<Integer> page_sizes, boolean locked) {
this.title = title;
this.page_sizes = page_sizes;
this.locked = locked;
buildPages();
}
/**
* Return inventory sizes
* @return int
*/
public ArrayList<Integer> getPageSizes() {
return page_sizes;
}
/**
* Set sizes
* @param page_sizes int
* @return IMenu
*/
public IMenu setPageSizes(ArrayList<Integer> page_sizes) {
this.page_sizes = page_sizes;
buildPages();
return this;
}
/**
* Get item of first page
* @param slot int
* @return MenuItem
*/
public MenuItem getItem(int slot) {
int[] data = getSlot(slot);
if(data == null)
return null;
return getItem(data[0], data[1]);
}
/**
* Get item on page
* @param page int
* @param slot int
* @return MenuItem
*/
public MenuItem getItem(int page, int slot) {
MenuPage mp = getPage(page);
if(mp == null)
return null;
return mp.getItem(slot);
}
/**
* Get items of first page
* @return IMenu
*/
public HashMap<Integer, MenuItem> getItems() {
return getItems(1);
}
/**
* Get items of MenuPage
* @param page int
* @return HashMap
*/
public HashMap<Integer, MenuItem> getItems(int page) {
if(page < 1)
return null;
MenuPage mp = pages.get(page);
if(mp == null)
return null;
return mp.getItems();
}
/**
* Set item to slot on first page
* @param slot int
* @param item MenuItem
* @return IMenu
*/
public IMenu setItem(int slot, MenuItem item) {
int[] data = getSlot(slot);
if(data == null)
return this;
return setItem(data[0], data[1], item);
}
/**
* Set item to slot on page
* @param page int
* @param slot int
* @param item MenuItem
* @return IMenu
*/
public IMenu setItem(int page, int slot, MenuItem item) {
MenuPage mp = getPage(page);
if(mp == null)
return this;
return mp.setItem(slot, item);
}
/**
* Set item to slot on first page
* @param slot int
* @param item ItemStack
* @return IMenu
*/
public IMenu setItem(int slot, ItemStack item) {
int[] data = getSlot(slot);
if(data == null)
return this;
return setItem(data[0], data[1], item);
}
/**
* Set item to slot on page
* @param page int
* @param slot int
* @param item ItemStack
* @return IMenu
*/
public IMenu setItem(int page, int slot, ItemStack item) {
MenuPage mp = getPage(page);
if(mp == null)
return this;
mp.setItem(slot, item);
return mp.setItem(slot, item);
}
/**
* Set items for first page
* @param items MenuItems
* @return IMenu
*/
public MultiMenu setItems(HashMap<Integer, MenuItem> items) {
return setItems(1, items);
}
/**
* Set items for MenuPage
* @param page int
* @param items HashMap
* @return IMenu
*/
public MultiMenu setItems(int page, HashMap<Integer, MenuItem> items) {
if(page < 1)
return this;
MenuPage mp = pages.get(page);
if(mp == null)
return this;
mp.setItems(items);
return this;
}
/**
* Update item on first page
* @param slot int
* @return IMenu
*/
public IMenu updateItem(int slot) {
int[] data = getSlot(slot);
if(data == null)
return this;
return updateItem(data[0], data[1]);
}
/**
* Update item on page
* @param page int
* @param slot int
* @return IMenu
*/
public IMenu updateItem(int page, int slot) {
MenuPage mp = getPage(page);
if(mp == null)
return this;
mp.updateItem(slot);
return this;
}
/**
* Get page from slot
* @param slot int
* @return int
*/
public int[] getSlot(int slot) {
int page = 1;
int temp = slot;
for(int size : page_sizes) {
temp -= size;
if(temp < 0) {
temp += size;
return new int[]{
page, temp
};
}
page++;
}
return null;
}
/**
* Get MenuPage
* @param page int
* @return MenuPage
*/
public MenuPage getPage(int page) {
if(!pages.containsKey(page))
return null;
return pages.get(page);
}
/**
* Get MenuPages
* @return HashMap
*/
public HashMap<Integer, MenuPage> getPages() {
return pages;
}
/**
* Get action on open
* @return InteractAction
*/
public InteractAction getOpenAction() {
return open;
}
/**
* Set action on open
* @param action InteractAction
* @return IMenu
*/
public IMenu setOpenAction(InteractAction action) {
open = action;
return this;
}
/**
* Get action on close
* @return InteractAction
*/
public InteractAction getCloseAction() {
return close;
}
/**
* Set action on close
* @param action InteractAction
* @return IMenu
*/
public IMenu setCloseAction(InteractAction action) {
close = action;
return this;
}
/**
* Get menu title
* @return String
*/
public String getTitle() {
return title;
}
/**
* Set menu title
* @param title String
* @return IMenu
*/
public IMenu setTitle(String title) {
this.title = title;
return this;
}
/**
* Get if menu is locked
* @return Boolean
*/
public boolean isLocked() {
return locked;
}
/**
* Set if menu is locked
* @param locked Boolean
* @return IMenu
*/
public IMenu setLocked(boolean locked) {
this.locked = locked;
for(MenuPage page : page_data.values())
page.setLocked(locked);
return this;
}
/**
* Build Menu
* @return IMenu
*/
public IMenu build() {
for(MenuPage page : pages.values())
page.build();
return this;
}
/**
* Get menu pages
* @return IMenu
*/
public IMenu buildPages() {
int i = 1;
for(int size : page_sizes) {
MenuPage page = getPage(i);
if(page == null) {
page = new MenuPage(title, size, locked,this);
} else{
page.setSize(size);
}
pages.put(i, page);
i++;
}
int difference = pages.size() - page_sizes.size();
if(difference > 0)
for(int j = 0; j < difference; j++)
pages.remove(page_sizes.size() + j);
return this;
}
/**
* Return null
* @return Inventory
*/
public Inventory getInventory() {
MenuPage mp = pages.get(1);
if(mp == null)
return null;
return mp.getInventory();
}
/**
* Execute non existing Click
* @param player Player
* @param slot int
* @param type ClickType
* @return cancel
*/
public boolean execute(Player player, int slot, ClickType type) {
return true;
}
/**
* See if MenuPage is open for User
* @param uuid UUID
* @param page MenuPage
* @return boolean
*/
public boolean isActivePage(UUID uuid, MenuPage page) {
if(uuid == null || page == null)
return false;
if(!page_data.containsKey(uuid))
return false;
MenuPage mp = page_data.get(uuid);
return mp == page;
}
/**
* Open first page of inventory
* @param player Player
*/
public void open(Player player) {
open(player, 1);
}
/**
* Open page of invetory
* @param player Player
* @param page int
*/
public void open(Player player, int page) {
if(player == null || page < 1)
return;
if(!pages.containsKey(page))
return;
MenuPage mp = pages.get(page);
page_data.put(player.getUniqueId(), mp);
mp.open(player);
}
/**
* Close inventory of Player
* @param player Player
*/
public void close(Player player) {
if(player == null)
return;
page_data.remove(player.getUniqueId());
player.closeInventory();
}
/**
* Clone Menu
* @return MultiMenu
*/
public MultiMenu clone() {
MultiMenu menu = new MultiMenu(title, page_sizes);
for(Map.Entry<Integer, MenuPage> entrySet : pages.entrySet())
menu.setItems(entrySet.getKey(), entrySet.getValue().getItems());
return menu;
}
}