mirror of
https://github.com/zDevelopers/ImageOnMap.git
synced 2024-09-21 03:12:28 +02:00
Added a GUI API
This commit is contained in:
parent
ec6b4ec997
commit
1534b574f2
@ -19,6 +19,7 @@
|
|||||||
package fr.moribus.imageonmap;
|
package fr.moribus.imageonmap;
|
||||||
|
|
||||||
import fr.moribus.imageonmap.commands.Commands;
|
import fr.moribus.imageonmap.commands.Commands;
|
||||||
|
import fr.moribus.imageonmap.gui.core.*;
|
||||||
import fr.moribus.imageonmap.image.ImageIOExecutor;
|
import fr.moribus.imageonmap.image.ImageIOExecutor;
|
||||||
import fr.moribus.imageonmap.image.ImageRendererExecutor;
|
import fr.moribus.imageonmap.image.ImageRendererExecutor;
|
||||||
import fr.moribus.imageonmap.image.MapInitEvent;
|
import fr.moribus.imageonmap.image.MapInitEvent;
|
||||||
@ -84,6 +85,7 @@ public final class ImageOnMap extends JavaPlugin
|
|||||||
Commands.init(this);
|
Commands.init(this);
|
||||||
MapInitEvent.init(this);
|
MapInitEvent.init(this);
|
||||||
MapItemManager.init();
|
MapItemManager.init();
|
||||||
|
GuiManager.init(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
110
src/main/java/fr/moribus/imageonmap/gui/core/AbstractGui.java
Normal file
110
src/main/java/fr/moribus/imageonmap/gui/core/AbstractGui.java
Normal file
@ -0,0 +1,110 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2013 Moribus
|
||||||
|
* Copyright (C) 2015 ProkopyL <prokopylmc@gmail.com>
|
||||||
|
*
|
||||||
|
* 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 <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
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<Integer, String> 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;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,67 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2013 Moribus
|
||||||
|
* Copyright (C) 2015 ProkopyL <prokopylmc@gmail.com>
|
||||||
|
*
|
||||||
|
* 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 <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
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());
|
||||||
|
}
|
||||||
|
}
|
79
src/main/java/fr/moribus/imageonmap/gui/core/GuiManager.java
Normal file
79
src/main/java/fr/moribus/imageonmap/gui/core/GuiManager.java
Normal file
@ -0,0 +1,79 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2013 Moribus
|
||||||
|
* Copyright (C) 2015 ProkopyL <prokopylmc@gmail.com>
|
||||||
|
*
|
||||||
|
* 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 <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
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<UUID, AbstractGui> 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<UUID, AbstractGui> getPlayersGui()
|
||||||
|
{
|
||||||
|
return currentGUIs;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user