mirror of
https://github.com/CarmJos/EasyPlugin.git
synced 2026-06-04 16:48:16 +08:00
修复在GUI中打开另一个GUI时监听器失效问题
This commit is contained in:
@@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -131,23 +132,31 @@ public class GUI {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void addFlag(String flag, Object obj) {
|
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);
|
this.flags.put(flag, obj);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Object getFlag(String flag) {
|
public Object getFlag(String flag) {
|
||||||
if (this.flags == null) return null;
|
if (this.flags == null) {
|
||||||
else
|
return null;
|
||||||
|
} else {
|
||||||
return this.flags.get(flag);
|
return this.flags.get(flag);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void setFlag(String flag, Object obj) {
|
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);
|
this.flags.replace(flag, obj);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void removeFlag(String flag) {
|
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);
|
this.flags.remove(flag);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -155,12 +164,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 +201,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());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package cc.carm.lib.easyplugin.gui;
|
package cc.carm.lib.easyplugin.gui;
|
||||||
|
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.event.EventHandler;
|
import org.bukkit.event.EventHandler;
|
||||||
import org.bukkit.event.HandlerList;
|
import org.bukkit.event.HandlerList;
|
||||||
@@ -23,10 +24,10 @@ public class GUIListener implements Listener {
|
|||||||
|
|
||||||
@EventHandler
|
@EventHandler
|
||||||
public void onInventoryClickEvent(InventoryClickEvent event) {
|
public void onInventoryClickEvent(InventoryClickEvent event) {
|
||||||
if (!(event.getWhoClicked() instanceof Player)) return;
|
if (!(event.getWhoClicked() instanceof Player)) { return; }
|
||||||
Player player = (Player) event.getWhoClicked();
|
Player player = (Player) event.getWhoClicked();
|
||||||
if (!GUI.hasOpenedGUI(player)) return;
|
if (!GUI.hasOpenedGUI(player)) { return;}
|
||||||
if (GUI.getOpenedGUI(player) != getCurrentGUI()) return;
|
if (GUI.getOpenedGUI(player) != getCurrentGUI()) { return; }
|
||||||
|
|
||||||
getCurrentGUI().rawClickListener(event);
|
getCurrentGUI().rawClickListener(event);
|
||||||
|
|
||||||
@@ -35,11 +36,11 @@ public class GUIListener implements Listener {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (event.getClickedInventory() == null) return;
|
if (event.getClickedInventory() == null) { return; }
|
||||||
|
|
||||||
if (event.getClickedInventory().equals(getCurrentGUI().inv)) {
|
if (event.getClickedInventory().equals(getCurrentGUI().inv)) {
|
||||||
|
|
||||||
if (getCurrentGUI().cancelOnTarget) event.setCancelled(true);
|
if (getCurrentGUI().cancelOnTarget) { event.setCancelled(true); }
|
||||||
|
|
||||||
if (event.getSlot() != -999) {
|
if (event.getSlot() != -999) {
|
||||||
GUIItem clickedItem = getCurrentGUI().getItem(event.getSlot());
|
GUIItem clickedItem = getCurrentGUI().getItem(event.getSlot());
|
||||||
@@ -61,7 +62,7 @@ public class GUIListener implements Listener {
|
|||||||
|
|
||||||
@EventHandler
|
@EventHandler
|
||||||
public void onDrag(InventoryDragEvent e) {
|
public void onDrag(InventoryDragEvent e) {
|
||||||
if (!(e.getWhoClicked() instanceof Player)) return;
|
if (!(e.getWhoClicked() instanceof Player)) { return; }
|
||||||
if (e.getInventory().equals(getCurrentGUI().inv)
|
if (e.getInventory().equals(getCurrentGUI().inv)
|
||||||
|| e.getInventory().equals(e.getWhoClicked().getInventory())) {
|
|| e.getInventory().equals(e.getWhoClicked().getInventory())) {
|
||||||
getCurrentGUI().onDrag(e);
|
getCurrentGUI().onDrag(e);
|
||||||
@@ -70,14 +71,18 @@ public class GUIListener implements Listener {
|
|||||||
|
|
||||||
@EventHandler
|
@EventHandler
|
||||||
public void onInventoryCloseEvent(InventoryCloseEvent event) {
|
public void onInventoryCloseEvent(InventoryCloseEvent event) {
|
||||||
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
|
||||||
|
|||||||
Reference in New Issue
Block a user