From 1534b574f2e132cca58be9a0094610de9b95416a Mon Sep 17 00:00:00 2001 From: Amaury Carrade Date: Thu, 9 Jul 2015 20:49:13 +0200 Subject: [PATCH] Added a GUI API --- .../fr/moribus/imageonmap/ImageOnMap.java | 2 + .../imageonmap/gui/core/AbstractGui.java | 110 ++++++++++++++++++ .../imageonmap/gui/core/GuiListener.java | 67 +++++++++++ .../imageonmap/gui/core/GuiManager.java | 79 +++++++++++++ 4 files changed, 258 insertions(+) create mode 100644 src/main/java/fr/moribus/imageonmap/gui/core/AbstractGui.java create mode 100644 src/main/java/fr/moribus/imageonmap/gui/core/GuiListener.java create mode 100644 src/main/java/fr/moribus/imageonmap/gui/core/GuiManager.java diff --git a/src/main/java/fr/moribus/imageonmap/ImageOnMap.java b/src/main/java/fr/moribus/imageonmap/ImageOnMap.java index 31aaff4..c843e45 100644 --- a/src/main/java/fr/moribus/imageonmap/ImageOnMap.java +++ b/src/main/java/fr/moribus/imageonmap/ImageOnMap.java @@ -19,6 +19,7 @@ package fr.moribus.imageonmap; import fr.moribus.imageonmap.commands.Commands; +import fr.moribus.imageonmap.gui.core.*; import fr.moribus.imageonmap.image.ImageIOExecutor; import fr.moribus.imageonmap.image.ImageRendererExecutor; import fr.moribus.imageonmap.image.MapInitEvent; @@ -84,6 +85,7 @@ public final class ImageOnMap extends JavaPlugin Commands.init(this); MapInitEvent.init(this); MapItemManager.init(); + GuiManager.init(this); } @Override diff --git a/src/main/java/fr/moribus/imageonmap/gui/core/AbstractGui.java b/src/main/java/fr/moribus/imageonmap/gui/core/AbstractGui.java new file mode 100644 index 0000000..a944082 --- /dev/null +++ b/src/main/java/fr/moribus/imageonmap/gui/core/AbstractGui.java @@ -0,0 +1,110 @@ +/* + * Copyright (C) 2013 Moribus + * Copyright (C) 2015 ProkopyL + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package fr.moribus.imageonmap.gui.core; + +import org.bukkit.*; +import org.bukkit.entity.*; +import org.bukkit.event.inventory.*; +import org.bukkit.inventory.*; +import org.bukkit.inventory.meta.*; + +import java.util.*; + + +/** + * @author IamBlueSlime + */ +public abstract class AbstractGui { + protected TreeMap actions = new TreeMap<>(); + protected Inventory inventory; + + public abstract void display(Player player); + + public void update(Player player) {} + + public void onClose(Player player) {} + + public void onClick(Player player, ItemStack stack, String action, ClickType clickType) + { + this.onClick(player, stack, action); + } + + public void onClick(Player player, ItemStack stack, String action) {} + + public void setSlotData(Inventory inv, String name, Material material, int slot, String[] description, String action) { + this.setSlotData(inv, name, new ItemStack(material, 1), slot, description, action); + } + + public void setSlotData(String name, Material material, int slot, String[] description, String action) + { + this.setSlotData(this.inventory, name, new ItemStack(material, 1), slot, description, action); + } + + public void setSlotData(String name, ItemStack item, int slot, String[] description, String action) + { + this.setSlotData(this.inventory, name, item, slot, description, action); + } + + public void setSlotData(Inventory inv, String name, ItemStack item, int slot, String[] description, String action) + { + this.actions.put(slot, action); + ItemMeta meta = item.getItemMeta(); + + meta.setDisplayName(name); + + if (description != null) + meta.setLore(Arrays.asList(description)); + + item.setItemMeta(meta); + inv.setItem(slot, item); + } + + public void setSlotData(Inventory inv, ItemStack item, int slot, String action) + { + this.actions.put(slot, action); + inv.setItem(slot, item); + } + + public void setSlotData(ItemStack item, int slot, String action) + { + setSlotData(this.inventory, item, slot, action); + } + + public String getAction(int slot) + { + if (!this.actions.containsKey(slot)) + return null; + + return this.actions.get(slot); + } + + public int getSlot(String action) + { + for (int slot : this.actions.keySet()) + if (this.actions.get(slot).equals(action)) + return slot; + + return 0; + } + + public Inventory getInventory() + { + return this.inventory; + } +} diff --git a/src/main/java/fr/moribus/imageonmap/gui/core/GuiListener.java b/src/main/java/fr/moribus/imageonmap/gui/core/GuiListener.java new file mode 100644 index 0000000..7431fdf --- /dev/null +++ b/src/main/java/fr/moribus/imageonmap/gui/core/GuiListener.java @@ -0,0 +1,67 @@ +/* + * Copyright (C) 2013 Moribus + * Copyright (C) 2015 ProkopyL + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package fr.moribus.imageonmap.gui.core; + +import fr.moribus.imageonmap.*; +import org.bukkit.entity.*; +import org.bukkit.event.*; +import org.bukkit.event.inventory.*; +import org.bukkit.inventory.*; + + +/** + * @author IamBlueSlime + * + * Changes by Amaury Carrade to use statics (beh, code style, these things). + */ +public class GuiListener implements Listener { + + public static void init(ImageOnMap plugin) + { + plugin.getServer().getPluginManager().registerEvents(new GuiListener(), plugin); + } + + @EventHandler + public void onInventoryClick(InventoryClickEvent event) + { + if (event.getWhoClicked() instanceof Player) { + Player player = (Player) event.getWhoClicked(); + AbstractGui gui = GuiManager.getPlayerGui(player); + + if (gui != null) { + if (event.getInventory() instanceof PlayerInventory) + return; + + String action = gui.getAction(event.getSlot()); + + if (action != null) + gui.onClick(player, event.getCurrentItem(), action, event.getClick()); + + event.setCancelled(true); + } + } + } + + @EventHandler + public void onInventoryClose(InventoryCloseEvent event) + { + if (GuiManager.getPlayerGui(event.getPlayer()) != null) + GuiManager.removeClosedGui((Player) event.getPlayer()); + } +} diff --git a/src/main/java/fr/moribus/imageonmap/gui/core/GuiManager.java b/src/main/java/fr/moribus/imageonmap/gui/core/GuiManager.java new file mode 100644 index 0000000..beaaebc --- /dev/null +++ b/src/main/java/fr/moribus/imageonmap/gui/core/GuiManager.java @@ -0,0 +1,79 @@ +/* + * Copyright (C) 2013 Moribus + * Copyright (C) 2015 ProkopyL + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package fr.moribus.imageonmap.gui.core; + +import fr.moribus.imageonmap.*; +import org.bukkit.entity.*; + +import java.util.*; +import java.util.concurrent.*; + + +/** + * @author IamBlueSlime (thanks c:) + * + * Changes by Amaury Carrade to use statics (beh, code style, these things). + */ +public class GuiManager { + protected static ConcurrentHashMap currentGUIs; + + public static void init(ImageOnMap plugin) + { + currentGUIs = new ConcurrentHashMap<>(); + + GuiListener.init(plugin); + } + + public static void openGui(Player player, AbstractGui gui) + { + if (currentGUIs.containsKey(player.getUniqueId())) + closeGui(player); + + currentGUIs.put(player.getUniqueId(), gui); + gui.display(player); + } + + public static void closeGui(Player player) + { + player.closeInventory(); + removeClosedGui(player); + } + + public static void removeClosedGui(Player player) + { + if (currentGUIs.containsKey(player.getUniqueId())) + { + getPlayerGui(player).onClose(player); + currentGUIs.remove(player.getUniqueId()); + } + } + + public static AbstractGui getPlayerGui(HumanEntity player) + { + if (currentGUIs.containsKey(player.getUniqueId())) + return currentGUIs.get(player.getUniqueId()); + + return null; + } + + public static ConcurrentHashMap getPlayersGui() + { + return currentGUIs; + } +}