From 9811e1fd6c4cdc1a418f4335a4e35820869bf2d2 Mon Sep 17 00:00:00 2001 From: Olof Larsson Date: Fri, 13 Mar 2015 13:02:58 +0100 Subject: [PATCH] ChestGUI --- .../massivecraft/massivecore/MassiveCore.java | 2 + .../massivecore/chestgui/ChestAction.java | 8 ++ .../chestgui/ChestActionCommand.java | 67 +++++++++++++++ .../massivecore/chestgui/ChestGui.java | 80 ++++++++++++++++++ .../massivecore/chestgui/EngineChestGui.java | 81 +++++++++++++++++++ 5 files changed, 238 insertions(+) create mode 100644 src/com/massivecraft/massivecore/chestgui/ChestAction.java create mode 100644 src/com/massivecraft/massivecore/chestgui/ChestActionCommand.java create mode 100644 src/com/massivecraft/massivecore/chestgui/ChestGui.java create mode 100644 src/com/massivecraft/massivecore/chestgui/EngineChestGui.java diff --git a/src/com/massivecraft/massivecore/MassiveCore.java b/src/com/massivecraft/massivecore/MassiveCore.java index bb9c6cbc..b7b3b505 100644 --- a/src/com/massivecraft/massivecore/MassiveCore.java +++ b/src/com/massivecraft/massivecore/MassiveCore.java @@ -23,6 +23,7 @@ import com.massivecraft.massivecore.adapter.MassiveTreeSetAdapter; import com.massivecraft.massivecore.adapter.ModdedEnumTypeAdapter; import com.massivecraft.massivecore.adapter.PlayerInventoryAdapter; import com.massivecraft.massivecore.adapter.UUIDAdapter; +import com.massivecraft.massivecore.chestgui.EngineChestGui; import com.massivecraft.massivecore.cmd.massivecore.CmdMassiveCore; import com.massivecraft.massivecore.cmd.massivecore.CmdMassiveCoreBuffer; import com.massivecraft.massivecore.cmd.massivecore.CmdMassiveCoreCmdurl; @@ -177,6 +178,7 @@ public class MassiveCore extends MassivePlugin MassiveCoreEngineWorldNameSet.get().activate(); MassiveCoreEngineCommandRegistration.get().activate(); PlayerUtil.get().activate(); + EngineChestGui.get().activate(); // Collections MultiverseColl.get().init(); diff --git a/src/com/massivecraft/massivecore/chestgui/ChestAction.java b/src/com/massivecraft/massivecore/chestgui/ChestAction.java new file mode 100644 index 00000000..e37db79a --- /dev/null +++ b/src/com/massivecraft/massivecore/chestgui/ChestAction.java @@ -0,0 +1,8 @@ +package com.massivecraft.massivecore.chestgui; + +import org.bukkit.event.inventory.InventoryClickEvent; + +public interface ChestAction +{ + public boolean onClick(InventoryClickEvent event); +} diff --git a/src/com/massivecraft/massivecore/chestgui/ChestActionCommand.java b/src/com/massivecraft/massivecore/chestgui/ChestActionCommand.java new file mode 100644 index 00000000..0b051531 --- /dev/null +++ b/src/com/massivecraft/massivecore/chestgui/ChestActionCommand.java @@ -0,0 +1,67 @@ +package com.massivecraft.massivecore.chestgui; + +import java.security.InvalidParameterException; + +import org.bukkit.entity.HumanEntity; +import org.bukkit.entity.Player; +import org.bukkit.event.inventory.InventoryClickEvent; + +import com.massivecraft.massivecore.mixin.Mixin; + +public class ChestActionCommand implements ChestAction +{ + // -------------------------------------------- // + // FIELD + // -------------------------------------------- // + + protected String command = null; + + public void setCommand(String command) + { + if ( ! command.startsWith("/")) throw new InvalidParameterException("Commands start with \"/\". Do include the leading slash."); + this.command = command; + } + + public String getCommand() + { + return this.command; + } + + public String getCommandLine() + { + if (this.command == null) return null; + return this.command.substring(1); + } + + // -------------------------------------------- // + // CONSTRUCT + // -------------------------------------------- // + + public ChestActionCommand() + { + + } + + public ChestActionCommand(String command) + { + this.setCommand(command); + } + + // -------------------------------------------- // + // OVERRIDE + // -------------------------------------------- // + + @Override + public boolean onClick(InventoryClickEvent event) + { + HumanEntity human = event.getWhoClicked(); + if ( ! (human instanceof Player)) return false; + Player player = (Player)human; + + String commandLine = this.getCommandLine(); + if (commandLine == null) return false; + + return Mixin.dispatchCommand(player, commandLine); + } + +} diff --git a/src/com/massivecraft/massivecore/chestgui/ChestGui.java b/src/com/massivecraft/massivecore/chestgui/ChestGui.java new file mode 100644 index 00000000..6e71016a --- /dev/null +++ b/src/com/massivecraft/massivecore/chestgui/ChestGui.java @@ -0,0 +1,80 @@ +package com.massivecraft.massivecore.chestgui; + +import java.util.Map; + +import org.bukkit.Sound; +import org.bukkit.entity.HumanEntity; +import org.bukkit.entity.Player; +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.ItemStack; + +import com.massivecraft.massivecore.collections.MassiveMap; + +public class ChestGui +{ + // -------------------------------------------- // + // STATIC REGISTRY + // -------------------------------------------- // + + protected static Map inventoryToGui = new MassiveMap(); + public static Map getInventoryToGui() { return inventoryToGui; } + public static ChestGui remove(Inventory inventory) { return inventoryToGui.remove(inventory); } + public static ChestGui set(Inventory inventory, ChestGui gui) { return inventoryToGui.put(inventory, gui); } + public static ChestGui get(Inventory inventory) { return inventoryToGui.get(inventory); } + public static ChestGui getCreative(Inventory inventory) + { + ChestGui gui = get(inventory); + if (gui != null) return gui; + gui = new ChestGui(); + set(inventory, gui); + return gui; + } + + // -------------------------------------------- // + // FIELDS: ACTIONS + // -------------------------------------------- // + + protected Map itemToAction = new MassiveMap(); + public Map getItemToAction() { return this.itemToAction; } + public ChestAction removeAction(ItemStack item) { return this.itemToAction.remove(item); } + public ChestAction setAction(ItemStack item, ChestAction action) { return this.itemToAction.put(item, action); } + public ChestAction setAction(ItemStack item, String command) { return this.setAction(item, new ChestActionCommand(command)); } + public ChestAction getAction(ItemStack item) { return this.itemToAction.get(item); } + + // -------------------------------------------- // + // FIELDS: SOUND + // -------------------------------------------- // + + protected Sound sound = Sound.CLICK; + public Sound getSound() { return this.sound; } + + protected float volume = 1.0f; + public float getVolume() { return this.volume; } + public ChestGui setVolume(float volume) { this.volume = volume; return this; } + + protected float pitch = 1.0f; + public float getPitch() { return this.pitch; } + public ChestGui setPitch(float pitch) { this.pitch = pitch; return this; } + + public void playSound(Player player) + { + player.playSound(player.getEyeLocation(), this.getSound(), this.getVolume(), this.getPitch()); + } + + public void playSound(HumanEntity human) + { + if ( ! (human instanceof Player)) return; + Player player = (Player)human; + this.playSound(player); + } + + // -------------------------------------------- // + // CONSTRUCT + // -------------------------------------------- // + + public ChestGui() + { + + } + +} diff --git a/src/com/massivecraft/massivecore/chestgui/EngineChestGui.java b/src/com/massivecraft/massivecore/chestgui/EngineChestGui.java new file mode 100644 index 00000000..0c468e57 --- /dev/null +++ b/src/com/massivecraft/massivecore/chestgui/EngineChestGui.java @@ -0,0 +1,81 @@ +package com.massivecraft.massivecore.chestgui; + +import org.bukkit.event.Event.Result; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.inventory.InventoryClickEvent; +import org.bukkit.event.inventory.InventoryCloseEvent; +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.ItemStack; +import org.bukkit.plugin.Plugin; + +import com.massivecraft.massivecore.EngineAbstract; +import com.massivecraft.massivecore.MassiveCore; +import com.massivecraft.massivecore.util.InventoryUtil; + +public class EngineChestGui extends EngineAbstract +{ + // -------------------------------------------- // + // INSTANCE & CONSTRUCT + // -------------------------------------------- // + + private static EngineChestGui i = new EngineChestGui(); + public static EngineChestGui get() { return i; } + + // -------------------------------------------- // + // OVERRIDE + // -------------------------------------------- // + + @Override + public Plugin getPlugin() + { + return MassiveCore.get(); + } + + // -------------------------------------------- // + // LISTENER + // -------------------------------------------- // + + @EventHandler(priority = EventPriority.LOW) + public void onClick(InventoryClickEvent event) + { + // If this inventory ... + Inventory inventory = event.getInventory(); + if (inventory == null) return; + + // ... is a gui ... + ChestGui gui = ChestGui.get(inventory); + if (gui == null) return; + + // ... then cancel the event ... + event.setCancelled(true); + event.setResult(Result.DENY); + + // ... and if there is an item ... + ItemStack item = event.getCurrentItem(); + if (InventoryUtil.isNothing(item)) return; + + // ... and this item has an action ... + ChestAction action = gui.getAction(item); + if (action == null) return; + + // ... then use that action ... + action.onClick(event); + + // ... play the sound ... + gui.playSound(event.getWhoClicked()); + + // ... and close the GUI. + event.getView().close(); + + } + + @EventHandler(priority = EventPriority.LOW) + public void onClose(InventoryCloseEvent event) + { + Inventory inventory = event.getInventory(); + if (inventory == null) return; + ChestGui.remove(inventory); + } + +}