Search in sources :

Example 1 with CustomTrigger

use of io.github.wysohn.triggerreactor.core.manager.trigger.AbstractCustomTriggerManager.CustomTrigger in project TriggerReactor by wysohn.

the class TriggerReactor method onCommand.

public boolean onCommand(ICommandSender sender, String command, String[] args) {
    if (command.equalsIgnoreCase("triggerreactor")) {
        if (!sender.hasPermission("triggerreactor.admin"))
            return true;
        if (args.length > 0) {
            if (args[0].equalsIgnoreCase("debug")) {
                debugging = !debugging;
                getLogger().info("Debugging is set to " + debugging);
                return true;
            } else if (args[0].equalsIgnoreCase("click") || args[0].equalsIgnoreCase("c")) {
                if (args.length == 1) {
                    getScriptEditManager().startEdit(sender, "Click Trigger", "", new SaveHandler() {

                        @Override
                        public void onSave(String script) {
                            if (getClickManager().startLocationSet((IPlayer) sender, script)) {
                                sender.sendMessage("&7Now click the block to set click trigger.");
                            } else {
                                sender.sendMessage("&7Already on progress.");
                            }
                        }
                    });
                } else {
                    StringBuilder builder = new StringBuilder();
                    for (int i = 1; i < args.length; i++) builder.append(args[i] + " ");
                    if (getClickManager().startLocationSet((IPlayer) sender, builder.toString())) {
                        sender.sendMessage("&7Now click the block to set click trigger.");
                    } else {
                        sender.sendMessage("&7Already on progress.");
                    }
                }
                return true;
            } else if (args[0].equalsIgnoreCase("walk") || args[0].equalsIgnoreCase("w")) {
                if (args.length == 1) {
                    getScriptEditManager().startEdit(sender, "Walk Trigger", "", new SaveHandler() {

                        @Override
                        public void onSave(String script) {
                            if (getWalkManager().startLocationSet((IPlayer) sender, script)) {
                                sender.sendMessage("&7Now click the block to set walk trigger.");
                            } else {
                                sender.sendMessage("&7Already on progress.");
                            }
                        }
                    });
                } else {
                    StringBuilder builder = new StringBuilder();
                    for (int i = 1; i < args.length; i++) builder.append(args[i] + " ");
                    if (getWalkManager().startLocationSet((IPlayer) sender, builder.toString())) {
                        sender.sendMessage("&7Now click the block to set walk trigger.");
                    } else {
                        sender.sendMessage("&7Already on progress.");
                    }
                }
                return true;
            } else if (args.length > 1 && (args[0].equalsIgnoreCase("command") || args[0].equalsIgnoreCase("cmd"))) {
                if (getCmdManager().hasCommandTrigger(args[1])) {
                    Trigger trigger = getCmdManager().getCommandTrigger(args[1]);
                    getScriptEditManager().startEdit(sender, trigger.getTriggerName(), trigger.getScript(), new SaveHandler() {

                        @Override
                        public void onSave(String script) {
                            try {
                                trigger.setScript(script);
                            } catch (Exception e) {
                                handleException(sender, e);
                            }
                            sender.sendMessage("&aScript is updated!");
                            saveAsynchronously(getCmdManager());
                        }
                    });
                } else {
                    if (args.length == 2) {
                        getScriptEditManager().startEdit(sender, "Command Trigger", "", new SaveHandler() {

                            @Override
                            public void onSave(String script) {
                                getCmdManager().addCommandTrigger(sender, args[1], script);
                                sender.sendMessage("&aCommand trigger is binded!");
                                saveAsynchronously(getCmdManager());
                            }
                        });
                    } else {
                        StringBuilder builder = new StringBuilder();
                        for (int i = 2; i < args.length; i++) builder.append(args[i] + " ");
                        getCmdManager().addCommandTrigger(sender, args[1], builder.toString());
                        sender.sendMessage("&aCommand trigger is binded!");
                        saveAsynchronously(getCmdManager());
                    }
                }
                return true;
            } else if ((args[0].equalsIgnoreCase("variables") || args[0].equalsIgnoreCase("vars"))) {
                if (args.length == 3) {
                    if (args[1].equalsIgnoreCase("Item")) {
                        String name = args[2];
                        if (!getVariableManager().isValidName(name)) {
                            sender.sendMessage("&c" + name + " is not a valid key!");
                            return true;
                        }
                        IItemStack IS = ((IPlayer) sender).getItemInMainHand();
                        if (IS == null) {
                            sender.sendMessage("&c" + "You are holding nothing on your main hand!");
                            return true;
                        }
                        try {
                            getVariableManager().put(name, IS.get());
                        } catch (Exception e) {
                            this.handleException(sender, e);
                        }
                        sender.sendMessage("&aItem saved!");
                    } else if (args[1].equalsIgnoreCase("Location")) {
                        String name = args[2];
                        if (!getVariableManager().isValidName(name)) {
                            sender.sendMessage("&c" + name + " is not a valid key!");
                            return true;
                        }
                        ILocation loc = ((IPlayer) sender).getLocation();
                        try {
                            getVariableManager().put(name, loc.get());
                        } catch (Exception e) {
                            this.handleException(sender, e);
                        }
                        sender.sendMessage("&aLocation saved!");
                    } else {
                        String name = args[1];
                        String value = args[2];
                        if (!getVariableManager().isValidName(name)) {
                            sender.sendMessage("&c" + name + " is not a valid key!");
                            return true;
                        }
                        if (value.matches(INTEGER_REGEX)) {
                            try {
                                getVariableManager().put(name, Integer.parseInt(value));
                            } catch (Exception e) {
                                this.handleException(sender, e);
                            }
                        } else if (value.matches(DOUBLE_REGEX)) {
                            try {
                                getVariableManager().put(name, Double.parseDouble(value));
                            } catch (Exception e) {
                                this.handleException(sender, e);
                            }
                        } else if (value.equals("true") || value.equals("false")) {
                            try {
                                getVariableManager().put(name, Boolean.parseBoolean(value));
                            } catch (Exception e) {
                                this.handleException(sender, e);
                            }
                        } else {
                            try {
                                getVariableManager().put(name, value);
                            } catch (Exception e) {
                                this.handleException(sender, e);
                            }
                        }
                        sender.sendMessage("&aVariable saved!");
                    }
                    return true;
                } else if (args.length == 2) {
                    String name = args[1];
                    sender.sendMessage("&7Value of " + name + ": " + getVariableManager().get(name));
                    return true;
                }
            } else if (args.length > 1 && args[0].equalsIgnoreCase("run")) {
                String script = mergeArguments(args, 1, args.length - 1);
                try {
                    Trigger trigger = getCmdManager().createTempCommandTrigger(script);
                    trigger.activate(createEmptyPlayerEvent(sender), new HashMap<>());
                } catch (Exception e) {
                    handleException(sender, e);
                }
                return true;
            } else if (args[0].equalsIgnoreCase("inventory") || args[0].equalsIgnoreCase("i")) {
                if (args.length > 3 && args[2].equalsIgnoreCase("create")) {
                    String name = args[1];
                    int size = -1;
                    try {
                        size = Integer.parseInt(args[3]);
                    } catch (NumberFormatException e) {
                        sender.sendMessage("&c" + "" + size + " is not a valid number");
                        return true;
                    }
                    if (args.length == 4) {
                        final int sizeCopy = size;
                        getScriptEditManager().startEdit(sender, "Inventory Trigger", "", new SaveHandler() {

                            @Override
                            public void onSave(String script) {
                                try {
                                    if (getInvManager().createTrigger(sizeCopy, name, script)) {
                                        sender.sendMessage("&aInventory Trigger created!");
                                        saveAsynchronously(getInvManager());
                                    } else {
                                        sender.sendMessage("&7Another Inventory Trigger with that name already exists");
                                    }
                                } catch (Exception e) {
                                    handleException(sender, e);
                                }
                            }
                        });
                    } else {
                        String script = mergeArguments(args, 4, args.length - 1);
                        try {
                            if (getInvManager().createTrigger(size, name, script)) {
                                sender.sendMessage("&aInventory Trigger created!");
                                saveAsynchronously(getInvManager());
                            } else {
                                sender.sendMessage("&7Another Inventory Trigger with that name already exists");
                            }
                        } catch (Exception e) {
                            handleException(sender, e);
                        }
                    }
                } else if (args.length == 3 && args[2].equalsIgnoreCase("delete")) {
                    String name = args[1];
                    if (getInvManager().deleteTrigger(name)) {
                        sender.sendMessage("&aDeleted!");
                        saveAsynchronously(getInvManager());
                    } else {
                        sender.sendMessage("&7No such inventory trigger found.");
                    }
                } else if (args.length == 4 && args[2].equals("item")) {
                    IItemStack IS = ((IPlayer) sender).getItemInMainHand();
                    IS = IS == null ? null : IS.clone();
                    String name = args[1];
                    int index = -1;
                    try {
                        index = Integer.parseInt(args[3]);
                    } catch (NumberFormatException e) {
                        sender.sendMessage("&c" + "" + index + " is not a valid number.");
                        return true;
                    }
                    InventoryTrigger trigger = getInvManager().getTriggerForName(name);
                    if (trigger == null) {
                        sender.sendMessage("&7No such Inventory Trigger named " + name);
                        return true;
                    }
                    if (index > trigger.getItems().length - 1) {
                        sender.sendMessage("&c" + "" + index + " is out of bound. (Size: " + trigger.getItems().length + ")");
                        return true;
                    }
                    trigger.getItems()[index] = IS;
                    saveAsynchronously(getInvManager());
                } else if (args.length > 2 && args[2].equalsIgnoreCase("open")) {
                    String name = args[1];
                    IPlayer forWhom = null;
                    if (args.length == 3) {
                        forWhom = (IPlayer) sender;
                    } else {
                        IPlayer p = getPlayer(args[3]);
                        if (p != null)
                            forWhom = p;
                    }
                    if (forWhom == null) {
                        sender.sendMessage("&7Can't find that player.");
                        return true;
                    }
                    IInventory opened = getInvManager().openGUI(forWhom, name);
                    if (opened == null) {
                        sender.sendMessage("&7No such Inventory Trigger named " + name);
                        return true;
                    }
                } else if (args.length == 3 && args[2].equalsIgnoreCase("edit")) {
                    String name = args[1];
                    InventoryTrigger trigger = getInvManager().getTriggerForName(name);
                    if (trigger == null) {
                        sender.sendMessage("&7No such Inventory Trigger named " + name);
                        return true;
                    }
                    getScriptEditManager().startEdit(sender, trigger.getTriggerName(), trigger.getScript(), new SaveHandler() {

                        @Override
                        public void onSave(String script) {
                            try {
                                trigger.setScript(script);
                            } catch (Exception e) {
                                handleException(sender, e);
                            }
                            sender.sendMessage("&aScript is updated!");
                            saveAsynchronously(getInvManager());
                        }
                    });
                } else {
                    sendCommandDesc(sender, "/triggerreactor[trg] inventory[i] <inventory name> create <size> [...]", "create a new inventory. <size> must be multiple of 9." + " The <size> cannot be larger than 54");
                    sendDetails(sender, "/trg i MyInventory create 54");
                    sendCommandDesc(sender, "/triggerreactor[trg] inventory[i] <inventory name> delete", "delete this inventory");
                    sendDetails(sender, "/trg i MyInventory delete");
                    sendCommandDesc(sender, "/triggerreactor[trg] inventory[i] <inventory name> item <index>", "set item of inventory to the held item. " + "Clears the slot if you are holding nothing.");
                    sendDetails(sender, "/trg i MyInventory item 0");
                    sendCommandDesc(sender, "/triggerreactor[trg] inventory[i] <inventory name> open", "Simply open GUI");
                    sendCommandDesc(sender, "/triggerreactor[trg] inventory[i] <inventory name> open <player name>", "Simply open GUI for <player name>");
                    sendCommandDesc(sender, "/triggerreactor[trg] inventory[i] <inventory name> edit", "Edit the inventory trigger.");
                }
                return true;
            } else if (args[0].equalsIgnoreCase("item")) {
                if (args.length > 2 && args[1].equalsIgnoreCase("title")) {
                    IItemStack IS = ((IPlayer) sender).getItemInMainHand();
                    if (IS == null) {
                        sender.sendMessage("&c" + "You are holding nothing.");
                        return true;
                    }
                    String title = mergeArguments(args, 2, args.length - 1);
                    setItemTitle(IS, title);
                    ((IPlayer) sender).setItemInMainHand(IS);
                    return true;
                } else if (args.length > 3 && args[1].equalsIgnoreCase("lore") && args[2].equalsIgnoreCase("add")) {
                    IItemStack IS = ((IPlayer) sender).getItemInMainHand();
                    if (IS == null) {
                        sender.sendMessage("&c" + "You are holding nothing.");
                        return true;
                    }
                    String lore = mergeArguments(args, 3, args.length - 1);
                    addItemLore(IS, lore);
                    ((IPlayer) sender).setItemInMainHand(IS);
                    return true;
                } else if (args.length > 4 && args[1].equalsIgnoreCase("lore") && args[2].equalsIgnoreCase("set")) {
                    IItemStack IS = ((IPlayer) sender).getItemInMainHand();
                    if (IS == null) {
                        sender.sendMessage("&c" + "You are holding nothing.");
                        return true;
                    }
                    int index = -1;
                    try {
                        index = Integer.parseInt(args[3]);
                    } catch (NumberFormatException e) {
                        sender.sendMessage("&c" + "" + index + " is not a valid number");
                        return true;
                    }
                    String lore = mergeArguments(args, 4, args.length - 1);
                    if (!setLore(IS, index, lore)) {
                        sender.sendMessage("&c" + "" + index + " is out of bound.");
                        return true;
                    }
                    ((IPlayer) sender).setItemInMainHand(IS);
                    return true;
                } else if (args.length == 4 && args[1].equalsIgnoreCase("lore") && args[2].equalsIgnoreCase("remove")) {
                    IItemStack IS = ((IPlayer) sender).getItemInMainHand();
                    if (IS == null) {
                        sender.sendMessage("&c" + "You are holding nothing.");
                        return true;
                    }
                    int index = -1;
                    try {
                        index = Integer.parseInt(args[3]);
                    } catch (NumberFormatException e) {
                        sender.sendMessage("&c" + "" + index + " is not a valid number");
                        return true;
                    }
                    if (!removeLore(IS, index)) {
                        sender.sendMessage("&7No lore at index " + index);
                        return true;
                    }
                    ((IPlayer) sender).setItemInMainHand(IS);
                    return true;
                } else {
                    sendCommandDesc(sender, "/triggerreactor[trg] item title <item title>", "Change the title of holding item");
                    sendCommandDesc(sender, "/triggerreactor[trg] item lore add <line>", "Append lore to the holding item");
                    sendCommandDesc(sender, "/triggerreactor[trg] item lore set <index> <line>", "Replace lore at the specified index." + "(Index start from 0)");
                    sendCommandDesc(sender, "/triggerreactor[trg] item lore remove <index>", "Delete lore at the specified index.");
                }
                return true;
            } else if (args.length > 0 && (args[0].equalsIgnoreCase("area") || args[0].equalsIgnoreCase("a"))) {
                if (args.length == 2 && args[1].equalsIgnoreCase("toggle")) {
                    boolean result = getSelectionManager().toggleSelection(((IPlayer) sender).getUniqueId());
                    sender.sendMessage("&7Area selection mode enabled: &6" + result);
                } else if (args.length == 3 && args[2].equals("create")) {
                    String name = args[1];
                    AreaTrigger trigger = getAreaManager().getArea(name);
                    if (trigger != null) {
                        sender.sendMessage("&c" + "Area Trigger " + name + " is already exists!");
                        return true;
                    }
                    Area selected = getSelectionManager().getSelection(((IPlayer) sender).getUniqueId());
                    if (selected == null) {
                        sender.sendMessage("&7Invalid or incomplete area selection.");
                        return true;
                    }
                    Set<Area> conflicts = getAreaManager().getConflictingAreas(selected);
                    if (!conflicts.isEmpty()) {
                        sender.sendMessage("&7Found [" + conflicts.size() + "] conflicting areas:");
                        for (Area conflict : conflicts) {
                            sender.sendMessage("&d  " + conflict);
                        }
                        return true;
                    }
                    if (getAreaManager().createArea(name, selected.getSmallest(), selected.getLargest())) {
                        sender.sendMessage("&aArea Trigger has created!");
                        saveAsynchronously(getAreaManager());
                        getSelectionManager().resetSelections(((IPlayer) sender).getUniqueId());
                    } else {
                        sender.sendMessage("&7Area Trigger " + name + " already exists.");
                    }
                } else if (args.length == 3 && args[2].equals("delete")) {
                    String name = args[1];
                    if (getAreaManager().deleteArea(name)) {
                        sender.sendMessage("&aArea Trigger deleted");
                        saveAsynchronously(getAreaManager());
                        getSelectionManager().resetSelections(((IPlayer) sender).getUniqueId());
                    } else {
                        sender.sendMessage("&7Area Trigger " + name + " does not exists.");
                    }
                } else if (args.length > 2 && args[2].equals("enter")) {
                    String name = args[1];
                    AreaTrigger trigger = getAreaManager().getArea(name);
                    if (trigger == null) {
                        sender.sendMessage("&7No Area Trigger found with that name.");
                        return true;
                    }
                    if (trigger.getEnterTrigger() != null) {
                        getScriptEditManager().startEdit(sender, trigger.getTriggerName(), trigger.getEnterTrigger().getScript(), new SaveHandler() {

                            @Override
                            public void onSave(String script) {
                                try {
                                    trigger.setEnterTrigger(script);
                                    saveAsynchronously(getAreaManager());
                                    sender.sendMessage("&aScript is updated!");
                                } catch (Exception e) {
                                    handleException(sender, e);
                                }
                            }
                        });
                    } else {
                        if (args.length == 3) {
                            getScriptEditManager().startEdit(sender, "Area Trigger [Enter]", "", new SaveHandler() {

                                @Override
                                public void onSave(String script) {
                                    try {
                                        trigger.setEnterTrigger(script);
                                        saveAsynchronously(getAreaManager());
                                    } catch (Exception e) {
                                        handleException(sender, e);
                                    }
                                }
                            });
                        } else {
                            try {
                                trigger.setEnterTrigger(mergeArguments(args, 3, args.length - 1));
                                saveAsynchronously(getAreaManager());
                            } catch (Exception e) {
                                handleException(sender, e);
                            }
                        }
                    }
                } else if (args.length > 2 && args[2].equals("exit")) {
                    String name = args[1];
                    AreaTrigger trigger = getAreaManager().getArea(name);
                    if (trigger == null) {
                        sender.sendMessage("&7No Area Trigger found with that name.");
                        return true;
                    }
                    if (trigger.getExitTrigger() != null) {
                        getScriptEditManager().startEdit(sender, trigger.getTriggerName(), trigger.getExitTrigger().getScript(), new SaveHandler() {

                            @Override
                            public void onSave(String script) {
                                try {
                                    trigger.setExitTrigger(script);
                                    saveAsynchronously(getAreaManager());
                                    sender.sendMessage("&aScript is updated!");
                                } catch (Exception e) {
                                    handleException(sender, e);
                                }
                            }
                        });
                    } else {
                        if (args.length == 3) {
                            getScriptEditManager().startEdit(sender, "Area Trigger [Exit]", "", new SaveHandler() {

                                @Override
                                public void onSave(String script) {
                                    try {
                                        trigger.setExitTrigger(script);
                                        saveAsynchronously(getAreaManager());
                                    } catch (Exception e) {
                                        handleException(sender, e);
                                    }
                                }
                            });
                        } else {
                            try {
                                trigger.setExitTrigger(mergeArguments(args, 3, args.length - 1));
                                saveAsynchronously(getAreaManager());
                            } catch (Exception e) {
                                handleException(sender, e);
                            }
                        }
                    }
                } else if (args.length == 3 && args[2].equals("sync")) {
                    String name = args[1];
                    AreaTrigger trigger = getAreaManager().getArea(name);
                    if (trigger == null) {
                        sender.sendMessage("&7No Area Trigger found with that name.");
                        return true;
                    }
                    trigger.setSync(!trigger.isSync());
                    saveAsynchronously(getAreaManager());
                    sender.sendMessage("&7Sync mode: " + (trigger.isSync() ? "&a" : "&c") + trigger.isSync());
                } else {
                    sendCommandDesc(sender, "/triggerreactor[trg] area[a] toggle", "Enable/Disable area selection mode.");
                    sendCommandDesc(sender, "/triggerreactor[trg] area[a] <name> create", "Create area trigger out of selected region.");
                    sendCommandDesc(sender, "/triggerreactor[trg] area[a] <name> delete", "Delete area trigger. BE CAREFUL!");
                    sendCommandDesc(sender, "/triggerreactor[trg] area[a] <name> enter [...]", "Enable/Disable area selection mode.");
                    sendDetails(sender, "/trg a TestingArea enter #MESSAGE \"Welcome\"");
                    sendCommandDesc(sender, "/triggerreactor[trg] area[a] <name> exit [...]", "Enable/Disable area selection mode.");
                    sendDetails(sender, "/trg a TestingArea exit #MESSAGE \"Bye\"");
                    sendCommandDesc(sender, "/triggerreactor[trg] area[a] <name> sync", "Enable/Disable sync mode.");
                    sendDetails(sender, "Setting it to true when you want to cancel event (with #CANCELEVENT)." + " However, setting sync mode will make the trigger run on server thread; keep in mind that" + " it can lag the server if you have too much things going on within the code." + " Set it to false always if you are not sure.");
                }
                return true;
            } else if (args.length > 2 && args[0].equalsIgnoreCase("custom")) {
                String eventName = args[1];
                String name = args[2];
                CustomTrigger trigger = getCustomManager().getTriggerForName(name);
                if (trigger != null) {
                    getScriptEditManager().startEdit(sender, trigger.getTriggerName(), trigger.getScript(), new SaveHandler() {

                        @Override
                        public void onSave(String script) {
                            try {
                                trigger.setScript(script);
                            } catch (Exception e) {
                                handleException(sender, e);
                            }
                            sender.sendMessage("&aScript is updated!");
                            saveAsynchronously(getCustomManager());
                        }
                    });
                } else {
                    if (args.length == 3) {
                        getScriptEditManager().startEdit(sender, "Custom Trigger[" + eventName.substring(Math.max(0, eventName.length() - 10)) + "]", "", new SaveHandler() {

                            @Override
                            public void onSave(String script) {
                                try {
                                    getCustomManager().createCustomTrigger(eventName, name, script);
                                    saveAsynchronously(getCustomManager());
                                    sender.sendMessage("&aCustom Trigger created!");
                                } catch (Exception e) {
                                    e.printStackTrace();
                                    sender.sendMessage("&c" + "Could not save! " + e.getMessage());
                                    sender.sendMessage("&c" + "See console for detailed messages.");
                                }
                            }
                        });
                    } else {
                        String script = mergeArguments(args, 3, args.length - 1);
                        try {
                            getCustomManager().createCustomTrigger(eventName, name, script);
                            saveAsynchronously(getCustomManager());
                            sender.sendMessage("&aCustom Trigger created!");
                        } catch (ClassNotFoundException e2) {
                            sender.sendMessage("&c" + "Could not save! " + e2.getMessage());
                            sender.sendMessage("&c" + "Provided event name is not valid.");
                        } catch (Exception e) {
                            handleException(sender, e);
                        }
                    }
                }
                return true;
            } else if (args.length > 0 && (args[0].equalsIgnoreCase("repeat") || args[0].equalsIgnoreCase("r"))) {
                if (args.length == 2) {
                    String name = args[1];
                    Trigger trigger = getRepeatManager().getTrigger(name);
                    if (trigger != null) {
                        getScriptEditManager().startEdit(sender, trigger.getTriggerName(), trigger.getScript(), new SaveHandler() {

                            @Override
                            public void onSave(String script) {
                                try {
                                    trigger.setScript(script);
                                } catch (Exception e) {
                                    handleException(sender, e);
                                }
                                sender.sendMessage("&aScript is updated!");
                                saveAsynchronously(getRepeatManager());
                            }
                        });
                    } else {
                        this.getScriptEditManager().startEdit(sender, "Repeating Trigger", "", new SaveHandler() {

                            @Override
                            public void onSave(String script) {
                                try {
                                    getRepeatManager().createTrigger(name, script);
                                } catch (Exception e) {
                                    handleException(sender, e);
                                }
                                saveAsynchronously(getRepeatManager());
                            }
                        });
                    }
                } else if (args.length == 4 && args[2].equalsIgnoreCase("interval")) {
                    String name = args[1];
                    RepeatingTrigger trigger = getRepeatManager().getTrigger(name);
                    if (trigger == null) {
                        sender.sendMessage("&7No Repeating Trigger with name " + name);
                        return true;
                    }
                    String intervalValue = args[3];
                    long interval = TimeUtil.parseTime(intervalValue);
                    trigger.setInterval(interval);
                    saveAsynchronously(getRepeatManager());
                    sender.sendMessage("&aNow " + "&6[" + name + "]" + "&a will run every " + "&6[" + TimeUtil.milliSecondsToString(interval) + "]");
                } else if (args.length == 3 && args[2].equalsIgnoreCase("autostart")) {
                    String name = args[1];
                    RepeatingTrigger trigger = getRepeatManager().getTrigger(name);
                    if (trigger == null) {
                        sender.sendMessage("&7No Repeating Trigger with name " + name);
                        return true;
                    }
                    trigger.setAutoStart(!trigger.isAutoStart());
                    saveAsynchronously(getRepeatManager());
                    sender.sendMessage("Auto start: " + (trigger.isAutoStart() ? "&a" : "&c") + trigger.isAutoStart());
                } else if (args.length == 3 && args[2].equalsIgnoreCase("toggle")) {
                    String name = args[1];
                    RepeatingTrigger trigger = getRepeatManager().getTrigger(name);
                    if (trigger == null) {
                        sender.sendMessage("&7No Repeating Trigger with name " + name);
                        return true;
                    }
                    if (getRepeatManager().isRunning(name)) {
                        getRepeatManager().stopTrigger(name);
                        sender.sendMessage("&aScheduled stop. It may take some time depends on CPU usage.");
                    } else {
                        getRepeatManager().startTrigger(name);
                        sender.sendMessage("&aScheduled start up. It may take some time depends on CPU usage.");
                    }
                } else if (args.length == 3 && args[2].equalsIgnoreCase("pause")) {
                    String name = args[1];
                    RepeatingTrigger trigger = getRepeatManager().getTrigger(name);
                    if (trigger == null) {
                        sender.sendMessage("&7No Repeating Trigger with name " + name);
                        return true;
                    }
                    trigger.setPaused(!trigger.isPaused());
                    sender.sendMessage("Paused: " + (trigger.isPaused() ? "&a" : "&c") + trigger.isPaused());
                } else if (args.length == 3 && args[2].equalsIgnoreCase("status")) {
                    String name = args[1];
                    RepeatingTrigger trigger = getRepeatManager().getTrigger(name);
                    if (trigger == null) {
                        sender.sendMessage("&7No Repeating Trigger with name " + name);
                        return true;
                    }
                    getRepeatManager().showTriggerInfo(sender, trigger);
                } else if (args.length == 3 && args[2].equalsIgnoreCase("delete")) {
                    String name = args[1];
                    RepeatingTrigger trigger = getRepeatManager().getTrigger(name);
                    if (trigger == null) {
                        sender.sendMessage("&7No Repeating Trigger with name " + name);
                        return true;
                    }
                    getRepeatManager().deleteTrigger(name);
                } else {
                    sendCommandDesc(sender, "/triggerreactor[trg] repeat[r] <name>", "Create Repeating Trigger.");
                    sendDetails(sender, "&4Quick create is not supported.");
                    sendDetails(sender, "This creates a Repeating Trigger with default settings. You probably will want to change default values" + " using other commands below. Also, creating Repeating Trigger doesn't start it automatically.");
                    sendCommandDesc(sender, "/triggerreactor[trg] repeat[r] <name> interval <time format>", "Change the interval of this trigger.");
                    sendDetails(sender, "Notice the <time format> is not just a number but has specific format for it. For example, you first" + " type what number you want to set and also define the unit of it. If you want it to repeat it every 1 hour, 20 minutes," + " and 50seconds, then it will be &6" + "/trg r BlahBlah interval 1h20m50s." + "&7 Currently only h, m," + " and s are supported for this format. Also notice that if you have two numbers with same format, they will add up as well. For example," + "&6 /trg r BlahBlah interval 30s40s" + "&7 will be added up to 70seconds total. All units other than" + " h, m, or s will be ignored.");
                    sendCommandDesc(sender, "/triggerreactor[trg] repeat[r] <name> autostart", "Enable/Disable automatic start for this trigger.");
                    sendDetails(sender, "By setting this to " + "&atrue" + "&7, this trigger will start on plugin enables itself. " + "Otherwise, you have to start it yourself every time.");
                    sendCommandDesc(sender, "/triggerreactor[trg] repeat[r] <name> toggle", "Start or stop the Repeating Trigger.");
                    sendCommandDesc(sender, "/triggerreactor[trg] repeat[r] <name> pause", "Pause or unpause the Repeating Trigger.");
                    sendCommandDesc(sender, "/triggerreactor[trg] repeat[r] <name> status", "See brief information about this trigger.");
                    sendCommandDesc(sender, "/triggerreactor[trg] repeat[r] <name> delete", "Delete repeating trigger.");
                }
                return true;
            } else if (args.length == 2 && (args[0].equalsIgnoreCase("synccustom") || args[0].equalsIgnoreCase("sync"))) {
                String name = args[1];
                CustomTrigger trigger = getCustomManager().getTriggerForName(name);
                if (trigger == null) {
                    sender.sendMessage("&7No Custom Trigger found with that name.");
                    return true;
                }
                trigger.setSync(!trigger.isSync());
                saveAsynchronously(getCustomManager());
                sender.sendMessage("&7Sync mode: " + (trigger.isSync() ? "&a" : "&c") + trigger.isSync());
                return true;
            } else if (args.length == 3 && (args[0].equalsIgnoreCase("delete") || args[0].equalsIgnoreCase("del"))) {
                String key = args[2];
                switch(args[1]) {
                    case "vars":
                    case "variables":
                        getVariableManager().remove(key);
                        sender.sendMessage("&aRemoved the variable &6" + key);
                        break;
                    case "cmd":
                    case "command":
                        if (getCmdManager().removeCommandTrigger(key)) {
                            sender.sendMessage("&aRemoved the command trigger &6" + key);
                            saveAsynchronously(getCmdManager());
                        } else {
                            sender.sendMessage("&7Command trigger &6" + key + "&7 does not exist");
                        }
                        break;
                    case "custom":
                        if (getCustomManager().removeTriggerForName(key)) {
                            sender.sendMessage("&aRemoved the custom trigger &6" + key);
                            saveAsynchronously(getCustomManager());
                        } else {
                            sender.sendMessage("&7Custom Trigger &6" + key + "&7 does not exist");
                        }
                        break;
                    default:
                        sender.sendMessage("Ex) /trg del vars player.count");
                        sender.sendMessage("List: variables[vars], command[cmd], custom");
                        break;
                }
                return true;
            } else if (args[0].equalsIgnoreCase("search")) {
                SimpleChunkLocation scloc = ((IPlayer) sender).getChunk();
                showGlowStones(sender, getClickManager().getTriggersInChunk(scloc));
                showGlowStones(sender, getWalkManager().getTriggersInChunk(scloc));
                sender.sendMessage("&7Now trigger blocks will be shown as &6" + "glowstone");
                return true;
            } else if (args[0].equalsIgnoreCase("saveall")) {
                for (Manager manager : Manager.getManagers()) manager.saveAll();
                sender.sendMessage("Save complete!");
                return true;
            } else if (args[0].equalsIgnoreCase("reload")) {
                for (Manager manager : Manager.getManagers()) manager.reload();
                getExecutorManager().reload();
                sender.sendMessage("Reload Complete!");
                return true;
            }
        }
        showHelp(sender);
    }
    return true;
}
Also used : IPlayer(io.github.wysohn.triggerreactor.core.bridge.player.IPlayer) IInventory(io.github.wysohn.triggerreactor.core.bridge.IInventory) Set(java.util.Set) HashMap(java.util.HashMap) AreaTrigger(io.github.wysohn.triggerreactor.core.manager.trigger.AbstractAreaTriggerManager.AreaTrigger) SimpleChunkLocation(io.github.wysohn.triggerreactor.core.manager.location.SimpleChunkLocation) AbstractAreaTriggerManager(io.github.wysohn.triggerreactor.core.manager.trigger.AbstractAreaTriggerManager) AbstractRepeatingTriggerManager(io.github.wysohn.triggerreactor.core.manager.trigger.AbstractRepeatingTriggerManager) AbstractCommandTriggerManager(io.github.wysohn.triggerreactor.core.manager.trigger.AbstractCommandTriggerManager) AbstractExecutorManager(io.github.wysohn.triggerreactor.core.manager.AbstractExecutorManager) AbstractPlaceholderManager(io.github.wysohn.triggerreactor.core.manager.AbstractPlaceholderManager) AbstractScriptEditManager(io.github.wysohn.triggerreactor.core.manager.AbstractScriptEditManager) AbstractPlayerLocationManager(io.github.wysohn.triggerreactor.core.manager.AbstractPlayerLocationManager) AbstractAreaSelectionManager(io.github.wysohn.triggerreactor.core.manager.AbstractAreaSelectionManager) AbstractNamedTriggerManager(io.github.wysohn.triggerreactor.core.manager.trigger.AbstractNamedTriggerManager) Manager(io.github.wysohn.triggerreactor.core.manager.Manager) AbstractCustomTriggerManager(io.github.wysohn.triggerreactor.core.manager.trigger.AbstractCustomTriggerManager) AbstractLocationBasedTriggerManager(io.github.wysohn.triggerreactor.core.manager.trigger.AbstractLocationBasedTriggerManager) AbstractPermissionManager(io.github.wysohn.triggerreactor.core.manager.AbstractPermissionManager) AbstractVariableManager(io.github.wysohn.triggerreactor.core.manager.AbstractVariableManager) AbstractInventoryTriggerManager(io.github.wysohn.triggerreactor.core.manager.trigger.AbstractInventoryTriggerManager) CustomTrigger(io.github.wysohn.triggerreactor.core.manager.trigger.AbstractCustomTriggerManager.CustomTrigger) RepeatingTrigger(io.github.wysohn.triggerreactor.core.manager.trigger.AbstractRepeatingTriggerManager.RepeatingTrigger) SaveHandler(io.github.wysohn.triggerreactor.tools.ScriptEditor.SaveHandler) InventoryTrigger(io.github.wysohn.triggerreactor.core.manager.trigger.AbstractInventoryTriggerManager.InventoryTrigger) Area(io.github.wysohn.triggerreactor.core.manager.location.Area) AreaTrigger(io.github.wysohn.triggerreactor.core.manager.trigger.AbstractAreaTriggerManager.AreaTrigger) Trigger(io.github.wysohn.triggerreactor.core.manager.trigger.AbstractTriggerManager.Trigger) CustomTrigger(io.github.wysohn.triggerreactor.core.manager.trigger.AbstractCustomTriggerManager.CustomTrigger) InventoryTrigger(io.github.wysohn.triggerreactor.core.manager.trigger.AbstractInventoryTriggerManager.InventoryTrigger) RepeatingTrigger(io.github.wysohn.triggerreactor.core.manager.trigger.AbstractRepeatingTriggerManager.RepeatingTrigger) ILocation(io.github.wysohn.triggerreactor.core.bridge.ILocation) IItemStack(io.github.wysohn.triggerreactor.core.bridge.IItemStack)

