Improvements to the ChestGUI
This commit is contained in:
parent
e75d52c584
commit
3f262e13bc
@ -1,20 +1,23 @@
|
|||||||
package com.massivecraft.massivecore.chestgui;
|
package com.massivecraft.massivecore.chestgui;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import org.bukkit.inventory.Inventory;
|
import org.bukkit.inventory.Inventory;
|
||||||
import org.bukkit.inventory.ItemStack;
|
import org.bukkit.inventory.ItemStack;
|
||||||
|
|
||||||
|
import com.massivecraft.massivecore.MassiveCoreMConf;
|
||||||
import com.massivecraft.massivecore.SoundEffect;
|
import com.massivecraft.massivecore.SoundEffect;
|
||||||
|
import com.massivecraft.massivecore.collections.MassiveList;
|
||||||
import com.massivecraft.massivecore.collections.MassiveMap;
|
import com.massivecraft.massivecore.collections.MassiveMap;
|
||||||
|
|
||||||
public class ChestGui
|
public class ChestGui
|
||||||
{
|
{
|
||||||
// -------------------------------------------- //
|
// -------------------------------------------- //
|
||||||
// STATIC REGISTRY
|
// REGISTRY
|
||||||
// -------------------------------------------- //
|
// -------------------------------------------- //
|
||||||
|
|
||||||
protected static Map<Inventory, ChestGui> inventoryToGui = new MassiveMap<Inventory, ChestGui>();
|
private static final Map<Inventory, ChestGui> inventoryToGui = new MassiveMap<Inventory, ChestGui>();
|
||||||
public static Map<Inventory, ChestGui> getInventoryToGui() { return inventoryToGui; }
|
public static Map<Inventory, ChestGui> getInventoryToGui() { return inventoryToGui; }
|
||||||
public static ChestGui remove(Inventory inventory) { return inventoryToGui.remove(inventory); }
|
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 set(Inventory inventory, ChestGui gui) { return inventoryToGui.put(inventory, gui); }
|
||||||
@ -29,10 +32,10 @@ public class ChestGui
|
|||||||
}
|
}
|
||||||
|
|
||||||
// -------------------------------------------- //
|
// -------------------------------------------- //
|
||||||
// FIELDS: ACTIONS
|
// ACTIONS
|
||||||
// -------------------------------------------- //
|
// -------------------------------------------- //
|
||||||
|
|
||||||
protected Map<Integer, ChestAction> indexToAction = new MassiveMap<Integer, ChestAction>();
|
private Map<Integer, ChestAction> indexToAction = new MassiveMap<Integer, ChestAction>();
|
||||||
public Map<Integer, ChestAction> getIndexToAction() { return this.indexToAction; }
|
public Map<Integer, ChestAction> getIndexToAction() { return this.indexToAction; }
|
||||||
public ChestAction removeAction(ItemStack item) { return this.indexToAction.remove(item); }
|
public ChestAction removeAction(ItemStack item) { return this.indexToAction.remove(item); }
|
||||||
public ChestAction setAction(int index, ChestAction action) { return this.indexToAction.put(index, action); }
|
public ChestAction setAction(int index, ChestAction action) { return this.indexToAction.put(index, action); }
|
||||||
@ -40,10 +43,35 @@ public class ChestGui
|
|||||||
public ChestAction getAction(int index) { return this.indexToAction.get(index); }
|
public ChestAction getAction(int index) { return this.indexToAction.get(index); }
|
||||||
|
|
||||||
// -------------------------------------------- //
|
// -------------------------------------------- //
|
||||||
// FIELDS: SOUND
|
// LAST ACTION
|
||||||
// -------------------------------------------- //
|
// -------------------------------------------- //
|
||||||
|
|
||||||
protected SoundEffect soundEffect = SoundEffect.valueOf("CLICK", 1.0f, 1.0f);
|
private ChestAction lastAction = null;
|
||||||
|
public ChestAction getLastAction() { return this.lastAction; }
|
||||||
|
public void setLastAction(ChestAction lastAction) { this.lastAction = lastAction; }
|
||||||
|
|
||||||
|
// -------------------------------------------- //
|
||||||
|
// META DATA
|
||||||
|
// -------------------------------------------- //
|
||||||
|
|
||||||
|
private final Map<String, Object> meta = new MassiveMap<>();
|
||||||
|
public Map<String, Object> getMeta() { return this.meta; }
|
||||||
|
|
||||||
|
// -------------------------------------------- //
|
||||||
|
// RUNNABLES
|
||||||
|
// -------------------------------------------- //
|
||||||
|
|
||||||
|
private final List<Runnable> runnablesOpen = new MassiveList<>();
|
||||||
|
public List<Runnable> getRunnablesOpen() { return this.runnablesOpen; }
|
||||||
|
|
||||||
|
private final List<Runnable> runnablesClose = new MassiveList<>();
|
||||||
|
public List<Runnable> getRunnablesClose() { return this.runnablesClose; }
|
||||||
|
|
||||||
|
// -------------------------------------------- //
|
||||||
|
// SOUND
|
||||||
|
// -------------------------------------------- //
|
||||||
|
|
||||||
|
private SoundEffect soundEffect = MassiveCoreMConf.get().clickSound;
|
||||||
public SoundEffect getSoundEffect() { return this.soundEffect; }
|
public SoundEffect getSoundEffect() { return this.soundEffect; }
|
||||||
public ChestGui setSoundEffect(SoundEffect soundEffect) { this.soundEffect = soundEffect; return this; }
|
public ChestGui setSoundEffect(SoundEffect soundEffect) { this.soundEffect = soundEffect; return this; }
|
||||||
|
|
||||||
|
@ -1,10 +1,12 @@
|
|||||||
package com.massivecraft.massivecore.engine;
|
package com.massivecraft.massivecore.engine;
|
||||||
|
|
||||||
import org.bukkit.event.Event.Result;
|
import org.bukkit.event.Event.Result;
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.event.EventHandler;
|
import org.bukkit.event.EventHandler;
|
||||||
import org.bukkit.event.EventPriority;
|
import org.bukkit.event.EventPriority;
|
||||||
import org.bukkit.event.inventory.InventoryClickEvent;
|
import org.bukkit.event.inventory.InventoryClickEvent;
|
||||||
import org.bukkit.event.inventory.InventoryCloseEvent;
|
import org.bukkit.event.inventory.InventoryCloseEvent;
|
||||||
|
import org.bukkit.event.inventory.InventoryOpenEvent;
|
||||||
import org.bukkit.inventory.Inventory;
|
import org.bukkit.inventory.Inventory;
|
||||||
import com.massivecraft.massivecore.Engine;
|
import com.massivecraft.massivecore.Engine;
|
||||||
import com.massivecraft.massivecore.chestgui.ChestAction;
|
import com.massivecraft.massivecore.chestgui.ChestAction;
|
||||||
@ -54,6 +56,9 @@ public class EngineMassiveCoreChestGui extends Engine
|
|||||||
ChestAction action = gui.getAction(index);
|
ChestAction action = gui.getAction(index);
|
||||||
if (action == null) return;
|
if (action == null) return;
|
||||||
|
|
||||||
|
// ... set last action ...
|
||||||
|
gui.setLastAction(action);
|
||||||
|
|
||||||
// ... then play the sound ...
|
// ... then play the sound ...
|
||||||
gui.getSoundEffect().run(event.getWhoClicked());
|
gui.getSoundEffect().run(event.getWhoClicked());
|
||||||
|
|
||||||
@ -64,12 +69,63 @@ public class EngineMassiveCoreChestGui extends Engine
|
|||||||
action.onClick(event);
|
action.onClick(event);
|
||||||
}
|
}
|
||||||
|
|
||||||
@EventHandler(priority = EventPriority.LOW)
|
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
|
||||||
|
public void onOpen(InventoryOpenEvent event)
|
||||||
|
{
|
||||||
|
// Get
|
||||||
|
final Inventory inventory = event.getInventory();
|
||||||
|
if (inventory == null) return;
|
||||||
|
final ChestGui gui = ChestGui.get(inventory);
|
||||||
|
if (gui == null) return;
|
||||||
|
|
||||||
|
// Runnables
|
||||||
|
Bukkit.getScheduler().runTask(getPlugin(), new Runnable()
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public void run()
|
||||||
|
{
|
||||||
|
for (Runnable runnable : gui.getRunnablesOpen())
|
||||||
|
{
|
||||||
|
runnable.run();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// NOTE:
|
||||||
|
|
||||||
|
@EventHandler(priority = EventPriority.MONITOR)
|
||||||
public void onClose(InventoryCloseEvent event)
|
public void onClose(InventoryCloseEvent event)
|
||||||
{
|
{
|
||||||
Inventory inventory = event.getInventory();
|
// Get
|
||||||
|
final Inventory inventory = event.getInventory();
|
||||||
if (inventory == null) return;
|
if (inventory == null) return;
|
||||||
|
final ChestGui gui = ChestGui.get(inventory);
|
||||||
|
if (gui == null) return;
|
||||||
|
|
||||||
|
// Runnables
|
||||||
|
Bukkit.getScheduler().runTask(getPlugin(), new Runnable()
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public void run()
|
||||||
|
{
|
||||||
|
for (Runnable runnable : gui.getRunnablesClose())
|
||||||
|
{
|
||||||
|
runnable.run();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// We save the inventory in the map for a little while.
|
||||||
|
// A plugin may want to do something upon the chest gui closing.
|
||||||
|
Bukkit.getScheduler().runTaskLater(this.getPlugin(), new Runnable()
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public void run()
|
||||||
|
{
|
||||||
ChestGui.remove(inventory);
|
ChestGui.remove(inventory);
|
||||||
}
|
}
|
||||||
|
}, 20);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user