1
mirror of https://github.com/CarmJos/EasyPlugin.git synced 2026-06-04 16:48:16 +08:00

修复在GUI中打开另一个GUI时监听器失效问题

This commit is contained in:
LSeng
2022-05-22 22:17:12 +08:00
parent 10f5961ccd
commit 12ff00e1d9
2 changed files with 41 additions and 22 deletions
@@ -16,12 +16,13 @@ import org.jetbrains.annotations.Nullable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.IntStream;
public class GUI {
private static JavaPlugin plugin;
private static final HashMap<Player, GUI> openedGUIs = new HashMap<>();
private static final HashMap<UUID, GUI> openedGUIs = new HashMap<>();
public static void initialize(JavaPlugin plugin) {
GUI.plugin = plugin;
@@ -31,7 +32,7 @@ public class GUI {
return plugin;
}
public static HashMap<Player, GUI> getOpenedGUIs() {
public static HashMap<UUID, GUI> getOpenedGUIs() {
return openedGUIs;
}
@@ -131,23 +132,31 @@ public class GUI {
}
public void addFlag(String flag, Object obj) {
if (this.flags == null) this.flags = new HashMap<>();
if (this.flags == null) {
this.flags = new HashMap<>();
}
this.flags.put(flag, obj);
}
public Object getFlag(String flag) {
if (this.flags == null) return null;
else
if (this.flags == null) {
return null;
} else {
return this.flags.get(flag);
}
}
public void setFlag(String flag, Object obj) {
if (this.flags == null) this.flags = new HashMap<>();
if (this.flags == null) {
this.flags = new HashMap<>();
}
this.flags.replace(flag, obj);
}
public void removeFlag(String flag) {
if (this.flags == null) this.flags = new HashMap<>();
if (this.flags == null) {
this.flags = new HashMap<>();
}
this.flags.remove(flag);
}
@@ -155,12 +164,17 @@ public class GUI {
}
public void openGUI(Player player) {
if (this.type == GUIType.CANCEL) throw new NullPointerException("被取消或不存在的GUI");
if (this.type == GUIType.CANCEL) { throw new IllegalStateException("被取消或不存在的GUI"); }
Inventory inv = Bukkit.createInventory(null, this.type.getSize(), this.name);
IntStream.range(0, inv.getSize()).forEach(index -> inv.setItem(index, new ItemStack(Material.AIR)));
getItems().forEach((index, item) -> inv.setItem(index, item.getDisplay()));
GUI previous = getOpenedGUI(player);
if(previous != null){
previous.listener.close(player);
}
setOpenedGUI(player, this);
this.inv = inv;
@@ -187,19 +201,19 @@ public class GUI {
public static void setOpenedGUI(Player player, GUI gui) {
getOpenedGUIs().put(player, gui);
getOpenedGUIs().put(player.getUniqueId(), gui);
}
public static boolean hasOpenedGUI(Player player) {
return getOpenedGUIs().containsKey(player);
return getOpenedGUIs().containsKey(player.getUniqueId());
}
public static GUI getOpenedGUI(Player player) {
return getOpenedGUIs().get(player);
return getOpenedGUIs().get(player.getUniqueId());
}
public static void removeOpenedGUI(Player player) {
getOpenedGUIs().remove(player);
getOpenedGUIs().remove(player.getUniqueId());
}
}
@@ -1,5 +1,6 @@
package cc.carm.lib.easyplugin.gui;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.HandlerList;
@@ -23,10 +24,10 @@ public class GUIListener implements Listener {
@EventHandler
public void onInventoryClickEvent(InventoryClickEvent event) {
if (!(event.getWhoClicked() instanceof Player)) return;
if (!(event.getWhoClicked() instanceof Player)) { return; }
Player player = (Player) event.getWhoClicked();
if (!GUI.hasOpenedGUI(player)) return;
if (GUI.getOpenedGUI(player) != getCurrentGUI()) return;
if (!GUI.hasOpenedGUI(player)) { return;}
if (GUI.getOpenedGUI(player) != getCurrentGUI()) { return; }
getCurrentGUI().rawClickListener(event);
@@ -35,11 +36,11 @@ public class GUIListener implements Listener {
return;
}
if (event.getClickedInventory() == null) return;
if (event.getClickedInventory() == null) { return; }
if (event.getClickedInventory().equals(getCurrentGUI().inv)) {
if (getCurrentGUI().cancelOnTarget) event.setCancelled(true);
if (getCurrentGUI().cancelOnTarget) { event.setCancelled(true); }
if (event.getSlot() != -999) {
GUIItem clickedItem = getCurrentGUI().getItem(event.getSlot());
@@ -61,7 +62,7 @@ public class GUIListener implements Listener {
@EventHandler
public void onDrag(InventoryDragEvent e) {
if (!(e.getWhoClicked() instanceof Player)) return;
if (!(e.getWhoClicked() instanceof Player)) { return; }
if (e.getInventory().equals(getCurrentGUI().inv)
|| e.getInventory().equals(e.getWhoClicked().getInventory())) {
getCurrentGUI().onDrag(e);
@@ -70,14 +71,18 @@ public class GUIListener implements Listener {
@EventHandler
public void onInventoryCloseEvent(InventoryCloseEvent event) {
if (!(event.getPlayer() instanceof Player)) return;
if (!event.getInventory().equals(getCurrentGUI().inv)) return;
if (!(event.getPlayer() instanceof Player)) { return; }
if (!event.getInventory().equals(getCurrentGUI().inv)) { return; }
close((Player) event.getPlayer());
}
protected void close(Player p){
HandlerList.unregisterAll(this);
getCurrentGUI().listener = null;
GUI.removeOpenedGUI((Player) event.getPlayer());
GUI.removeOpenedGUI(p);
getCurrentGUI().onClose();
}
@EventHandler