2019-02-19 14:23:20 +01:00
|
|
|
package com.songoda.epicenchants.utils.objects;
|
2019-01-24 12:00:57 +01:00
|
|
|
|
|
|
|
import org.bukkit.Bukkit;
|
|
|
|
import org.bukkit.entity.Player;
|
|
|
|
import org.bukkit.event.EventHandler;
|
|
|
|
import org.bukkit.event.Listener;
|
2021-07-10 23:41:20 +02:00
|
|
|
import org.bukkit.event.inventory.ClickType;
|
|
|
|
import org.bukkit.event.inventory.InventoryAction;
|
|
|
|
import org.bukkit.event.inventory.InventoryClickEvent;
|
|
|
|
import org.bukkit.event.inventory.InventoryCloseEvent;
|
|
|
|
import org.bukkit.event.inventory.InventoryType;
|
2019-01-24 12:00:57 +01:00
|
|
|
import org.bukkit.event.server.PluginDisableEvent;
|
|
|
|
import org.bukkit.inventory.Inventory;
|
|
|
|
import org.bukkit.inventory.InventoryHolder;
|
|
|
|
import org.bukkit.inventory.ItemStack;
|
|
|
|
import org.bukkit.plugin.Plugin;
|
|
|
|
import org.bukkit.scheduler.BukkitTask;
|
|
|
|
|
2021-07-10 23:41:20 +02:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Set;
|
2019-01-24 12:00:57 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A fast API to easily create advanced GUI.
|
|
|
|
* The project is on <a href="https://github.com/MrMicky-FR/FastInv">GitHub</a>
|
|
|
|
*
|
|
|
|
* @author MrMicky
|
|
|
|
* @version 2.0.3 - Now supports async operations
|
|
|
|
*/
|
|
|
|
public class FastInv implements InventoryHolder {
|
|
|
|
|
|
|
|
private static Plugin plugin = null;
|
|
|
|
private boolean cancelTasksOnClose = true;
|
2019-01-24 20:41:01 +01:00
|
|
|
private final Set<FastInvClickListener> clickListeners = new HashSet<>();
|
|
|
|
private final Set<FastInvCloseListener> closeListeners = new HashSet<>();
|
2019-01-24 12:00:57 +01:00
|
|
|
private Inventory inventory;
|
2019-01-24 20:41:01 +01:00
|
|
|
private final Map<Integer, FastInvClickListener> itemListeners = new HashMap<>();
|
|
|
|
private final Set<BukkitTask> tasks = new HashSet<>();
|
2019-01-24 12:00:57 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new FastInv with a custom size and title.
|
|
|
|
*
|
|
|
|
* @param size The size of the menus.
|
|
|
|
* @param title The title (name) of the menus.
|
|
|
|
*/
|
|
|
|
public FastInv(int size, String title) {
|
|
|
|
this(size, InventoryType.CHEST, title);
|
|
|
|
}
|
|
|
|
|
|
|
|
private FastInv(int size, InventoryType type, String title) {
|
|
|
|
if (plugin == null) {
|
|
|
|
throw new IllegalStateException("FastInv is not initialised");
|
|
|
|
}
|
|
|
|
|
|
|
|
runSync(() -> {
|
|
|
|
if (type == InventoryType.CHEST && size > 0) {
|
2023-06-25 13:50:27 +02:00
|
|
|
this.inventory = Bukkit.createInventory(this, size, title);
|
2019-01-24 12:00:57 +01:00
|
|
|
} else {
|
2023-06-25 13:50:27 +02:00
|
|
|
this.inventory = Bukkit.createInventory(this, type, title);
|
2019-01-24 12:00:57 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register your FastInv instance.
|
|
|
|
*
|
|
|
|
* @param plugin The plugin that uses FastInv.
|
|
|
|
*/
|
|
|
|
public static void init(Plugin plugin) {
|
|
|
|
if (FastInv.plugin == null) {
|
|
|
|
FastInv.plugin = plugin;
|
|
|
|
Bukkit.getPluginManager().registerEvents(getListener(), plugin);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-19 14:23:20 +01:00
|
|
|
public static class FastInvClickEvent extends FastInvEvent {
|
|
|
|
private final InventoryClickEvent event;
|
|
|
|
private final InventoryAction action;
|
|
|
|
private final ClickType clickType;
|
|
|
|
private final ItemStack item;
|
|
|
|
private final int slot;
|
2019-01-24 12:00:57 +01:00
|
|
|
|
2019-02-19 14:23:20 +01:00
|
|
|
private FastInvClickEvent(Player player, FastInv inventory, InventoryClickEvent event, int slot, ItemStack item,
|
|
|
|
boolean cancelled, InventoryAction action, ClickType clickType) {
|
|
|
|
super(player, inventory, cancelled);
|
|
|
|
this.event = event;
|
|
|
|
this.slot = slot;
|
|
|
|
this.item = item;
|
|
|
|
this.action = action;
|
|
|
|
this.clickType = clickType;
|
|
|
|
}
|
2019-01-24 12:00:57 +01:00
|
|
|
|
2019-02-19 14:23:20 +01:00
|
|
|
/**
|
|
|
|
* @return The action of the event
|
|
|
|
*/
|
|
|
|
public InventoryAction getAction() {
|
|
|
|
return this.action;
|
|
|
|
}
|
2019-01-24 12:00:57 +01:00
|
|
|
|
2019-02-19 14:23:20 +01:00
|
|
|
/**
|
|
|
|
* @return The click type
|
|
|
|
*/
|
|
|
|
public ClickType getClickType() {
|
|
|
|
return this.clickType;
|
|
|
|
}
|
2019-01-24 12:00:57 +01:00
|
|
|
|
2019-02-19 14:23:20 +01:00
|
|
|
/**
|
|
|
|
* Get the clicked {@link ItemStack}
|
|
|
|
*
|
|
|
|
* @return The clicked item
|
|
|
|
*/
|
|
|
|
public ItemStack getItem() {
|
|
|
|
return this.item;
|
|
|
|
}
|
2019-01-24 12:00:57 +01:00
|
|
|
|
2019-02-19 14:23:20 +01:00
|
|
|
/**
|
|
|
|
* Get the number of the clicked slot
|
|
|
|
*
|
|
|
|
* @return The slot number
|
|
|
|
*/
|
|
|
|
public int getSlot() {
|
|
|
|
return this.slot;
|
|
|
|
}
|
2019-01-24 12:00:57 +01:00
|
|
|
|
2019-02-19 14:23:20 +01:00
|
|
|
public InventoryClickEvent getEvent() {
|
2023-06-25 13:50:27 +02:00
|
|
|
return this.event;
|
2019-02-19 14:23:20 +01:00
|
|
|
}
|
2019-01-24 12:00:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add an {@link ItemStack} to the menus.
|
|
|
|
*
|
|
|
|
* @param item The item to add
|
2021-07-10 23:41:20 +02:00
|
|
|
*
|
2019-01-24 12:00:57 +01:00
|
|
|
* @return This FastInv instance, for chaining.
|
|
|
|
*/
|
|
|
|
public FastInv addItem(ItemStack item) {
|
|
|
|
return addItem(item, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add an {@link ItemStack} to the menus with a {@link FastInvClickListener} to handle clicks.
|
|
|
|
*
|
|
|
|
* @param item The item to add.
|
|
|
|
* @param listener The {@link FastInvClickListener} for the item.
|
2021-07-10 23:41:20 +02:00
|
|
|
*
|
2019-01-24 12:00:57 +01:00
|
|
|
* @return This FastInv instance, for chaining.
|
|
|
|
*/
|
|
|
|
public FastInv addItem(ItemStack item, FastInvClickListener listener) {
|
|
|
|
runSync(() -> {
|
2023-06-25 13:50:27 +02:00
|
|
|
int slot = this.inventory.firstEmpty();
|
2019-01-24 12:00:57 +01:00
|
|
|
if (slot >= 0) {
|
|
|
|
addItem(slot, item, listener);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add an {@link ItemStack} to the menus on a specific slot.
|
|
|
|
*
|
|
|
|
* @param slot The slot of the item.
|
|
|
|
* @param item The item to add.
|
2021-07-10 23:41:20 +02:00
|
|
|
*
|
2019-01-24 12:00:57 +01:00
|
|
|
* @return This FastInv instance, for chaining.
|
|
|
|
*/
|
|
|
|
public FastInv addItem(int slot, ItemStack item) {
|
|
|
|
return addItem(slot, item, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add an {@link ItemStack} to the menus on specific slot with a {@link FastInvClickListener} to handle clicks.
|
|
|
|
*
|
|
|
|
* @param slot The slot of the item.
|
|
|
|
* @param item The item to add.
|
|
|
|
* @param listener The FastInvClickListener for the item.
|
2021-07-10 23:41:20 +02:00
|
|
|
*
|
2019-01-24 12:00:57 +01:00
|
|
|
* @return This FastInv instance, for chaining.
|
|
|
|
*/
|
|
|
|
public FastInv addItem(int slot, ItemStack item, FastInvClickListener listener) {
|
|
|
|
runSync(() -> {
|
2023-06-25 13:50:27 +02:00
|
|
|
this.inventory.setItem(slot, item);
|
2019-01-24 12:00:57 +01:00
|
|
|
|
|
|
|
if (listener != null) {
|
2023-06-25 13:50:27 +02:00
|
|
|
this.itemListeners.put(slot, listener);
|
2019-01-24 12:00:57 +01:00
|
|
|
} else {
|
2023-06-25 13:50:27 +02:00
|
|
|
this.itemListeners.remove(slot);
|
2019-01-24 12:00:57 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add an {@link ItemStack} to the menus on a range of slots.
|
|
|
|
*
|
|
|
|
* @param slotFrom Starting slot to put the item in.
|
|
|
|
* @param slotTo Ending slot to put the item in.
|
|
|
|
* @param item The item to add.
|
2021-07-10 23:41:20 +02:00
|
|
|
*
|
2019-01-24 12:00:57 +01:00
|
|
|
* @return This FastInv instance, for chaining.
|
|
|
|
*/
|
|
|
|
public FastInv addItem(int slotFrom, int slotTo, ItemStack item) {
|
|
|
|
return addItem(slotFrom, slotTo, item, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add an {@link ItemStack} to the menus on a range of slots with a {@link FastInvClickListener} to handle clicks.
|
|
|
|
*
|
|
|
|
* @param slotFrom Starting slot to put the item in.
|
|
|
|
* @param slotTo Ending slot to put the item in.
|
|
|
|
* @param item The item to add.
|
|
|
|
* @param listener The FastInvClickListener for the item.
|
2021-07-10 23:41:20 +02:00
|
|
|
*
|
2019-01-24 12:00:57 +01:00
|
|
|
* @return This FastInv instance, for chaining.
|
|
|
|
*/
|
|
|
|
public FastInv addItem(int slotFrom, int slotTo, ItemStack item, FastInvClickListener listener) {
|
|
|
|
for (int i = slotFrom; i <= slotTo; i++) {
|
|
|
|
addItem(i, item, listener);
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add an {@link ItemStack} to the menus on multiple slots.
|
|
|
|
*
|
|
|
|
* @param slots The slot of the item.
|
|
|
|
* @param item The item to add.
|
2021-07-10 23:41:20 +02:00
|
|
|
*
|
2019-01-24 12:00:57 +01:00
|
|
|
* @return This FastInv instance, for chaining.
|
|
|
|
*/
|
|
|
|
public FastInv addItem(int[] slots, ItemStack item) {
|
|
|
|
return addItem(slots, item, null);
|
|
|
|
}
|
|
|
|
|
2019-04-03 15:58:13 +02:00
|
|
|
/**
|
|
|
|
* Clear a spot in the inventory.
|
|
|
|
*
|
|
|
|
* @param slot The slot to clear.
|
2021-07-10 23:41:20 +02:00
|
|
|
*
|
2019-04-03 15:58:13 +02:00
|
|
|
* @return This FastInv instance, for chaining.
|
|
|
|
*/
|
|
|
|
public FastInv clear(int slot) {
|
|
|
|
return addItem(slot, null);
|
|
|
|
}
|
|
|
|
|
2019-01-24 12:00:57 +01:00
|
|
|
/**
|
|
|
|
* Add an {@link ItemStack} to the menus on the edges.
|
|
|
|
*
|
|
|
|
* @param item The item to add.
|
2021-07-10 23:41:20 +02:00
|
|
|
*
|
2019-01-24 12:00:57 +01:00
|
|
|
* @return This FastInv instance, for chaining.
|
|
|
|
*/
|
|
|
|
public FastInv edge(ItemStack item) {
|
2023-06-25 13:50:27 +02:00
|
|
|
int height = this.inventory.getSize() / 9;
|
2019-01-24 12:00:57 +01:00
|
|
|
|
|
|
|
addItem(0, 9, item);
|
2023-06-25 13:50:27 +02:00
|
|
|
addItem(this.inventory.getSize() - 9, this.inventory.getSize() - 1, item);
|
2019-01-24 12:00:57 +01:00
|
|
|
|
|
|
|
for (int i = 0; i < height; i++) {
|
|
|
|
addItem(i * 9, item);
|
|
|
|
addItem(i * 9 + 8, item);
|
|
|
|
}
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add an {@link ItemStack} to the menus on multiples slots with a {@link FastInvClickListener} to handle click.
|
|
|
|
*
|
|
|
|
* @param slots The slots to place the item.
|
|
|
|
* @param item The item to add.
|
|
|
|
* @param listener The FastInvClickListener for the item.
|
2021-07-10 23:41:20 +02:00
|
|
|
*
|
2019-01-24 12:00:57 +01:00
|
|
|
* @return This FastInv instance, for chaining.
|
|
|
|
*/
|
|
|
|
public FastInv addItem(int[] slots, ItemStack item, FastInvClickListener listener) {
|
|
|
|
for (int slot : slots) {
|
|
|
|
addItem(slot, item, listener);
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public FastInv fill(ItemStack itemStack) {
|
|
|
|
runSync(() -> {
|
2023-06-25 13:50:27 +02:00
|
|
|
for (int i = 0; i < this.inventory.getSize(); i++) {
|
|
|
|
if (this.inventory.getItem(i) == null) {
|
2019-01-24 12:00:57 +01:00
|
|
|
addItem(i, itemStack);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a {@link FastInvCloseListener} to listen on menus close.
|
|
|
|
*
|
|
|
|
* @param listener The {@link FastInvCloseListener} to add.
|
2021-07-10 23:41:20 +02:00
|
|
|
*
|
2019-01-24 12:00:57 +01:00
|
|
|
* @return This FastInv instance, for chaining.
|
|
|
|
*/
|
|
|
|
public FastInv onClose(FastInvCloseListener listener) {
|
2023-06-25 13:50:27 +02:00
|
|
|
this.closeListeners.add(listener);
|
2019-01-24 12:00:57 +01:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a {@link FastInvClickListener} to listen on menus click.
|
|
|
|
*
|
|
|
|
* @param listener The {@link FastInvClickListener} to add.
|
2021-07-10 23:41:20 +02:00
|
|
|
*
|
2019-01-24 12:00:57 +01:00
|
|
|
* @return This FastInv instance, for chaining.
|
|
|
|
*/
|
|
|
|
public FastInv onClick(FastInvClickListener listener) {
|
2023-06-25 13:50:27 +02:00
|
|
|
this.clickListeners.add(listener);
|
2019-01-24 12:00:57 +01:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Schedule a task to run.
|
|
|
|
*
|
|
|
|
* @param period Delay between each run.
|
|
|
|
* @param runnable The {@link Runnable} task to run.
|
2021-07-10 23:41:20 +02:00
|
|
|
*
|
2019-01-24 12:00:57 +01:00
|
|
|
* @return This FastInv instance, for chaining.
|
|
|
|
*/
|
|
|
|
public FastInv onUpdate(long period, Runnable runnable) {
|
|
|
|
return onUpdate(period, period, runnable);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Schedule a task to run with a delay before starting.
|
|
|
|
*
|
|
|
|
* @param delay Ticks to wait before starting the task.
|
|
|
|
* @param period Delay between each run.
|
|
|
|
* @param runnable The {@link Runnable} task to run.
|
2021-07-10 23:41:20 +02:00
|
|
|
*
|
2019-01-24 12:00:57 +01:00
|
|
|
* @return This FastInv instance, for chaining
|
|
|
|
*/
|
|
|
|
public FastInv onUpdate(long delay, long period, Runnable runnable) {
|
2023-06-25 13:50:27 +02:00
|
|
|
this.tasks.add(Bukkit.getScheduler().runTaskTimer(plugin, runnable, delay, period));
|
2019-01-24 12:00:57 +01:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Open the menus to a player.
|
|
|
|
*
|
|
|
|
* @param player The player to open the menu.
|
|
|
|
*/
|
|
|
|
public void open(Player player) {
|
2023-06-25 13:50:27 +02:00
|
|
|
Bukkit.getScheduler().runTask(plugin, () -> player.openInventory(this.inventory));
|
2019-01-24 12:00:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cancel all tasks.
|
|
|
|
*/
|
|
|
|
public void cancelTasks() {
|
2023-06-25 13:50:27 +02:00
|
|
|
this.tasks.forEach(BukkitTask::cancel);
|
|
|
|
this.tasks.clear();
|
2019-01-24 12:00:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Run a task on the server primary thread.
|
|
|
|
*
|
|
|
|
* @param runnable The runnable to run on the main thread
|
|
|
|
*/
|
|
|
|
public void runSync(Runnable runnable) {
|
|
|
|
if (Bukkit.isPrimaryThread()) {
|
|
|
|
runnable.run();
|
|
|
|
} else {
|
|
|
|
Bukkit.getScheduler().runTask(plugin, runnable);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the Bukkit menus associated with this FastInv instance.
|
|
|
|
*
|
|
|
|
* @return The Bukkit {@link Inventory}.
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public Inventory getInventory() {
|
2023-06-25 13:50:27 +02:00
|
|
|
return this.inventory;
|
2019-01-24 12:00:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-06-25 13:50:27 +02:00
|
|
|
* Set if the tasks will be canceled on menus close.
|
2019-01-24 12:00:57 +01:00
|
|
|
*
|
2023-06-25 13:50:27 +02:00
|
|
|
* @param cancelTasksOnClose Set if the tasks will be canceled
|
2021-07-10 23:41:20 +02:00
|
|
|
*
|
2019-01-24 12:00:57 +01:00
|
|
|
* @return This FastInv instance, for chaining.
|
|
|
|
*/
|
|
|
|
public FastInv setCancelTasksOnClose(boolean cancelTasksOnClose) {
|
|
|
|
this.cancelTasksOnClose = cancelTasksOnClose;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public interface FastInvClickListener {
|
|
|
|
void onClick(FastInvClickEvent event);
|
|
|
|
}
|
|
|
|
|
|
|
|
public interface FastInvCloseListener {
|
|
|
|
void onClose(FastInvCloseEvent event);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static abstract class FastInvEvent {
|
|
|
|
private boolean cancelled;
|
2019-01-24 20:41:01 +01:00
|
|
|
private final FastInv inventory;
|
|
|
|
private final Player player;
|
2019-01-24 12:00:57 +01:00
|
|
|
|
|
|
|
FastInvEvent(Player player, FastInv inventory, boolean cancelled) {
|
|
|
|
this.player = player;
|
|
|
|
this.inventory = inventory;
|
|
|
|
this.cancelled = cancelled;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the FastInv menus.
|
|
|
|
*
|
|
|
|
* @return This associated FastInv instance.
|
|
|
|
*/
|
|
|
|
public FastInv getInventory() {
|
2023-06-25 13:50:27 +02:00
|
|
|
return this.inventory;
|
2019-01-24 12:00:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the {@link Player} who clicked.
|
|
|
|
*
|
|
|
|
* @return the player who clicked.
|
|
|
|
*/
|
|
|
|
public Player getPlayer() {
|
2023-06-25 13:50:27 +02:00
|
|
|
return this.player;
|
2019-01-24 12:00:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get if the event is cancelled or not.
|
|
|
|
*
|
|
|
|
* @return Whether the event was cancelled.
|
|
|
|
*/
|
|
|
|
public boolean isCancelled() {
|
2023-06-25 13:50:27 +02:00
|
|
|
return this.cancelled;
|
2019-01-24 12:00:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-06-25 13:50:27 +02:00
|
|
|
* Set if the event will be canceled or not.
|
2019-01-24 12:00:57 +01:00
|
|
|
*
|
|
|
|
* @param cancel Whether the event should be cancelled.
|
|
|
|
*/
|
|
|
|
public void setCancelled(boolean cancel) {
|
|
|
|
this.cancelled = cancel;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-19 14:23:20 +01:00
|
|
|
private static Listener getListener() {
|
|
|
|
return new Listener() {
|
2019-01-24 12:00:57 +01:00
|
|
|
|
2019-02-19 14:23:20 +01:00
|
|
|
@EventHandler
|
|
|
|
public void onClick(InventoryClickEvent event) {
|
|
|
|
if (event.getInventory().getHolder() instanceof FastInv && event.getWhoClicked() instanceof Player) {
|
|
|
|
int slot = event.getRawSlot();
|
|
|
|
FastInv inv = (FastInv) event.getInventory().getHolder();
|
2019-01-24 12:00:57 +01:00
|
|
|
|
2019-02-19 14:23:20 +01:00
|
|
|
FastInvClickEvent clickEvent = new FastInvClickEvent((Player) event.getWhoClicked(), inv, event, slot,
|
|
|
|
event.getCurrentItem(), true, event.getAction(), event.getClick());
|
2019-01-24 12:00:57 +01:00
|
|
|
|
2019-02-19 14:23:20 +01:00
|
|
|
if (inv.itemListeners.containsKey(slot)) {
|
|
|
|
inv.itemListeners.get(slot).onClick(clickEvent);
|
|
|
|
}
|
2019-01-24 12:00:57 +01:00
|
|
|
|
2019-02-19 14:23:20 +01:00
|
|
|
inv.clickListeners.forEach(listener -> listener.onClick(clickEvent));
|
2019-01-24 12:00:57 +01:00
|
|
|
|
2019-02-19 14:23:20 +01:00
|
|
|
if (clickEvent.isCancelled()) {
|
|
|
|
event.setCancelled(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-01-24 12:00:57 +01:00
|
|
|
|
2019-02-19 14:23:20 +01:00
|
|
|
@EventHandler
|
|
|
|
public void onClose(InventoryCloseEvent event) {
|
|
|
|
if (event.getInventory().getHolder() instanceof FastInv && event.getPlayer() instanceof Player) {
|
|
|
|
Player player = (Player) event.getPlayer();
|
|
|
|
FastInv inv = (FastInv) event.getInventory().getHolder();
|
|
|
|
|
|
|
|
FastInvCloseEvent closeEvent = new FastInvCloseEvent(player, inv, false);
|
|
|
|
inv.closeListeners.forEach(listener -> listener.onClose(closeEvent));
|
|
|
|
|
|
|
|
Bukkit.getScheduler().runTask(plugin, () -> {
|
|
|
|
// Tiny delay to prevent errors.
|
|
|
|
if (closeEvent.isCancelled() && player.isOnline()) {
|
|
|
|
player.openInventory(inv.getInventory());
|
|
|
|
} else if (inv.getInventory().getViewers().isEmpty() && inv.cancelTasksOnClose) {
|
|
|
|
inv.cancelTasks();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@EventHandler
|
|
|
|
public void onDisable(PluginDisableEvent event) {
|
|
|
|
if (event.getPlugin().equals(plugin)) {
|
|
|
|
for (Player player : Bukkit.getOnlinePlayers()) {
|
|
|
|
if (player.getOpenInventory().getTopInventory().getHolder() instanceof FastInv) {
|
|
|
|
player.closeInventory();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2019-01-24 12:00:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public static class FastInvCloseEvent extends FastInvEvent {
|
|
|
|
private FastInvCloseEvent(Player player, FastInv inventory, boolean cancelled) {
|
|
|
|
super(player, inventory, cancelled);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|