This commit is contained in:
Olof Larsson 2015-03-13 13:02:58 +01:00
parent 86ddbdcf15
commit 9811e1fd6c
5 changed files with 238 additions and 0 deletions

View File

@ -23,6 +23,7 @@ import com.massivecraft.massivecore.adapter.MassiveTreeSetAdapter;
import com.massivecraft.massivecore.adapter.ModdedEnumTypeAdapter; import com.massivecraft.massivecore.adapter.ModdedEnumTypeAdapter;
import com.massivecraft.massivecore.adapter.PlayerInventoryAdapter; import com.massivecraft.massivecore.adapter.PlayerInventoryAdapter;
import com.massivecraft.massivecore.adapter.UUIDAdapter; 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.CmdMassiveCore;
import com.massivecraft.massivecore.cmd.massivecore.CmdMassiveCoreBuffer; import com.massivecraft.massivecore.cmd.massivecore.CmdMassiveCoreBuffer;
import com.massivecraft.massivecore.cmd.massivecore.CmdMassiveCoreCmdurl; import com.massivecraft.massivecore.cmd.massivecore.CmdMassiveCoreCmdurl;
@ -177,6 +178,7 @@ public class MassiveCore extends MassivePlugin
MassiveCoreEngineWorldNameSet.get().activate(); MassiveCoreEngineWorldNameSet.get().activate();
MassiveCoreEngineCommandRegistration.get().activate(); MassiveCoreEngineCommandRegistration.get().activate();
PlayerUtil.get().activate(); PlayerUtil.get().activate();
EngineChestGui.get().activate();
// Collections // Collections
MultiverseColl.get().init(); MultiverseColl.get().init();

View File

@ -0,0 +1,8 @@
package com.massivecraft.massivecore.chestgui;
import org.bukkit.event.inventory.InventoryClickEvent;
public interface ChestAction
{
public boolean onClick(InventoryClickEvent event);
}

View File

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

View File

@ -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<Inventory, ChestGui> inventoryToGui = new MassiveMap<Inventory, ChestGui>();
public static Map<Inventory, ChestGui> 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<ItemStack, ChestAction> itemToAction = new MassiveMap<ItemStack, ChestAction>();
public Map<ItemStack, ChestAction> 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()
{
}
}

View File

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