Skip to content

Instantly share code, notes, and snippets.

@Amejonah1200
Last active July 9, 2019 19:06
Show Gist options
  • Save Amejonah1200/0dd1ee777d757dbdc73949e83c732a92 to your computer and use it in GitHub Desktop.
Save Amejonah1200/0dd1ee777d757dbdc73949e83c732a92 to your computer and use it in GitHub Desktop.
Menu-System Konzept, tested
//HowToOpen first Menu
MenuRegistery#open(null, new OneMenu((Player) arg0, getMenuRegistery()));
package ap.apbuy.menu;
import java.util.regex.Pattern;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.inventory.Inventory;
import ap.apbuy.APBuy;
public abstract class Menu {
private Player viewer;
private String title;
private int size;
private Menu prevMenu;
private MenuRegistery menuRegistery;
private boolean ignoreClose;
private APBuy plugin;
public Menu(Player viewer, String title, int size, Menu prevMenu, MenuRegistery menuRegistery) {
this.viewer = viewer;
this.title = title;
this.size = size;
this.prevMenu = prevMenu;
this.menuRegistery = menuRegistery;
this.plugin = menuRegistery.getPlugin();
}
public Menu(Player viewer, String title, int size, Menu prevMenu) {
this.viewer = viewer;
this.title = title;
this.size = size;
this.prevMenu = prevMenu;
this.menuRegistery = prevMenu.getMenuRegistery();
this.plugin = prevMenu.getPlugin();
}
public Menu(Player viewer, String title, int size, MenuRegistery menuRegistery) {
this.viewer = viewer;
this.title = title;
this.size = size;
this.menuRegistery = menuRegistery;
this.plugin = menuRegistery.getPlugin();
}
public void open() {
this.setIgnoreClose(true);
this.getViewer()
.openInventory(this.assembleInventory(Bukkit.createInventory(null, this.getSize(), this.getTitle())));
this.setIgnoreClose(false);
}
public abstract Inventory assembleInventory(Inventory inventory);
public void open(Menu menu) {
System.out.println(this.getViewer().getName() + ": " + getClassName(this.getClass()) + " -> "
+ getClassName(menu.getClass()));
this.getMenuRegistery().open(this, menu);
}
private String getClassName(Class<?> clazz) {
return this.getClass().getName()
.split(Pattern.quote("."))[this.getClass().getName().split(Pattern.quote(".")).length - 1];
}
public boolean onClick(InventoryClickEvent event) {
return true;
}
public void onClose(InventoryCloseEvent event) {
}
public String getTitle() {
return title;
}
public int getSize() {
return size;
}
public APBuy getPlugin() {
return plugin;
}
public Menu getPrevMenu() {
return prevMenu;
}
public Player getViewer() {
return viewer;
}
public MenuRegistery getMenuRegistery() {
return menuRegistery;
}
public boolean isIgnoreClose() {
return ignoreClose;
}
public void setIgnoreClose(boolean ignoreClose) {
this.ignoreClose = ignoreClose;
}
}
package ap.apbuy.menu;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryCloseEvent;
import ap.apbuy.APBuy;
public class MenuRegistery implements Listener {
private List<Menu> openedMenus;
private APBuy plugin;
public MenuRegistery(APBuy plugin) {
this.openedMenus = new ArrayList<>();
this.plugin = plugin;
}
public void open(Menu menu, Menu menuToOpen) {
if (menu != null) {
openedMenus.remove(menu);
}
menuToOpen.open();
openedMenus.add(menuToOpen);
}
public void forceClose(Player player) {
this.remove(player);
player.closeInventory();
}
public void close(Player player) {
player.closeInventory();
}
public void remove(Player player) {
Iterator<Menu> menus = openedMenus.iterator();
while (menus.hasNext()) {
if (menus.next().getViewer().equals(player)) {
menus.remove();
break;
}
}
}
public void remove(Menu menu) {
this.openedMenus.remove(menu);
}
public APBuy getPlugin() {
return plugin;
}
@EventHandler
public void onClose(InventoryCloseEvent event) {
Menu menu = this.getMenuByPlayer((Player) event.getPlayer());
if (menu != null) {
menu.onClose(event);
this.remove(menu);
}
System.out.println(openedMenus.size());
}
@EventHandler
public void onClick(InventoryClickEvent event) {
Menu menu = this.getMenuByPlayer((Player) event.getWhoClicked());
if (menu != null) {
event.setCancelled(menu.onClick(event));
}
}
public Menu getMenuByPlayer(Player player) {
for (Menu menu : openedMenus) {
if (menu.getViewer().equals(player)) {
return menu;
}
}
return null;
}
}
package ap.apbuy.menu.menus.market;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.Inventory;
import ap.apbuy.menu.Menu;
import ap.apbuy.menu.MenuRegistery;
import ap.apbuy.utils.Data;
import ap.apbuy.utils.InventoryBuilder;
import ap.apbuy.utils.ItemStackBuilder;
public class OneMenu extends Menu {
public OneMenu(Player viewer, MenuRegistery registery) {
super(viewer, "§0§lA§3§lP§r§8Buy", 54, registery);
}
public OneMenu(Player viewer, Menu prevMenu) {
super(viewer, "§0§lA§3§lP§r§8Buy", 54, prevMenu);
}
@Override
public Inventory assembleInventory(Inventory inventory) {
return new InventoryBuilder(inventory).fillWithItemStack(Data.getGlassGrey()).setItem(4, new ItemStackBuilder("§3§lMy Market", Material.BARRIER).addLineToLore("Here == Your market.").build()).build();
}
@Override
public boolean onClick(InventoryClickEvent event) {
if(event.getSlot() == 49) {
this.open(new TestOtherMenu(this.getViewer(), this));
}
return true;
}
}
package ap.apbuy.menu.menus.market;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import ap.apbuy.menu.Menu;
import ap.apbuy.utils.Data;
import ap.apbuy.utils.InventoryBuilder;
public class TestOtherMenu extends Menu {
public TestOtherMenu(Player viewer, Menu prevMenu) {
super(viewer, "Test", 54, prevMenu);
}
@Override
public Inventory assembleInventory(Inventory inventory) {
return new InventoryBuilder(inventory).fillWithItemStack(Data.getGlassGrey())
.setItem(49, new ItemStack(Material.BARRIER)).build();
}
@Override
public boolean onClick(InventoryClickEvent event) {
if (event.getSlot() == 49) {
this.open(this.getPrevMenu());
}
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment