Search in sources :

Example 1 with Trigger

use of io.github.wysohn.triggerreactor.core.manager.trigger.AbstractTriggerManager.Trigger 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 2 with Trigger

use of io.github.wysohn.triggerreactor.core.manager.trigger.AbstractTriggerManager.Trigger in project TriggerReactor by wysohn.

the class JavaPluginBridge method showGlowStones.

@Override
@SuppressWarnings("deprecation")
protected void showGlowStones(ICommandSender sender, Set<Entry<SimpleLocation, Trigger>> set) {
    for (Entry<SimpleLocation, Trigger> entry : set) {
        SimpleLocation sloc = entry.getKey();
        Player player = sender.get();
        player.sendBlockChange(new Location(Bukkit.getWorld(sloc.getWorld()), sloc.getX(), sloc.getY(), sloc.getZ()), Material.GLOWSTONE, (byte) 0);
    }
}
Also used : 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) Trigger(io.github.wysohn.triggerreactor.core.manager.trigger.AbstractTriggerManager.Trigger) InventoryTrigger(io.github.wysohn.triggerreactor.core.manager.trigger.AbstractInventoryTriggerManager.InventoryTrigger) SimpleLocation(io.github.wysohn.triggerreactor.core.manager.location.SimpleLocation) Location(org.bukkit.Location) SimpleLocation(io.github.wysohn.triggerreactor.core.manager.location.SimpleLocation)

Example 3 with Trigger

use of io.github.wysohn.triggerreactor.core.manager.trigger.AbstractTriggerManager.Trigger in project TriggerReactor by wysohn.

the class AbstractLocationBasedTriggerManager method loadTriggers.

private void loadTriggers(File[] target) {
    for (File file : target) {
        if (file.isDirectory()) {
            loadTriggers(file.listFiles());
        } else {
            if (!isTriggerFile(file))
                continue;
            String triggerName = extractName(file);
            SimpleLocation sloc = null;
            try {
                sloc = stringToSloc(triggerName);
            } catch (Exception e) {
                e.printStackTrace();
                continue;
            }
            String script = null;
            try {
                script = FileUtil.readFromFile(file);
            } catch (IOException e1) {
                e1.printStackTrace();
                continue;
            }
            T trigger = null;
            try {
                trigger = constructTrigger(sloc.toString(), script);
            } catch (TriggerInitFailedException e) {
                e.printStackTrace();
                continue;
            }
            if (sloc != null && trigger != null) {
                SimpleChunkLocation scloc = new SimpleChunkLocation(sloc);
                Map<SimpleLocation, T> triggerMap = locationTriggers.get(scloc);
                if (!locationTriggers.containsKey(scloc)) {
                    triggerMap = new ConcurrentHashMap<>();
                    locationTriggers.put(scloc, triggerMap);
                }
                if (triggerMap.containsKey(sloc)) {
                    Trigger previous = triggerMap.get(sloc);
                    plugin.getLogger().warning("Found a duplicating " + trigger.getClass().getSimpleName());
                    plugin.getLogger().warning("Existing: " + previous.file.getAbsolutePath());
                    plugin.getLogger().warning("Skipped: " + trigger.file.getAbsolutePath());
                } else {
                    triggerMap.put(sloc, trigger);
                }
            }
        }
    }
}
Also used : Trigger(io.github.wysohn.triggerreactor.core.manager.trigger.AbstractTriggerManager.Trigger) SimpleChunkLocation(io.github.wysohn.triggerreactor.core.manager.location.SimpleChunkLocation) SimpleLocation(io.github.wysohn.triggerreactor.core.manager.location.SimpleLocation) IOException(java.io.IOException) File(java.io.File) IOException(java.io.IOException)

Example 4 with Trigger

use of io.github.wysohn.triggerreactor.core.manager.trigger.AbstractTriggerManager.Trigger in project TriggerReactor by wysohn.