Aggregations

IInventory (io.github.wysohn.triggerreactor.core.bridge.IInventory)1 IItemStack (io.github.wysohn.triggerreactor.core.bridge.IItemStack)1 ILocation (io.github.wysohn.triggerreactor.core.bridge.ILocation)1 IPlayer (io.github.wysohn.triggerreactor.core.bridge.player.IPlayer)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 AbstractPlaceholderManager (io.github.wysohn.triggerreactor.core.manager.AbstractPlaceholderManager)1 AbstractPlayerLocationManager (io.github.wysohn.triggerreactor.core.manager.AbstractPlayerLocationManager)1 AbstractScriptEditManager (io.github.wysohn.triggerreactor.core.manager.AbstractScriptEditManager)1 AbstractVariableManager (io.github.wysohn.triggerreactor.core.manager.AbstractVariableManager)1 Manager (io.github.wysohn.triggerreactor.core.manager.Manager)1 Area (io.github.wysohn.triggerreactor.core.manager.location.Area)1 SimpleChunkLocation (io.github.wysohn.triggerreactor.core.manager.location.SimpleChunkLocation)1 AbstractAreaTriggerManager (io.github.wysohn.triggerreactor.core.manager.trigger.AbstractAreaTriggerManager)1 AreaTrigger (io.github.wysohn.triggerreactor.core.manager.trigger.AbstractAreaTriggerManager.AreaTrigger)1 AbstractCommandTriggerManager (io.github.wysohn.triggerreactor.core.manager.trigger.AbstractCommandTriggerManager)1 AbstractCustomTriggerManager (io.github.wysohn.triggerreactor.core.manager.trigger.AbstractCustomTriggerManager)1 CustomTrigger (io.github.wysohn.triggerreactor.core.manager.trigger.AbstractCustomTriggerManager.CustomTrigger)1 AbstractInventoryTriggerManager (io.github.wysohn.triggerreactor.core.manager.trigger.AbstractInventoryTriggerManager)1