use of io.github.wysohn.triggerreactor.core.bridge.ICommandSender in project TriggerReactor by wysohn.
the class TriggerReactorCore method onCommand.
public boolean onCommand(ICommandSender sender, String command, String[] args) {
if (command.equalsIgnoreCase("triggerreactor")) {
if (!sender.hasPermission(PERMISSION))
return true;
if (!isEnabled()) {
sender.sendMessage("&cTriggerReactor is disabled. Check your latest.log to see why the plugin is not" + " loaded properly. If there was an error while loading, please report it through github issue" + " or our discord channel.");
return true;
}
if (args.length > 0) {
if (args[0].equalsIgnoreCase("debug")) {
debugging = !debugging;
String color;
if (debugging) {
color = "a";
} else {
color = "c";
}
sender.sendMessage("Debugging is set to &" + color + debugging);
getLogger().info("Debugging state: " + debugging);
return true;
} else if (args[0].equalsIgnoreCase("version")) {
sender.sendMessage("Current version: " + getVersion());
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]).append(" ");
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]).append(" ");
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 (args.length == 3 && getCmdManager().has(args[1]) && args[2].equals("sync")) {
Trigger trigger = getCmdManager().get(args[1]);
trigger.setSync(!trigger.isSync());
sender.sendMessage("&7Sync mode: " + (trigger.isSync() ? "&a" : "&c") + trigger.isSync());
saveAsynchronously(getCmdManager());
} else if (args.length > 2 && getCmdManager().has(args[1]) && (args[2].equals("p") || args[2].equals("permission"))) {
CommandTrigger trigger = getCmdManager().get(args[1]);
// if no permission is given, delete all permission required
String[] permissions = null;
if (args.length == 3) {
trigger.setPermissions(null);
} else {
permissions = new String[args.length - 3];
for (int i = 3; i < args.length; i++) {
permissions[i - 3] = args[i];
}
trigger.setPermissions(permissions);
}
if (permissions == null) {
sender.sendMessage("&7Cleared permissions");
} else {
sender.sendMessage("&7Set permissions.");
}
saveAsynchronously(getCmdManager());
} else if (args.length > 2 && getCmdManager().has(args[1]) && (args[2].equals("a") || args[2].equals("aliases"))) {
CommandTrigger trigger = getCmdManager().get(args[1]);
// if no aliases are given, delete all aliases
String[] aliases = null;
if (args.length == 3) {
trigger.setAliases(null);
} else {
aliases = new String[args.length - 3];
for (int i = 3; i < args.length; i++) {
aliases[i - 3] = args[i];
}
trigger.setAliases(aliases);
}
if (aliases == null) {
sender.sendMessage("&7Cleared aliases");
} else {
sender.sendMessage("&7Set Aliases");
}
saveAsynchronously(getCmdManager());
getCmdManager().reregisterCommand(args[1]);
} else if (args.length > 2 && getCmdManager().has(args[1]) && (args[2].equals("tab") || args[2].equals("settab"))) {
TriggerInfo info = Optional.of(getCmdManager()).map(man -> man.get(args[1])).map(Trigger::getInfo).orElseThrow(() -> new RuntimeException("Missing TriggerInfo"));
// trg cmd <name> settab a b c
List<Map<String, Object>> tabs = new ArrayList<>();
for (int i = 3; i < args.length; i++) {
String[] split = args[i].split(":", 2);
String hint = split.length == 2 ? split[0] : null;
String values = split.length == 2 ? split[1] : split[0];
Map<String, Object> tab = new LinkedHashMap<>();
if (hint != null)
tab.put(AbstractCommandTriggerManager.HINT, hint);
tab.put(AbstractCommandTriggerManager.CANDIDATES, values);
tabs.add(tab);
}
info.getConfig().put(AbstractCommandTriggerManager.TABS, tabs);
getCmdManager().reload(args[1]);
sender.sendMessage("&7Set tab-completer");
} else if (getCmdManager().has(args[1])) {
Trigger trigger = getCmdManager().get(args[1]);
getScriptEditManager().startEdit(sender, trigger.getInfo().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 (!GlobalVariableManager.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 (!GlobalVariableManager.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 (!GlobalVariableManager.isValidName(name)) {
sender.sendMessage("&c" + name + " is not a valid key!");
return true;
}
if (INTEGER_PATTERN.matcher(value).matches()) {
try {
getVariableManager().put(name, Integer.parseInt(value));
} catch (Exception e) {
this.handleException(sender, e);
}
} else if (DECIMAL_PATTERN.matcher(value).matches()) {
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.length > 2 && args[0].equalsIgnoreCase("sudo")) {
String playerName = args[1];
String script = mergeArguments(args, 2, args.length - 1);
IPlayer targetPlayer = getPlayer(playerName);
if (targetPlayer == null) {
sender.sendMessage("&cNo such player named &6" + playerName + "&c!");
return true;
}
try {
Trigger trigger = getCmdManager().createTempCommandTrigger(script);
trigger.activate(createEmptyPlayerEvent(targetPlayer), new HashMap<>());
} catch (Exception e) {
handleException(sender, e);
}
return true;
} else if (args.length > 1 && args[0].equalsIgnoreCase("call")) {
String namedTriggerName = args[1];
String script = args.length > 2 ? mergeArguments(args, 2, args.length - 1) : "";
try {
Trigger trigger = getCmdManager().createTempCommandTrigger(script);
Trigger targetTrigger = getNamedTriggerManager().get(namedTriggerName);
if (targetTrigger == null) {
sender.sendMessage("&cCannot find &6" + namedTriggerName + "&c! &7Remember that the folder" + " hierarchy is represented with ':' sign. (ex. FolderA:FolderB:Trigger)");
return true;
}
// shares same variable space
Map<String, Object> variables = new HashMap<>();
trigger.activate(createEmptyPlayerEvent(sender), variables);
targetTrigger.activate(createEmptyPlayerEvent(sender), variables);
} 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 (size > 54 || size < 9) {
sender.sendMessage("&csize must be between 9 and 54");
return true;
}
if (size % 9 != 0) {
sender.sendMessage("&csize must be a multiple of 9");
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().remove(name) != null) {
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;
try {
index = Integer.parseInt(args[3]);
} catch (NumberFormatException e) {
sender.sendMessage("&c" + "" + args[3] + " is not a valid number.");
return true;
}
InventoryTrigger trigger = getInvManager().get(name);
if (trigger == null) {
sender.sendMessage("&7No such Inventory Trigger named " + name);
return true;
}
if (index > trigger.getItems().length || index < 1) {
sender.sendMessage("&c" + "" + index + " is out of bounds. (Size: " + (trigger.getItems().length) + ")");
return true;
}
trigger.getItems()[index - 1] = IS;
saveAsynchronously(getInvManager());
sender.sendMessage("Successfully set item " + index);
} 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().get(name);
if (trigger == null) {
sender.sendMessage("&7No such Inventory Trigger named " + name);
return true;
}
getScriptEditManager().startEdit(sender, trigger.getInfo().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 if (args.length == 4 && args[2].equals("row")) {
IItemStack IS = ((IPlayer) sender).getItemInMainHand();
IS = IS == null ? null : IS.clone();
String name = args[1];
int index;
try {
index = Integer.parseInt(args[3]);
} catch (NumberFormatException e) {
sender.sendMessage("&c" + "" + args[3] + " is not a valid number.");
return true;
}
InventoryTrigger trigger = getInvManager().get(name);
if (trigger == null) {
sender.sendMessage("&7No such Inventory Trigger named " + name);
return true;
}
int rows = trigger.getItems().length / 9;
if (index > rows || index < 1) {
sender.sendMessage("&c" + "" + index + " is out of bounds. (Maximum: " + rows + ")");
return true;
}
for (int i = 0; i < 9; i++) {
trigger.getItems()[(index - 1) * 9 + i] = IS;
}
saveAsynchronously(getInvManager());
sender.sendMessage("Successfully filled row " + index);
} else if (args.length == 4 && args[2].equals("column")) {
IItemStack IS = ((IPlayer) sender).getItemInMainHand();
IS = IS == null ? null : IS.clone();
String name = args[1];
int index;
try {
index = Integer.parseInt(args[3]);
} catch (NumberFormatException e) {
sender.sendMessage("&c" + "" + args[3] + " is not a valid number.");
return true;
}
InventoryTrigger trigger = getInvManager().get(name);
if (trigger == null) {
sender.sendMessage("&7No such Inventory Trigger named " + name);
return true;
}
int rows = trigger.getItems().length / 9;
if (index > 9 || index < 1) {
sender.sendMessage("&c" + "" + index + " is out of bounds. (Maximum: 9)");
return true;
}
for (int i = 0; i < rows; i++) {
trigger.getItems()[index - 1 + i * 9] = IS;
}
saveAsynchronously(getInvManager());
sender.sendMessage("Successfully filled column " + index);
} else if (args.length == 3 && args[2].equalsIgnoreCase("edititems")) {
String name = args[1];
InventoryTrigger trigger = getInvManager().get(name);
if (trigger == null) {
sender.sendMessage("&7No such Inventory Trigger named " + name);
return true;
}
getInvEditManager().startEdit((IPlayer) sender, trigger);
return true;
} 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>", "sets 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> column <index>", "same as the item subcommand, but applied to an entire column." + "Clears the slot if you are holding nothing.");
sendCommandDesc(sender, "/triggerreactor[trg] inventory[i] <inventory name> row <index>", "same as the item subcommand, but applied to an entire row.");
sendCommandDesc(sender, "/triggerreactor[trg] inventory[i] <inventory name> open", "Preview the inventory");
sendCommandDesc(sender, "/triggerreactor[trg] inventory[i] <inventory name> open <player name>", "Send <player name> a preview of the inventory");
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 - 1, lore)) {
sender.sendMessage("&c" + "" + index + " is out of bounds.");
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 - 1)) {
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];
// validate the name
if (!NAME_PATTERN.matcher(name).matches()) {
sender.sendMessage("&cThe name " + name + " has not allowed character!");
sender.sendMessage("&7Use only character, number, and underscore(_).");
return true;
}
AreaTrigger trigger = getAreaManager().get(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, selected::equals);
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("&aCreated area trigger: " + name);
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().remove(name) != null) {
sender.sendMessage("&aArea Trigger deleted");
saveAsynchronously(getAreaManager());
getSelectionManager().resetSelections(((IPlayer) sender).getUniqueId());
} else {
sender.sendMessage("&7Area Trigger " + name + " does not exist.");
}
} else if (args.length > 2 && args[2].equals("enter")) {
String name = args[1];
AreaTrigger trigger = getAreaManager().get(name);
if (trigger == null) {
sender.sendMessage("&7No Area Trigger found with that name.");
return true;
}
if (trigger.getEnterTrigger() != null) {
getScriptEditManager().startEdit(sender, trigger.getInfo().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().get(name);
if (trigger == null) {
sender.sendMessage("&7No Area Trigger found with that name.");
return true;
}
if (trigger.getExitTrigger() != null) {
getScriptEditManager().startEdit(sender, trigger.getInfo().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().get(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().get(name);
if (trigger != null) {
getScriptEditManager().startEdit(sender, trigger.getInfo().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().get(name);
if (trigger != null) {
getScriptEditManager().startEdit(sender, trigger.getInfo().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().get(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().get(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().get(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().get(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().get(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().get(name);
if (trigger == null) {
sender.sendMessage("&7No Repeating Trigger with name " + name);
return true;
}
getRepeatManager().remove(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," + " 50seconds, and 10ticks, then it will be &6" + "/trg r BlahBlah interval 1h20m50s10t." + "&7 Currently only h, m," + " s, and t 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, s, or t 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().get(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().remove(key) != null) {
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().remove(key) != null) {
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("list")) {
sender.sendMessage("- - - - - Result - - - - ");
for (Manager manager : Manager.getManagers()) {
if (!(manager instanceof AbstractTriggerManager<?>))
continue;
for (String val : ((AbstractTriggerManager<? extends Trigger>) manager).getTriggerList((name) -> {
for (int i = 1; i < args.length; i++) {
if (!name.contains(args[i]))
return false;
}
return true;
})) {
sender.sendMessage("&d" + val);
}
}
sender.sendMessage(" ");
return true;
} else if (args[0].equalsIgnoreCase("timings")) {
if (args.length == 2 && args[1].equalsIgnoreCase("toggle")) {
Timings.on = !Timings.on;
if (Timings.on) {
sender.sendMessage("&aEnabled");
} else {
sender.sendMessage("&cDisabled");
}
} else if (args.length == 2 && args[1].equalsIgnoreCase("reset")) {
Timings.reset();
sender.sendMessage("&aReset Complete.");
} else if (args.length > 1 && args[1].equalsIgnoreCase("print")) {
OutputStream os;
if (args.length > 2) {
String fileName = args[2];
File folder = new File(getDataFolder(), "timings");
if (!folder.exists())
folder.mkdirs();
File file = new File(folder, fileName + ".timings");
if (file.exists())
file.delete();
try {
file.createNewFile();
os = new FileOutputStream(file);
} catch (IOException ex) {
ex.printStackTrace();
sender.sendMessage("&cCould not create log file. Check console for details.");
return true;
}
} else {
os = new SenderOutputStream(sender);
}
try {
Timings.printAll(os);
} catch (IOException ex) {
ex.printStackTrace();
}
} else {
sendCommandDesc(sender, "/triggerreactor[trg] timings toggle", "turn on/off timings analysis. Also analysis will be reset.");
sendCommandDesc(sender, "/triggerreactor[trg] timings reset", "turn on/off timings analysis. Also analysis will be reset.");
sendCommandDesc(sender, "/triggerreactor[trg] timings print", "Show analysis result.");
sendCommandDesc(sender, "/triggerreactor[trg] timings print xx", "Save analysis to file named xx.timings");
}
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();
getPlaceholderManager().reload();
sender.sendMessage("Reload Complete!");
return true;
} else if (args[0].equalsIgnoreCase("help")) {
int page = 0;
if (args.length > 1) {
if (!args[1].matches("[0-9]+")) {
sender.sendMessage("&c" + args[1] + " is not a valid page number.");
return true;
}
page = Integer.parseInt(args[1]);
}
showHelp(sender, page);
return true;
} else if (args[0].equalsIgnoreCase("links")) {
if (args.length < 2) {
return true;
}
AbstractInventoryEditManager manager = getInvEditManager();
IPlayer player = (IPlayer) sender;
switch(args[1]) {
case "inveditsave":
manager.saveEdit(player);
return true;
case "inveditcontinue":
manager.continueEdit(player);
return true;
case "inveditdiscard":
manager.discardEdit(player);
return true;
}
}
}
showHelp(sender);
}
return true;
}
use of io.github.wysohn.triggerreactor.core.bridge.ICommandSender in project TriggerReactor by wysohn.
the class CommandTriggerManager method registerCommand.
@Override
protected boolean registerCommand(String triggerName, CommandTrigger trigger) {
String[] commandArr = new String[1 + trigger.getAliases().length];
commandArr[0] = triggerName;
for (int i = 1; i < commandArr.length; i++) commandArr[i] = trigger.getAliases()[i - 1];
commandManager.register(plugin.getMain(), new CommandCallable() {
@Override
public CommandResult process(CommandSource source, String arguments) throws CommandException {
if (!(source instanceof Player)) {
source.sendMessage(Text.of("CommandTrigger works only for Players."));
return CommandResult.success();
}
ICommandSender commandSender = plugin.getPlayer(source.getName());
String[] args = arguments.split(" ");
execute(plugin.createPlayerCommandEvent(commandSender, triggerName, args), (Player) source, triggerName, args, trigger);
return CommandResult.success();
}
@Override
public List<String> getSuggestions(CommandSource source, String arguments, @Nullable Location<World> targetPosition) throws CommandException {
String[] args;
if (arguments.length() > 0) {
args = arguments.split(" ");
} else {
args = new String[0];
}
ITabCompleter tabCompleter = Optional.ofNullable(trigger.getTabCompleters()).filter(iTabCompleters -> iTabCompleters.length >= args.length).map(iTabCompleters -> iTabCompleters[args.length - 1]).orElse(ITabCompleter.EMPTY);
String partial = args[args.length - 1];
if (partial.length() < 1) {
// show hint if nothing is entered yet
return tabCompleter.getHint();
} else {
return tabCompleter.getCandidates(partial);
}
}
@Override
public boolean testPermission(CommandSource source) {
for (String permission : trigger.getPermissions()) {
if (!source.hasPermission(permission))
return false;
}
return true;
}
@Override
public Optional<Text> getShortDescription(CommandSource source) {
return Optional.empty();
}
@Override
public Optional<Text> getHelp(CommandSource source) {
return Optional.empty();
}
@Override
public Text getUsage(CommandSource source) {
return null;
}
}, commandArr).ifPresent(commandMapping -> mappings.put(triggerName, commandMapping));
return false;
}
use of io.github.wysohn.triggerreactor.core.bridge.ICommandSender in project TriggerReactor by wysohn.
the class CommandTriggerManager method registerCommand.
@Override
protected boolean registerCommand(String triggerName, CommandTrigger trigger) {
if (commandMap.containsKey(triggerName) && overridens.containsKey(triggerName))
return false;
PluginCommand command = createCommand(plugin, triggerName);
command.setAliases(Arrays.stream(trigger.getAliases()).collect(Collectors.toList()));
command.setTabCompleter((sender, command12, alias, args) -> {
ITabCompleter tabCompleter = Optional.ofNullable(trigger.getTabCompleters()).filter(iTabCompleters -> iTabCompleters.length >= args.length).map(iTabCompleters -> iTabCompleters[args.length - 1]).orElse(ITabCompleter.EMPTY);
String partial = args[args.length - 1];
if (partial.length() < 1) {
// show hint if nothing is entered yet
return tabCompleter.getHint();
} else {
return tabCompleter.getCandidates(partial);
}
});
command.setExecutor((sender, command1, label, args) -> {
if (!(sender instanceof Player)) {
sender.sendMessage("CommandTrigger works only for Players.");
return true;
}
ICommandSender commandSender = plugin.getPlayer(sender.getName());
execute(plugin.createPlayerCommandEvent(commandSender, label, args), (Player) sender, triggerName, args, trigger);
return true;
});
Optional.ofNullable(commandMap.get(triggerName)).ifPresent(c -> overridens.put(triggerName, c));
commandMap.put(triggerName, command);
// register aliases manually here
for (String alias : trigger.getAliases()) {
Optional.ofNullable(commandMap.get(alias)).ifPresent(c -> overridens.put(alias, c));
commandMap.put(alias, command);
}
return true;
}
use of io.github.wysohn.triggerreactor.core.bridge.ICommandSender in project TriggerReactor by wysohn.
the class TriggerReactorCore method handleException.
/**
* Handle the exception caused by Executors or Triggers. The 'e' is the context when the 'event' was
* happened. For Bukkit API, it is child classes of Event. You may extract the player instance who is
* related to this Exception and show useful information to the game.
*
* @param e the context
* @param throwable the exception that was thrown
*/
public final void handleException(Object e, Throwable throwable) {
if (isDebugging()) {
throwable.printStackTrace();
}
ICommandSender sender = extractPlayerFromContext(e);
if (sender == null)
sender = getConsoleSender();
sendExceptionMessage(sender, throwable);
}
Aggregations