the class JavaPluginBridge method createInterrupter.

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

        @Override
        public boolean onNodeProcess(Node node) {
            if (interpreter.isCooldown() && e instanceof PlayerEvent) {
                Player player = ((PlayerEvent) e).getPlayer();
                UUID uuid = player.getUniqueId();
                cooldowns.put(uuid, interpreter.getCooldownEnd());
            }
            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 : ProcessInterrupter(io.github.wysohn.triggerreactor.core.script.interpreter.Interpreter.ProcessInterrupter) 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) Trigger(io.github.wysohn.triggerreactor.core.manager.trigger.AbstractTriggerManager.Trigger) InventoryTrigger(io.github.wysohn.triggerreactor.core.manager.trigger.AbstractInventoryTriggerManager.InventoryTrigger) Cancellable(org.bukkit.event.Cancellable) Node(io.github.wysohn.triggerreactor.core.script.parser.Node) PlayerEvent(org.bukkit.event.player.PlayerEvent) UUID(java.util.UUID)

Example 5 with Trigger

use of io.github.wysohn.triggerreactor.core.manager.trigger.AbstractTriggerManager.Trigger in project TriggerReactor by wysohn.

the class LocationBasedTriggerManager method showTriggerInfo.

protected void showTriggerInfo(ICommandSender sender, Block clicked) {
    Trigger trigger = getTriggerForLocation(LocationUtil.convertToSimpleLocation(clicked.getLocation()));
    if (trigger == null) {
        return;
    }
    sender.sendMessage("- - - - - - - - - - - - - -");
    sender.sendMessage("Trigger: " + getTriggerTypeName());
    sender.sendMessage("Block Type: " + clicked.getType().name());
    sender.sendMessage("Location: " + clicked.getWorld().getName() + "@" + clicked.getLocation().getBlockX() + "," + clicked.getLocation().getBlockY() + "," + clicked.getLocation().getBlockZ());
    sender.sendMessage("");
    sender.sendMessage("Script:");
    sender.sendMessage(trigger.getScript());
    sender.sendMessage("- - - - - - - - - - - - - -");
}
Also used : Trigger(io.github.wysohn.triggerreactor.core.manager.trigger.AbstractTriggerManager.Trigger)

Aggregations

Trigger (io.github.wysohn.triggerreactor.core.manager.trigger.AbstractTriggerManager.Trigger)7 IPlayer (io.github.wysohn.triggerreactor.core.bridge.player.IPlayer)4 InventoryTrigger (io.github.wysohn.triggerreactor.core.manager.trigger.AbstractInventoryTriggerManager.InventoryTrigger)4 BukkitPlayer (io.github.wysohn.triggerreactor.bukkit.bridge.player.BukkitPlayer)3 DelegatedPlayer (io.github.wysohn.triggerreactor.bukkit.tools.DelegatedPlayer)3 SimpleChunkLocation (io.github.wysohn.triggerreactor.core.manager.location.SimpleChunkLocation)3 SimpleLocation (io.github.wysohn.triggerreactor.core.manager.location.SimpleLocation)3 Player (org.bukkit.entity.Player)3 IInventory (io.github.wysohn.triggerreactor.core.bridge.IInventory)2 ProcessInterrupter (io.github.wysohn.triggerreactor.core.script.interpreter.Interpreter.ProcessInterrupter)2 Node (io.github.wysohn.triggerreactor.core.script.parser.Node)2 UUID (java.util.UUID)2 Location (org.bukkit.Location)2 Cancellable (org.bukkit.event.Cancellable)2 BukkitInventory (io.github.wysohn.triggerreactor.bukkit.bridge.BukkitInventory)1 IItemStack (io.github.wysohn.triggerreactor.core.bridge.IItemStack)1 ILocation (io.github.wysohn.triggerreactor.core.bridge.ILocation)1 AbstractAreaSelectionManager (io.github.wysohn.triggerreactor.core.manager.AbstractAreaSelectionManager)1 AbstractExecutorManager (io.github.wysohn.triggerreactor.core.manager.AbstractExecutorManager)1 AbstractPermissionManager (io.github.wysohn.triggerreactor.core.manager.AbstractPermissionManager)1