diff --git a/bukkit/src/main/java/me/lucko/luckperms/bukkit/migration/MigrationBPermissions.java b/bukkit/src/main/java/me/lucko/luckperms/bukkit/migration/MigrationBPermissions.java index 12c9e2d43..c55911798 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/bukkit/migration/MigrationBPermissions.java +++ b/bukkit/src/main/java/me/lucko/luckperms/bukkit/migration/MigrationBPermissions.java @@ -26,6 +26,7 @@ import de.bananaco.bpermissions.api.*; import me.lucko.luckperms.api.Logger; import me.lucko.luckperms.api.MetaUtils; import me.lucko.luckperms.common.LuckPermsPlugin; +import me.lucko.luckperms.common.commands.CommandException; import me.lucko.luckperms.common.commands.CommandResult; import me.lucko.luckperms.common.commands.Sender; import me.lucko.luckperms.common.commands.SubCommand; @@ -86,7 +87,7 @@ public class MigrationBPermissions extends SubCommand { } @Override - public CommandResult execute(LuckPermsPlugin plugin, Sender sender, Object o, List args, String label) { + public CommandResult execute(LuckPermsPlugin plugin, Sender sender, Object o, List args, String label) throws CommandException { final Logger log = plugin.getLog(); WorldManager worldManager = WorldManager.getInstance(); diff --git a/bukkit/src/main/java/me/lucko/luckperms/bukkit/migration/MigrationGroupManager.java b/bukkit/src/main/java/me/lucko/luckperms/bukkit/migration/MigrationGroupManager.java index cfe5cdda3..9de0b1ea2 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/bukkit/migration/MigrationGroupManager.java +++ b/bukkit/src/main/java/me/lucko/luckperms/bukkit/migration/MigrationGroupManager.java @@ -24,10 +24,7 @@ package me.lucko.luckperms.bukkit.migration; import me.lucko.luckperms.api.Logger; import me.lucko.luckperms.common.LuckPermsPlugin; -import me.lucko.luckperms.common.commands.Arg; -import me.lucko.luckperms.common.commands.CommandResult; -import me.lucko.luckperms.common.commands.Sender; -import me.lucko.luckperms.common.commands.SubCommand; +import me.lucko.luckperms.common.commands.*; import me.lucko.luckperms.common.constants.Constants; import me.lucko.luckperms.common.constants.Permission; import me.lucko.luckperms.common.core.NodeFactory; @@ -53,7 +50,7 @@ public class MigrationGroupManager extends SubCommand { } @Override - public CommandResult execute(LuckPermsPlugin plugin, Sender sender, Object o, List args, String label) { + public CommandResult execute(LuckPermsPlugin plugin, Sender sender, Object o, List args, String label) throws CommandException { final Logger log = plugin.getLog(); if (!plugin.isPluginLoaded("GroupManager")) { log.severe("GroupManager Migration: Error -> GroupManager is not loaded."); diff --git a/bukkit/src/main/java/me/lucko/luckperms/bukkit/migration/MigrationPermissionsEx.java b/bukkit/src/main/java/me/lucko/luckperms/bukkit/migration/MigrationPermissionsEx.java index 5b92ed17f..ade21dfe2 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/bukkit/migration/MigrationPermissionsEx.java +++ b/bukkit/src/main/java/me/lucko/luckperms/bukkit/migration/MigrationPermissionsEx.java @@ -26,10 +26,7 @@ import me.lucko.luckperms.api.Logger; import me.lucko.luckperms.api.MetaUtils; import me.lucko.luckperms.api.PlatformType; import me.lucko.luckperms.common.LuckPermsPlugin; -import me.lucko.luckperms.common.commands.Arg; -import me.lucko.luckperms.common.commands.CommandResult; -import me.lucko.luckperms.common.commands.Sender; -import me.lucko.luckperms.common.commands.SubCommand; +import me.lucko.luckperms.common.commands.*; import me.lucko.luckperms.common.constants.Constants; import me.lucko.luckperms.common.constants.Permission; import me.lucko.luckperms.common.data.LogEntry; @@ -57,7 +54,7 @@ public class MigrationPermissionsEx extends SubCommand { @SuppressWarnings("deprecation") @Override - public CommandResult execute(LuckPermsPlugin plugin, Sender sender, Object o, List args, String label) { + public CommandResult execute(LuckPermsPlugin plugin, Sender sender, Object o, List args, String label) throws CommandException { final Logger log = plugin.getLog(); if (!plugin.isPluginLoaded("PermissionsEx")) { log.severe("PermissionsEx Migration: Error -> PermissionsEx is not loaded."); diff --git a/bukkit/src/main/java/me/lucko/luckperms/bukkit/migration/MigrationPowerfulPerms.java b/bukkit/src/main/java/me/lucko/luckperms/bukkit/migration/MigrationPowerfulPerms.java index 270cd2070..b6b2c07ee 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/bukkit/migration/MigrationPowerfulPerms.java +++ b/bukkit/src/main/java/me/lucko/luckperms/bukkit/migration/MigrationPowerfulPerms.java @@ -30,10 +30,7 @@ import me.lucko.luckperms.api.Logger; import me.lucko.luckperms.api.data.Callback; import me.lucko.luckperms.bukkit.migration.utils.LPResultRunnable; import me.lucko.luckperms.common.LuckPermsPlugin; -import me.lucko.luckperms.common.commands.Arg; -import me.lucko.luckperms.common.commands.CommandResult; -import me.lucko.luckperms.common.commands.Sender; -import me.lucko.luckperms.common.commands.SubCommand; +import me.lucko.luckperms.common.commands.*; import me.lucko.luckperms.common.constants.Constants; import me.lucko.luckperms.common.core.PermissionHolder; import me.lucko.luckperms.common.data.LogEntry; @@ -132,7 +129,7 @@ public class MigrationPowerfulPerms extends SubCommand { } @Override - public CommandResult execute(LuckPermsPlugin plugin, Sender sender, Object o, List args, String label) { + public CommandResult execute(LuckPermsPlugin plugin, Sender sender, Object o, List args, String label) throws CommandException { try { return run(plugin, args); } catch (Throwable t) { diff --git a/bukkit/src/main/java/me/lucko/luckperms/bukkit/migration/MigrationZPermissions.java b/bukkit/src/main/java/me/lucko/luckperms/bukkit/migration/MigrationZPermissions.java index f5d52cae7..c9e3805d7 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/bukkit/migration/MigrationZPermissions.java +++ b/bukkit/src/main/java/me/lucko/luckperms/bukkit/migration/MigrationZPermissions.java @@ -25,10 +25,7 @@ package me.lucko.luckperms.bukkit.migration; import me.lucko.luckperms.api.Logger; import me.lucko.luckperms.api.MetaUtils; import me.lucko.luckperms.common.LuckPermsPlugin; -import me.lucko.luckperms.common.commands.Arg; -import me.lucko.luckperms.common.commands.CommandResult; -import me.lucko.luckperms.common.commands.Sender; -import me.lucko.luckperms.common.commands.SubCommand; +import me.lucko.luckperms.common.commands.*; import me.lucko.luckperms.common.constants.Constants; import me.lucko.luckperms.common.constants.Permission; import me.lucko.luckperms.common.data.LogEntry; @@ -52,7 +49,7 @@ public class MigrationZPermissions extends SubCommand { } @Override - public CommandResult execute(LuckPermsPlugin plugin, Sender sender, Object o, List args, String label) { + public CommandResult execute(LuckPermsPlugin plugin, Sender sender, Object o, List args, String label) throws CommandException { final Logger log = plugin.getLog(); if (!plugin.isPluginLoaded("zPermissions")) { log.severe("zPermissions Migration: Error -> zPermissions is not loaded."); diff --git a/bungee/src/main/java/me/lucko/luckperms/bungee/migration/MigrationBungeePerms.java b/bungee/src/main/java/me/lucko/luckperms/bungee/migration/MigrationBungeePerms.java index 8a8f731cd..445baf681 100644 --- a/bungee/src/main/java/me/lucko/luckperms/bungee/migration/MigrationBungeePerms.java +++ b/bungee/src/main/java/me/lucko/luckperms/bungee/migration/MigrationBungeePerms.java @@ -25,6 +25,7 @@ package me.lucko.luckperms.bungee.migration; import me.lucko.luckperms.api.Logger; import me.lucko.luckperms.api.MetaUtils; import me.lucko.luckperms.common.LuckPermsPlugin; +import me.lucko.luckperms.common.commands.CommandException; import me.lucko.luckperms.common.commands.CommandResult; import me.lucko.luckperms.common.commands.Sender; import me.lucko.luckperms.common.commands.SubCommand; @@ -47,7 +48,7 @@ public class MigrationBungeePerms extends SubCommand { } @Override - public CommandResult execute(LuckPermsPlugin plugin, Sender sender, Object o, List args, String label) { + public CommandResult execute(LuckPermsPlugin plugin, Sender sender, Object o, List args, String label) throws CommandException { final Logger log = plugin.getLog(); BungeePerms bp = BungeePerms.getInstance(); diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/ArgumentUtils.java b/common/src/main/java/me/lucko/luckperms/common/commands/ArgumentUtils.java new file mode 100644 index 000000000..4e072dc69 --- /dev/null +++ b/common/src/main/java/me/lucko/luckperms/common/commands/ArgumentUtils.java @@ -0,0 +1,132 @@ +/* + * Copyright (c) 2016 Lucko (Luck) + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package me.lucko.luckperms.common.commands; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import me.lucko.luckperms.common.utils.ArgumentChecker; +import me.lucko.luckperms.common.utils.DateUtil; + +import java.util.List; +import java.util.function.Function; + +/** + * Utility class to help process arguments, and throw checked exceptions if the arguments are invalid. + */ +public class ArgumentUtils { + public static final Function WRAPPER = s -> s.contains(" ") ? "\"" + s + "\"" : s; + + public static String handleNodeWithoutCheck(int index, List args) { + return args.get(index).replace("{SPACE}", " "); + } + + public static String handleNode(int index, List args) throws ArgumentException { + String node = args.get(index).replace("{SPACE}", " "); + if (ArgumentChecker.checkNode(node)) { + throw new DetailedUsageException(); + } + + if (node.toLowerCase().startsWith("group.")) { + throw new UseInheritException(); + } + + return node; + } + + public static String handleName(int index, List args) throws ArgumentException { + String groupName = args.get(index).toLowerCase(); + if (ArgumentChecker.checkName(groupName)) { + throw new DetailedUsageException(); + } + return groupName; + } + + public static boolean handleBoolean(int index, List args) throws ArgumentException { + String bool = args.get(index); + if (!bool.equalsIgnoreCase("true") && !bool.equalsIgnoreCase("false")) { + throw new DetailedUsageException(); + } + return Boolean.parseBoolean(bool); + } + + public static String handleServer(int index, List args) throws ArgumentException { + if (args.size() > index) { + final String server = args.get(index).toLowerCase(); + if (ArgumentChecker.checkServer(server)) { + throw new InvalidServerException(); + } + return server; + } + return null; + } + + public static long handleDuration(int index, List args) throws ArgumentException { + long duration; + try { + duration = Long.parseLong(args.get(index)); + } catch (NumberFormatException e) { + try { + duration = DateUtil.parseDateDiff(args.get(index), true); + } catch (DateUtil.IllegalDateException e1) { + throw new InvalidDateException(args.get(index)); + } + } + + if (DateUtil.shouldExpire(duration)) { + throw new PastDateException(); + } + + return duration; + } + + public static String handleWorld(int index, List args) { + return args.size() > index ? args.get(index).toLowerCase() : null; + } + + public static int handlePriority(int index, List args) throws ArgumentException { + try { + return Integer.parseInt(args.get(index)); + } catch (NumberFormatException e) { + throw new InvalidPriorityException(args.get(index)); + } + } + + public static abstract class ArgumentException extends CommandException {} + public static class DetailedUsageException extends ArgumentException {} + public static class UseInheritException extends ArgumentException {} + public static class InvalidServerException extends ArgumentException {} + public static class PastDateException extends ArgumentException {} + + @Getter + @AllArgsConstructor + public static class InvalidDateException extends ArgumentException { + private final String invalidDate; + } + + @Getter + @AllArgsConstructor + public static class InvalidPriorityException extends ArgumentException { + private final String invalidPriority; + } + +} diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/CommandException.java b/common/src/main/java/me/lucko/luckperms/common/commands/CommandException.java new file mode 100644 index 000000000..3c7774015 --- /dev/null +++ b/common/src/main/java/me/lucko/luckperms/common/commands/CommandException.java @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2016 Lucko (Luck) + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package me.lucko.luckperms.common.commands; + +/** + * Exception to be thrown if there is an error processing a command + */ +public class CommandException extends Exception { + +} diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/ContextHelper.java b/common/src/main/java/me/lucko/luckperms/common/commands/ContextHelper.java new file mode 100644 index 000000000..8b93b4a91 --- /dev/null +++ b/common/src/main/java/me/lucko/luckperms/common/commands/ContextHelper.java @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2016 Lucko (Luck) + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package me.lucko.luckperms.common.commands; + +/** + * Utility used to help in commands that take arguments for different contexts + */ +public class ContextHelper { + + public enum CommandContext { + NONE, SERVER, SERVER_AND_WORLD + } + + public static CommandContext determine(String server, String world) { + return server == null ? CommandContext.NONE : (world == null ? CommandContext.SERVER : CommandContext.SERVER_AND_WORLD); + } + +} diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/MainCommand.java b/common/src/main/java/me/lucko/luckperms/common/commands/MainCommand.java index e5a06132b..4be2822cc 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/MainCommand.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/MainCommand.java @@ -105,7 +105,12 @@ public abstract class MainCommand { final String name = args.get(0).toLowerCase(); T t = getTarget(name, plugin, sender); if (t != null) { - CommandResult result = sub.execute(plugin, sender, t, strippedArgs, label); + CommandResult result; + try { + result = sub.execute(plugin, sender, t, strippedArgs, label); + } catch (CommandException e) { + result = handleException(e, sender, sub); + } cleanup(t, plugin); return result; } @@ -113,6 +118,44 @@ public abstract class MainCommand { return CommandResult.LOADING_ERROR; } + private static CommandResult handleException(CommandException e, Sender sender, SubCommand command) { + if (e instanceof ArgumentUtils.ArgumentException) { + if (e instanceof ArgumentUtils.DetailedUsageException) { + command.sendDetailedUsage(sender); + return CommandResult.INVALID_ARGS; + } + + if (e instanceof ArgumentUtils.UseInheritException) { + Message.USE_INHERIT_COMMAND.send(sender); + return CommandResult.INVALID_ARGS; + } + + if (e instanceof ArgumentUtils.InvalidServerException) { + Message.SERVER_INVALID_ENTRY.send(sender); + return CommandResult.INVALID_ARGS; + } + + if (e instanceof ArgumentUtils.PastDateException) { + Message.PAST_DATE_ERROR.send(sender); + return CommandResult.INVALID_ARGS; + } + + if (e instanceof ArgumentUtils.InvalidDateException) { + Message.ILLEGAL_DATE_ERROR.send(sender, ((ArgumentUtils.InvalidDateException) e).getInvalidDate()); + return CommandResult.INVALID_ARGS; + } + + if (e instanceof ArgumentUtils.InvalidPriorityException) { + Message.META_INVALID_PRIORITY.send(sender, ((ArgumentUtils.InvalidPriorityException) e).getInvalidPriority()); + return CommandResult.INVALID_ARGS; + } + } + + // Not something we can catch. + e.printStackTrace(); + return CommandResult.FAILURE; + } + /** * Gets the object the command is acting upon, and runs the callback if successful * @param target the name of the object to be looked up diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/SubCommand.java b/common/src/main/java/me/lucko/luckperms/common/commands/SubCommand.java index 0fe1b9f0f..854f38719 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/SubCommand.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/SubCommand.java @@ -76,7 +76,7 @@ public abstract class SubCommand { * @param args the stripped arguments given * @param label the command label used */ - public abstract CommandResult execute(LuckPermsPlugin plugin, Sender sender, T t, List args, String label); + public abstract CommandResult execute(LuckPermsPlugin plugin, Sender sender, T t, List args, String label) throws CommandException; /** * Returns a list of suggestions, which are empty by default. Sub classes that give tab complete suggestions override diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/generic/SecondaryMainCommand.java b/common/src/main/java/me/lucko/luckperms/common/commands/generic/SecondaryMainCommand.java index 70fbb0f58..8038cf6de 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/generic/SecondaryMainCommand.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/generic/SecondaryMainCommand.java @@ -23,10 +23,7 @@ package me.lucko.luckperms.common.commands.generic; import me.lucko.luckperms.common.LuckPermsPlugin; -import me.lucko.luckperms.common.commands.CommandResult; -import me.lucko.luckperms.common.commands.Sender; -import me.lucko.luckperms.common.commands.SubCommand; -import me.lucko.luckperms.common.commands.Util; +import me.lucko.luckperms.common.commands.*; import me.lucko.luckperms.common.constants.Message; import me.lucko.luckperms.common.core.PermissionHolder; import me.lucko.luckperms.common.utils.Predicates; @@ -48,7 +45,7 @@ public class SecondaryMainCommand extends SubCommand } @Override - public CommandResult execute(LuckPermsPlugin plugin, Sender sender, T t, List args, String label) { + public CommandResult execute(LuckPermsPlugin plugin, Sender sender, T t, List args, String label) throws CommandException { if (args.size() == 0) { sendUsageDetailed(sender, user, label); return CommandResult.INVALID_ARGS; @@ -80,7 +77,51 @@ public class SecondaryMainCommand extends SubCommand return CommandResult.INVALID_ARGS; } - return sub.execute(plugin, sender, t, strippedArgs); + CommandResult result; + try { + result = sub.execute(plugin, sender, t, strippedArgs); + } catch (CommandException e) { + result = handleException(e, sender, sub); + } + return result; + } + + private static CommandResult handleException(CommandException e, Sender sender, SecondarySubCommand command) { + if (e instanceof ArgumentUtils.ArgumentException) { + if (e instanceof ArgumentUtils.DetailedUsageException) { + command.sendDetailedUsage(sender); + return CommandResult.INVALID_ARGS; + } + + if (e instanceof ArgumentUtils.UseInheritException) { + Message.USE_INHERIT_COMMAND.send(sender); + return CommandResult.INVALID_ARGS; + } + + if (e instanceof ArgumentUtils.InvalidServerException) { + Message.SERVER_INVALID_ENTRY.send(sender); + return CommandResult.INVALID_ARGS; + } + + if (e instanceof ArgumentUtils.PastDateException) { + Message.PAST_DATE_ERROR.send(sender); + return CommandResult.INVALID_ARGS; + } + + if (e instanceof ArgumentUtils.InvalidDateException) { + Message.ILLEGAL_DATE_ERROR.send(sender, ((ArgumentUtils.InvalidDateException) e).getInvalidDate()); + return CommandResult.INVALID_ARGS; + } + + if (e instanceof ArgumentUtils.InvalidPriorityException) { + Message.META_INVALID_PRIORITY.send(sender, ((ArgumentUtils.InvalidPriorityException) e).getInvalidPriority()); + return CommandResult.INVALID_ARGS; + } + } + + // Not something we can catch. + e.printStackTrace(); + return CommandResult.FAILURE; } @Override diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/generic/SecondarySubCommand.java b/common/src/main/java/me/lucko/luckperms/common/commands/generic/SecondarySubCommand.java index 96319e459..c167209d0 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/generic/SecondarySubCommand.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/generic/SecondarySubCommand.java @@ -63,7 +63,7 @@ public abstract class SecondarySubCommand { private final ImmutableList args; - public abstract CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args); + public abstract CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args) throws CommandException; public List onTabComplete(LuckPermsPlugin plugin, Sender sender, List args) { return Collections.emptyList(); diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/generic/meta/MetaAddPrefix.java b/common/src/main/java/me/lucko/luckperms/common/commands/generic/meta/MetaAddPrefix.java index f438627c8..e76a74b39 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/generic/meta/MetaAddPrefix.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/generic/meta/MetaAddPrefix.java @@ -24,23 +24,22 @@ package me.lucko.luckperms.common.commands.generic.meta; import me.lucko.luckperms.api.MetaUtils; import me.lucko.luckperms.common.LuckPermsPlugin; -import me.lucko.luckperms.common.commands.Arg; -import me.lucko.luckperms.common.commands.CommandResult; -import me.lucko.luckperms.common.commands.Sender; +import me.lucko.luckperms.common.commands.*; import me.lucko.luckperms.common.commands.generic.SecondarySubCommand; import me.lucko.luckperms.common.constants.Message; import me.lucko.luckperms.common.constants.Permission; import me.lucko.luckperms.common.core.PermissionHolder; import me.lucko.luckperms.common.data.LogEntry; -import me.lucko.luckperms.common.utils.ArgumentChecker; import me.lucko.luckperms.common.utils.Predicates; import me.lucko.luckperms.exceptions.ObjectAlreadyHasException; import java.util.List; +import java.util.stream.Collectors; public class MetaAddPrefix extends SecondarySubCommand { public MetaAddPrefix() { - super("addprefix", "Adds a prefix", Permission.USER_META_ADDPREFIX, Permission.GROUP_META_ADDPREFIX, Predicates.notInRange(2, 4), + super("addprefix", "Adds a prefix", Permission.USER_META_ADDPREFIX, Permission.GROUP_META_ADDPREFIX, + Predicates.notInRange(2, 4), Arg.list( Arg.create("priority", true, "the priority to add the prefix at"), Arg.create("prefix", true, "the prefix string"), @@ -51,51 +50,37 @@ public class MetaAddPrefix extends SecondarySubCommand { } @Override - public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args) { - final String prefix = args.get(1).replace("{SPACE}", " "); - int priority; - try { - priority = Integer.parseInt(args.get(0)); - } catch (NumberFormatException e) { - Message.META_INVALID_PRIORITY.send(sender, args.get(0)); - return CommandResult.INVALID_ARGS; - } + public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args) throws CommandException { + int priority = ArgumentUtils.handlePriority(0, args); + String prefix = ArgumentUtils.handleNodeWithoutCheck(1, args); + String server = ArgumentUtils.handleServer(2, args); + String world = ArgumentUtils.handleWorld(3, args); final String node = "prefix." + priority + "." + MetaUtils.escapeCharacters(prefix); try { - if (args.size() >= 3) { - final String server = args.get(2).toLowerCase(); - if (ArgumentChecker.checkServer(server)) { - Message.SERVER_INVALID_ENTRY.send(sender); - return CommandResult.INVALID_ARGS; - } - - if (args.size() == 3) { + switch (ContextHelper.determine(server, world)) { + case NONE: + holder.setPermission(node, true); + Message.ADDPREFIX_SUCCESS.send(sender, holder.getFriendlyName(), prefix, priority); + break; + case SERVER: holder.setPermission(node, true, server); Message.ADDPREFIX_SERVER_SUCCESS.send(sender, holder.getFriendlyName(), prefix, priority, server); - LogEntry.build().actor(sender).acted(holder) - .action("meta addprefix " + priority + " " + args.get(1) + " " + server) - .build().submit(plugin, sender); - } else { - final String world = args.get(3).toLowerCase(); + break; + case SERVER_AND_WORLD: holder.setPermission(node, true, server, world); Message.ADDPREFIX_SERVER_WORLD_SUCCESS.send(sender, holder.getFriendlyName(), prefix, priority, server, world); - LogEntry.build().actor(sender).acted(holder) - .action("meta addprefix " + priority + " " + args.get(1) + " " + server + " " + world) - .build().submit(plugin, sender); - } - - } else { - holder.setPermission(node, true); - Message.ADDPREFIX_SUCCESS.send(sender, holder.getFriendlyName(), prefix, priority); - LogEntry.build().actor(sender).acted(holder) - .action("meta addprefix " + priority + " " + args.get(1)) - .build().submit(plugin, sender); + break; } + LogEntry.build().actor(sender).acted(holder) + .action("meta addprefix " + args.stream().map(ArgumentUtils.WRAPPER).collect(Collectors.joining(" "))) + .build().submit(plugin, sender); + save(holder, sender, plugin); return CommandResult.SUCCESS; + } catch (ObjectAlreadyHasException e) { Message.ALREADY_HAS_PREFIX.send(sender, holder.getFriendlyName()); return CommandResult.STATE_ERROR; diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/generic/meta/MetaAddSuffix.java b/common/src/main/java/me/lucko/luckperms/common/commands/generic/meta/MetaAddSuffix.java index 6d44089a4..9af77e213 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/generic/meta/MetaAddSuffix.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/generic/meta/MetaAddSuffix.java @@ -24,23 +24,22 @@ package me.lucko.luckperms.common.commands.generic.meta; import me.lucko.luckperms.api.MetaUtils; import me.lucko.luckperms.common.LuckPermsPlugin; -import me.lucko.luckperms.common.commands.Arg; -import me.lucko.luckperms.common.commands.CommandResult; -import me.lucko.luckperms.common.commands.Sender; +import me.lucko.luckperms.common.commands.*; import me.lucko.luckperms.common.commands.generic.SecondarySubCommand; import me.lucko.luckperms.common.constants.Message; import me.lucko.luckperms.common.constants.Permission; import me.lucko.luckperms.common.core.PermissionHolder; import me.lucko.luckperms.common.data.LogEntry; -import me.lucko.luckperms.common.utils.ArgumentChecker; import me.lucko.luckperms.common.utils.Predicates; import me.lucko.luckperms.exceptions.ObjectAlreadyHasException; import java.util.List; +import java.util.stream.Collectors; public class MetaAddSuffix extends SecondarySubCommand { public MetaAddSuffix() { - super("addsuffix", "Adds a suffix", Permission.USER_META_ADDSUFFIX, Permission.GROUP_META_ADDSUFFIX, Predicates.notInRange(2, 4), + super("addsuffix", "Adds a suffix", Permission.USER_META_ADDSUFFIX, Permission.GROUP_META_ADDSUFFIX, + Predicates.notInRange(2, 4), Arg.list( Arg.create("priority", true, "the priority to add the suffix at"), Arg.create("suffix", true, "the suffix string"), @@ -51,51 +50,37 @@ public class MetaAddSuffix extends SecondarySubCommand { } @Override - public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args) { - final String suffix = args.get(1).replace("{SPACE}", " "); - int priority; - try { - priority = Integer.parseInt(args.get(0)); - } catch (NumberFormatException e) { - Message.META_INVALID_PRIORITY.send(sender, args.get(0)); - return CommandResult.INVALID_ARGS; - } + public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args) throws CommandException { + int priority = ArgumentUtils.handlePriority(0, args); + String suffix = ArgumentUtils.handleNodeWithoutCheck(1, args); + String server = ArgumentUtils.handleServer(2, args); + String world = ArgumentUtils.handleWorld(3, args); final String node = "suffix." + priority + "." + MetaUtils.escapeCharacters(suffix); try { - if (args.size() >= 3) { - final String server = args.get(2).toLowerCase(); - if (ArgumentChecker.checkServer(server)) { - Message.SERVER_INVALID_ENTRY.send(sender); - return CommandResult.INVALID_ARGS; - } - - if (args.size() == 3) { + switch (ContextHelper.determine(server, world)) { + case NONE: + holder.setPermission(node, true); + Message.ADDSUFFIX_SUCCESS.send(sender, holder.getFriendlyName(), suffix, priority); + break; + case SERVER: holder.setPermission(node, true, server); Message.ADDSUFFIX_SERVER_SUCCESS.send(sender, holder.getFriendlyName(), suffix, priority, server); - LogEntry.build().actor(sender).acted(holder) - .action("meta addsuffix " + priority + " " + args.get(1) + " " + server) - .build().submit(plugin, sender); - } else { - final String world = args.get(3).toLowerCase(); + break; + case SERVER_AND_WORLD: holder.setPermission(node, true, server, world); Message.ADDSUFFIX_SERVER_WORLD_SUCCESS.send(sender, holder.getFriendlyName(), suffix, priority, server, world); - LogEntry.build().actor(sender).acted(holder) - .action("meta addsuffix " + priority + " " + args.get(1) + " " + server + " " + world) - .build().submit(plugin, sender); - } - - } else { - holder.setPermission(node, true); - Message.ADDSUFFIX_SUCCESS.send(sender, holder.getFriendlyName(), suffix, priority); - LogEntry.build().actor(sender).acted(holder) - .action("meta addsuffix " + priority + " " + args.get(1)) - .build().submit(plugin, sender); + break; } + LogEntry.build().actor(sender).acted(holder) + .action("meta addsuffix " + args.stream().map(ArgumentUtils.WRAPPER).collect(Collectors.joining(" "))) + .build().submit(plugin, sender); + save(holder, sender, plugin); return CommandResult.SUCCESS; + } catch (ObjectAlreadyHasException e) { Message.ALREADY_HAS_SUFFIX.send(sender, holder.getFriendlyName()); return CommandResult.STATE_ERROR; diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/generic/meta/MetaAddTempPrefix.java b/common/src/main/java/me/lucko/luckperms/common/commands/generic/meta/MetaAddTempPrefix.java index 75be96a7e..40f04910f 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/generic/meta/MetaAddTempPrefix.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/generic/meta/MetaAddTempPrefix.java @@ -24,24 +24,23 @@ package me.lucko.luckperms.common.commands.generic.meta; import me.lucko.luckperms.api.MetaUtils; import me.lucko.luckperms.common.LuckPermsPlugin; -import me.lucko.luckperms.common.commands.Arg; -import me.lucko.luckperms.common.commands.CommandResult; -import me.lucko.luckperms.common.commands.Sender; +import me.lucko.luckperms.common.commands.*; import me.lucko.luckperms.common.commands.generic.SecondarySubCommand; import me.lucko.luckperms.common.constants.Message; import me.lucko.luckperms.common.constants.Permission; import me.lucko.luckperms.common.core.PermissionHolder; import me.lucko.luckperms.common.data.LogEntry; -import me.lucko.luckperms.common.utils.ArgumentChecker; import me.lucko.luckperms.common.utils.DateUtil; import me.lucko.luckperms.common.utils.Predicates; import me.lucko.luckperms.exceptions.ObjectAlreadyHasException; import java.util.List; +import java.util.stream.Collectors; public class MetaAddTempPrefix extends SecondarySubCommand { public MetaAddTempPrefix() { - super("addtempprefix", "Adds a prefix temporarily", Permission.USER_META_ADDTEMP_PREFIX, Permission.GROUP_META_ADDTEMP_PREFIX, Predicates.notInRange(3, 5), + super("addtempprefix", "Adds a prefix temporarily", Permission.USER_META_ADDTEMP_PREFIX, + Permission.GROUP_META_ADDTEMP_PREFIX, Predicates.notInRange(3, 5), Arg.list( Arg.create("priority", true, "the priority to add the prefix at"), Arg.create("prefix", true, "the prefix string"), @@ -53,68 +52,44 @@ public class MetaAddTempPrefix extends SecondarySubCommand { } @Override - public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args) { - final String prefix = args.get(1).replace("{SPACE}", " "); - int priority; - try { - priority = Integer.parseInt(args.get(0)); - } catch (NumberFormatException e) { - Message.META_INVALID_PRIORITY.send(sender, args.get(0)); - return CommandResult.INVALID_ARGS; - } - - long duration; - try { - duration = Long.parseLong(args.get(2)); - } catch (NumberFormatException e) { - try { - duration = DateUtil.parseDateDiff(args.get(2), true); - } catch (DateUtil.IllegalDateException e1) { - Message.ILLEGAL_DATE_ERROR.send(sender, args.get(2)); - return CommandResult.INVALID_ARGS; - } - } - - if (DateUtil.shouldExpire(duration)) { - Message.PAST_DATE_ERROR.send(sender); - return CommandResult.INVALID_ARGS; - } + public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args) throws CommandException { + int priority = ArgumentUtils.handlePriority(0, args); + String prefix = ArgumentUtils.handleNodeWithoutCheck(1, args); + long duration = ArgumentUtils.handleDuration(2, args); + String server = ArgumentUtils.handleServer(3, args); + String world = ArgumentUtils.handleWorld(4, args); final String node = "prefix." + priority + "." + MetaUtils.escapeCharacters(prefix); try { - if (args.size() >= 4) { - final String server = args.get(3).toLowerCase(); - if (ArgumentChecker.checkServer(server)) { - Message.SERVER_INVALID_ENTRY.send(sender); - return CommandResult.INVALID_ARGS; - } - - if (args.size() == 4) { + switch (ContextHelper.determine(server, world)) { + case NONE: + holder.setPermission(node, true, duration); + Message.ADD_TEMP_PREFIX_SUCCESS.send(sender, holder.getFriendlyName(), prefix, priority, + DateUtil.formatDateDiff(duration) + ); + break; + case SERVER: holder.setPermission(node, true, server, duration); - Message.ADD_TEMP_PREFIX_SERVER_SUCCESS.send(sender, holder.getFriendlyName(), prefix, priority, server, DateUtil.formatDateDiff(duration)); - LogEntry.build().actor(sender).acted(holder) - .action("meta addtempprefix " + priority + " " + args.get(1) + " " + duration + " " + server) - .build().submit(plugin, sender); - } else { - final String world = args.get(4).toLowerCase(); + Message.ADD_TEMP_PREFIX_SERVER_SUCCESS.send(sender, holder.getFriendlyName(), prefix, priority, + server, DateUtil.formatDateDiff(duration) + ); + break; + case SERVER_AND_WORLD: holder.setPermission(node, true, server, world, duration); - Message.ADD_TEMP_PREFIX_SERVER_WORLD_SUCCESS.send(sender, holder.getFriendlyName(), prefix, priority, server, world, DateUtil.formatDateDiff(duration)); - LogEntry.build().actor(sender).acted(holder) - .action("meta addtempprefix " + priority + " " + args.get(1) + " " + duration + " " + server + " " + world) - .build().submit(plugin, sender); - } - - } else { - holder.setPermission(node, true, duration); - Message.ADD_TEMP_PREFIX_SUCCESS.send(sender, holder.getFriendlyName(), prefix, priority, DateUtil.formatDateDiff(duration)); - LogEntry.build().actor(sender).acted(holder) - .action("meta addtempprefix " + priority + " " + args.get(1) + " " + duration) - .build().submit(plugin, sender); + Message.ADD_TEMP_PREFIX_SERVER_WORLD_SUCCESS.send(sender, holder.getFriendlyName(), prefix, priority, + server, world, DateUtil.formatDateDiff(duration) + ); + break; } + LogEntry.build().actor(sender).acted(holder) + .action("meta addtempprefix " + args.stream().map(ArgumentUtils.WRAPPER).collect(Collectors.joining(" "))) + .build().submit(plugin, sender); + save(holder, sender, plugin); return CommandResult.SUCCESS; + } catch (ObjectAlreadyHasException e) { Message.ALREADY_HAS_PREFIX.send(sender, holder.getFriendlyName()); return CommandResult.STATE_ERROR; diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/generic/meta/MetaAddTempSuffix.java b/common/src/main/java/me/lucko/luckperms/common/commands/generic/meta/MetaAddTempSuffix.java index 9faae3492..431b638fb 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/generic/meta/MetaAddTempSuffix.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/generic/meta/MetaAddTempSuffix.java @@ -24,24 +24,23 @@ package me.lucko.luckperms.common.commands.generic.meta; import me.lucko.luckperms.api.MetaUtils; import me.lucko.luckperms.common.LuckPermsPlugin; -import me.lucko.luckperms.common.commands.Arg; -import me.lucko.luckperms.common.commands.CommandResult; -import me.lucko.luckperms.common.commands.Sender; +import me.lucko.luckperms.common.commands.*; import me.lucko.luckperms.common.commands.generic.SecondarySubCommand; import me.lucko.luckperms.common.constants.Message; import me.lucko.luckperms.common.constants.Permission; import me.lucko.luckperms.common.core.PermissionHolder; import me.lucko.luckperms.common.data.LogEntry; -import me.lucko.luckperms.common.utils.ArgumentChecker; import me.lucko.luckperms.common.utils.DateUtil; import me.lucko.luckperms.common.utils.Predicates; import me.lucko.luckperms.exceptions.ObjectAlreadyHasException; import java.util.List; +import java.util.stream.Collectors; public class MetaAddTempSuffix extends SecondarySubCommand { public MetaAddTempSuffix() { - super("addtempsuffix", "Adds a suffix temporarily", Permission.USER_META_ADDTEMP_SUFFIX, Permission.GROUP_META_ADDTEMP_SUFFIX, Predicates.notInRange(3, 5), + super("addtempsuffix", "Adds a suffix temporarily", Permission.USER_META_ADDTEMP_SUFFIX, + Permission.GROUP_META_ADDTEMP_SUFFIX, Predicates.notInRange(3, 5), Arg.list( Arg.create("priority", true, "the priority to add the suffix at"), Arg.create("suffix", true, "the suffix string"), @@ -53,68 +52,44 @@ public class MetaAddTempSuffix extends SecondarySubCommand { } @Override - public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args) { - final String suffix = args.get(1).replace("{SPACE}", " "); - int priority; - try { - priority = Integer.parseInt(args.get(0)); - } catch (NumberFormatException e) { - Message.META_INVALID_PRIORITY.send(sender, args.get(0)); - return CommandResult.INVALID_ARGS; - } - - long duration; - try { - duration = Long.parseLong(args.get(2)); - } catch (NumberFormatException e) { - try { - duration = DateUtil.parseDateDiff(args.get(2), true); - } catch (DateUtil.IllegalDateException e1) { - Message.ILLEGAL_DATE_ERROR.send(sender, args.get(2)); - return CommandResult.INVALID_ARGS; - } - } - - if (DateUtil.shouldExpire(duration)) { - Message.PAST_DATE_ERROR.send(sender); - return CommandResult.INVALID_ARGS; - } + public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args) throws CommandException { + int priority = ArgumentUtils.handlePriority(0, args); + String suffix = ArgumentUtils.handleNodeWithoutCheck(1, args); + long duration = ArgumentUtils.handleDuration(2, args); + String server = ArgumentUtils.handleServer(3, args); + String world = ArgumentUtils.handleWorld(4, args); final String node = "suffix." + priority + "." + MetaUtils.escapeCharacters(suffix); try { - if (args.size() >= 4) { - final String server = args.get(3).toLowerCase(); - if (ArgumentChecker.checkServer(server)) { - Message.SERVER_INVALID_ENTRY.send(sender); - return CommandResult.INVALID_ARGS; - } - - if (args.size() == 4) { + switch (ContextHelper.determine(server, world)) { + case NONE: + holder.setPermission(node, true, duration); + Message.ADD_TEMP_SUFFIX_SUCCESS.send(sender, holder.getFriendlyName(), suffix, priority, + DateUtil.formatDateDiff(duration) + ); + break; + case SERVER: holder.setPermission(node, true, server, duration); - Message.ADD_TEMP_SUFFIX_SERVER_SUCCESS.send(sender, holder.getFriendlyName(), suffix, priority, server, DateUtil.formatDateDiff(duration)); - LogEntry.build().actor(sender).acted(holder) - .action("meta addtempsuffix " + priority + " " + args.get(1) + " " + duration + " " + server) - .build().submit(plugin, sender); - } else { - final String world = args.get(4).toLowerCase(); + Message.ADD_TEMP_SUFFIX_SERVER_SUCCESS.send(sender, holder.getFriendlyName(), suffix, priority, + server, DateUtil.formatDateDiff(duration) + ); + break; + case SERVER_AND_WORLD: holder.setPermission(node, true, server, world, duration); - Message.ADD_TEMP_SUFFIX_SERVER_WORLD_SUCCESS.send(sender, holder.getFriendlyName(), suffix, priority, server, world, DateUtil.formatDateDiff(duration)); - LogEntry.build().actor(sender).acted(holder) - .action("meta addtempsuffix " + priority + " " + args.get(1) + " " + duration + " " + server + " " + world) - .build().submit(plugin, sender); - } - - } else { - holder.setPermission(node, true, duration); - Message.ADD_TEMP_SUFFIX_SUCCESS.send(sender, holder.getFriendlyName(), suffix, priority, DateUtil.formatDateDiff(duration)); - LogEntry.build().actor(sender).acted(holder) - .action("meta addtempsuffix " + priority + " " + args.get(1) + " " + duration) - .build().submit(plugin, sender); + Message.ADD_TEMP_SUFFIX_SERVER_WORLD_SUCCESS.send(sender, holder.getFriendlyName(), suffix, priority, + server, world, DateUtil.formatDateDiff(duration) + ); + break; } + LogEntry.build().actor(sender).acted(holder) + .action("meta addtempsuffix " + args.stream().map(ArgumentUtils.WRAPPER).collect(Collectors.joining(" "))) + .build().submit(plugin, sender); + save(holder, sender, plugin); return CommandResult.SUCCESS; + } catch (ObjectAlreadyHasException e) { Message.ALREADY_HAS_SUFFIX.send(sender, holder.getFriendlyName()); return CommandResult.STATE_ERROR; diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/generic/meta/MetaClear.java b/common/src/main/java/me/lucko/luckperms/common/commands/generic/meta/MetaClear.java index f3c59042c..adb0193b0 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/generic/meta/MetaClear.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/generic/meta/MetaClear.java @@ -23,15 +23,12 @@ package me.lucko.luckperms.common.commands.generic.meta; import me.lucko.luckperms.common.LuckPermsPlugin; -import me.lucko.luckperms.common.commands.Arg; -import me.lucko.luckperms.common.commands.CommandResult; -import me.lucko.luckperms.common.commands.Sender; +import me.lucko.luckperms.common.commands.*; import me.lucko.luckperms.common.commands.generic.SecondarySubCommand; import me.lucko.luckperms.common.constants.Message; import me.lucko.luckperms.common.constants.Permission; import me.lucko.luckperms.common.core.PermissionHolder; import me.lucko.luckperms.common.data.LogEntry; -import me.lucko.luckperms.common.utils.ArgumentChecker; import me.lucko.luckperms.common.utils.Predicates; import java.util.List; @@ -48,25 +45,22 @@ public class MetaClear extends SecondarySubCommand { } @Override - public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args) { + public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args) throws CommandException { int before = holder.getNodes().size(); - if (args.size() == 0) { - holder.clearMeta(); - } else { - final String server = args.get(0); - if (ArgumentChecker.checkServer(server)) { - Message.SERVER_INVALID_ENTRY.send(sender); - return CommandResult.INVALID_ARGS; - } + String server = ArgumentUtils.handleServer(0, args); + String world = ArgumentUtils.handleWorld(1, args); - if (args.size() == 2) { - final String world = args.get(1); - holder.clearMeta(server, world); - - } else { + switch (ContextHelper.determine(server, world)) { + case NONE: + holder.clearMeta(); + break; + case SERVER: holder.clearMeta(server); - } + break; + case SERVER_AND_WORLD: + holder.clearMeta(server, world); + break; } int changed = before - holder.getNodes().size(); @@ -76,7 +70,10 @@ public class MetaClear extends SecondarySubCommand { Message.META_CLEAR_SUCCESS.send(sender, holder.getFriendlyName(), changed); } - LogEntry.build().actor(sender).acted(holder).action("meta clear " + args.stream().collect(Collectors.joining(" "))).build().submit(plugin, sender); + LogEntry.build().actor(sender).acted(holder) + .action("meta clear " + args.stream().map(ArgumentUtils.WRAPPER).collect(Collectors.joining(" "))) + .build().submit(plugin, sender); + save(holder, sender, plugin); return CommandResult.SUCCESS; } diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/generic/meta/MetaInfo.java b/common/src/main/java/me/lucko/luckperms/common/commands/generic/meta/MetaInfo.java index 6150036b1..708354a1c 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/generic/meta/MetaInfo.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/generic/meta/MetaInfo.java @@ -25,6 +25,7 @@ package me.lucko.luckperms.common.commands.generic.meta; import me.lucko.luckperms.api.Contexts; import me.lucko.luckperms.api.Node; import me.lucko.luckperms.common.LuckPermsPlugin; +import me.lucko.luckperms.common.commands.CommandException; import me.lucko.luckperms.common.commands.CommandResult; import me.lucko.luckperms.common.commands.Sender; import me.lucko.luckperms.common.commands.Util; @@ -42,7 +43,7 @@ public class MetaInfo extends SecondarySubCommand { } @Override - public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args) { + public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args) throws CommandException { SortedSet> prefixes = new TreeSet<>(Util.getMetaComparator().reversed()); SortedSet> suffixes = new TreeSet<>(Util.getMetaComparator().reversed()); Set meta = new HashSet<>(); diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/generic/meta/MetaRemovePrefix.java b/common/src/main/java/me/lucko/luckperms/common/commands/generic/meta/MetaRemovePrefix.java index 9cbaee6ed..8eb9b8b39 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/generic/meta/MetaRemovePrefix.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/generic/meta/MetaRemovePrefix.java @@ -25,24 +25,23 @@ package me.lucko.luckperms.common.commands.generic.meta; import me.lucko.luckperms.api.MetaUtils; import me.lucko.luckperms.api.Node; import me.lucko.luckperms.common.LuckPermsPlugin; -import me.lucko.luckperms.common.commands.Arg; -import me.lucko.luckperms.common.commands.CommandResult; -import me.lucko.luckperms.common.commands.Sender; +import me.lucko.luckperms.common.commands.*; import me.lucko.luckperms.common.commands.generic.SecondarySubCommand; import me.lucko.luckperms.common.constants.Message; import me.lucko.luckperms.common.constants.Permission; import me.lucko.luckperms.common.core.PermissionHolder; import me.lucko.luckperms.common.data.LogEntry; -import me.lucko.luckperms.common.utils.ArgumentChecker; import me.lucko.luckperms.common.utils.Predicates; import me.lucko.luckperms.exceptions.ObjectLacksException; import java.util.ArrayList; import java.util.List; +import java.util.stream.Collectors; public class MetaRemovePrefix extends SecondarySubCommand { public MetaRemovePrefix() { - super("removeprefix", "Removes a prefix", Permission.USER_META_REMOVEPREFIX, Permission.GROUP_META_REMOVEPREFIX, Predicates.notInRange(2, 4), + super("removeprefix", "Removes a prefix", Permission.USER_META_REMOVEPREFIX, Permission.GROUP_META_REMOVEPREFIX, + Predicates.notInRange(2, 4), Arg.list( Arg.create("priority", true, "the priority to add the prefix at"), Arg.create("prefix", true, "the prefix string"), @@ -53,32 +52,14 @@ public class MetaRemovePrefix extends SecondarySubCommand { } @Override - public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args) { - final String prefix = args.get(1).replace("{SPACE}", " "); - int priority; - try { - priority = Integer.parseInt(args.get(0)); - } catch (NumberFormatException e) { - Message.META_INVALID_PRIORITY.send(sender, args.get(0)); - return CommandResult.INVALID_ARGS; - } + public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args) throws CommandException { + int priority = ArgumentUtils.handlePriority(0, args); + String prefix = ArgumentUtils.handleNodeWithoutCheck(1, args); + String server = ArgumentUtils.handleServer(2, args); + String world = ArgumentUtils.handleWorld(3, args); + // Handle bulk removal if (prefix.equalsIgnoreCase("null")) { - String server = null; - String world = null; - - if (args.size() >= 3) { - server = args.get(2).toLowerCase(); - if (ArgumentChecker.checkServer(server)) { - Message.SERVER_INVALID_ENTRY.send(sender); - return CommandResult.INVALID_ARGS; - } - - if (args.size() != 3) { - world = args.get(3).toLowerCase(); - } - } - List toRemove = new ArrayList<>(); for (Node node : holder.getNodes()) { if (!node.isPrefix()) continue; @@ -111,48 +92,36 @@ public class MetaRemovePrefix extends SecondarySubCommand { Message.BULK_CHANGE_SUCCESS.send(sender, toRemove.size()); save(holder, sender, plugin); return CommandResult.SUCCESS; + } - } else { + final String node = "prefix." + priority + "." + MetaUtils.escapeCharacters(prefix); - final String node = "prefix." + priority + "." + MetaUtils.escapeCharacters(prefix); - - try { - if (args.size() >= 3) { - final String server = args.get(2).toLowerCase(); - if (ArgumentChecker.checkServer(server)) { - Message.SERVER_INVALID_ENTRY.send(sender); - return CommandResult.INVALID_ARGS; - } - - if (args.size() == 3) { - holder.unsetPermission(node, server); - Message.REMOVEPREFIX_SERVER_SUCCESS.send(sender, holder.getFriendlyName(), prefix, priority, server); - LogEntry.build().actor(sender).acted(holder) - .action("meta removeprefix " + priority + " " + args.get(1) + " " + server) - .build().submit(plugin, sender); - } else { - final String world = args.get(3).toLowerCase(); - holder.unsetPermission(node, server, world); - Message.REMOVEPREFIX_SERVER_WORLD_SUCCESS.send(sender, holder.getFriendlyName(), prefix, priority, server, world); - LogEntry.build().actor(sender).acted(holder) - .action("meta removeprefix " + priority + " " + args.get(1) + " " + server + " " + world) - .build().submit(plugin, sender); - } - - } else { + try { + switch (ContextHelper.determine(server, world)) { + case NONE: holder.unsetPermission(node); Message.REMOVEPREFIX_SUCCESS.send(sender, holder.getFriendlyName(), prefix, priority); - LogEntry.build().actor(sender).acted(holder) - .action("meta removeprefix " + priority + " " + args.get(1)) - .build().submit(plugin, sender); - } - - save(holder, sender, plugin); - return CommandResult.SUCCESS; - } catch (ObjectLacksException e) { - Message.DOES_NOT_HAVE_PREFIX.send(sender, holder.getFriendlyName()); - return CommandResult.STATE_ERROR; + break; + case SERVER: + holder.unsetPermission(node, server); + Message.REMOVEPREFIX_SERVER_SUCCESS.send(sender, holder.getFriendlyName(), prefix, priority, server); + break; + case SERVER_AND_WORLD: + holder.unsetPermission(node, server, world); + Message.REMOVEPREFIX_SERVER_WORLD_SUCCESS.send(sender, holder.getFriendlyName(), prefix, priority, server, world); + break; } + + LogEntry.build().actor(sender).acted(holder) + .action("meta removeprefix " + args.stream().map(ArgumentUtils.WRAPPER).collect(Collectors.joining(" "))) + .build().submit(plugin, sender); + + save(holder, sender, plugin); + return CommandResult.SUCCESS; + + } catch (ObjectLacksException e) { + Message.DOES_NOT_HAVE_PREFIX.send(sender, holder.getFriendlyName()); + return CommandResult.STATE_ERROR; } } } diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/generic/meta/MetaRemoveSuffix.java b/common/src/main/java/me/lucko/luckperms/common/commands/generic/meta/MetaRemoveSuffix.java index b1df3aad5..606d2dedc 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/generic/meta/MetaRemoveSuffix.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/generic/meta/MetaRemoveSuffix.java @@ -25,24 +25,23 @@ package me.lucko.luckperms.common.commands.generic.meta; import me.lucko.luckperms.api.MetaUtils; import me.lucko.luckperms.api.Node; import me.lucko.luckperms.common.LuckPermsPlugin; -import me.lucko.luckperms.common.commands.Arg; -import me.lucko.luckperms.common.commands.CommandResult; -import me.lucko.luckperms.common.commands.Sender; +import me.lucko.luckperms.common.commands.*; import me.lucko.luckperms.common.commands.generic.SecondarySubCommand; import me.lucko.luckperms.common.constants.Message; import me.lucko.luckperms.common.constants.Permission; import me.lucko.luckperms.common.core.PermissionHolder; import me.lucko.luckperms.common.data.LogEntry; -import me.lucko.luckperms.common.utils.ArgumentChecker; import me.lucko.luckperms.common.utils.Predicates; import me.lucko.luckperms.exceptions.ObjectLacksException; import java.util.ArrayList; import java.util.List; +import java.util.stream.Collectors; public class MetaRemoveSuffix extends SecondarySubCommand { public MetaRemoveSuffix() { - super("removesuffix", "Removes a suffix", Permission.USER_META_REMOVESUFFIX, Permission.GROUP_META_REMOVESUFFIX, Predicates.notInRange(2, 4), + super("removesuffix", "Removes a suffix", Permission.USER_META_REMOVESUFFIX, Permission.GROUP_META_REMOVESUFFIX, + Predicates.notInRange(2, 4), Arg.list( Arg.create("priority", true, "the priority to add the suffix at"), Arg.create("suffix", true, "the suffix string"), @@ -53,32 +52,14 @@ public class MetaRemoveSuffix extends SecondarySubCommand { } @Override - public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args) { - final String suffix = args.get(1).replace("{SPACE}", " "); - int priority; - try { - priority = Integer.parseInt(args.get(0)); - } catch (NumberFormatException e) { - Message.META_INVALID_PRIORITY.send(sender, args.get(0)); - return CommandResult.INVALID_ARGS; - } + public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args) throws CommandException { + int priority = ArgumentUtils.handlePriority(0, args); + String suffix = ArgumentUtils.handleNodeWithoutCheck(1, args); + String server = ArgumentUtils.handleServer(2, args); + String world = ArgumentUtils.handleWorld(3, args); + // Handle bulk removal if (suffix.equalsIgnoreCase("null")) { - String server = null; - String world = null; - - if (args.size() >= 3) { - server = args.get(2).toLowerCase(); - if (ArgumentChecker.checkServer(server)) { - Message.SERVER_INVALID_ENTRY.send(sender); - return CommandResult.INVALID_ARGS; - } - - if (args.size() != 3) { - world = args.get(3).toLowerCase(); - } - } - List toRemove = new ArrayList<>(); for (Node node : holder.getNodes()) { if (!node.isSuffix()) continue; @@ -111,48 +92,36 @@ public class MetaRemoveSuffix extends SecondarySubCommand { Message.BULK_CHANGE_SUCCESS.send(sender, toRemove.size()); save(holder, sender, plugin); return CommandResult.SUCCESS; + } - } else { + final String node = "suffix." + priority + "." + MetaUtils.escapeCharacters(suffix); - final String node = "suffix." + priority + "." + MetaUtils.escapeCharacters(suffix); - - try { - if (args.size() >= 3) { - final String server = args.get(2).toLowerCase(); - if (ArgumentChecker.checkServer(server)) { - Message.SERVER_INVALID_ENTRY.send(sender); - return CommandResult.INVALID_ARGS; - } - - if (args.size() == 3) { - holder.unsetPermission(node, server); - Message.REMOVESUFFIX_SERVER_SUCCESS.send(sender, holder.getFriendlyName(), suffix, priority, server); - LogEntry.build().actor(sender).acted(holder) - .action("meta removesuffix " + priority + " " + args.get(1) + " " + server) - .build().submit(plugin, sender); - } else { - final String world = args.get(3).toLowerCase(); - holder.unsetPermission(node, server, world); - Message.REMOVESUFFIX_SERVER_WORLD_SUCCESS.send(sender, holder.getFriendlyName(), suffix, priority, server, world); - LogEntry.build().actor(sender).acted(holder) - .action("meta removesuffix " + priority + " " + args.get(1) + " " + server + " " + world) - .build().submit(plugin, sender); - } - - } else { + try { + switch (ContextHelper.determine(server, world)) { + case NONE: holder.unsetPermission(node); Message.REMOVESUFFIX_SUCCESS.send(sender, holder.getFriendlyName(), suffix, priority); - LogEntry.build().actor(sender).acted(holder) - .action("meta removesuffix " + priority + " " + args.get(1)) - .build().submit(plugin, sender); - } - - save(holder, sender, plugin); - return CommandResult.SUCCESS; - } catch (ObjectLacksException e) { - Message.DOES_NOT_HAVE_SUFFIX.send(sender, holder.getFriendlyName()); - return CommandResult.STATE_ERROR; + break; + case SERVER: + holder.unsetPermission(node, server); + Message.REMOVESUFFIX_SERVER_SUCCESS.send(sender, holder.getFriendlyName(), suffix, priority, server); + break; + case SERVER_AND_WORLD: + holder.unsetPermission(node, server, world); + Message.REMOVESUFFIX_SERVER_WORLD_SUCCESS.send(sender, holder.getFriendlyName(), suffix, priority, server, world); + break; } + + LogEntry.build().actor(sender).acted(holder) + .action("meta removesuffix " + args.stream().map(ArgumentUtils.WRAPPER).collect(Collectors.joining(" "))) + .build().submit(plugin, sender); + + save(holder, sender, plugin); + return CommandResult.SUCCESS; + + } catch (ObjectLacksException e) { + Message.DOES_NOT_HAVE_SUFFIX.send(sender, holder.getFriendlyName()); + return CommandResult.STATE_ERROR; } } } diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/generic/meta/MetaRemoveTempPrefix.java b/common/src/main/java/me/lucko/luckperms/common/commands/generic/meta/MetaRemoveTempPrefix.java index 5461b1aa2..92b1a5434 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/generic/meta/MetaRemoveTempPrefix.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/generic/meta/MetaRemoveTempPrefix.java @@ -25,20 +25,18 @@ package me.lucko.luckperms.common.commands.generic.meta; import me.lucko.luckperms.api.MetaUtils; import me.lucko.luckperms.api.Node; import me.lucko.luckperms.common.LuckPermsPlugin; -import me.lucko.luckperms.common.commands.Arg; -import me.lucko.luckperms.common.commands.CommandResult; -import me.lucko.luckperms.common.commands.Sender; +import me.lucko.luckperms.common.commands.*; import me.lucko.luckperms.common.commands.generic.SecondarySubCommand; import me.lucko.luckperms.common.constants.Message; import me.lucko.luckperms.common.constants.Permission; import me.lucko.luckperms.common.core.PermissionHolder; import me.lucko.luckperms.common.data.LogEntry; -import me.lucko.luckperms.common.utils.ArgumentChecker; import me.lucko.luckperms.common.utils.Predicates; import me.lucko.luckperms.exceptions.ObjectLacksException; import java.util.ArrayList; import java.util.List; +import java.util.stream.Collectors; public class MetaRemoveTempPrefix extends SecondarySubCommand { public MetaRemoveTempPrefix() { @@ -53,32 +51,14 @@ public class MetaRemoveTempPrefix extends SecondarySubCommand { } @Override - public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args) { - final String prefix = args.get(1).replace("{SPACE}", " "); - int priority; - try { - priority = Integer.parseInt(args.get(0)); - } catch (NumberFormatException e) { - Message.META_INVALID_PRIORITY.send(sender, args.get(0)); - return CommandResult.INVALID_ARGS; - } + public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args) throws CommandException { + int priority = ArgumentUtils.handlePriority(0, args); + String prefix = ArgumentUtils.handleNodeWithoutCheck(1, args); + String server = ArgumentUtils.handleServer(2, args); + String world = ArgumentUtils.handleWorld(3, args); + // Handle bulk removal if (prefix.equalsIgnoreCase("null")) { - String server = null; - String world = null; - - if (args.size() >= 3) { - server = args.get(2).toLowerCase(); - if (ArgumentChecker.checkServer(server)) { - Message.SERVER_INVALID_ENTRY.send(sender); - return CommandResult.INVALID_ARGS; - } - - if (args.size() != 3) { - world = args.get(3).toLowerCase(); - } - } - List toRemove = new ArrayList<>(); for (Node node : holder.getNodes()) { if (!node.isPrefix()) continue; @@ -111,48 +91,36 @@ public class MetaRemoveTempPrefix extends SecondarySubCommand { Message.BULK_CHANGE_SUCCESS.send(sender, toRemove.size()); save(holder, sender, plugin); return CommandResult.SUCCESS; + } - } else { + final String node = "prefix." + priority + "." + MetaUtils.escapeCharacters(prefix); - final String node = "prefix." + priority + "." + MetaUtils.escapeCharacters(prefix); - - try { - if (args.size() >= 3) { - final String server = args.get(2).toLowerCase(); - if (ArgumentChecker.checkServer(server)) { - Message.SERVER_INVALID_ENTRY.send(sender); - return CommandResult.INVALID_ARGS; - } - - if (args.size() == 3) { - holder.unsetPermission(node, server, true); - Message.REMOVE_TEMP_PREFIX_SERVER_SUCCESS.send(sender, holder.getFriendlyName(), prefix, priority, server); - LogEntry.build().actor(sender).acted(holder) - .action("meta removetempprefix " + priority + " " + args.get(1) + " " + server) - .build().submit(plugin, sender); - } else { - final String world = args.get(3).toLowerCase(); - holder.unsetPermission(node, server, world, true); - Message.REMOVE_TEMP_PREFIX_SERVER_WORLD_SUCCESS.send(sender, holder.getFriendlyName(), prefix, priority, server, world); - LogEntry.build().actor(sender).acted(holder) - .action("meta removetempprefix " + priority + " " + args.get(1) + " " + server + " " + world) - .build().submit(plugin, sender); - } - - } else { + try { + switch (ContextHelper.determine(server, world)) { + case NONE: holder.unsetPermission(node, true); Message.REMOVE_TEMP_PREFIX_SUCCESS.send(sender, holder.getFriendlyName(), prefix, priority); - LogEntry.build().actor(sender).acted(holder) - .action("meta removetempprefix " + priority + " " + args.get(1)) - .build().submit(plugin, sender); - } - - save(holder, sender, plugin); - return CommandResult.SUCCESS; - } catch (ObjectLacksException e) { - Message.DOES_NOT_HAVE_PREFIX.send(sender, holder.getFriendlyName()); - return CommandResult.STATE_ERROR; + break; + case SERVER: + holder.unsetPermission(node, server, true); + Message.REMOVE_TEMP_PREFIX_SERVER_SUCCESS.send(sender, holder.getFriendlyName(), prefix, priority, server); + break; + case SERVER_AND_WORLD: + holder.unsetPermission(node, server, world, true); + Message.REMOVE_TEMP_PREFIX_SERVER_WORLD_SUCCESS.send(sender, holder.getFriendlyName(), prefix, priority, server, world); + break; } + + LogEntry.build().actor(sender).acted(holder) + .action("meta removetempprefix " + args.stream().map(ArgumentUtils.WRAPPER).collect(Collectors.joining(" "))) + .build().submit(plugin, sender); + + save(holder, sender, plugin); + return CommandResult.SUCCESS; + + } catch (ObjectLacksException e) { + Message.DOES_NOT_HAVE_PREFIX.send(sender, holder.getFriendlyName()); + return CommandResult.STATE_ERROR; } } } diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/generic/meta/MetaRemoveTempSuffix.java b/common/src/main/java/me/lucko/luckperms/common/commands/generic/meta/MetaRemoveTempSuffix.java index 7fcfa2bb9..1708dbfe4 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/generic/meta/MetaRemoveTempSuffix.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/generic/meta/MetaRemoveTempSuffix.java @@ -25,20 +25,18 @@ package me.lucko.luckperms.common.commands.generic.meta; import me.lucko.luckperms.api.MetaUtils; import me.lucko.luckperms.api.Node; import me.lucko.luckperms.common.LuckPermsPlugin; -import me.lucko.luckperms.common.commands.Arg; -import me.lucko.luckperms.common.commands.CommandResult; -import me.lucko.luckperms.common.commands.Sender; +import me.lucko.luckperms.common.commands.*; import me.lucko.luckperms.common.commands.generic.SecondarySubCommand; import me.lucko.luckperms.common.constants.Message; import me.lucko.luckperms.common.constants.Permission; import me.lucko.luckperms.common.core.PermissionHolder; import me.lucko.luckperms.common.data.LogEntry; -import me.lucko.luckperms.common.utils.ArgumentChecker; import me.lucko.luckperms.common.utils.Predicates; import me.lucko.luckperms.exceptions.ObjectLacksException; import java.util.ArrayList; import java.util.List; +import java.util.stream.Collectors; public class MetaRemoveTempSuffix extends SecondarySubCommand { public MetaRemoveTempSuffix() { @@ -53,32 +51,14 @@ public class MetaRemoveTempSuffix extends SecondarySubCommand { } @Override - public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args) { - final String suffix = args.get(1).replace("{SPACE}", " "); - int priority; - try { - priority = Integer.parseInt(args.get(0)); - } catch (NumberFormatException e) { - Message.META_INVALID_PRIORITY.send(sender, args.get(0)); - return CommandResult.INVALID_ARGS; - } + public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args) throws CommandException { + int priority = ArgumentUtils.handlePriority(0, args); + String suffix = ArgumentUtils.handleNodeWithoutCheck(1, args); + String server = ArgumentUtils.handleServer(2, args); + String world = ArgumentUtils.handleWorld(3, args); + // Handle bulk removal if (suffix.equalsIgnoreCase("null")) { - String server = null; - String world = null; - - if (args.size() >= 3) { - server = args.get(2).toLowerCase(); - if (ArgumentChecker.checkServer(server)) { - Message.SERVER_INVALID_ENTRY.send(sender); - return CommandResult.INVALID_ARGS; - } - - if (args.size() != 3) { - world = args.get(3).toLowerCase(); - } - } - List toRemove = new ArrayList<>(); for (Node node : holder.getNodes()) { if (!node.isSuffix()) continue; @@ -111,48 +91,36 @@ public class MetaRemoveTempSuffix extends SecondarySubCommand { Message.BULK_CHANGE_SUCCESS.send(sender, toRemove.size()); save(holder, sender, plugin); return CommandResult.SUCCESS; + } - } else { + final String node = "suffix." + priority + "." + MetaUtils.escapeCharacters(suffix); - final String node = "suffix." + priority + "." + MetaUtils.escapeCharacters(suffix); - - try { - if (args.size() >= 3) { - final String server = args.get(2).toLowerCase(); - if (ArgumentChecker.checkServer(server)) { - Message.SERVER_INVALID_ENTRY.send(sender); - return CommandResult.INVALID_ARGS; - } - - if (args.size() == 3) { - holder.unsetPermission(node, server, true); - Message.REMOVE_TEMP_SUFFIX_SERVER_SUCCESS.send(sender, holder.getFriendlyName(), suffix, priority, server); - LogEntry.build().actor(sender).acted(holder) - .action("meta removetempsuffix " + priority + " " + args.get(1) + " " + server) - .build().submit(plugin, sender); - } else { - final String world = args.get(3).toLowerCase(); - holder.unsetPermission(node, server, world, true); - Message.REMOVE_TEMP_SUFFIX_SERVER_WORLD_SUCCESS.send(sender, holder.getFriendlyName(), suffix, priority, server, world); - LogEntry.build().actor(sender).acted(holder) - .action("meta removetempsuffix " + priority + " " + args.get(1) + " " + server + " " + world) - .build().submit(plugin, sender); - } - - } else { + try { + switch (ContextHelper.determine(server, world)) { + case NONE: holder.unsetPermission(node, true); Message.REMOVE_TEMP_SUFFIX_SUCCESS.send(sender, holder.getFriendlyName(), suffix, priority); - LogEntry.build().actor(sender).acted(holder) - .action("meta removetempsuffix " + priority + " " + args.get(1)) - .build().submit(plugin, sender); - } - - save(holder, sender, plugin); - return CommandResult.SUCCESS; - } catch (ObjectLacksException e) { - Message.DOES_NOT_HAVE_SUFFIX.send(sender, holder.getFriendlyName()); - return CommandResult.STATE_ERROR; + break; + case SERVER: + holder.unsetPermission(node, server, true); + Message.REMOVE_TEMP_SUFFIX_SERVER_SUCCESS.send(sender, holder.getFriendlyName(), suffix, priority, server); + break; + case SERVER_AND_WORLD: + holder.unsetPermission(node, server, world, true); + Message.REMOVE_TEMP_SUFFIX_SERVER_WORLD_SUCCESS.send(sender, holder.getFriendlyName(), suffix, priority, server, world); + break; } + + LogEntry.build().actor(sender).acted(holder) + .action("meta removetempsuffix " + args.stream().map(ArgumentUtils.WRAPPER).collect(Collectors.joining(" "))) + .build().submit(plugin, sender); + + save(holder, sender, plugin); + return CommandResult.SUCCESS; + + } catch (ObjectLacksException e) { + Message.DOES_NOT_HAVE_SUFFIX.send(sender, holder.getFriendlyName()); + return CommandResult.STATE_ERROR; } } } diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/generic/meta/MetaSet.java b/common/src/main/java/me/lucko/luckperms/common/commands/generic/meta/MetaSet.java index a71c31d9e..b5a287c4d 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/generic/meta/MetaSet.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/generic/meta/MetaSet.java @@ -25,20 +25,18 @@ package me.lucko.luckperms.common.commands.generic.meta; import me.lucko.luckperms.api.MetaUtils; import me.lucko.luckperms.api.Node; import me.lucko.luckperms.common.LuckPermsPlugin; -import me.lucko.luckperms.common.commands.Arg; -import me.lucko.luckperms.common.commands.CommandResult; -import me.lucko.luckperms.common.commands.Sender; +import me.lucko.luckperms.common.commands.*; import me.lucko.luckperms.common.commands.generic.SecondarySubCommand; import me.lucko.luckperms.common.constants.Message; import me.lucko.luckperms.common.constants.Permission; import me.lucko.luckperms.common.core.NodeBuilder; import me.lucko.luckperms.common.core.PermissionHolder; import me.lucko.luckperms.common.data.LogEntry; -import me.lucko.luckperms.common.utils.ArgumentChecker; import me.lucko.luckperms.common.utils.Predicates; import me.lucko.luckperms.exceptions.ObjectAlreadyHasException; import java.util.List; +import java.util.stream.Collectors; public class MetaSet extends SecondarySubCommand { public MetaSet() { @@ -53,25 +51,13 @@ public class MetaSet extends SecondarySubCommand { } @Override - public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args) { + public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args) throws CommandException { String key = MetaUtils.escapeCharacters(args.get(0)); String value = MetaUtils.escapeCharacters(args.get(1)); String node = "meta." + key + "." + value; - String server = null; - String world = null; - - if (args.size() >= 3) { - server = args.get(2).toLowerCase(); - if (ArgumentChecker.checkServer(server)) { - Message.SERVER_INVALID_ENTRY.send(sender); - return CommandResult.INVALID_ARGS; - } - - if (args.size() != 3) { - world = args.get(3).toLowerCase(); - } - } + String server = ArgumentUtils.handleServer(2, args); + String world = ArgumentUtils.handleWorld(3, args); Node n = new NodeBuilder(node).setServer(server).setWorld(world).build(); @@ -86,25 +72,22 @@ public class MetaSet extends SecondarySubCommand { holder.setPermission(n); } catch (ObjectAlreadyHasException ignored) {} - if (server == null) { - Message.SET_META_SUCCESS.send(sender, key, value, holder.getFriendlyName()); - LogEntry.build().actor(sender).acted(holder) - .action("meta set " + key + " " + value) - .build().submit(plugin, sender); - } else { - if (world == null) { + switch (ContextHelper.determine(server, world)) { + case NONE: + Message.SET_META_SUCCESS.send(sender, key, value, holder.getFriendlyName()); + break; + case SERVER: Message.SET_META_SERVER_SUCCESS.send(sender, key, value, holder.getFriendlyName(), server); - LogEntry.build().actor(sender).acted(holder) - .action("meta set " + key + " " + value + " " + server) - .build().submit(plugin, sender); - } else { + break; + case SERVER_AND_WORLD: Message.SET_META_SERVER_WORLD_SUCCESS.send(sender, key, value, holder.getFriendlyName(), server, world); - LogEntry.build().actor(sender).acted(holder) - .action("meta set " + key + " " + value + " " + server + " " + world) - .build().submit(plugin, sender); - } + break; } + LogEntry.build().actor(sender).acted(holder) + .action("meta set " + args.stream().map(ArgumentUtils.WRAPPER).collect(Collectors.joining(" "))) + .build().submit(plugin, sender); + save(holder, sender, plugin); return CommandResult.SUCCESS; } diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/generic/meta/MetaSetTemp.java b/common/src/main/java/me/lucko/luckperms/common/commands/generic/meta/MetaSetTemp.java index 5c3d897f3..5bfb9981b 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/generic/meta/MetaSetTemp.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/generic/meta/MetaSetTemp.java @@ -25,21 +25,19 @@ package me.lucko.luckperms.common.commands.generic.meta; import me.lucko.luckperms.api.MetaUtils; import me.lucko.luckperms.api.Node; import me.lucko.luckperms.common.LuckPermsPlugin; -import me.lucko.luckperms.common.commands.Arg; -import me.lucko.luckperms.common.commands.CommandResult; -import me.lucko.luckperms.common.commands.Sender; +import me.lucko.luckperms.common.commands.*; import me.lucko.luckperms.common.commands.generic.SecondarySubCommand; import me.lucko.luckperms.common.constants.Message; import me.lucko.luckperms.common.constants.Permission; import me.lucko.luckperms.common.core.NodeBuilder; import me.lucko.luckperms.common.core.PermissionHolder; import me.lucko.luckperms.common.data.LogEntry; -import me.lucko.luckperms.common.utils.ArgumentChecker; import me.lucko.luckperms.common.utils.DateUtil; import me.lucko.luckperms.common.utils.Predicates; import me.lucko.luckperms.exceptions.ObjectAlreadyHasException; import java.util.List; +import java.util.stream.Collectors; public class MetaSetTemp extends SecondarySubCommand { public MetaSetTemp() { @@ -55,42 +53,13 @@ public class MetaSetTemp extends SecondarySubCommand { } @Override - public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args) { - long duration; - try { - duration = Long.parseLong(args.get(2)); - } catch (NumberFormatException e) { - try { - duration = DateUtil.parseDateDiff(args.get(2), true); - } catch (DateUtil.IllegalDateException e1) { - Message.ILLEGAL_DATE_ERROR.send(sender, args.get(2)); - return CommandResult.INVALID_ARGS; - } - } - - if (DateUtil.shouldExpire(duration)) { - Message.PAST_DATE_ERROR.send(sender); - return CommandResult.INVALID_ARGS; - } - + public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args) throws CommandException { String key = MetaUtils.escapeCharacters(args.get(0)); String value = MetaUtils.escapeCharacters(args.get(1)); - + long duration = ArgumentUtils.handleDuration(2, args); String node = "meta." + key + "." + value; - String server = null; - String world = null; - - if (args.size() >= 3) { - server = args.get(2).toLowerCase(); - if (ArgumentChecker.checkServer(server)) { - Message.SERVER_INVALID_ENTRY.send(sender); - return CommandResult.INVALID_ARGS; - } - - if (args.size() != 3) { - world = args.get(3).toLowerCase(); - } - } + String server = ArgumentUtils.handleServer(3, args); + String world = ArgumentUtils.handleWorld(4, args); Node n = new NodeBuilder(node).setServer(server).setWorld(world).setExpiry(duration).build(); @@ -105,25 +74,22 @@ public class MetaSetTemp extends SecondarySubCommand { holder.setPermission(n); } catch (ObjectAlreadyHasException ignored) {} - if (server == null) { - Message.SET_META_TEMP_SUCCESS.send(sender, key, value, holder.getFriendlyName(), DateUtil.formatDateDiff(duration)); - LogEntry.build().actor(sender).acted(holder) - .action("meta settemp " + key + " " + value + " " + duration) - .build().submit(plugin, sender); - } else { - if (world == null) { + switch (ContextHelper.determine(server, world)) { + case NONE: + Message.SET_META_TEMP_SUCCESS.send(sender, key, value, holder.getFriendlyName(), DateUtil.formatDateDiff(duration)); + break; + case SERVER: Message.SET_META_TEMP_SERVER_SUCCESS.send(sender, key, value, holder.getFriendlyName(), server, DateUtil.formatDateDiff(duration)); - LogEntry.build().actor(sender).acted(holder) - .action("meta settemp " + key + " " + value + " " + duration + " " + server) - .build().submit(plugin, sender); - } else { + break; + case SERVER_AND_WORLD: Message.SET_META_TEMP_SERVER_WORLD_SUCCESS.send(sender, key, value, holder.getFriendlyName(), server, world, DateUtil.formatDateDiff(duration)); - LogEntry.build().actor(sender).acted(holder) - .action("meta settemp " + key + " " + value + " " + duration + " " + server + " " + world) - .build().submit(plugin, sender); - } + break; } + LogEntry.build().actor(sender).acted(holder) + .action("meta settemp " + args.stream().map(ArgumentUtils.WRAPPER).collect(Collectors.joining(" "))) + .build().submit(plugin, sender); + save(holder, sender, plugin); return CommandResult.SUCCESS; } diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/generic/meta/MetaUnset.java b/common/src/main/java/me/lucko/luckperms/common/commands/generic/meta/MetaUnset.java index 637d1cf28..a26015617 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/generic/meta/MetaUnset.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/generic/meta/MetaUnset.java @@ -24,15 +24,12 @@ package me.lucko.luckperms.common.commands.generic.meta; import me.lucko.luckperms.api.MetaUtils; import me.lucko.luckperms.common.LuckPermsPlugin; -import me.lucko.luckperms.common.commands.Arg; -import me.lucko.luckperms.common.commands.CommandResult; -import me.lucko.luckperms.common.commands.Sender; +import me.lucko.luckperms.common.commands.*; import me.lucko.luckperms.common.commands.generic.SecondarySubCommand; import me.lucko.luckperms.common.constants.Message; import me.lucko.luckperms.common.constants.Permission; import me.lucko.luckperms.common.core.PermissionHolder; import me.lucko.luckperms.common.data.LogEntry; -import me.lucko.luckperms.common.utils.ArgumentChecker; import me.lucko.luckperms.common.utils.Predicates; import java.util.List; @@ -40,7 +37,8 @@ import java.util.stream.Collectors; public class MetaUnset extends SecondarySubCommand { public MetaUnset() { - super("unset", "Unsets a meta value", Permission.USER_META_UNSET, Permission.GROUP_META_UNSET, Predicates.notInRange(1, 3), + super("unset", "Unsets a meta value", Permission.USER_META_UNSET, Permission.GROUP_META_UNSET, + Predicates.notInRange(1, 3), Arg.list( Arg.create("key", true, "the key to unset"), Arg.create("server", false, "the server to remove the meta pair on"), @@ -50,38 +48,27 @@ public class MetaUnset extends SecondarySubCommand { } @Override - public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args) { + public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args) throws CommandException { String key = MetaUtils.escapeCharacters(args.get(0)); - - String server = null; - String world = null; - - if (args.size() >= 2) { - server = args.get(1).toLowerCase(); - if (ArgumentChecker.checkServer(server)) { - Message.SERVER_INVALID_ENTRY.send(sender); - return CommandResult.INVALID_ARGS; - } - - if (args.size() != 2) { - world = args.get(2).toLowerCase(); - } - } + String server = ArgumentUtils.handleServer(1, args); + String world = ArgumentUtils.handleWorld(2, args); holder.clearMetaKeys(key, server, world, false); - if (server == null) { - Message.UNSET_META_SUCCESS.send(sender, key, holder.getFriendlyName()); - } else { - if (world == null) { + switch (ContextHelper.determine(server, world)) { + case NONE: + Message.UNSET_META_SUCCESS.send(sender, key, holder.getFriendlyName()); + break; + case SERVER: Message.UNSET_META_SERVER_SUCCESS.send(sender, key, holder.getFriendlyName(), server); - } else { + break; + case SERVER_AND_WORLD: Message.UNSET_META_SERVER_WORLD_SUCCESS.send(sender, key, holder.getFriendlyName(), server, world); - } + break; } LogEntry.build().actor(sender).acted(holder) - .action("meta unset " + args.stream().collect(Collectors.joining(" "))) + .action("meta unset " + args.stream().map(ArgumentUtils.WRAPPER).collect(Collectors.joining(" "))) .build().submit(plugin, sender); save(holder, sender, plugin); diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/generic/meta/MetaUnsetTemp.java b/common/src/main/java/me/lucko/luckperms/common/commands/generic/meta/MetaUnsetTemp.java index 8e457efe7..57f188d9a 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/generic/meta/MetaUnsetTemp.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/generic/meta/MetaUnsetTemp.java @@ -24,15 +24,12 @@ package me.lucko.luckperms.common.commands.generic.meta; import me.lucko.luckperms.api.MetaUtils; import me.lucko.luckperms.common.LuckPermsPlugin; -import me.lucko.luckperms.common.commands.Arg; -import me.lucko.luckperms.common.commands.CommandResult; -import me.lucko.luckperms.common.commands.Sender; +import me.lucko.luckperms.common.commands.*; import me.lucko.luckperms.common.commands.generic.SecondarySubCommand; import me.lucko.luckperms.common.constants.Message; import me.lucko.luckperms.common.constants.Permission; import me.lucko.luckperms.common.core.PermissionHolder; import me.lucko.luckperms.common.data.LogEntry; -import me.lucko.luckperms.common.utils.ArgumentChecker; import me.lucko.luckperms.common.utils.Predicates; import java.util.List; @@ -40,7 +37,8 @@ import java.util.stream.Collectors; public class MetaUnsetTemp extends SecondarySubCommand { public MetaUnsetTemp() { - super("unsettemp", "Unsets a temporary meta value", Permission.USER_META_UNSETTEMP, Permission.GROUP_META_UNSETTEMP, Predicates.notInRange(1, 3), + super("unsettemp", "Unsets a temporary meta value", Permission.USER_META_UNSETTEMP, Permission.GROUP_META_UNSETTEMP, + Predicates.notInRange(1, 3), Arg.list( Arg.create("key", true, "the key to unset"), Arg.create("server", false, "the server to remove the meta pair on"), @@ -50,38 +48,27 @@ public class MetaUnsetTemp extends SecondarySubCommand { } @Override - public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args) { + public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args) throws CommandException { String key = MetaUtils.escapeCharacters(args.get(0)); - - String server = null; - String world = null; - - if (args.size() >= 2) { - server = args.get(1).toLowerCase(); - if (ArgumentChecker.checkServer(server)) { - Message.SERVER_INVALID_ENTRY.send(sender); - return CommandResult.INVALID_ARGS; - } - - if (args.size() != 2) { - world = args.get(2).toLowerCase(); - } - } + String server = ArgumentUtils.handleServer(1, args); + String world = ArgumentUtils.handleWorld(2, args); holder.clearMetaKeys(key, server, world, true); - if (server == null) { - Message.UNSET_META_TEMP_SUCCESS.send(sender, key, holder.getFriendlyName()); - } else { - if (world == null) { + switch (ContextHelper.determine(server, world)) { + case NONE: + Message.UNSET_META_TEMP_SUCCESS.send(sender, key, holder.getFriendlyName()); + break; + case SERVER: Message.UNSET_META_TEMP_SERVER_SUCCESS.send(sender, key, holder.getFriendlyName(), server); - } else { + break; + case SERVER_AND_WORLD: Message.UNSET_META_TEMP_SERVER_WORLD_SUCCESS.send(sender, key, holder.getFriendlyName(), server, world); - } + break; } LogEntry.build().actor(sender).acted(holder) - .action("meta unsettemp " + args.stream().collect(Collectors.joining(" "))) + .action("meta unsettemp " + args.stream().map(ArgumentUtils.WRAPPER).collect(Collectors.joining(" "))) .build().submit(plugin, sender); save(holder, sender, plugin); diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/generic/other/HolderShowTracks.java b/common/src/main/java/me/lucko/luckperms/common/commands/generic/other/HolderShowTracks.java index d8454b4de..86f4fc7aa 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/generic/other/HolderShowTracks.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/generic/other/HolderShowTracks.java @@ -24,10 +24,7 @@ package me.lucko.luckperms.common.commands.generic.other; import me.lucko.luckperms.api.Node; import me.lucko.luckperms.common.LuckPermsPlugin; -import me.lucko.luckperms.common.commands.CommandResult; -import me.lucko.luckperms.common.commands.Sender; -import me.lucko.luckperms.common.commands.SubCommand; -import me.lucko.luckperms.common.commands.Util; +import me.lucko.luckperms.common.commands.*; import me.lucko.luckperms.common.constants.Message; import me.lucko.luckperms.common.constants.Permission; import me.lucko.luckperms.common.core.PermissionHolder; @@ -44,7 +41,7 @@ public class HolderShowTracks extends SubCommand } @Override - public CommandResult execute(LuckPermsPlugin plugin, Sender sender, T holder, List args, String label) { + public CommandResult execute(LuckPermsPlugin plugin, Sender sender, T holder, List args, String label) throws CommandException { if (!plugin.getDatastore().loadAllTracks().getUnchecked()) { Message.TRACKS_LOAD_ERROR.send(sender); return CommandResult.LOADING_ERROR; diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/generic/parent/ParentAdd.java b/common/src/main/java/me/lucko/luckperms/common/commands/generic/parent/ParentAdd.java index f5f038cdf..a443fc668 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/generic/parent/ParentAdd.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/generic/parent/ParentAdd.java @@ -23,27 +23,25 @@ package me.lucko.luckperms.common.commands.generic.parent; import me.lucko.luckperms.common.LuckPermsPlugin; -import me.lucko.luckperms.common.commands.Arg; -import me.lucko.luckperms.common.commands.CommandResult; -import me.lucko.luckperms.common.commands.Sender; +import me.lucko.luckperms.common.commands.*; import me.lucko.luckperms.common.commands.generic.SecondarySubCommand; import me.lucko.luckperms.common.constants.Message; import me.lucko.luckperms.common.constants.Permission; import me.lucko.luckperms.common.core.PermissionHolder; import me.lucko.luckperms.common.data.LogEntry; import me.lucko.luckperms.common.groups.Group; -import me.lucko.luckperms.common.utils.ArgumentChecker; import me.lucko.luckperms.common.utils.Predicates; import me.lucko.luckperms.exceptions.ObjectAlreadyHasException; import java.util.List; +import java.util.stream.Collectors; import static me.lucko.luckperms.common.commands.SubCommand.getGroupTabComplete; public class ParentAdd extends SecondarySubCommand { public ParentAdd() { - super("add", "Sets another group for the object to inherit permissions from", - Permission.USER_PARENT_ADD, Permission.GROUP_PARENT_ADD, Predicates.notInRange(1, 3), + super("add", "Sets another group for the object to inherit permissions from", Permission.USER_PARENT_ADD, + Permission.GROUP_PARENT_ADD, Predicates.notInRange(1, 3), Arg.list( Arg.create("group", true, "the group to inherit from"), Arg.create("server", false, "the server to inherit the group on"), @@ -53,13 +51,10 @@ public class ParentAdd extends SecondarySubCommand { } @Override - public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args) { - String groupName = args.get(0).toLowerCase(); - - if (ArgumentChecker.checkName(groupName)) { - sendDetailedUsage(sender); - return CommandResult.INVALID_ARGS; - } + public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args) throws CommandException { + String groupName = ArgumentUtils.handleName(0, args); + String server = ArgumentUtils.handleServer(1, args); + String world = ArgumentUtils.handleWorld(2, args); if (!plugin.getDatastore().loadGroup(groupName).getUnchecked()) { Message.GROUP_DOES_NOT_EXIST.send(sender); @@ -73,38 +68,28 @@ public class ParentAdd extends SecondarySubCommand { } try { - if (args.size() >= 2) { - final String server = args.get(1).toLowerCase(); - if (ArgumentChecker.checkServer(server)) { - Message.SERVER_INVALID_ENTRY.send(sender); - return CommandResult.INVALID_ARGS; - } - - if (args.size() == 2) { + switch (ContextHelper.determine(server, world)) { + case NONE: + holder.setInheritGroup(group); + Message.SET_INHERIT_SUCCESS.send(sender, holder.getFriendlyName(), group.getDisplayName()); + break; + case SERVER: holder.setInheritGroup(group, server); Message.SET_INHERIT_SERVER_SUCCESS.send(sender, holder.getFriendlyName(), group.getDisplayName(), server); - LogEntry.build().actor(sender).acted(holder) - .action("parent add " + group.getName() + " " + server) - .build().submit(plugin, sender); - } else { - final String world = args.get(2).toLowerCase(); + break; + case SERVER_AND_WORLD: holder.setInheritGroup(group, server, world); Message.SET_INHERIT_SERVER_WORLD_SUCCESS.send(sender, holder.getFriendlyName(), group.getDisplayName(), server, world); - LogEntry.build().actor(sender).acted(holder) - .action("parent add " + group.getName() + " " + server + " " + world) - .build().submit(plugin, sender); - } - - } else { - holder.setInheritGroup(group); - Message.SET_INHERIT_SUCCESS.send(sender, holder.getFriendlyName(), group.getDisplayName()); - LogEntry.build().actor(sender).acted(holder) - .action("parent add " + group.getName()) - .build().submit(plugin, sender); + break; } + LogEntry.build().actor(sender).acted(holder) + .action("parent add " + args.stream().map(ArgumentUtils.WRAPPER).collect(Collectors.joining(" "))) + .build().submit(plugin, sender); + save(holder, sender, plugin); return CommandResult.SUCCESS; + } catch (ObjectAlreadyHasException e) { Message.ALREADY_INHERITS.send(sender, holder.getFriendlyName(), group.getDisplayName()); return CommandResult.STATE_ERROR; diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/generic/parent/ParentAddTemp.java b/common/src/main/java/me/lucko/luckperms/common/commands/generic/parent/ParentAddTemp.java index 47230e16c..c04c180ad 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/generic/parent/ParentAddTemp.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/generic/parent/ParentAddTemp.java @@ -23,21 +23,19 @@ package me.lucko.luckperms.common.commands.generic.parent; import me.lucko.luckperms.common.LuckPermsPlugin; -import me.lucko.luckperms.common.commands.Arg; -import me.lucko.luckperms.common.commands.CommandResult; -import me.lucko.luckperms.common.commands.Sender; +import me.lucko.luckperms.common.commands.*; import me.lucko.luckperms.common.commands.generic.SecondarySubCommand; import me.lucko.luckperms.common.constants.Message; import me.lucko.luckperms.common.constants.Permission; import me.lucko.luckperms.common.core.PermissionHolder; import me.lucko.luckperms.common.data.LogEntry; import me.lucko.luckperms.common.groups.Group; -import me.lucko.luckperms.common.utils.ArgumentChecker; import me.lucko.luckperms.common.utils.DateUtil; import me.lucko.luckperms.common.utils.Predicates; import me.lucko.luckperms.exceptions.ObjectAlreadyHasException; import java.util.List; +import java.util.stream.Collectors; import static me.lucko.luckperms.common.commands.SubCommand.getGroupTabComplete; @@ -55,30 +53,11 @@ public class ParentAddTemp extends SecondarySubCommand { } @Override - public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args) { - String groupName = args.get(0).toLowerCase(); - - if (ArgumentChecker.checkName(groupName)) { - sendDetailedUsage(sender); - return CommandResult.INVALID_ARGS; - } - - long duration; - try { - duration = Long.parseLong(args.get(1)); - } catch (NumberFormatException e) { - try { - duration = DateUtil.parseDateDiff(args.get(1), true); - } catch (DateUtil.IllegalDateException e1) { - Message.ILLEGAL_DATE_ERROR.send(sender, args.get(1)); - return CommandResult.INVALID_ARGS; - } - } - - if (DateUtil.shouldExpire(duration)) { - Message.PAST_DATE_ERROR.send(sender); - return CommandResult.INVALID_ARGS; - } + public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args) throws CommandException { + String groupName = ArgumentUtils.handleName(0, args); + long duration = ArgumentUtils.handleDuration(1, args); + String server = ArgumentUtils.handleServer(2, args); + String world = ArgumentUtils.handleWorld(3, args); if (!plugin.getDatastore().loadGroup(groupName).getUnchecked()) { Message.GROUP_DOES_NOT_EXIST.send(sender); @@ -92,40 +71,34 @@ public class ParentAddTemp extends SecondarySubCommand { } try { - if (args.size() >= 3) { - final String server = args.get(2).toLowerCase(); - if (ArgumentChecker.checkServer(server)) { - Message.SERVER_INVALID_ENTRY.send(sender); - return CommandResult.INVALID_ARGS; - } - - if (args.size() == 3) { + switch (ContextHelper.determine(server, world)) { + case NONE: + holder.setInheritGroup(group, duration); + Message.SET_TEMP_INHERIT_SUCCESS.send(sender, holder.getFriendlyName(), group.getDisplayName(), + DateUtil.formatDateDiff(duration) + ); + break; + case SERVER: holder.setInheritGroup(group, server, duration); - Message.SET_TEMP_INHERIT_SERVER_SUCCESS.send(sender, holder.getFriendlyName(), group.getDisplayName(), server, - DateUtil.formatDateDiff(duration)); - LogEntry.build().actor(sender).acted(holder) - .action("parent addtemp " + group.getName() + " " + duration + " " + server) - .build().submit(plugin, sender); - } else { - final String world = args.get(3).toLowerCase(); + Message.SET_TEMP_INHERIT_SERVER_SUCCESS.send(sender, holder.getFriendlyName(), group.getDisplayName(), + server, DateUtil.formatDateDiff(duration) + ); + break; + case SERVER_AND_WORLD: holder.setInheritGroup(group, server, world, duration); - Message.SET_TEMP_INHERIT_SERVER_WORLD_SUCCESS.send(sender, holder.getFriendlyName(), group.getDisplayName(), server, - world, DateUtil.formatDateDiff(duration)); - LogEntry.build().actor(sender).acted(holder) - .action("parent addtemp " + group.getName() + " " + duration + " " + server + " " + world) - .build().submit(plugin, sender); - } - - } else { - holder.setInheritGroup(group, duration); - Message.SET_TEMP_INHERIT_SUCCESS.send(sender, holder.getFriendlyName(), group.getDisplayName(), DateUtil.formatDateDiff(duration)); - LogEntry.build().actor(sender).acted(holder) - .action("parent addtemp " + group.getName() + " " + duration) - .build().submit(plugin, sender); + Message.SET_TEMP_INHERIT_SERVER_WORLD_SUCCESS.send(sender, holder.getFriendlyName(), group.getDisplayName(), + server, world, DateUtil.formatDateDiff(duration) + ); + break; } + LogEntry.build().actor(sender).acted(holder) + .action("parent addtemp " + args.stream().map(ArgumentUtils.WRAPPER).collect(Collectors.joining(" "))) + .build().submit(plugin, sender); + save(holder, sender, plugin); return CommandResult.SUCCESS; + } catch (ObjectAlreadyHasException e) { Message.ALREADY_TEMP_INHERITS.send(sender, holder.getFriendlyName(), group.getDisplayName()); return CommandResult.STATE_ERROR; diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/generic/parent/ParentInfo.java b/common/src/main/java/me/lucko/luckperms/common/commands/generic/parent/ParentInfo.java index ba5f5afc8..89b87bf5d 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/generic/parent/ParentInfo.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/generic/parent/ParentInfo.java @@ -23,6 +23,7 @@ package me.lucko.luckperms.common.commands.generic.parent; import me.lucko.luckperms.common.LuckPermsPlugin; +import me.lucko.luckperms.common.commands.CommandException; import me.lucko.luckperms.common.commands.CommandResult; import me.lucko.luckperms.common.commands.Sender; import me.lucko.luckperms.common.commands.Util; @@ -41,7 +42,7 @@ public class ParentInfo extends SecondarySubCommand { } @Override - public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args) { + public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args) throws CommandException { Message.LISTPARENTS.send(sender, holder.getFriendlyName(), Util.permGroupsToString(holder.getPermissions(false))); Message.LISTPARENTS_TEMP.send(sender, holder.getFriendlyName(), Util.tempGroupsToString(holder.getPermissions(false))); return CommandResult.SUCCESS; diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/generic/parent/ParentRemove.java b/common/src/main/java/me/lucko/luckperms/common/commands/generic/parent/ParentRemove.java index bb13c9348..ddd4557ab 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/generic/parent/ParentRemove.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/generic/parent/ParentRemove.java @@ -23,27 +23,25 @@ package me.lucko.luckperms.common.commands.generic.parent; import me.lucko.luckperms.common.LuckPermsPlugin; -import me.lucko.luckperms.common.commands.Arg; -import me.lucko.luckperms.common.commands.CommandResult; -import me.lucko.luckperms.common.commands.Sender; +import me.lucko.luckperms.common.commands.*; import me.lucko.luckperms.common.commands.generic.SecondarySubCommand; import me.lucko.luckperms.common.constants.Message; import me.lucko.luckperms.common.constants.Permission; import me.lucko.luckperms.common.core.PermissionHolder; import me.lucko.luckperms.common.data.LogEntry; import me.lucko.luckperms.common.users.User; -import me.lucko.luckperms.common.utils.ArgumentChecker; import me.lucko.luckperms.common.utils.Predicates; import me.lucko.luckperms.exceptions.ObjectLacksException; import java.util.List; +import java.util.stream.Collectors; import static me.lucko.luckperms.common.commands.SubCommand.getGroupTabComplete; public class ParentRemove extends SecondarySubCommand { public ParentRemove() { - super("remove", "Removes a previously set inheritance rule", Permission.USER_PARENT_REMOVE, Permission.GROUP_PARENT_REMOVE, - Predicates.notInRange(1, 3), + super("remove", "Removes a previously set inheritance rule", Permission.USER_PARENT_REMOVE, + Permission.GROUP_PARENT_REMOVE, Predicates.notInRange(1, 3), Arg.list( Arg.create("group", true, "the group to remove"), Arg.create("server", false, "the server to remove the group on"), @@ -53,56 +51,49 @@ public class ParentRemove extends SecondarySubCommand { } @Override - public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args) { - String groupName = args.get(0).toLowerCase(); + public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args) throws CommandException { + String groupName = ArgumentUtils.handleName(0, args); + String server = ArgumentUtils.handleServer(1, args); + String world = ArgumentUtils.handleWorld(2, args); - if (ArgumentChecker.checkName(groupName)) { - sendDetailedUsage(sender); - return CommandResult.INVALID_ARGS; - } + ContextHelper.CommandContext context = ContextHelper.determine(server, world); if (holder instanceof User) { User user = (User) holder; - if ((args.size() == 1 || (args.size() == 2 && args.get(1).equalsIgnoreCase("global"))) - && user.getPrimaryGroup().equalsIgnoreCase(groupName)) { + + boolean shouldPrevent = (context == ContextHelper.CommandContext.NONE || + (context == ContextHelper.CommandContext.SERVER && server.equalsIgnoreCase("global"))) && + user.getPrimaryGroup().equalsIgnoreCase(groupName); + + if (shouldPrevent) { Message.USER_REMOVEGROUP_ERROR_PRIMARY.send(sender); return CommandResult.STATE_ERROR; } } try { - if (args.size() >= 2) { - final String server = args.get(1).toLowerCase(); - if (ArgumentChecker.checkServer(server)) { - Message.SERVER_INVALID_ENTRY.send(sender); - return CommandResult.INVALID_ARGS; - } - - if (args.size() == 2) { + switch (context) { + case NONE: + holder.unsetPermission("group." + groupName); + Message.UNSET_INHERIT_SUCCESS.send(sender, holder.getFriendlyName(), groupName); + break; + case SERVER: holder.unsetPermission("group." + groupName, server); Message.UNSET_INHERIT_SERVER_SUCCESS.send(sender, holder.getFriendlyName(), groupName, server); - LogEntry.build().actor(sender).acted(holder) - .action("parent remove " + groupName + " " + server) - .build().submit(plugin, sender); - } else { - final String world = args.get(2).toLowerCase(); + break; + case SERVER_AND_WORLD: holder.unsetPermission("group." + groupName, server, world); Message.UNSET_INHERIT_SERVER_WORLD_SUCCESS.send(sender, holder.getFriendlyName(), groupName, server, world); - LogEntry.build().actor(sender).acted(holder) - .action("parent remove " + groupName + " " + server + " " + world) - .build().submit(plugin, sender); - } - - } else { - holder.unsetPermission("group." + groupName); - Message.UNSET_INHERIT_SUCCESS.send(sender, holder.getFriendlyName(), groupName); - LogEntry.build().actor(sender).acted(holder) - .action("parent remove " + groupName) - .build().submit(plugin, sender); + break; } + LogEntry.build().actor(sender).acted(holder) + .action("parent remove " + args.stream().map(ArgumentUtils.WRAPPER).collect(Collectors.joining(" "))) + .build().submit(plugin, sender); + save(holder, sender, plugin); return CommandResult.SUCCESS; + } catch (ObjectLacksException e) { Message.DOES_NOT_INHERIT.send(sender, holder.getFriendlyName(), groupName); return CommandResult.STATE_ERROR; diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/generic/parent/ParentRemoveTemp.java b/common/src/main/java/me/lucko/luckperms/common/commands/generic/parent/ParentRemoveTemp.java index 485a17a75..b9ec01235 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/generic/parent/ParentRemoveTemp.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/generic/parent/ParentRemoveTemp.java @@ -23,19 +23,17 @@ package me.lucko.luckperms.common.commands.generic.parent; import me.lucko.luckperms.common.LuckPermsPlugin; -import me.lucko.luckperms.common.commands.Arg; -import me.lucko.luckperms.common.commands.CommandResult; -import me.lucko.luckperms.common.commands.Sender; +import me.lucko.luckperms.common.commands.*; import me.lucko.luckperms.common.commands.generic.SecondarySubCommand; import me.lucko.luckperms.common.constants.Message; import me.lucko.luckperms.common.constants.Permission; import me.lucko.luckperms.common.core.PermissionHolder; import me.lucko.luckperms.common.data.LogEntry; -import me.lucko.luckperms.common.utils.ArgumentChecker; import me.lucko.luckperms.common.utils.Predicates; import me.lucko.luckperms.exceptions.ObjectLacksException; import java.util.List; +import java.util.stream.Collectors; import static me.lucko.luckperms.common.commands.SubCommand.getGroupTabComplete; @@ -52,47 +50,34 @@ public class ParentRemoveTemp extends SecondarySubCommand { } @Override - public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args) { - String groupName = args.get(0).toLowerCase(); - - if (ArgumentChecker.checkName(groupName)) { - sendDetailedUsage(sender); - return CommandResult.INVALID_ARGS; - } + public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args) throws CommandException { + String groupName = ArgumentUtils.handleName(0, args); + String server = ArgumentUtils.handleServer(1, args); + String world = ArgumentUtils.handleWorld(2, args); try { - if (args.size() >= 2) { - final String server = args.get(1).toLowerCase(); - if (ArgumentChecker.checkServer(server)) { - Message.SERVER_INVALID_ENTRY.send(sender); - return CommandResult.INVALID_ARGS; - } - - if (args.size() == 2) { + switch (ContextHelper.determine(server, world)) { + case NONE: + holder.unsetPermission("group." + groupName, true); + Message.UNSET_TEMP_INHERIT_SUCCESS.send(sender, holder.getFriendlyName(), groupName); + break; + case SERVER: holder.unsetPermission("group." + groupName, server, true); Message.UNSET_TEMP_INHERIT_SERVER_SUCCESS.send(sender, holder.getFriendlyName(), groupName, server); - LogEntry.build().actor(sender).acted(holder) - .action("parent removetemp " + groupName + " " + server) - .build().submit(plugin, sender); - } else { - final String world = args.get(2).toLowerCase(); + break; + case SERVER_AND_WORLD: holder.unsetPermission("group." + groupName, server, world, true); Message.UNSET_TEMP_INHERIT_SERVER_WORLD_SUCCESS.send(sender, holder.getFriendlyName(), groupName, server, world); - LogEntry.build().actor(sender).acted(holder) - .action("parent removetemp " + groupName + " " + server + " " + world) - .build().submit(plugin, sender); - } - - } else { - holder.unsetPermission("group." + groupName, true); - Message.UNSET_TEMP_INHERIT_SUCCESS.send(sender, holder.getFriendlyName(), groupName); - LogEntry.build().actor(sender).acted(holder) - .action("parent removetemp " + groupName) - .build().submit(plugin, sender); + break; } + LogEntry.build().actor(sender).acted(holder) + .action("parent removetemp " + args.stream().map(ArgumentUtils.WRAPPER).collect(Collectors.joining(" "))) + .build().submit(plugin, sender); + save(holder, sender, plugin); return CommandResult.SUCCESS; + } catch (ObjectLacksException e) { Message.DOES_NOT_TEMP_INHERIT.send(sender, holder.getFriendlyName(), groupName); return CommandResult.STATE_ERROR; diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/generic/parent/ParentSet.java b/common/src/main/java/me/lucko/luckperms/common/commands/generic/parent/ParentSet.java index f22f2982a..bc9f17aed 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/generic/parent/ParentSet.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/generic/parent/ParentSet.java @@ -23,9 +23,7 @@ package me.lucko.luckperms.common.commands.generic.parent; import me.lucko.luckperms.common.LuckPermsPlugin; -import me.lucko.luckperms.common.commands.Arg; -import me.lucko.luckperms.common.commands.CommandResult; -import me.lucko.luckperms.common.commands.Sender; +import me.lucko.luckperms.common.commands.*; import me.lucko.luckperms.common.commands.generic.SecondarySubCommand; import me.lucko.luckperms.common.constants.Message; import me.lucko.luckperms.common.constants.Permission; @@ -33,11 +31,11 @@ import me.lucko.luckperms.common.core.PermissionHolder; import me.lucko.luckperms.common.data.LogEntry; import me.lucko.luckperms.common.groups.Group; import me.lucko.luckperms.common.users.User; -import me.lucko.luckperms.common.utils.ArgumentChecker; import me.lucko.luckperms.common.utils.Predicates; import me.lucko.luckperms.exceptions.ObjectAlreadyHasException; import java.util.List; +import java.util.stream.Collectors; import static me.lucko.luckperms.common.commands.SubCommand.getGroupTabComplete; @@ -54,13 +52,10 @@ public class ParentSet extends SecondarySubCommand { } @Override - public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args) { - String groupName = args.get(0).toLowerCase(); - - if (ArgumentChecker.checkNode(groupName)) { - sendDetailedUsage(sender); - return CommandResult.INVALID_ARGS; - } + public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args) throws CommandException { + String groupName = ArgumentUtils.handleName(0, args); + String server = ArgumentUtils.handleServer(1, args); + String world = ArgumentUtils.handleWorld(2, args); if (!plugin.getDatastore().loadGroup(groupName).getUnchecked()) { Message.GROUP_DOES_NOT_EXIST.send(sender); @@ -73,54 +68,50 @@ public class ParentSet extends SecondarySubCommand { return CommandResult.LOADING_ERROR; } - try { - if (args.size() >= 2) { - final String server = args.get(1).toLowerCase(); - if (ArgumentChecker.checkServer(server)) { - Message.SERVER_INVALID_ENTRY.send(sender); - return CommandResult.INVALID_ARGS; - } - - if (args.size() == 2) { - - holder.clearParents(server, null); - holder.setInheritGroup(group, server); - - Message.SET_PARENT_SERVER_SUCCESS.send(sender, holder.getFriendlyName(), group.getDisplayName(), server); - LogEntry.build().actor(sender).acted(holder) - .action("parent set " + group.getName() + " " + server) - .build().submit(plugin, sender); - } else { - final String world = args.get(2).toLowerCase(); - - holder.clearParents(server, world); - holder.setInheritGroup(group, server, world); - - Message.SET_PARENT_SERVER_WORLD_SUCCESS.send(sender, holder.getFriendlyName(), group.getDisplayName(), server, world); - LogEntry.build().actor(sender).acted(holder) - .action("parent set " + group.getName() + " " + server + " " + world) - .build().submit(plugin, sender); - } - - } else { + switch (ContextHelper.determine(server, world)) { + case NONE: holder.clearParents(null, null); - holder.setInheritGroup(group); + + try { + holder.setInheritGroup(group); + } catch (ObjectAlreadyHasException ignored) {} + if (holder instanceof User) { ((User) holder).setPrimaryGroup(group.getName()); } Message.SET_PARENT_SUCCESS.send(sender, holder.getFriendlyName(), group.getDisplayName()); - LogEntry.build().actor(sender).acted(holder) - .action("parent set " + group.getName()) - .build().submit(plugin, sender); - } + break; + case SERVER: + holder.clearParents(server, null); - save(holder, sender, plugin); - return CommandResult.SUCCESS; - } catch (ObjectAlreadyHasException e) { - Message.ALREADY_INHERITS.send(sender, holder.getFriendlyName(), group.getDisplayName()); - return CommandResult.STATE_ERROR; + try { + holder.setInheritGroup(group, server); + } catch (ObjectAlreadyHasException ignored) {} + + if (server.equalsIgnoreCase("global") && holder instanceof User) { + ((User) holder).setPrimaryGroup(group.getName()); + } + + Message.SET_PARENT_SERVER_SUCCESS.send(sender, holder.getFriendlyName(), group.getDisplayName(), server); + break; + case SERVER_AND_WORLD: + holder.clearParents(server, world); + + try { + holder.setInheritGroup(group, server, world); + } catch (ObjectAlreadyHasException ignored) {} + + Message.SET_PARENT_SERVER_WORLD_SUCCESS.send(sender, holder.getFriendlyName(), group.getDisplayName(), server, world); + break; } + + LogEntry.build().actor(sender).acted(holder) + .action("parent set " + args.stream().map(ArgumentUtils.WRAPPER).collect(Collectors.joining(" "))) + .build().submit(plugin, sender); + + save(holder, sender, plugin); + return CommandResult.SUCCESS; } @Override diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/generic/permission/PermissionCheck.java b/common/src/main/java/me/lucko/luckperms/common/commands/generic/permission/PermissionCheck.java index 98df29e2d..d6aa945f5 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/generic/permission/PermissionCheck.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/generic/permission/PermissionCheck.java @@ -23,24 +23,19 @@ package me.lucko.luckperms.common.commands.generic.permission; import me.lucko.luckperms.common.LuckPermsPlugin; -import me.lucko.luckperms.common.commands.Arg; -import me.lucko.luckperms.common.commands.CommandResult; -import me.lucko.luckperms.common.commands.Sender; -import me.lucko.luckperms.common.commands.Util; +import me.lucko.luckperms.common.commands.*; import me.lucko.luckperms.common.commands.generic.SecondarySubCommand; -import me.lucko.luckperms.common.constants.Message; import me.lucko.luckperms.common.constants.Permission; import me.lucko.luckperms.common.core.NodeBuilder; import me.lucko.luckperms.common.core.PermissionHolder; -import me.lucko.luckperms.common.utils.ArgumentChecker; import me.lucko.luckperms.common.utils.Predicates; import java.util.List; public class PermissionCheck extends SecondarySubCommand { public PermissionCheck() { - super("check", "Checks to see if the object has a certain permission node", Permission.USER_PERM_CHECK, Permission.GROUP_PERM_CHECK, - Predicates.notInRange(1, 3), + super("check", "Checks to see if the object has a certain permission node", Permission.USER_PERM_CHECK, + Permission.GROUP_PERM_CHECK, Predicates.notInRange(1, 3), Arg.list( Arg.create("node", true, "the permission node to check for"), Arg.create("server", false, "the server to check on"), @@ -50,22 +45,23 @@ public class PermissionCheck extends SecondarySubCommand { } @Override - public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args) { - if (args.size() >= 2) { - if (ArgumentChecker.checkServer(args.get(1))) { - Message.SERVER_INVALID_ENTRY.send(sender); - return CommandResult.INVALID_ARGS; - } + public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args) throws CommandException { + String node = ArgumentUtils.handleNodeWithoutCheck(0, args); + String server = ArgumentUtils.handleServer(1, args); + String world = ArgumentUtils.handleWorld(2, args); - if (args.size() == 2) { - Util.sendTristate(sender, args.get(0), holder.hasPermission(new NodeBuilder(args.get(0)).setServer(args.get(1)).build())); - } else { - Util.sendTristate(sender, args.get(0), holder.hasPermission(new NodeBuilder(args.get(0)).setServer(args.get(1)).setWorld(args.get(2)).build())); - } - - } else { - Util.sendTristate(sender, args.get(0), holder.hasPermission(new NodeBuilder(args.get(0)).build())); + switch (ContextHelper.determine(server, world)) { + case NONE: + Util.sendTristate(sender, node, holder.hasPermission(new NodeBuilder(node).build())); + break; + case SERVER: + Util.sendTristate(sender, node, holder.hasPermission(new NodeBuilder(node).setServer(server).build())); + break; + case SERVER_AND_WORLD: + Util.sendTristate(sender, node, holder.hasPermission(new NodeBuilder(node).setServer(server).setWorld(world).build())); + break; } + return CommandResult.SUCCESS; } } diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/generic/permission/PermissionCheckInherits.java b/common/src/main/java/me/lucko/luckperms/common/commands/generic/permission/PermissionCheckInherits.java index ec6f66c4c..05627648a 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/generic/permission/PermissionCheckInherits.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/generic/permission/PermissionCheckInherits.java @@ -23,25 +23,20 @@ package me.lucko.luckperms.common.commands.generic.permission; import me.lucko.luckperms.common.LuckPermsPlugin; -import me.lucko.luckperms.common.commands.Arg; -import me.lucko.luckperms.common.commands.CommandResult; -import me.lucko.luckperms.common.commands.Sender; -import me.lucko.luckperms.common.commands.Util; +import me.lucko.luckperms.common.commands.*; import me.lucko.luckperms.common.commands.generic.SecondarySubCommand; -import me.lucko.luckperms.common.constants.Message; import me.lucko.luckperms.common.constants.Permission; import me.lucko.luckperms.common.core.InheritanceInfo; import me.lucko.luckperms.common.core.NodeBuilder; import me.lucko.luckperms.common.core.PermissionHolder; -import me.lucko.luckperms.common.utils.ArgumentChecker; import me.lucko.luckperms.common.utils.Predicates; import java.util.List; public class PermissionCheckInherits extends SecondarySubCommand { public PermissionCheckInherits() { - super("checkinherits", "Checks to see if the object inherits a certain permission node", Permission.USER_PERM_CHECK_INHERITS, Permission.GROUP_PERM_CHECK_INHERITS, - Predicates.notInRange(1, 3), + super("checkinherits", "Checks to see if the object inherits a certain permission node", + Permission.USER_PERM_CHECK_INHERITS, Permission.GROUP_PERM_CHECK_INHERITS, Predicates.notInRange(1, 3), Arg.list( Arg.create("node", true, "the permission node to check for"), Arg.create("server", false, "the server to check on"), @@ -51,22 +46,22 @@ public class PermissionCheckInherits extends SecondarySubCommand { } @Override - public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args) { - InheritanceInfo result; - if (args.size() >= 2) { - if (ArgumentChecker.checkServer(args.get(1))) { - Message.SERVER_INVALID_ENTRY.send(sender); - return CommandResult.INVALID_ARGS; - } + public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args) throws CommandException { + String node = ArgumentUtils.handleNodeWithoutCheck(0, args); + String server = ArgumentUtils.handleServer(1, args); + String world = ArgumentUtils.handleWorld(2, args); - if (args.size() == 2) { - result = holder.inheritsPermissionInfo(new NodeBuilder(args.get(0)).setServer(args.get(1)).build()); - } else { - result = holder.inheritsPermissionInfo(new NodeBuilder(args.get(0)).setServer(args.get(1)).setWorld(args.get(2)).build()); - } - - } else { - result = holder.inheritsPermissionInfo(new NodeBuilder(args.get(0)).build()); + InheritanceInfo result = null; + switch (ContextHelper.determine(server, world)) { + case NONE: + result = holder.inheritsPermissionInfo(new NodeBuilder(node).build()); + break; + case SERVER: + result = holder.inheritsPermissionInfo(new NodeBuilder(node).setServer(server).build()); + break; + case SERVER_AND_WORLD: + result = holder.inheritsPermissionInfo(new NodeBuilder(node).setServer(server).setWorld(world).build()); + break; } String location = null; @@ -78,7 +73,7 @@ public class PermissionCheckInherits extends SecondarySubCommand { } } - Util.sendPluginMessage(sender, "&b" + args.get(0) + ": " + Util.formatTristate(result.getResult()) + + Util.sendPluginMessage(sender, "&b" + node + ": " + Util.formatTristate(result.getResult()) + (result.getLocation().isPresent() ? " &7(inherited from &a" + location + "&7)" : "")); return CommandResult.SUCCESS; } diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/generic/permission/PermissionInfo.java b/common/src/main/java/me/lucko/luckperms/common/commands/generic/permission/PermissionInfo.java index 9c37ee37d..2417ea380 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/generic/permission/PermissionInfo.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/generic/permission/PermissionInfo.java @@ -23,6 +23,7 @@ package me.lucko.luckperms.common.commands.generic.permission; import me.lucko.luckperms.common.LuckPermsPlugin; +import me.lucko.luckperms.common.commands.CommandException; import me.lucko.luckperms.common.commands.CommandResult; import me.lucko.luckperms.common.commands.Sender; import me.lucko.luckperms.common.commands.Util; @@ -40,7 +41,7 @@ public class PermissionInfo extends SecondarySubCommand { } @Override - public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args) { + public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args) throws CommandException { Message.LISTNODES.send(sender, holder.getFriendlyName(), Util.permNodesToString(holder.getPermissions(false))); Message.LISTNODES_TEMP.send(sender, holder.getFriendlyName(), Util.tempNodesToString(holder.getPermissions(false))); return CommandResult.SUCCESS; diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/generic/permission/PermissionSet.java b/common/src/main/java/me/lucko/luckperms/common/commands/generic/permission/PermissionSet.java index 09f95a34b..0e6e4dad6 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/generic/permission/PermissionSet.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/generic/permission/PermissionSet.java @@ -23,25 +23,24 @@ package me.lucko.luckperms.common.commands.generic.permission; import me.lucko.luckperms.common.LuckPermsPlugin; -import me.lucko.luckperms.common.commands.Arg; -import me.lucko.luckperms.common.commands.CommandResult; -import me.lucko.luckperms.common.commands.Sender; +import me.lucko.luckperms.common.commands.*; import me.lucko.luckperms.common.commands.generic.SecondarySubCommand; import me.lucko.luckperms.common.constants.Message; import me.lucko.luckperms.common.constants.Permission; import me.lucko.luckperms.common.core.PermissionHolder; import me.lucko.luckperms.common.data.LogEntry; -import me.lucko.luckperms.common.utils.ArgumentChecker; import me.lucko.luckperms.common.utils.Predicates; import me.lucko.luckperms.exceptions.ObjectAlreadyHasException; import java.util.List; +import java.util.stream.Collectors; import static me.lucko.luckperms.common.commands.SubCommand.getBoolTabComplete; public class PermissionSet extends SecondarySubCommand { public PermissionSet() { - super("set", "Sets a permission for the object", Permission.USER_PERM_SET, Permission.GROUP_PERM_SET, Predicates.notInRange(2, 4), + super("set", "Sets a permission for the object", Permission.USER_PERM_SET, Permission.GROUP_PERM_SET, + Predicates.notInRange(2, 4), Arg.list( Arg.create("node", true, "the permission node to set"), Arg.create("true|false", true, "the value of the node"), @@ -52,60 +51,36 @@ public class PermissionSet extends SecondarySubCommand { } @Override - public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args) { - String node = args.get(0).replace("{SPACE}", " "); - String bool = args.get(1).toLowerCase(); + public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args) throws CommandException { + String node = ArgumentUtils.handleNode(0, args); + boolean b = ArgumentUtils.handleBoolean(1, args); - if (ArgumentChecker.checkNode(node)) { - sendDetailedUsage(sender); - return CommandResult.INVALID_ARGS; - } - - if (node.toLowerCase().startsWith("group.")) { - Message.USE_INHERIT_COMMAND.send(sender); - return CommandResult.INVALID_ARGS; - } - - if (!bool.equalsIgnoreCase("true") && !bool.equalsIgnoreCase("false")) { - sendDetailedUsage(sender); - return CommandResult.INVALID_ARGS; - } - - boolean b = Boolean.parseBoolean(bool); + String server = ArgumentUtils.handleServer(2, args); + String world = ArgumentUtils.handleWorld(3, args); try { - if (args.size() >= 3) { - final String server = args.get(2).toLowerCase(); - if (ArgumentChecker.checkServer(server)) { - Message.SERVER_INVALID_ENTRY.send(sender); - return CommandResult.INVALID_ARGS; - } - - if (args.size() == 3) { + switch (ContextHelper.determine(server, world)) { + case NONE: + holder.setPermission(node, b); + Message.SETPERMISSION_SUCCESS.send(sender, node, b, holder.getFriendlyName()); + break; + case SERVER: holder.setPermission(node, b, server); - Message.SETPERMISSION_SERVER_SUCCESS.send(sender, node, bool, holder.getFriendlyName(), server); - LogEntry.build().actor(sender).acted(holder) - .action("permission set " + node + " " + b + " " + server) - .build().submit(plugin, sender); - } else { - final String world = args.get(3).toLowerCase(); + Message.SETPERMISSION_SERVER_SUCCESS.send(sender, node, b, holder.getFriendlyName(), server); + break; + case SERVER_AND_WORLD: holder.setPermission(node, b, server, world); - Message.SETPERMISSION_SERVER_WORLD_SUCCESS.send(sender, node, bool, holder.getFriendlyName(), server, world); - LogEntry.build().actor(sender).acted(holder) - .action("permission set " + node + " " + b + " " + server + " " + world) - .build().submit(plugin, sender); - } - - } else { - holder.setPermission(node, b); - Message.SETPERMISSION_SUCCESS.send(sender, node, bool, holder.getFriendlyName()); - LogEntry.build().actor(sender).acted(holder) - .action("permission set " + node + " " + b) - .build().submit(plugin, sender); + Message.SETPERMISSION_SERVER_WORLD_SUCCESS.send(sender, node, b, holder.getFriendlyName(), server, world); + break; } + LogEntry.build().actor(sender).acted(holder) + .action("permission set " + args.stream().map(ArgumentUtils.WRAPPER).collect(Collectors.joining(" "))) + .build().submit(plugin, sender); + save(holder, sender, plugin); return CommandResult.SUCCESS; + } catch (ObjectAlreadyHasException e) { Message.ALREADY_HASPERMISSION.send(sender, holder.getFriendlyName()); return CommandResult.STATE_ERROR; diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/generic/permission/PermissionSetTemp.java b/common/src/main/java/me/lucko/luckperms/common/commands/generic/permission/PermissionSetTemp.java index c10df85c0..21812aa9d 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/generic/permission/PermissionSetTemp.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/generic/permission/PermissionSetTemp.java @@ -23,27 +23,25 @@ package me.lucko.luckperms.common.commands.generic.permission; import me.lucko.luckperms.common.LuckPermsPlugin; -import me.lucko.luckperms.common.commands.Arg; -import me.lucko.luckperms.common.commands.CommandResult; -import me.lucko.luckperms.common.commands.Sender; +import me.lucko.luckperms.common.commands.*; import me.lucko.luckperms.common.commands.generic.SecondarySubCommand; import me.lucko.luckperms.common.constants.Message; import me.lucko.luckperms.common.constants.Permission; import me.lucko.luckperms.common.core.PermissionHolder; import me.lucko.luckperms.common.data.LogEntry; -import me.lucko.luckperms.common.utils.ArgumentChecker; import me.lucko.luckperms.common.utils.DateUtil; import me.lucko.luckperms.common.utils.Predicates; import me.lucko.luckperms.exceptions.ObjectAlreadyHasException; import java.util.List; +import java.util.stream.Collectors; import static me.lucko.luckperms.common.commands.SubCommand.getBoolTabComplete; public class PermissionSetTemp extends SecondarySubCommand { public PermissionSetTemp() { - super("settemp", "Sets a permission for the object temporarily", Permission.USER_PERM_SETTEMP, Permission.GROUP_PERM_SETTEMP, - Predicates.notInRange(3, 5), + super("settemp", "Sets a permission for the object temporarily", Permission.USER_PERM_SETTEMP, + Permission.GROUP_PERM_SETTEMP, Predicates.notInRange(3, 5), Arg.list( Arg.create("node", true, "the permission node to set"), Arg.create("true|false", true, "the value of the node"), @@ -55,79 +53,44 @@ public class PermissionSetTemp extends SecondarySubCommand { } @Override - public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args) { - String node = args.get(0).replace("{SPACE}", " "); - String bool = args.get(1).toLowerCase(); + public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args) throws CommandException { + String node = ArgumentUtils.handleNode(0, args); + boolean b = ArgumentUtils.handleBoolean(1, args); - if (ArgumentChecker.checkNode(node)) { - sendDetailedUsage(sender); - return CommandResult.INVALID_ARGS; - } + long duration = ArgumentUtils.handleDuration(2, args); - if (node.toLowerCase().startsWith("group.")) { - Message.USE_INHERIT_COMMAND.send(sender); - return CommandResult.INVALID_ARGS; - } - - if (!bool.equalsIgnoreCase("true") && !bool.equalsIgnoreCase("false")) { - sendDetailedUsage(sender); - return CommandResult.INVALID_ARGS; - } - - boolean b = Boolean.parseBoolean(bool); - - long duration; - try { - duration = Long.parseLong(args.get(2)); - } catch (NumberFormatException e) { - try { - duration = DateUtil.parseDateDiff(args.get(2), true); - } catch (DateUtil.IllegalDateException e1) { - Message.ILLEGAL_DATE_ERROR.send(sender, args.get(2)); - return CommandResult.INVALID_ARGS; - } - } - - if (DateUtil.shouldExpire(duration)) { - Message.PAST_DATE_ERROR.send(sender); - return CommandResult.INVALID_ARGS; - } + String server = ArgumentUtils.handleServer(3, args); + String world = ArgumentUtils.handleWorld(4, args); try { - if (args.size() >= 4) { - final String server = args.get(3).toLowerCase(); - if (ArgumentChecker.checkServer(server)) { - Message.SERVER_INVALID_ENTRY.send(sender); - return CommandResult.INVALID_ARGS; - } - - if (args.size() == 4) { + switch (ContextHelper.determine(server, world)) { + case NONE: + holder.setPermission(node, b, duration); + Message.SETPERMISSION_TEMP_SUCCESS.send(sender, node, b, holder.getFriendlyName(), + DateUtil.formatDateDiff(duration) + ); + break; + case SERVER: holder.setPermission(node, b, server, duration); - Message.SETPERMISSION_TEMP_SERVER_SUCCESS.send(sender, node, bool, holder.getFriendlyName(), server, - DateUtil.formatDateDiff(duration)); - LogEntry.build().actor(sender).acted(holder) - .action("permission settemp " + node + " " + b + " " + duration + " " + server) - .build().submit(plugin, sender); - } else { - final String world = args.get(4).toLowerCase(); + Message.SETPERMISSION_TEMP_SERVER_SUCCESS.send(sender, node, b, holder.getFriendlyName(), server, + DateUtil.formatDateDiff(duration) + ); + break; + case SERVER_AND_WORLD: holder.setPermission(node, b, server, world, duration); - Message.SETPERMISSION_TEMP_SERVER_WORLD_SUCCESS.send(sender, node, bool, holder.getFriendlyName(), server, - world, DateUtil.formatDateDiff(duration)); - LogEntry.build().actor(sender).acted(holder) - .action("permission settemp " + node + " " + b + " " + duration + " " + server + " " + world) - .build().submit(plugin, sender); - } - - } else { - holder.setPermission(node, b, duration); - Message.SETPERMISSION_TEMP_SUCCESS.send(sender, node, bool, holder.getFriendlyName(), DateUtil.formatDateDiff(duration)); - LogEntry.build().actor(sender).acted(holder) - .action("permission settemp " + node + " " + b + " " + duration) - .build().submit(plugin, sender); + Message.SETPERMISSION_TEMP_SERVER_WORLD_SUCCESS.send(sender, node, b, holder.getFriendlyName(), + server, world, DateUtil.formatDateDiff(duration) + ); + break; } + LogEntry.build().actor(sender).acted(holder) + .action("permission settemp " + args.stream().map(ArgumentUtils.WRAPPER).collect(Collectors.joining(" "))) + .build().submit(plugin, sender); + save(holder, sender, plugin); return CommandResult.SUCCESS; + } catch (ObjectAlreadyHasException e) { Message.ALREADY_HAS_TEMP_PERMISSION.send(sender, holder.getFriendlyName()); return CommandResult.STATE_ERROR; diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/generic/permission/PermissionUnset.java b/common/src/main/java/me/lucko/luckperms/common/commands/generic/permission/PermissionUnset.java index 5bfaa99b1..9b8e06d43 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/generic/permission/PermissionUnset.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/generic/permission/PermissionUnset.java @@ -23,23 +23,22 @@ package me.lucko.luckperms.common.commands.generic.permission; import me.lucko.luckperms.common.LuckPermsPlugin; -import me.lucko.luckperms.common.commands.Arg; -import me.lucko.luckperms.common.commands.CommandResult; -import me.lucko.luckperms.common.commands.Sender; +import me.lucko.luckperms.common.commands.*; import me.lucko.luckperms.common.commands.generic.SecondarySubCommand; import me.lucko.luckperms.common.constants.Message; import me.lucko.luckperms.common.constants.Permission; import me.lucko.luckperms.common.core.PermissionHolder; import me.lucko.luckperms.common.data.LogEntry; -import me.lucko.luckperms.common.utils.ArgumentChecker; import me.lucko.luckperms.common.utils.Predicates; import me.lucko.luckperms.exceptions.ObjectLacksException; import java.util.List; +import java.util.stream.Collectors; public class PermissionUnset extends SecondarySubCommand { public PermissionUnset() { - super("unset", "Unsets a permission for the object", Permission.USER_PERM_UNSET, Permission.GROUP_PERM_UNSET, Predicates.notInRange(1, 3), + super("unset", "Unsets a permission for the object", Permission.USER_PERM_UNSET, Permission.GROUP_PERM_UNSET, + Predicates.notInRange(1, 3), Arg.list( Arg.create("node", true, "the permission node to unset"), Arg.create("server", false, "the server to remove the permission node on"), @@ -49,52 +48,34 @@ public class PermissionUnset extends SecondarySubCommand { } @Override - public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args) { - String node = args.get(0).replace("{SPACE}", " "); - - if (ArgumentChecker.checkNode(node)) { - sendDetailedUsage(sender); - return CommandResult.INVALID_ARGS; - } - - if (node.toLowerCase().startsWith("group.")) { - Message.USE_UNINHERIT_COMMAND.send(sender); - return CommandResult.INVALID_ARGS; - } + public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args) throws CommandException { + String node = ArgumentUtils.handleNode(0, args); + String server = ArgumentUtils.handleServer(1, args); + String world = ArgumentUtils.handleWorld(2, args); try { - if (args.size() >= 2) { - final String server = args.get(1).toLowerCase(); - if (ArgumentChecker.checkServer(server)) { - Message.SERVER_INVALID_ENTRY.send(sender); - return CommandResult.INVALID_ARGS; - } - - if (args.size() == 2) { + switch (ContextHelper.determine(server, world)) { + case NONE: + holder.unsetPermission(node); + Message.UNSETPERMISSION_SUCCESS.send(sender, node, holder.getFriendlyName()); + break; + case SERVER: holder.unsetPermission(node, server); Message.UNSETPERMISSION_SERVER_SUCCESS.send(sender, node, holder.getFriendlyName(), server); - LogEntry.build().actor(sender).acted(holder) - .action("permission unset " + node + " " + server) - .build().submit(plugin, sender); - } else { - final String world = args.get(2).toLowerCase(); + break; + case SERVER_AND_WORLD: holder.unsetPermission(node, server, world); Message.UNSETPERMISSION_SERVER_WORLD_SUCCESS.send(sender, node, holder.getFriendlyName(), server, world); - LogEntry.build().actor(sender).acted(holder) - .action("permission unset " + node + " " + server + " " + world) - .build().submit(plugin, sender); - } - - } else { - holder.unsetPermission(node); - Message.UNSETPERMISSION_SUCCESS.send(sender, node, holder.getFriendlyName()); - LogEntry.build().actor(sender).acted(holder) - .action("permission unset " + node) - .build().submit(plugin, sender); + break; } + LogEntry.build().actor(sender).acted(holder) + .action("permission unset " + args.stream().map(ArgumentUtils.WRAPPER).collect(Collectors.joining(" "))) + .build().submit(plugin, sender); + save(holder, sender, plugin); return CommandResult.SUCCESS; + } catch (ObjectLacksException e) { Message.DOES_NOT_HAVEPERMISSION.send(sender, holder.getFriendlyName()); return CommandResult.STATE_ERROR; diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/generic/permission/PermissionUnsetTemp.java b/common/src/main/java/me/lucko/luckperms/common/commands/generic/permission/PermissionUnsetTemp.java index 99da61f5d..f8c24d5f6 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/generic/permission/PermissionUnsetTemp.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/generic/permission/PermissionUnsetTemp.java @@ -23,24 +23,22 @@ package me.lucko.luckperms.common.commands.generic.permission; import me.lucko.luckperms.common.LuckPermsPlugin; -import me.lucko.luckperms.common.commands.Arg; -import me.lucko.luckperms.common.commands.CommandResult; -import me.lucko.luckperms.common.commands.Sender; +import me.lucko.luckperms.common.commands.*; import me.lucko.luckperms.common.commands.generic.SecondarySubCommand; import me.lucko.luckperms.common.constants.Message; import me.lucko.luckperms.common.constants.Permission; import me.lucko.luckperms.common.core.PermissionHolder; import me.lucko.luckperms.common.data.LogEntry; -import me.lucko.luckperms.common.utils.ArgumentChecker; import me.lucko.luckperms.common.utils.Predicates; import me.lucko.luckperms.exceptions.ObjectLacksException; import java.util.List; +import java.util.stream.Collectors; public class PermissionUnsetTemp extends SecondarySubCommand { public PermissionUnsetTemp() { - super("unsettemp", "Unsets a temporary permission for the object", Permission.USER_PERM_UNSETTEMP, Permission.GROUP_PERM_UNSETTEMP, - Predicates.notInRange(1, 3), + super("unsettemp", "Unsets a temporary permission for the object", Permission.USER_PERM_UNSETTEMP, + Permission.GROUP_PERM_UNSETTEMP, Predicates.notInRange(1, 3), Arg.list( Arg.create("node", true, "the permission node to unset"), Arg.create("server", false, "the server to remove the permission node on"), @@ -50,52 +48,34 @@ public class PermissionUnsetTemp extends SecondarySubCommand { } @Override - public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args) { - String node = args.get(0).replace("{SPACE}", " "); - - if (ArgumentChecker.checkNode(node)) { - sendDetailedUsage(sender); - return CommandResult.INVALID_ARGS; - } - - if (node.toLowerCase().startsWith("group.")) { - Message.USE_UNINHERIT_COMMAND.send(sender); - return CommandResult.INVALID_ARGS; - } + public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List args) throws CommandException { + String node = ArgumentUtils.handleNode(0, args); + String server = ArgumentUtils.handleServer(1, args); + String world = ArgumentUtils.handleWorld(2, args); try { - if (args.size() >= 2) { - final String server = args.get(1).toLowerCase(); - if (ArgumentChecker.checkServer(server)) { - Message.SERVER_INVALID_ENTRY.send(sender); - return CommandResult.INVALID_ARGS; - } - - if (args.size() == 2) { - holder.unsetPermission(node, server); + switch (ContextHelper.determine(server, world)) { + case NONE: + holder.unsetPermission(node, true); + Message.UNSET_TEMP_PERMISSION_SUCCESS.send(sender, node, holder.getFriendlyName()); + break; + case SERVER: + holder.unsetPermission(node, server, true); Message.UNSET_TEMP_PERMISSION_SERVER_SUCCESS.send(sender, node, holder.getFriendlyName(), server); - LogEntry.build().actor(sender).acted(holder) - .action("permission unsettemp " + node + " " + server) - .build().submit(plugin, sender); - } else { - final String world = args.get(2).toLowerCase(); - holder.unsetPermission(node, server, world); + break; + case SERVER_AND_WORLD: + holder.unsetPermission(node, server, world, true); Message.UNSET_TEMP_PERMISSION_SERVER_WORLD_SUCCESS.send(sender, node, holder.getFriendlyName(), server, world); - LogEntry.build().actor(sender).acted(holder) - .action("permission unsettemp " + node + " " + server + " " + world) - .build().submit(plugin, sender); - } - - } else { - holder.unsetPermission(node, true); - Message.UNSET_TEMP_PERMISSION_SUCCESS.send(sender, node, holder.getFriendlyName()); - LogEntry.build().actor(sender).acted(holder) - .action("permission unsettemp " + node) - .build().submit(plugin, sender); + break; } + LogEntry.build().actor(sender).acted(holder) + .action("permission unsettemp " + args.stream().map(ArgumentUtils.WRAPPER).collect(Collectors.joining(" "))) + .build().submit(plugin, sender); + save(holder, sender, plugin); return CommandResult.SUCCESS; + } catch (ObjectLacksException e) { Message.DOES_NOT_HAVE_TEMP_PERMISSION.send(sender, holder.getFriendlyName()); return CommandResult.STATE_ERROR; diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/group/subcommands/GroupBulkChange.java b/common/src/main/java/me/lucko/luckperms/common/commands/group/subcommands/GroupBulkChange.java index 09ad61051..025903da5 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/group/subcommands/GroupBulkChange.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/group/subcommands/GroupBulkChange.java @@ -24,10 +24,7 @@ package me.lucko.luckperms.common.commands.group.subcommands; import me.lucko.luckperms.api.Node; import me.lucko.luckperms.common.LuckPermsPlugin; -import me.lucko.luckperms.common.commands.Arg; -import me.lucko.luckperms.common.commands.CommandResult; -import me.lucko.luckperms.common.commands.Sender; -import me.lucko.luckperms.common.commands.SubCommand; +import me.lucko.luckperms.common.commands.*; import me.lucko.luckperms.common.constants.Message; import me.lucko.luckperms.common.constants.Permission; import me.lucko.luckperms.common.core.NodeFactory; @@ -53,7 +50,7 @@ public class GroupBulkChange extends SubCommand { } @Override - public CommandResult execute(LuckPermsPlugin plugin, Sender sender, Group group, List args, String label) { + public CommandResult execute(LuckPermsPlugin plugin, Sender sender, Group group, List args, String label) throws CommandException { String type = args.get(0).toLowerCase(); String from = args.get(1); String to = args.get(2); diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/group/subcommands/GroupClear.java b/common/src/main/java/me/lucko/luckperms/common/commands/group/subcommands/GroupClear.java index fb9bd72dd..66ddbe93c 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/group/subcommands/GroupClear.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/group/subcommands/GroupClear.java @@ -23,15 +23,11 @@ package me.lucko.luckperms.common.commands.group.subcommands; import me.lucko.luckperms.common.LuckPermsPlugin; -import me.lucko.luckperms.common.commands.Arg; -import me.lucko.luckperms.common.commands.CommandResult; -import me.lucko.luckperms.common.commands.Sender; -import me.lucko.luckperms.common.commands.SubCommand; +import me.lucko.luckperms.common.commands.*; import me.lucko.luckperms.common.constants.Message; import me.lucko.luckperms.common.constants.Permission; import me.lucko.luckperms.common.data.LogEntry; import me.lucko.luckperms.common.groups.Group; -import me.lucko.luckperms.common.utils.ArgumentChecker; import me.lucko.luckperms.common.utils.Predicates; import java.util.List; @@ -48,25 +44,22 @@ public class GroupClear extends SubCommand { } @Override - public CommandResult execute(LuckPermsPlugin plugin, Sender sender, Group group, List args, String label) { + public CommandResult execute(LuckPermsPlugin plugin, Sender sender, Group group, List args, String label) throws CommandException { int before = group.getNodes().size(); - if (args.size() == 0) { - group.clearNodes(); - } else { - final String server = args.get(0); - if (ArgumentChecker.checkServer(server)) { - Message.SERVER_INVALID_ENTRY.send(sender); - return CommandResult.INVALID_ARGS; - } + String server = ArgumentUtils.handleServer(0, args); + String world = ArgumentUtils.handleWorld(1, args); - if (args.size() == 2) { - final String world = args.get(1); - group.clearNodes(server, world); - - } else { + switch (ContextHelper.determine(server, world)) { + case NONE: + group.clearNodes(); + break; + case SERVER: group.clearNodes(server); - } + break; + case SERVER_AND_WORLD: + group.clearNodes(server, world); + break; } int changed = before - group.getNodes().size(); @@ -76,7 +69,10 @@ public class GroupClear extends SubCommand { Message.CLEAR_SUCCESS.send(sender, group.getName(), changed); } - LogEntry.build().actor(sender).acted(group).action("clear " + args.stream().collect(Collectors.joining(" "))).build().submit(plugin, sender); + LogEntry.build().actor(sender).acted(group) + .action("clear " + args.stream().map(ArgumentUtils.WRAPPER).collect(Collectors.joining(" "))) + .build().submit(plugin, sender); + save(group, sender, plugin); return CommandResult.SUCCESS; } diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/group/subcommands/GroupClone.java b/common/src/main/java/me/lucko/luckperms/common/commands/group/subcommands/GroupClone.java index fe47677f0..1f998170f 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/group/subcommands/GroupClone.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/group/subcommands/GroupClone.java @@ -23,10 +23,7 @@ package me.lucko.luckperms.common.commands.group.subcommands; import me.lucko.luckperms.common.LuckPermsPlugin; -import me.lucko.luckperms.common.commands.Arg; -import me.lucko.luckperms.common.commands.CommandResult; -import me.lucko.luckperms.common.commands.Sender; -import me.lucko.luckperms.common.commands.SubCommand; +import me.lucko.luckperms.common.commands.*; import me.lucko.luckperms.common.constants.Message; import me.lucko.luckperms.common.constants.Permission; import me.lucko.luckperms.common.data.LogEntry; @@ -44,7 +41,7 @@ public class GroupClone extends SubCommand { } @Override - public CommandResult execute(LuckPermsPlugin plugin, Sender sender, Group group, List args, String label) { + public CommandResult execute(LuckPermsPlugin plugin, Sender sender, Group group, List args, String label) throws CommandException { String newGroupName = args.get(0).toLowerCase(); if (ArgumentChecker.checkName(newGroupName)) { Message.GROUP_INVALID_ENTRY.send(sender); diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/group/subcommands/GroupInfo.java b/common/src/main/java/me/lucko/luckperms/common/commands/group/subcommands/GroupInfo.java index c9bb51d82..74240243d 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/group/subcommands/GroupInfo.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/group/subcommands/GroupInfo.java @@ -23,6 +23,7 @@ package me.lucko.luckperms.common.commands.group.subcommands; import me.lucko.luckperms.common.LuckPermsPlugin; +import me.lucko.luckperms.common.commands.CommandException; import me.lucko.luckperms.common.commands.CommandResult; import me.lucko.luckperms.common.commands.Sender; import me.lucko.luckperms.common.commands.SubCommand; @@ -39,7 +40,7 @@ public class GroupInfo extends SubCommand { } @Override - public CommandResult execute(LuckPermsPlugin plugin, Sender sender, Group group, List args, String label) { + public CommandResult execute(LuckPermsPlugin plugin, Sender sender, Group group, List args, String label) throws CommandException { Message.GROUP_INFO.send(sender, group.getDisplayName(), group.getPermanentNodes().size(), diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/group/subcommands/GroupRename.java b/common/src/main/java/me/lucko/luckperms/common/commands/group/subcommands/GroupRename.java index b399a1a37..98027cc6f 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/group/subcommands/GroupRename.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/group/subcommands/GroupRename.java @@ -23,10 +23,7 @@ package me.lucko.luckperms.common.commands.group.subcommands; import me.lucko.luckperms.common.LuckPermsPlugin; -import me.lucko.luckperms.common.commands.Arg; -import me.lucko.luckperms.common.commands.CommandResult; -import me.lucko.luckperms.common.commands.Sender; -import me.lucko.luckperms.common.commands.SubCommand; +import me.lucko.luckperms.common.commands.*; import me.lucko.luckperms.common.constants.Message; import me.lucko.luckperms.common.constants.Permission; import me.lucko.luckperms.common.data.LogEntry; @@ -44,7 +41,7 @@ public class GroupRename extends SubCommand { } @Override - public CommandResult execute(LuckPermsPlugin plugin, Sender sender, Group group, List args, String label) { + public CommandResult execute(LuckPermsPlugin plugin, Sender sender, Group group, List args, String label) throws CommandException { String newGroupName = args.get(0).toLowerCase(); if (ArgumentChecker.checkName(newGroupName)) { Message.GROUP_INVALID_ENTRY.send(sender); diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/log/subcommands/LogExport.java b/common/src/main/java/me/lucko/luckperms/common/commands/log/subcommands/LogExport.java index e9cad6b3b..575ab3cf4 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/log/subcommands/LogExport.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/log/subcommands/LogExport.java @@ -24,10 +24,7 @@ package me.lucko.luckperms.common.commands.log.subcommands; import me.lucko.luckperms.api.LogEntry; import me.lucko.luckperms.common.LuckPermsPlugin; -import me.lucko.luckperms.common.commands.Arg; -import me.lucko.luckperms.common.commands.CommandResult; -import me.lucko.luckperms.common.commands.Sender; -import me.lucko.luckperms.common.commands.SubCommand; +import me.lucko.luckperms.common.commands.*; import me.lucko.luckperms.common.constants.Message; import me.lucko.luckperms.common.constants.Permission; import me.lucko.luckperms.common.data.Log; @@ -48,7 +45,7 @@ public class LogExport extends SubCommand { } @Override - public CommandResult execute(LuckPermsPlugin plugin, Sender sender, Log log, List args, String label) { + public CommandResult execute(LuckPermsPlugin plugin, Sender sender, Log log, List args, String label) throws CommandException { File f = new File(plugin.getMainDir(), args.get(0)); if (f.exists()) { Message.LOG_EXPORT_ALREADY_EXISTS.send(sender, f.getAbsolutePath()); diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/log/subcommands/LogGroupHistory.java b/common/src/main/java/me/lucko/luckperms/common/commands/log/subcommands/LogGroupHistory.java index fc795b24c..0c936ba0d 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/log/subcommands/LogGroupHistory.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/log/subcommands/LogGroupHistory.java @@ -24,10 +24,7 @@ package me.lucko.luckperms.common.commands.log.subcommands; import me.lucko.luckperms.api.LogEntry; import me.lucko.luckperms.common.LuckPermsPlugin; -import me.lucko.luckperms.common.commands.Arg; -import me.lucko.luckperms.common.commands.CommandResult; -import me.lucko.luckperms.common.commands.Sender; -import me.lucko.luckperms.common.commands.SubCommand; +import me.lucko.luckperms.common.commands.*; import me.lucko.luckperms.common.constants.Message; import me.lucko.luckperms.common.constants.Permission; import me.lucko.luckperms.common.data.Log; @@ -50,7 +47,7 @@ public class LogGroupHistory extends SubCommand { } @Override - public CommandResult execute(LuckPermsPlugin plugin, Sender sender, Log log, List args, String label) { + public CommandResult execute(LuckPermsPlugin plugin, Sender sender, Log log, List args, String label) throws CommandException { String group = args.get(0).toLowerCase(); int page = -999; diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/log/subcommands/LogNotify.java b/common/src/main/java/me/lucko/luckperms/common/commands/log/subcommands/LogNotify.java index 0dfae5295..e7eea3a01 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/log/subcommands/LogNotify.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/log/subcommands/LogNotify.java @@ -23,10 +23,7 @@ package me.lucko.luckperms.common.commands.log.subcommands; import me.lucko.luckperms.common.LuckPermsPlugin; -import me.lucko.luckperms.common.commands.Arg; -import me.lucko.luckperms.common.commands.CommandResult; -import me.lucko.luckperms.common.commands.Sender; -import me.lucko.luckperms.common.commands.SubCommand; +import me.lucko.luckperms.common.commands.*; import me.lucko.luckperms.common.constants.Message; import me.lucko.luckperms.common.constants.Permission; import me.lucko.luckperms.common.data.Log; @@ -44,7 +41,7 @@ public class LogNotify extends SubCommand { } @Override - public CommandResult execute(LuckPermsPlugin plugin, Sender sender, Log log, List args, String label) { + public CommandResult execute(LuckPermsPlugin plugin, Sender sender, Log log, List args, String label) throws CommandException { final Set ignoring = plugin.getIgnoringLogs(); final UUID uuid = sender.getUuid(); if (args.size() == 0) { diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/log/subcommands/LogRecent.java b/common/src/main/java/me/lucko/luckperms/common/commands/log/subcommands/LogRecent.java index b92dd9303..bc64191c0 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/log/subcommands/LogRecent.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/log/subcommands/LogRecent.java @@ -48,7 +48,7 @@ public class LogRecent extends SubCommand { } @Override - public CommandResult execute(LuckPermsPlugin plugin, Sender sender, Log log, List args, String label) { + public CommandResult execute(LuckPermsPlugin plugin, Sender sender, Log log, List args, String label) throws CommandException { if (args.size() == 0) { // No page or user return showLog(log.getRecentMaxPages(), null, sender, log); diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/log/subcommands/LogSearch.java b/common/src/main/java/me/lucko/luckperms/common/commands/log/subcommands/LogSearch.java index fed27f965..cee629a7d 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/log/subcommands/LogSearch.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/log/subcommands/LogSearch.java @@ -24,10 +24,7 @@ package me.lucko.luckperms.common.commands.log.subcommands; import me.lucko.luckperms.api.LogEntry; import me.lucko.luckperms.common.LuckPermsPlugin; -import me.lucko.luckperms.common.commands.Arg; -import me.lucko.luckperms.common.commands.CommandResult; -import me.lucko.luckperms.common.commands.Sender; -import me.lucko.luckperms.common.commands.SubCommand; +import me.lucko.luckperms.common.commands.*; import me.lucko.luckperms.common.constants.Message; import me.lucko.luckperms.common.constants.Permission; import me.lucko.luckperms.common.data.Log; @@ -50,7 +47,7 @@ public class LogSearch extends SubCommand { } @Override - public CommandResult execute(LuckPermsPlugin plugin, Sender sender, Log log, List args, String label) { + public CommandResult execute(LuckPermsPlugin plugin, Sender sender, Log log, List args, String label) throws CommandException { int page = -999; if (args.size() > 1) { try { diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/log/subcommands/LogTrackHistory.java b/common/src/main/java/me/lucko/luckperms/common/commands/log/subcommands/LogTrackHistory.java index 854d8794c..a57c0f430 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/log/subcommands/LogTrackHistory.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/log/subcommands/LogTrackHistory.java @@ -24,10 +24,7 @@ package me.lucko.luckperms.common.commands.log.subcommands; import me.lucko.luckperms.api.LogEntry; import me.lucko.luckperms.common.LuckPermsPlugin; -import me.lucko.luckperms.common.commands.Arg; -import me.lucko.luckperms.common.commands.CommandResult; -import me.lucko.luckperms.common.commands.Sender; -import me.lucko.luckperms.common.commands.SubCommand; +import me.lucko.luckperms.common.commands.*; import me.lucko.luckperms.common.constants.Message; import me.lucko.luckperms.common.constants.Permission; import me.lucko.luckperms.common.data.Log; @@ -50,7 +47,7 @@ public class LogTrackHistory extends SubCommand { } @Override - public CommandResult execute(LuckPermsPlugin plugin, Sender sender, Log log, List args, String label) { + public CommandResult execute(LuckPermsPlugin plugin, Sender sender, Log log, List args, String label) throws CommandException { String track = args.get(0).toLowerCase(); int page = -999; diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/log/subcommands/LogUserHistory.java b/common/src/main/java/me/lucko/luckperms/common/commands/log/subcommands/LogUserHistory.java index 1f81f9c21..4035e4097 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/log/subcommands/LogUserHistory.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/log/subcommands/LogUserHistory.java @@ -48,7 +48,7 @@ public class LogUserHistory extends SubCommand { } @Override - public CommandResult execute(LuckPermsPlugin plugin, Sender sender, Log log, List args, String label) { + public CommandResult execute(LuckPermsPlugin plugin, Sender sender, Log log, List args, String label) throws CommandException { String user = args.get(0); int page = -999; diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/track/subcommands/TrackAppend.java b/common/src/main/java/me/lucko/luckperms/common/commands/track/subcommands/TrackAppend.java index 92fc72a02..a33fb8c56 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/track/subcommands/TrackAppend.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/track/subcommands/TrackAppend.java @@ -43,7 +43,7 @@ public class TrackAppend extends SubCommand { } @Override - public CommandResult execute(LuckPermsPlugin plugin, Sender sender, Track track, List args, String label) { + public CommandResult execute(LuckPermsPlugin plugin, Sender sender, Track track, List args, String label) throws CommandException { String groupName = args.get(0).toLowerCase(); if (ArgumentChecker.checkNode(groupName)) { diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/track/subcommands/TrackClear.java b/common/src/main/java/me/lucko/luckperms/common/commands/track/subcommands/TrackClear.java index 8110f17d5..682e97c8c 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/track/subcommands/TrackClear.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/track/subcommands/TrackClear.java @@ -23,6 +23,7 @@ package me.lucko.luckperms.common.commands.track.subcommands; import me.lucko.luckperms.common.LuckPermsPlugin; +import me.lucko.luckperms.common.commands.CommandException; import me.lucko.luckperms.common.commands.CommandResult; import me.lucko.luckperms.common.commands.Sender; import me.lucko.luckperms.common.commands.SubCommand; @@ -40,7 +41,7 @@ public class TrackClear extends SubCommand { } @Override - public CommandResult execute(LuckPermsPlugin plugin, Sender sender, Track track, List args, String label) { + public CommandResult execute(LuckPermsPlugin plugin, Sender sender, Track track, List args, String label) throws CommandException { track.clearGroups(); Message.TRACK_CLEAR.send(sender, track.getName()); LogEntry.build().actor(sender).acted(track).action("clear").build().submit(plugin, sender); diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/track/subcommands/TrackClone.java b/common/src/main/java/me/lucko/luckperms/common/commands/track/subcommands/TrackClone.java index b6efa8c25..3898c9817 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/track/subcommands/TrackClone.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/track/subcommands/TrackClone.java @@ -23,10 +23,7 @@ package me.lucko.luckperms.common.commands.track.subcommands; import me.lucko.luckperms.common.LuckPermsPlugin; -import me.lucko.luckperms.common.commands.Arg; -import me.lucko.luckperms.common.commands.CommandResult; -import me.lucko.luckperms.common.commands.Sender; -import me.lucko.luckperms.common.commands.SubCommand; +import me.lucko.luckperms.common.commands.*; import me.lucko.luckperms.common.constants.Message; import me.lucko.luckperms.common.constants.Permission; import me.lucko.luckperms.common.data.LogEntry; @@ -44,7 +41,7 @@ public class TrackClone extends SubCommand { } @Override - public CommandResult execute(LuckPermsPlugin plugin, Sender sender, Track track, List args, String label) { + public CommandResult execute(LuckPermsPlugin plugin, Sender sender, Track track, List args, String label) throws CommandException { String newTrackName = args.get(0).toLowerCase(); if (ArgumentChecker.checkName(newTrackName)) { Message.TRACK_INVALID_ENTRY.send(sender); diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/track/subcommands/TrackInfo.java b/common/src/main/java/me/lucko/luckperms/common/commands/track/subcommands/TrackInfo.java index 11161bf55..5cee99440 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/track/subcommands/TrackInfo.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/track/subcommands/TrackInfo.java @@ -23,10 +23,7 @@ package me.lucko.luckperms.common.commands.track.subcommands; import me.lucko.luckperms.common.LuckPermsPlugin; -import me.lucko.luckperms.common.commands.CommandResult; -import me.lucko.luckperms.common.commands.Sender; -import me.lucko.luckperms.common.commands.SubCommand; -import me.lucko.luckperms.common.commands.Util; +import me.lucko.luckperms.common.commands.*; import me.lucko.luckperms.common.constants.Message; import me.lucko.luckperms.common.constants.Permission; import me.lucko.luckperms.common.tracks.Track; @@ -40,7 +37,7 @@ public class TrackInfo extends SubCommand { } @Override - public CommandResult execute(LuckPermsPlugin plugin, Sender sender, Track track, List args, String label) { + public CommandResult execute(LuckPermsPlugin plugin, Sender sender, Track track, List args, String label) throws CommandException { Message.TRACK_INFO.send(sender, track.getName(), Util.listToArrowSep(track.getGroups())); return CommandResult.SUCCESS; } diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/track/subcommands/TrackInsert.java b/common/src/main/java/me/lucko/luckperms/common/commands/track/subcommands/TrackInsert.java index ce4ea19f4..828ca5c4c 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/track/subcommands/TrackInsert.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/track/subcommands/TrackInsert.java @@ -46,7 +46,7 @@ public class TrackInsert extends SubCommand { } @Override - public CommandResult execute(LuckPermsPlugin plugin, Sender sender, Track track, List args, String label) { + public CommandResult execute(LuckPermsPlugin plugin, Sender sender, Track track, List args, String label) throws CommandException { String groupName = args.get(0).toLowerCase(); if (ArgumentChecker.checkNode(groupName)) { diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/track/subcommands/TrackRemove.java b/common/src/main/java/me/lucko/luckperms/common/commands/track/subcommands/TrackRemove.java index 0f52f879e..74640241b 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/track/subcommands/TrackRemove.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/track/subcommands/TrackRemove.java @@ -42,7 +42,7 @@ public class TrackRemove extends SubCommand { } @Override - public CommandResult execute(LuckPermsPlugin plugin, Sender sender, Track track, List args, String label) { + public CommandResult execute(LuckPermsPlugin plugin, Sender sender, Track track, List args, String label) throws CommandException { String groupName = args.get(0).toLowerCase(); if (ArgumentChecker.checkNode(groupName)) { diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/track/subcommands/TrackRename.java b/common/src/main/java/me/lucko/luckperms/common/commands/track/subcommands/TrackRename.java index 4e7ccc785..a818d783e 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/track/subcommands/TrackRename.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/track/subcommands/TrackRename.java @@ -23,10 +23,7 @@ package me.lucko.luckperms.common.commands.track.subcommands; import me.lucko.luckperms.common.LuckPermsPlugin; -import me.lucko.luckperms.common.commands.Arg; -import me.lucko.luckperms.common.commands.CommandResult; -import me.lucko.luckperms.common.commands.Sender; -import me.lucko.luckperms.common.commands.SubCommand; +import me.lucko.luckperms.common.commands.*; import me.lucko.luckperms.common.constants.Message; import me.lucko.luckperms.common.constants.Permission; import me.lucko.luckperms.common.data.LogEntry; @@ -44,7 +41,7 @@ public class TrackRename extends SubCommand { } @Override - public CommandResult execute(LuckPermsPlugin plugin, Sender sender, Track track, List args, String label) { + public CommandResult execute(LuckPermsPlugin plugin, Sender sender, Track track, List args, String label) throws CommandException { String newTrackName = args.get(0).toLowerCase(); if (ArgumentChecker.checkName(newTrackName)) { Message.TRACK_INVALID_ENTRY.send(sender); diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/user/subcommands/UserBulkChange.java b/common/src/main/java/me/lucko/luckperms/common/commands/user/subcommands/UserBulkChange.java index 3e4ba0e51..cbb608c0f 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/user/subcommands/UserBulkChange.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/user/subcommands/UserBulkChange.java @@ -24,10 +24,7 @@ package me.lucko.luckperms.common.commands.user.subcommands; import me.lucko.luckperms.api.Node; import me.lucko.luckperms.common.LuckPermsPlugin; -import me.lucko.luckperms.common.commands.Arg; -import me.lucko.luckperms.common.commands.CommandResult; -import me.lucko.luckperms.common.commands.Sender; -import me.lucko.luckperms.common.commands.SubCommand; +import me.lucko.luckperms.common.commands.*; import me.lucko.luckperms.common.constants.Message; import me.lucko.luckperms.common.constants.Permission; import me.lucko.luckperms.common.core.NodeFactory; @@ -53,7 +50,7 @@ public class UserBulkChange extends SubCommand { } @Override - public CommandResult execute(LuckPermsPlugin plugin, Sender sender, User user, List args, String label) { + public CommandResult execute(LuckPermsPlugin plugin, Sender sender, User user, List args, String label) throws CommandException { String type = args.get(0).toLowerCase(); String from = args.get(1); String to = args.get(2); diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/user/subcommands/UserClear.java b/common/src/main/java/me/lucko/luckperms/common/commands/user/subcommands/UserClear.java index b3925f72c..47dd5347a 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/user/subcommands/UserClear.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/user/subcommands/UserClear.java @@ -23,15 +23,11 @@ package me.lucko.luckperms.common.commands.user.subcommands; import me.lucko.luckperms.common.LuckPermsPlugin; -import me.lucko.luckperms.common.commands.Arg; -import me.lucko.luckperms.common.commands.CommandResult; -import me.lucko.luckperms.common.commands.Sender; -import me.lucko.luckperms.common.commands.SubCommand; +import me.lucko.luckperms.common.commands.*; import me.lucko.luckperms.common.constants.Message; import me.lucko.luckperms.common.constants.Permission; import me.lucko.luckperms.common.data.LogEntry; import me.lucko.luckperms.common.users.User; -import me.lucko.luckperms.common.utils.ArgumentChecker; import me.lucko.luckperms.common.utils.Predicates; import java.util.List; @@ -48,25 +44,22 @@ public class UserClear extends SubCommand { } @Override - public CommandResult execute(LuckPermsPlugin plugin, Sender sender, User user, List args, String label) { + public CommandResult execute(LuckPermsPlugin plugin, Sender sender, User user, List args, String label) throws CommandException { int before = user.getNodes().size(); - if (args.size() == 0) { - user.clearNodes(); - } else { - final String server = args.get(0); - if (ArgumentChecker.checkServer(server)) { - Message.SERVER_INVALID_ENTRY.send(sender); - return CommandResult.INVALID_ARGS; - } + String server = ArgumentUtils.handleServer(0, args); + String world = ArgumentUtils.handleWorld(1, args); - if (args.size() == 2) { - final String world = args.get(1); - user.clearNodes(server, world); - - } else { + switch (ContextHelper.determine(server, world)) { + case NONE: + user.clearNodes(); + break; + case SERVER: user.clearNodes(server); - } + break; + case SERVER_AND_WORLD: + user.clearNodes(server, world); + break; } int changed = before - user.getNodes().size(); @@ -76,7 +69,10 @@ public class UserClear extends SubCommand { Message.CLEAR_SUCCESS.send(sender, user.getName(), changed); } - LogEntry.build().actor(sender).acted(user).action("clear " + args.stream().collect(Collectors.joining(" "))).build().submit(plugin, sender); + LogEntry.build().actor(sender).acted(user) + .action("clear " + args.stream().map(ArgumentUtils.WRAPPER).collect(Collectors.joining(" "))) + .build().submit(plugin, sender); + save(user, sender, plugin); return CommandResult.SUCCESS; } diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/user/subcommands/UserDemote.java b/common/src/main/java/me/lucko/luckperms/common/commands/user/subcommands/UserDemote.java index a8aefa023..eca1be292 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/user/subcommands/UserDemote.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/user/subcommands/UserDemote.java @@ -59,7 +59,7 @@ public class UserDemote extends SubCommand { } @Override - public CommandResult execute(LuckPermsPlugin plugin, Sender sender, User user, List args, String label) { + public CommandResult execute(LuckPermsPlugin plugin, Sender sender, User user, List args, String label) throws CommandException { final String trackName = args.get(0).toLowerCase(); if (ArgumentChecker.checkName(trackName)) { Message.TRACK_INVALID_ENTRY.send(sender); @@ -82,19 +82,8 @@ public class UserDemote extends SubCommand { return CommandResult.STATE_ERROR; } - String server = null; - String world = null; - - if (args.size() > 1) { - server = args.get(1); - if (ArgumentChecker.checkServer(server)) { - Message.SERVER_INVALID_ENTRY.send(sender); - return CommandResult.INVALID_ARGS; - } - if (args.size() > 2) { - world = args.get(2); - } - } + String server = ArgumentUtils.handleServer(1, args); + String world = ArgumentUtils.handleWorld(2, args); // Load applicable groups Set nodes = new HashSet<>(); diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/user/subcommands/UserGetUUID.java b/common/src/main/java/me/lucko/luckperms/common/commands/user/subcommands/UserGetUUID.java index 232d52761..00c950903 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/user/subcommands/UserGetUUID.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/user/subcommands/UserGetUUID.java @@ -23,6 +23,7 @@ package me.lucko.luckperms.common.commands.user.subcommands; import me.lucko.luckperms.common.LuckPermsPlugin; +import me.lucko.luckperms.common.commands.CommandException; import me.lucko.luckperms.common.commands.CommandResult; import me.lucko.luckperms.common.commands.Sender; import me.lucko.luckperms.common.commands.SubCommand; @@ -39,7 +40,7 @@ public class UserGetUUID extends SubCommand { } @Override - public CommandResult execute(LuckPermsPlugin plugin, Sender sender, User user, List args, String label) { + public CommandResult execute(LuckPermsPlugin plugin, Sender sender, User user, List args, String label) throws CommandException { Message.USER_GETUUID.send(sender, user.getName(), user.getUuid().toString()); return CommandResult.SUCCESS; } diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/user/subcommands/UserInfo.java b/common/src/main/java/me/lucko/luckperms/common/commands/user/subcommands/UserInfo.java index 05d56fcba..f2cb40d92 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/user/subcommands/UserInfo.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/user/subcommands/UserInfo.java @@ -23,6 +23,7 @@ package me.lucko.luckperms.common.commands.user.subcommands; import me.lucko.luckperms.common.LuckPermsPlugin; +import me.lucko.luckperms.common.commands.CommandException; import me.lucko.luckperms.common.commands.CommandResult; import me.lucko.luckperms.common.commands.Sender; import me.lucko.luckperms.common.commands.SubCommand; @@ -39,7 +40,7 @@ public class UserInfo extends SubCommand { } @Override - public CommandResult execute(LuckPermsPlugin plugin, Sender sender, User user, List args, String label) { + public CommandResult execute(LuckPermsPlugin plugin, Sender sender, User user, List args, String label) throws CommandException { Message.USER_INFO.send(sender, user.getName(), user.getUuid(), diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/user/subcommands/UserPromote.java b/common/src/main/java/me/lucko/luckperms/common/commands/user/subcommands/UserPromote.java index def7d944c..136345661 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/user/subcommands/UserPromote.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/user/subcommands/UserPromote.java @@ -59,7 +59,7 @@ public class UserPromote extends SubCommand { } @Override - public CommandResult execute(LuckPermsPlugin plugin, Sender sender, User user, List args, String label) { + public CommandResult execute(LuckPermsPlugin plugin, Sender sender, User user, List args, String label) throws CommandException { final String trackName = args.get(0).toLowerCase(); if (ArgumentChecker.checkName(trackName)) { Message.TRACK_INVALID_ENTRY.send(sender); @@ -82,19 +82,8 @@ public class UserPromote extends SubCommand { return CommandResult.STATE_ERROR; } - String server = null; - String world = null; - - if (args.size() > 1) { - server = args.get(1); - if (ArgumentChecker.checkServer(server)) { - Message.SERVER_INVALID_ENTRY.send(sender); - return CommandResult.INVALID_ARGS; - } - if (args.size() > 2) { - world = args.get(2); - } - } + String server = ArgumentUtils.handleServer(1, args); + String world = ArgumentUtils.handleWorld(2, args); // Load applicable groups Set nodes = new HashSet<>(); diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/user/subcommands/UserSetPrimaryGroup.java b/common/src/main/java/me/lucko/luckperms/common/commands/user/subcommands/UserSetPrimaryGroup.java index 07f281725..dd2f18528 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/user/subcommands/UserSetPrimaryGroup.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/user/subcommands/UserSetPrimaryGroup.java @@ -23,10 +23,7 @@ package me.lucko.luckperms.common.commands.user.subcommands; import me.lucko.luckperms.common.LuckPermsPlugin; -import me.lucko.luckperms.common.commands.Arg; -import me.lucko.luckperms.common.commands.CommandResult; -import me.lucko.luckperms.common.commands.Sender; -import me.lucko.luckperms.common.commands.SubCommand; +import me.lucko.luckperms.common.commands.*; import me.lucko.luckperms.common.constants.Message; import me.lucko.luckperms.common.constants.Permission; import me.lucko.luckperms.common.data.LogEntry; @@ -45,7 +42,7 @@ public class UserSetPrimaryGroup extends SubCommand { } @Override - public CommandResult execute(LuckPermsPlugin plugin, Sender sender, User user, List args, String label) { + public CommandResult execute(LuckPermsPlugin plugin, Sender sender, User user, List args, String label) throws CommandException { Group group = plugin.getGroupManager().get(args.get(0).toLowerCase()); if (group == null) { Message.GROUP_DOES_NOT_EXIST.send(sender); diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/usersbulkedit/subcommands/BulkEditGroup.java b/common/src/main/java/me/lucko/luckperms/common/commands/usersbulkedit/subcommands/BulkEditGroup.java index a0c9b8e49..b0aed9415 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/usersbulkedit/subcommands/BulkEditGroup.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/usersbulkedit/subcommands/BulkEditGroup.java @@ -24,10 +24,7 @@ package me.lucko.luckperms.common.commands.usersbulkedit.subcommands; import me.lucko.luckperms.api.Node; import me.lucko.luckperms.common.LuckPermsPlugin; -import me.lucko.luckperms.common.commands.Arg; -import me.lucko.luckperms.common.commands.CommandResult; -import me.lucko.luckperms.common.commands.Sender; -import me.lucko.luckperms.common.commands.SubCommand; +import me.lucko.luckperms.common.commands.*; import me.lucko.luckperms.common.constants.Message; import me.lucko.luckperms.common.constants.Permission; import me.lucko.luckperms.common.core.NodeFactory; @@ -52,7 +49,7 @@ public class BulkEditGroup extends SubCommand { } @Override - public CommandResult execute(LuckPermsPlugin plugin, Sender sender, Datastore datastore, List args, String label) { + public CommandResult execute(LuckPermsPlugin plugin, Sender sender, Datastore datastore, List args, String label) throws CommandException { String group = args.get(0); String type = args.get(1).toLowerCase(); String from = args.get(2); diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/usersbulkedit/subcommands/BulkEditPermission.java b/common/src/main/java/me/lucko/luckperms/common/commands/usersbulkedit/subcommands/BulkEditPermission.java index 225c61919..d741bc725 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/usersbulkedit/subcommands/BulkEditPermission.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/usersbulkedit/subcommands/BulkEditPermission.java @@ -24,10 +24,7 @@ package me.lucko.luckperms.common.commands.usersbulkedit.subcommands; import me.lucko.luckperms.api.Node; import me.lucko.luckperms.common.LuckPermsPlugin; -import me.lucko.luckperms.common.commands.Arg; -import me.lucko.luckperms.common.commands.CommandResult; -import me.lucko.luckperms.common.commands.Sender; -import me.lucko.luckperms.common.commands.SubCommand; +import me.lucko.luckperms.common.commands.*; import me.lucko.luckperms.common.constants.Message; import me.lucko.luckperms.common.constants.Permission; import me.lucko.luckperms.common.core.NodeFactory; @@ -52,7 +49,7 @@ public class BulkEditPermission extends SubCommand { } @Override - public CommandResult execute(LuckPermsPlugin plugin, Sender sender, Datastore datastore, List args, String label) { + public CommandResult execute(LuckPermsPlugin plugin, Sender sender, Datastore datastore, List args, String label) throws CommandException { String node = args.get(0); String type = args.get(1).toLowerCase(); String from = args.get(2); diff --git a/common/src/main/java/me/lucko/luckperms/common/constants/Message.java b/common/src/main/java/me/lucko/luckperms/common/constants/Message.java index 7a5613c57..15336113e 100644 --- a/common/src/main/java/me/lucko/luckperms/common/constants/Message.java +++ b/common/src/main/java/me/lucko/luckperms/common/constants/Message.java @@ -77,8 +77,7 @@ public enum Message { GROUP_INVALID_ENTRY("Group names can only contain alphanumeric characters.", true), TRACK_INVALID_ENTRY("Track names can only contain alphanumeric characters.", true), SERVER_INVALID_ENTRY("Server names can only contain alphanumeric characters.", true), - USE_INHERIT_COMMAND("Use the 'parent add' command instead of specifying the node.", true), - USE_UNINHERIT_COMMAND("Use the 'parent remove' command instead of specifying the node.", true), + USE_INHERIT_COMMAND("Use the 'parent add' and 'parent remove' commands instead of specifying the node.", true), /* diff --git a/default-lang.yml b/default-lang.yml index 2060d1243..a15d2051d 100644 --- a/default-lang.yml +++ b/default-lang.yml @@ -41,7 +41,6 @@ track-invalid-entry: "Track names can only contain alphanumeric characters." server-invalid-entry: "Server names can only contain alphanumeric characters." use-inherit-command: "Use the 'parent add' command instead of specifying the node." -use-uninherit-command: "Use the 'parent remove' command instead of specifying the node." diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/migration/MigrationPermissionsEx.java b/sponge/src/main/java/me/lucko/luckperms/sponge/migration/MigrationPermissionsEx.java index f211919e0..2199d276f 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/migration/MigrationPermissionsEx.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/migration/MigrationPermissionsEx.java @@ -26,10 +26,7 @@ import me.lucko.luckperms.api.Logger; import me.lucko.luckperms.api.context.ContextSet; import me.lucko.luckperms.api.context.MutableContextSet; import me.lucko.luckperms.common.LuckPermsPlugin; -import me.lucko.luckperms.common.commands.CommandResult; -import me.lucko.luckperms.common.commands.Sender; -import me.lucko.luckperms.common.commands.SubCommand; -import me.lucko.luckperms.common.commands.Util; +import me.lucko.luckperms.common.commands.*; import me.lucko.luckperms.common.constants.Permission; import me.lucko.luckperms.common.core.NodeBuilder; import me.lucko.luckperms.common.core.PermissionHolder; @@ -52,7 +49,7 @@ public class MigrationPermissionsEx extends SubCommand { } @Override - public CommandResult execute(LuckPermsPlugin plugin, Sender sender, Object o, List args, String label) { + public CommandResult execute(LuckPermsPlugin plugin, Sender sender, Object o, List args, String label) throws CommandException { final Logger log = plugin.getLog(); Optional pex = Sponge.getPluginManager().getPlugin("permissionsex");