Search in sources :

Example 1 with BukkitInventory

use of io.github.wysohn.triggerreactor.bukkit.bridge.BukkitInventory in project TriggerReactor by wysohn.

the class InventoryTriggerManager method onClick.

@EventHandler(ignoreCancelled = true)
public void onClick(InventoryClickEvent e) {
    Inventory inventory = e.getInventory();
    if (!this.hasInventoryOpen(new BukkitInventory(inventory)))
        return;
    InventoryTrigger trigger = getTriggerForOpenInventory(new BukkitInventory(inventory));
    // just always cancel if it's GUI
    e.setCancelled(true);
    if (!(e.getWhoClicked() instanceof Player))
        return;
    if (e.getRawSlot() < 0)
        return;
    Map<String, Object> varMap = getSharedVarsForInventory(new BukkitInventory(inventory));
    if (e.getRawSlot() < trigger.getItems().length) {
        if (trigger.getItems()[e.getRawSlot()] == null)
            varMap.put("item", new ItemStack(Material.AIR));
        else {
            ItemStack item = trigger.getItems()[e.getRawSlot()].get();
            varMap.put("item", item.clone());
        }
    }
    varMap.put("slot", e.getRawSlot());
    varMap.put("click", e.getClick().name());
    varMap.put("hotbar", e.getHotbarButton());
    varMap.put("trigger", "click");
    trigger.activate(e, varMap);
}
Also used : Player(org.bukkit.entity.Player) BukkitPlayer(io.github.wysohn.triggerreactor.bukkit.bridge.player.BukkitPlayer) BukkitInventory(io.github.wysohn.triggerreactor.bukkit.bridge.BukkitInventory) BukkitItemStack(io.github.wysohn.triggerreactor.bukkit.bridge.BukkitItemStack) IItemStack(io.github.wysohn.triggerreactor.core.bridge.IItemStack) ItemStack(org.bukkit.inventory.ItemStack) BukkitInventory(io.github.wysohn.triggerreactor.bukkit.bridge.BukkitInventory) Inventory(org.bukkit.inventory.Inventory) IInventory(io.github.wysohn.triggerreactor.core.bridge.IInventory) EventHandler(org.bukkit.event.EventHandler)

Example 2 with BukkitInventory

use of io.github.wysohn.triggerreactor.bukkit.bridge.BukkitInventory in project TriggerReactor by wysohn.

the class InventoryTriggerManager method onDrag.

@EventHandler(ignoreCancelled = true)
public void onDrag(InventoryDragEvent e) {
    Inventory inventory = e.getInventory();
    if (!this.hasInventoryOpen(new BukkitInventory(inventory)))
        return;
    e.setCancelled(true);
}
Also used : BukkitInventory(io.github.wysohn.triggerreactor.bukkit.bridge.BukkitInventory) BukkitInventory(io.github.wysohn.triggerreactor.bukkit.bridge.BukkitInventory) Inventory(org.bukkit.inventory.Inventory) IInventory(io.github.wysohn.triggerreactor.core.bridge.IInventory) EventHandler(org.bukkit.event.EventHandler)

Example 3 with BukkitInventory

use of io.github.wysohn.triggerreactor.bukkit.bridge.BukkitInventory in project TriggerReactor by wysohn.

the class JavaPluginBridge method createInterrupterForInv.

