Added a GUI API

This commit is contained in:
Amaury Carrade 2015-07-09 20:49:13 +02:00
parent ec6b4ec997
commit 1534b574f2
4 changed files with 258 additions and 0 deletions

View File

@ -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

View 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;
}
}

View File

@ -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());
}
}

View 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;
}
}