GUI is working
This commit is contained in:
parent
e817867af0
commit
43c81b4ab6
@ -1,5 +1,6 @@
|
|||||||
package de.polyfish0.survivalGames.gui;
|
package de.polyfish0.survivalGames.gui;
|
||||||
|
|
||||||
|
import org.bukkit.event.inventory.InventoryClickEvent;
|
||||||
import org.bukkit.inventory.Inventory;
|
import org.bukkit.inventory.Inventory;
|
||||||
import org.bukkit.inventory.InventoryView;
|
import org.bukkit.inventory.InventoryView;
|
||||||
|
|
||||||
@ -7,4 +8,5 @@ public interface GuiInterface {
|
|||||||
void setInventoryView(InventoryView view);
|
void setInventoryView(InventoryView view);
|
||||||
Inventory getInventory();
|
Inventory getInventory();
|
||||||
InventoryView getInventoryView();
|
InventoryView getInventoryView();
|
||||||
|
void click(InventoryClickEvent event);
|
||||||
}
|
}
|
||||||
|
@ -1,15 +1,26 @@
|
|||||||
package de.polyfish0.survivalGames.gui;
|
package de.polyfish0.survivalGames.gui;
|
||||||
|
|
||||||
|
import de.polyfish0.survivalGames.annotations.EventClass;
|
||||||
|
import org.bukkit.ChatColor;
|
||||||
import org.bukkit.entity.Player;
|
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 org.bukkit.inventory.Inventory;
|
||||||
|
import org.bukkit.inventory.InventoryView;
|
||||||
|
|
||||||
import java.lang.reflect.InvocationTargetException;
|
import java.lang.reflect.InvocationTargetException;
|
||||||
import java.util.EmptyStackException;
|
import java.util.EmptyStackException;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.HashSet;
|
||||||
import java.util.Stack;
|
import java.util.Stack;
|
||||||
|
|
||||||
public class GuiManager {
|
@EventClass
|
||||||
|
public class GuiManager implements Listener {
|
||||||
private final static GuiManager INSTANCE = new GuiManager();
|
private final static GuiManager INSTANCE = new GuiManager();
|
||||||
private final HashMap<Player, Stack<GuiInterface>> interfaceHistory = new HashMap<>();
|
private final HashMap<Player, Stack<GuiInterface>> interfaceHistory = new HashMap<>();
|
||||||
|
private final HashSet<Inventory> openedGuiInterfaces = new HashSet<>();
|
||||||
|
|
||||||
public static GuiManager getInstance() {
|
public static GuiManager getInstance() {
|
||||||
return INSTANCE;
|
return INSTANCE;
|
||||||
@ -28,7 +39,10 @@ public class GuiManager {
|
|||||||
interfaceHistory.get(p).push(interfaceInstance);
|
interfaceHistory.get(p).push(interfaceInstance);
|
||||||
}
|
}
|
||||||
|
|
||||||
interfaceInstance.setInventoryView(p.openInventory(interfaceInstance.getInventory()));
|
p.closeInventory();
|
||||||
|
InventoryView view = p.openInventory(interfaceInstance.getInventory());
|
||||||
|
interfaceInstance.setInventoryView(view);
|
||||||
|
openedGuiInterfaces.add(view.getTopInventory());
|
||||||
} catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
|
} catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
@ -44,15 +58,31 @@ public class GuiManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void popInventory(Player p) {
|
public void popInventory(Player p) {
|
||||||
GuiInterface lastInterface;
|
|
||||||
try {
|
try {
|
||||||
lastInterface = interfaceHistory.get(p).pop();
|
interfaceHistory.get(p).pop().getInventoryView().close();
|
||||||
|
|
||||||
|
InventoryView view = p.openInventory(interfaceHistory.get(p).peek().getInventory());
|
||||||
|
interfaceHistory.get(p).peek().setInventoryView(view);
|
||||||
|
openedGuiInterfaces.add(view.getTopInventory());
|
||||||
} catch (EmptyStackException e) {
|
} catch (EmptyStackException e) {
|
||||||
p.closeInventory();
|
p.closeInventory();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
p.closeInventory();
|
@EventHandler
|
||||||
lastInterface.setInventoryView(p.openInventory(lastInterface.getInventory()));
|
public void onInventoryClosed(InventoryCloseEvent e) {
|
||||||
|
getInstance().openedGuiInterfaces.remove(e.getInventory());
|
||||||
|
}
|
||||||
|
|
||||||
|
@EventHandler
|
||||||
|
public void onInventoryClick(InventoryClickEvent e) {
|
||||||
|
if(!(e.getWhoClicked() instanceof Player)) return;
|
||||||
|
|
||||||
|
Player p = (Player) e.getWhoClicked();
|
||||||
|
|
||||||
|
if(!getInstance().interfaceHistory.containsKey(p) || !getInstance().openedGuiInterfaces.contains(e.getClickedInventory())) return;
|
||||||
|
|
||||||
|
getInstance().interfaceHistory.get(p).peek().click(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,7 @@ import de.polyfish0.survivalGames.annotations.EventClass;
|
|||||||
import de.polyfish0.survivalGames.gui.GuiInterface;
|
import de.polyfish0.survivalGames.gui.GuiInterface;
|
||||||
import de.polyfish0.survivalGames.gui.GuiManager;
|
import de.polyfish0.survivalGames.gui.GuiManager;
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
|
import org.bukkit.ChatColor;
|
||||||
import org.bukkit.Material;
|
import org.bukkit.Material;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.event.EventHandler;
|
import org.bukkit.event.EventHandler;
|
||||||
@ -18,8 +19,7 @@ import java.util.HashMap;
|
|||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
@EventClass
|
public class MainMenu implements GuiInterface {
|
||||||
public class MainMenu implements GuiInterface, Listener {
|
|
||||||
private Inventory inventory;
|
private Inventory inventory;
|
||||||
private final HashMap<ItemStack, Consumer<Player>> methodRegister = new HashMap<>();
|
private final HashMap<ItemStack, Consumer<Player>> methodRegister = new HashMap<>();
|
||||||
private InventoryView inventoryView;
|
private InventoryView inventoryView;
|
||||||
@ -47,11 +47,8 @@ public class MainMenu implements GuiInterface, Listener {
|
|||||||
return inventory;
|
return inventory;
|
||||||
}
|
}
|
||||||
|
|
||||||
@EventHandler
|
@Override
|
||||||
public void onClick(InventoryClickEvent e) {
|
public void click(InventoryClickEvent e) {
|
||||||
if(e.getClickedInventory() != inventoryView.getInventory(0))
|
|
||||||
return;
|
|
||||||
|
|
||||||
if(!methodRegister.containsKey(e.getCurrentItem())) {
|
if(!methodRegister.containsKey(e.getCurrentItem())) {
|
||||||
e.setCancelled(true);
|
e.setCancelled(true);
|
||||||
return;
|
return;
|
||||||
@ -63,8 +60,6 @@ public class MainMenu implements GuiInterface, Listener {
|
|||||||
@Override
|
@Override
|
||||||
public void setInventoryView(InventoryView view) {
|
public void setInventoryView(InventoryView view) {
|
||||||
inventoryView = view;
|
inventoryView = view;
|
||||||
System.out.println(view);
|
|
||||||
System.out.println(inventoryView);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
Loading…
Reference in New Issue
Block a user