@Override
public ProcessInterrupter createInterrupterForInv(Object e, Interpreter interpreter, Map<UUID, Long> cooldowns, Map<IInventory, InventoryTrigger> inventoryMap) {
    return new ProcessInterrupter() {

        @Override
        public boolean onNodeProcess(Node node) {
            if (interpreter.isCooldown()) {
                if (e instanceof InventoryInteractEvent) {
                    HumanEntity he = ((InventoryInteractEvent) e).getWhoClicked();
                    if (he instanceof Player) {
                        Player player = (Player) he;
                        UUID uuid = player.getUniqueId();
                        cooldowns.put(uuid, interpreter.getCooldownEnd());
                    }
                }
                return false;
            }
            // is still running after the inventory is closed.
            if (e instanceof InventoryOpenEvent || e instanceof InventoryClickEvent) {
                Inventory inv = ((InventoryEvent) e).getInventory();
                // it's not GUI so stop execution
                if (!inventoryMap.containsKey(new BukkitInventory(inv)))
                    return true;
            }
            return false;
        }

        @Override
        public boolean onCommand(Object context, String command, Object[] args) {
            if ("CALL".equals(command)) {
                if (args.length < 1)
                    throw new RuntimeException("Need parameter [String] or [String, boolean]");
                if (args[0] instanceof String) {
                    Trigger trigger = getNamedTriggerManager().getTriggerForName((String) args[0]);
                    if (trigger == null)
                        throw new RuntimeException("No trigger found for Named Trigger " + args[0]);
                    if (args.length > 1 && args[1] instanceof Boolean) {
                        trigger.setSync((boolean) args[1]);
                    } else {
                        trigger.setSync(true);
                    }
                    if (trigger.isSync()) {
                        trigger.activate(e, interpreter.getVars());
                    } else {
                        // use snapshot to avoid concurrent modification
                        trigger.activate(e, new HashMap<>(interpreter.getVars()));
                    }
                    return true;
                } else {
                    throw new RuntimeException("Parameter type not match; it should be a String." + " Make sure to put double quotes, if you provided String literal.");
                }
            } else if ("CANCELEVENT".equals(command)) {
                if (!interpreter.isSync())
                    throw new RuntimeException("CANCELEVENT is illegal in async mode!");
                if (context instanceof Cancellable) {
                    ((Cancellable) context).setCancelled(true);
                    return true;
                } else {
                    throw new RuntimeException(context + " is not a Cancellable event!");
                }
            }
            return false;
        }
    };
}
Also used : InventoryOpenEvent(org.bukkit.event.inventory.InventoryOpenEvent) InventoryEvent(org.bukkit.event.inventory.InventoryEvent) DelegatedPlayer(io.github.wysohn.triggerreactor.bukkit.tools.DelegatedPlayer) IPlayer(io.github.wysohn.triggerreactor.core.bridge.player.IPlayer) Player(org.bukkit.entity.Player) BukkitPlayer(io.github.wysohn.triggerreactor.bukkit.bridge.player.BukkitPlayer) Cancellable(org.bukkit.event.Cancellable) Node(io.github.wysohn.triggerreactor.core.script.parser.Node) ProcessInterrupter(io.github.wysohn.triggerreactor.core.script.interpreter.Interpreter.ProcessInterrupter) InventoryClickEvent(org.bukkit.event.inventory.InventoryClickEvent) Trigger(io.github.wysohn.triggerreactor.core.manager.trigger.AbstractTriggerManager.Trigger) InventoryTrigger(io.github.wysohn.triggerreactor.core.manager.trigger.AbstractInventoryTriggerManager.InventoryTrigger) BukkitInventory(io.github.wysohn.triggerreactor.bukkit.bridge.BukkitInventory) HumanEntity(org.bukkit.entity.HumanEntity) InventoryInteractEvent(org.bukkit.event.inventory.InventoryInteractEvent) UUID(java.util.UUID) BukkitInventory(io.github.wysohn.triggerreactor.bukkit.bridge.BukkitInventory) IInventory(io.github.wysohn.triggerreactor.core.bridge.IInventory) Inventory(org.bukkit.inventory.Inventory)

Example 4 with BukkitInventory

use of io.github.wysohn.triggerreactor.bukkit.bridge.BukkitInventory in project TriggerReactor by wysohn.

the class InventoryTriggerManager method onOpen.

@EventHandler
public void onOpen(InventoryOpenEvent e) {
    Inventory inventory = e.getInventory();
    if (!this.hasInventoryOpen(new BukkitInventory(inventory)))
        return;
    InventoryTrigger trigger = getTriggerForOpenInventory(new BukkitInventory(inventory));
    Map<String, Object> varMap = getSharedVarsForInventory(new BukkitInventory(inventory));
    varMap.put("player", e.getPlayer());
    varMap.put("trigger", "open");
    trigger.activate(e, varMap);
}
Also used : BukkitInventory(io.github.wysohn.triggerreactor.bukkit.bridge.BukkitInventory) BukkitInventory(io.github.wysohn.triggerreactor.bukkit.bridge.BukkitInventory) Inventory(org.bukkit.inventory.Inventory) IInventory(io.github.wysohn.triggerreactor.core.bridge.IInventory) EventHandler(org.bukkit.event.EventHandler)

Aggregations

BukkitInventory (io.github.wysohn.triggerreactor.bukkit.bridge.BukkitInventory)4 IInventory (io.github.wysohn.triggerreactor.core.bridge.IInventory)4 Inventory (org.bukkit.inventory.Inventory)4 EventHandler (org.bukkit.event.EventHandler)3 BukkitPlayer (io.github.wysohn.triggerreactor.bukkit.bridge.player.BukkitPlayer)2 Player (org.bukkit.entity.Player)2 BukkitItemStack (io.github.wysohn.triggerreactor.bukkit.bridge.BukkitItemStack)1 DelegatedPlayer (io.github.wysohn.triggerreactor.bukkit.tools.DelegatedPlayer)1 IItemStack (io.github.wysohn.triggerreactor.core.bridge.IItemStack)1 IPlayer (io.github.wysohn.triggerreactor.core.bridge.player.IPlayer)1 InventoryTrigger (io.github.wysohn.triggerreactor.core.manager.trigger.AbstractInventoryTriggerManager.InventoryTrigger)1 Trigger (io.github.wysohn.triggerreactor.core.manager.trigger.AbstractTriggerManager.Trigger)1 ProcessInterrupter (io.github.wysohn.triggerreactor.core.script.interpreter.Interpreter.ProcessInterrupter)1 Node (io.github.wysohn.triggerreactor.core.script.parser.Node)1 UUID (java.util.UUID)1 HumanEntity (org.bukkit.entity.HumanEntity)1 Cancellable (org.bukkit.event.Cancellable)1 InventoryClickEvent (org.bukkit.event.inventory.InventoryClickEvent)1 InventoryEvent (org.bukkit.event.inventory.InventoryEvent)1 InventoryInteractEvent (org.bukkit.event.inventory.InventoryInteractEvent)1