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

Merge pull request #3 from MociLSeng/master

修复在GUI中打开另一个GUI时监听器失效问题
This commit is contained in:
2022-05-22 22:30:51 +08:00
committed by GitHub
2 changed files with 19 additions and 9 deletions
@@ -16,12 +16,13 @@ import org.jetbrains.annotations.Nullable;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.UUID;
import java.util.stream.IntStream; import java.util.stream.IntStream;
public class GUI { public class GUI {
private static JavaPlugin plugin; 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) { public static void initialize(JavaPlugin plugin) {
GUI.plugin = plugin; GUI.plugin = plugin;
@@ -31,7 +32,7 @@ public class GUI {
return plugin; return plugin;
} }
public static HashMap<Player, GUI> getOpenedGUIs() { public static HashMap<UUID, GUI> getOpenedGUIs() {
return openedGUIs; return openedGUIs;
} }
@@ -155,12 +156,17 @@ public class GUI {
} }
public void openGUI(Player player) { 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); Inventory inv = Bukkit.createInventory(null, this.type.getSize(), this.name);
IntStream.range(0, inv.getSize()).forEach(index -> inv.setItem(index, new ItemStack(Material.AIR))); IntStream.range(0, inv.getSize()).forEach(index -> inv.setItem(index, new ItemStack(Material.AIR)));
getItems().forEach((index, item) -> inv.setItem(index, item.getDisplay())); getItems().forEach((index, item) -> inv.setItem(index, item.getDisplay()));
GUI previous = getOpenedGUI(player);
if(previous != null){
previous.listener.close(player);
}
setOpenedGUI(player, this); setOpenedGUI(player, this);
this.inv = inv; this.inv = inv;
@@ -187,19 +193,19 @@ public class GUI {
public static void setOpenedGUI(Player player, GUI gui) { public static void setOpenedGUI(Player player, GUI gui) {
getOpenedGUIs().put(player, gui); getOpenedGUIs().put(player.getUniqueId(), gui);
} }
public static boolean hasOpenedGUI(Player player) { public static boolean hasOpenedGUI(Player player) {
return getOpenedGUIs().containsKey(player); return getOpenedGUIs().containsKey(player.getUniqueId());
} }
public static GUI getOpenedGUI(Player player) { public static GUI getOpenedGUI(Player player) {
return getOpenedGUIs().get(player); return getOpenedGUIs().get(player.getUniqueId());
} }
public static void removeOpenedGUI(Player player) { public static void removeOpenedGUI(Player player) {
getOpenedGUIs().remove(player); getOpenedGUIs().remove(player.getUniqueId());
} }
} }
@@ -73,11 +73,15 @@ public class GUIListener implements Listener {
if (!(event.getPlayer() instanceof Player)) return; if (!(event.getPlayer() instanceof Player)) return;
if (!event.getInventory().equals(getCurrentGUI().inv)) return; if (!event.getInventory().equals(getCurrentGUI().inv)) return;
close((Player) event.getPlayer());
}
protected void close(Player p){
HandlerList.unregisterAll(this); HandlerList.unregisterAll(this);
getCurrentGUI().listener = null; getCurrentGUI().listener = null;
GUI.removeOpenedGUI((Player) event.getPlayer()); GUI.removeOpenedGUI(p);
getCurrentGUI().onClose(); getCurrentGUI().onClose();
} }
@EventHandler @EventHandler