GUI is working

This commit is contained in:
Mika 2025-01-26 21:20:37 +01:00
parent e817867af0
commit 43c81b4ab6
3 changed files with 42 additions and 15 deletions

View File

@ -1,5 +1,6 @@
package de.polyfish0.survivalGames.gui;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryView;
@ -7,4 +8,5 @@ public interface GuiInterface {
void setInventoryView(InventoryView view);
Inventory getInventory();
InventoryView getInventoryView();
void click(InventoryClickEvent event);
}

View File

@ -1,15 +1,26 @@
package de.polyfish0.survivalGames.gui;
import de.polyfish0.survivalGames.annotations.EventClass;
import org.bukkit.ChatColor;
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.util.EmptyStackException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Stack;
public class GuiManager {
@EventClass
public class GuiManager implements Listener {
private final static GuiManager INSTANCE = new GuiManager();
private final HashMap<Player, Stack<GuiInterface>> interfaceHistory = new HashMap<>();
private final HashSet<Inventory> openedGuiInterfaces = new HashSet<>();
public static GuiManager getInstance() {
return INSTANCE;
@ -28,7 +39,10 @@ public class GuiManager {
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) {
throw new RuntimeException(e);
}
@ -44,15 +58,31 @@ public class GuiManager {
}
public void popInventory(Player p) {
GuiInterface lastInterface;
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) {
p.closeInventory();
return;
}
}
p.closeInventory();
lastInterface.setInventoryView(p.openInventory(lastInterface.getInventory()));
@EventHandler
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);
}
}

View File

@ -4,6 +4,7 @@ import de.polyfish0.survivalGames.annotations.EventClass;
import de.polyfish0.survivalGames.gui.GuiInterface;
import de.polyfish0.survivalGames.gui.GuiManager;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
@ -18,8 +19,7 @@ import java.util.HashMap;
import java.util.UUID;
import java.util.function.Consumer;
@EventClass
public class MainMenu implements GuiInterface, Listener {
public class MainMenu implements GuiInterface {
private Inventory inventory;
private final HashMap<ItemStack, Consumer<Player>> methodRegister = new HashMap<>();
private InventoryView inventoryView;
@ -47,11 +47,8 @@ public class MainMenu implements GuiInterface, Listener {
return inventory;
}
@EventHandler
public void onClick(InventoryClickEvent e) {
if(e.getClickedInventory() != inventoryView.getInventory(0))
return;
@Override
public void click(InventoryClickEvent e) {
if(!methodRegister.containsKey(e.getCurrentItem())) {
e.setCancelled(true);
return;
@ -63,8 +60,6 @@ public class MainMenu implements GuiInterface, Listener {
@Override
public void setInventoryView(InventoryView view) {
inventoryView = view;
System.out.println(view);
System.out.println(inventoryView);
}
@Override