diff --git a/common/src/main/java/me/lucko/luckperms/common/LuckPermsPlugin.java b/common/src/main/java/me/lucko/luckperms/common/LuckPermsPlugin.java index 426454d6..39fc4962 100644 --- a/common/src/main/java/me/lucko/luckperms/common/LuckPermsPlugin.java +++ b/common/src/main/java/me/lucko/luckperms/common/LuckPermsPlugin.java @@ -27,6 +27,7 @@ import me.lucko.luckperms.api.Logger; import me.lucko.luckperms.api.PlatformType; import me.lucko.luckperms.common.api.ApiProvider; import me.lucko.luckperms.common.calculators.CalculatorFactory; +import me.lucko.luckperms.common.commands.BaseCommand; import me.lucko.luckperms.common.commands.ConsecutiveExecutor; import me.lucko.luckperms.common.commands.sender.Sender; import me.lucko.luckperms.common.config.LPConfiguration; @@ -44,6 +45,7 @@ import me.lucko.luckperms.common.utils.DebugHandler; import me.lucko.luckperms.common.utils.LocaleManager; import java.io.File; +import java.util.Collections; import java.util.List; import java.util.Set; import java.util.UUID; @@ -247,6 +249,10 @@ public interface LuckPermsPlugin { */ Set getPreProcessContexts(boolean op); + default List getExtraCommands() { + return Collections.emptyList(); + } + /** * Gets a set of players ignoring logging output * @return a {@link Set} of {@link UUID}s diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/CommandManager.java b/common/src/main/java/me/lucko/luckperms/common/commands/CommandManager.java index b6b2687c..6660a8db 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/CommandManager.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/CommandManager.java @@ -23,7 +23,6 @@ package me.lucko.luckperms.common.commands; import com.google.common.collect.ImmutableList; -import lombok.AllArgsConstructor; import lombok.Getter; import me.lucko.luckperms.common.LuckPermsPlugin; import me.lucko.luckperms.common.commands.group.CreateGroup; @@ -51,34 +50,41 @@ import java.util.Optional; import java.util.function.Consumer; import java.util.stream.Collectors; -@AllArgsConstructor public class CommandManager { @Getter private final LuckPermsPlugin plugin; @Getter - private final List mainCommands = ImmutableList.builder() - .add(new UserMainCommand()) - .add(new GroupMainCommand()) - .add(new TrackMainCommand()) - .add(new LogMainCommand()) - .add(new SyncCommand()) - .add(new NetworkSyncCommand()) - .add(new InfoCommand()) - .add(new DebugCommand()) - .add(new VerboseCommand()) - .add(new ImportCommand()) - .add(new ExportCommand()) - .add(new QueueCommand()) - .add(new MigrationMainCommand()) - .add(new UsersBulkEditMainCommand()) - .add(new CreateGroup()) - .add(new DeleteGroup()) - .add(new ListGroups()) - .add(new CreateTrack()) - .add(new DeleteTrack()) - .add(new ListTracks()) - .build(); + private final List mainCommands; + + public CommandManager(LuckPermsPlugin plugin) { + this.plugin = plugin; + + ImmutableList.Builder l = ImmutableList.builder(); + l.add(new UserMainCommand()) + .add(new GroupMainCommand()) + .add(new TrackMainCommand()) + .addAll(plugin.getExtraCommands()) + .add(new LogMainCommand()) + .add(new SyncCommand()) + .add(new NetworkSyncCommand()) + .add(new InfoCommand()) + .add(new DebugCommand()) + .add(new VerboseCommand()) + .add(new ImportCommand()) + .add(new ExportCommand()) + .add(new QueueCommand()) + .add(new MigrationMainCommand()) + .add(new UsersBulkEditMainCommand()) + .add(new CreateGroup()) + .add(new DeleteGroup()) + .add(new ListGroups()) + .add(new CreateTrack()) + .add(new DeleteTrack()) + .add(new ListTracks()); + + mainCommands = l.build(); + } /** * Generic on command method to be called from the command executor object of the platform diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/utils/ArgumentUtils.java b/common/src/main/java/me/lucko/luckperms/common/commands/utils/ArgumentUtils.java index 775b0110..ef0f20cc 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/utils/ArgumentUtils.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/utils/ArgumentUtils.java @@ -24,6 +24,8 @@ package me.lucko.luckperms.common.commands.utils; import lombok.AllArgsConstructor; import lombok.Getter; +import me.lucko.luckperms.api.context.ContextSet; +import me.lucko.luckperms.api.context.MutableContextSet; import me.lucko.luckperms.common.commands.CommandException; import me.lucko.luckperms.common.utils.ArgumentChecker; import me.lucko.luckperms.common.utils.DateUtil; @@ -112,6 +114,33 @@ public class ArgumentUtils { } } + public static ContextSet handleContexts(int fromIndex, List args) { + if (args.size() <= fromIndex) { + return ContextSet.empty(); + } + + MutableContextSet contextSet = new MutableContextSet(); + List toQuery = args.subList(fromIndex, args.size()); + for (String s : toQuery) { + int index = s.indexOf('='); + if (index != -1) { + String key = s.substring(0, index); + if (key.equals("")) { + continue; + } + + String value = s.substring(index + 1); + if (value.equals("")) { + continue; + } + + contextSet.add(key, value); + } + } + + return contextSet.makeImmutable(); + } + public static abstract class ArgumentException extends CommandException {} public static class DetailedUsageException extends ArgumentException {} public static class UseInheritException extends ArgumentException {} diff --git a/common/src/main/java/me/lucko/luckperms/common/constants/Permission.java b/common/src/main/java/me/lucko/luckperms/common/constants/Permission.java index 82a99a8e..7ea31269 100644 --- a/common/src/main/java/me/lucko/luckperms/common/constants/Permission.java +++ b/common/src/main/java/me/lucko/luckperms/common/constants/Permission.java @@ -134,6 +134,18 @@ public enum Permission { LOG_NOTIFY(set("notify"), Type.LOG), LOG_EXPORT(set("export"), Type.LOG), + SPONGE_PERMISSION_INFO(set("permission.info"), Type.SPONGE), + SPONGE_PERMISSION_SET(set("permission.set"), Type.SPONGE), + SPONGE_PERMISSION_CLEAR(set("permission.clear"), Type.SPONGE), + SPONGE_PARENT_INFO(set("parent.info"), Type.SPONGE), + SPONGE_PARENT_ADD(set("parent.add"), Type.SPONGE), + SPONGE_PARENT_REMOVE(set("parent.remove"), Type.SPONGE), + SPONGE_PARENT_CLEAR(set("parent.clear"), Type.SPONGE), + SPONGE_OPTION_INFO(set("option.info"), Type.SPONGE), + SPONGE_OPTION_SET(set("option.set"), Type.SPONGE), + SPONGE_OPTION_UNSET(set("option.unset"), Type.SPONGE), + SPONGE_OPTION_CLEAR(set("option.clear"), Type.SPONGE), + MIGRATION(set("migration"), Type.NONE); private static final String IDENTIFIER = "luckperms."; @@ -167,7 +179,8 @@ public enum Permission { USER("user"), GROUP("group"), TRACK("track"), - LOG("log"); + LOG("log"), + SPONGE("sponge"); private final String tag; diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/LPSpongePlugin.java b/sponge/src/main/java/me/lucko/luckperms/sponge/LPSpongePlugin.java index 89c55879..df431a9b 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/LPSpongePlugin.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/LPSpongePlugin.java @@ -31,6 +31,7 @@ import me.lucko.luckperms.api.PlatformType; import me.lucko.luckperms.common.LuckPermsPlugin; import me.lucko.luckperms.common.api.ApiProvider; import me.lucko.luckperms.common.calculators.CalculatorFactory; +import me.lucko.luckperms.common.commands.BaseCommand; import me.lucko.luckperms.common.commands.ConsecutiveExecutor; import me.lucko.luckperms.common.commands.sender.Sender; import me.lucko.luckperms.common.config.LPConfiguration; @@ -51,6 +52,7 @@ import me.lucko.luckperms.common.utils.BufferedRequest; import me.lucko.luckperms.common.utils.DebugHandler; import me.lucko.luckperms.common.utils.LocaleManager; import me.lucko.luckperms.common.utils.LogFactory; +import me.lucko.luckperms.sponge.commands.SpongeMainCommand; import me.lucko.luckperms.sponge.contexts.WorldCalculator; import me.lucko.luckperms.sponge.service.LuckPermsService; import me.lucko.luckperms.sponge.timings.LPTimings; @@ -375,6 +377,11 @@ public class LPSpongePlugin implements LuckPermsPlugin { scheduler.createTaskBuilder().async().intervalTicks(interval).execute(r).submit(this); } + @Override + public List getExtraCommands() { + return Collections.singletonList(new SpongeMainCommand(this)); + } + private void registerPermission(PermissionService p, String node) { Optional builder = p.newDescriptionBuilder(this); if (!builder.isPresent()) return; diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/commands/OptionClear.java b/sponge/src/main/java/me/lucko/luckperms/sponge/commands/OptionClear.java new file mode 100644 index 00000000..4274cb03 --- /dev/null +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/commands/OptionClear.java @@ -0,0 +1,61 @@ +/* + * 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.sponge.commands; + +import me.lucko.luckperms.api.context.ContextSet; +import me.lucko.luckperms.common.LuckPermsPlugin; +import me.lucko.luckperms.common.commands.Arg; +import me.lucko.luckperms.common.commands.CommandException; +import me.lucko.luckperms.common.commands.CommandResult; +import me.lucko.luckperms.common.commands.SubCommand; +import me.lucko.luckperms.common.commands.sender.Sender; +import me.lucko.luckperms.common.commands.utils.ArgumentUtils; +import me.lucko.luckperms.common.commands.utils.Util; +import me.lucko.luckperms.common.constants.Permission; +import me.lucko.luckperms.common.utils.Predicates; +import org.spongepowered.api.service.permission.SubjectData; + +import java.util.List; + +public class OptionClear extends SubCommand { + public OptionClear() { + super("clear", "Clears the Subjects options", Permission.SPONGE_OPTION_CLEAR, Predicates.alwaysFalse(), + Arg.list( + Arg.create("contexts...", false, "the contexts to clear options in") + ) + ); + } + + @Override + public CommandResult execute(LuckPermsPlugin plugin, Sender sender, SubjectData subjectData, List args, String label) throws CommandException { + ContextSet contextSet = ArgumentUtils.handleContexts(0, args); + if (contextSet.isEmpty()) { + subjectData.clearOptions(); + Util.sendPluginMessage(sender, "&aCleared options matching contexts &bANY&a."); + } else { + subjectData.clearOptions(SpongeUtils.convertContexts(contextSet)); + Util.sendPluginMessage(sender, "&aCleared options matching contexts &b" + SpongeUtils.contextToString(contextSet)); + } + return CommandResult.SUCCESS; + } +} diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/commands/OptionInfo.java b/sponge/src/main/java/me/lucko/luckperms/sponge/commands/OptionInfo.java new file mode 100644 index 00000000..434329fe --- /dev/null +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/commands/OptionInfo.java @@ -0,0 +1,79 @@ +/* + * 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.sponge.commands; + +import me.lucko.luckperms.api.context.ContextSet; +import me.lucko.luckperms.common.LuckPermsPlugin; +import me.lucko.luckperms.common.commands.Arg; +import me.lucko.luckperms.common.commands.CommandException; +import me.lucko.luckperms.common.commands.CommandResult; +import me.lucko.luckperms.common.commands.SubCommand; +import me.lucko.luckperms.common.commands.sender.Sender; +import me.lucko.luckperms.common.commands.utils.ArgumentUtils; +import me.lucko.luckperms.common.commands.utils.Util; +import me.lucko.luckperms.common.constants.Permission; +import me.lucko.luckperms.common.utils.Predicates; +import org.spongepowered.api.service.context.Context; +import org.spongepowered.api.service.permission.SubjectData; + +import java.util.List; +import java.util.Map; +import java.util.Set; + +public class OptionInfo extends SubCommand { + public OptionInfo() { + super("info", "Shows info about the subject's options", Permission.SPONGE_OPTION_INFO, Predicates.alwaysFalse(), + Arg.list(Arg.create("contexts...", false, "the contexts to filter by")) + ); + } + + @Override + public CommandResult execute(LuckPermsPlugin plugin, Sender sender, SubjectData subjectData, List args, String label) throws CommandException { + ContextSet contextSet = ArgumentUtils.handleContexts(0, args); + if (contextSet.isEmpty()) { + Util.sendPluginMessage(sender, "&aShowing options matching contexts &bANY&a."); + Map, Map> options = subjectData.getAllOptions(); + if (options.isEmpty()) { + Util.sendPluginMessage(sender, "That subject does not have any options defined."); + return CommandResult.SUCCESS; + } + + for (Map.Entry, Map> e : options.entrySet()) { + ContextSet set = SpongeUtils.convertContexts(e.getKey()); + Util.sendPluginMessage(sender, "&3>> &bContext: " + SpongeUtils.contextToString(set) + "\n" + SpongeUtils.optionsToString(e.getValue())); + } + + } else { + Map options = subjectData.getOptions(SpongeUtils.convertContexts(contextSet)); + if (options.isEmpty()) { + Util.sendPluginMessage(sender, "That subject does not have any options defined in those contexts."); + return CommandResult.SUCCESS; + } + + Util.sendPluginMessage(sender, "&aShowing options matching contexts &b" + + SpongeUtils.contextToString(contextSet) + "&a.\n" + SpongeUtils.optionsToString(options)); + + } + return CommandResult.SUCCESS; + } +} diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/commands/OptionSet.java b/sponge/src/main/java/me/lucko/luckperms/sponge/commands/OptionSet.java new file mode 100644 index 00000000..681cc268 --- /dev/null +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/commands/OptionSet.java @@ -0,0 +1,65 @@ +/* + * 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.sponge.commands; + +import me.lucko.luckperms.api.context.ContextSet; +import me.lucko.luckperms.common.LuckPermsPlugin; +import me.lucko.luckperms.common.commands.Arg; +import me.lucko.luckperms.common.commands.CommandException; +import me.lucko.luckperms.common.commands.CommandResult; +import me.lucko.luckperms.common.commands.SubCommand; +import me.lucko.luckperms.common.commands.sender.Sender; +import me.lucko.luckperms.common.commands.utils.ArgumentUtils; +import me.lucko.luckperms.common.commands.utils.Util; +import me.lucko.luckperms.common.constants.Permission; +import me.lucko.luckperms.common.utils.Predicates; +import org.spongepowered.api.service.permission.SubjectData; + +import java.util.List; + +public class OptionSet extends SubCommand { + public OptionSet() { + super("set", "Sets an option for the Subject", Permission.SPONGE_OPTION_SET, Predicates.inRange(0, 1), + Arg.list( + Arg.create("key", true, "the key to set"), + Arg.create("value", true, "the value to set the key to"), + Arg.create("contexts...", false, "the contexts to set the option in") + ) + ); + } + + @Override + public CommandResult execute(LuckPermsPlugin plugin, Sender sender, SubjectData subjectData, List args, String label) throws CommandException { + String key = args.get(0); + String value = args.get(1); + ContextSet contextSet = ArgumentUtils.handleContexts(2, args); + + if (subjectData.setOption(SpongeUtils.convertContexts(contextSet), key, value)) { + Util.sendPluginMessage(sender, "&aSet &f\"" + key + "&f\"&a to &f\"" + value + "&f\"&a in context " + SpongeUtils.contextToString(contextSet)); + } else { + Util.sendPluginMessage(sender, "Unable to set option. Does the Subject already have it set?"); + } + + return CommandResult.SUCCESS; + } +} diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/commands/OptionUnset.java b/sponge/src/main/java/me/lucko/luckperms/sponge/commands/OptionUnset.java new file mode 100644 index 00000000..5e51be31 --- /dev/null +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/commands/OptionUnset.java @@ -0,0 +1,62 @@ +/* + * 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.sponge.commands; + +import me.lucko.luckperms.api.context.ContextSet; +import me.lucko.luckperms.common.LuckPermsPlugin; +import me.lucko.luckperms.common.commands.Arg; +import me.lucko.luckperms.common.commands.CommandException; +import me.lucko.luckperms.common.commands.CommandResult; +import me.lucko.luckperms.common.commands.SubCommand; +import me.lucko.luckperms.common.commands.sender.Sender; +import me.lucko.luckperms.common.commands.utils.ArgumentUtils; +import me.lucko.luckperms.common.commands.utils.Util; +import me.lucko.luckperms.common.constants.Permission; +import me.lucko.luckperms.common.utils.Predicates; +import org.spongepowered.api.service.permission.SubjectData; + +import java.util.List; + +public class OptionUnset extends SubCommand { + public OptionUnset() { + super("unset", "Unsets an option for the Subject", Permission.SPONGE_OPTION_UNSET, Predicates.is(0), + Arg.list( + Arg.create("key", true, "the key to unset"), + Arg.create("contexts...", false, "the contexts to unset the key in") + ) + ); + } + + @Override + public CommandResult execute(LuckPermsPlugin plugin, Sender sender, SubjectData subjectData, List args, String label) throws CommandException { + String key = args.get(0); + ContextSet contextSet = ArgumentUtils.handleContexts(1, args); + + if (subjectData.setOption(SpongeUtils.convertContexts(contextSet), key, null)) { + Util.sendPluginMessage(sender, "&aUnset &f\"" + key + "&f\"&a in context " + SpongeUtils.contextToString(contextSet)); + } else { + Util.sendPluginMessage(sender, "Unable to unset option. Are you sure the Subject has it set?"); + } + return CommandResult.SUCCESS; + } +} diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/commands/ParentAdd.java b/sponge/src/main/java/me/lucko/luckperms/sponge/commands/ParentAdd.java new file mode 100644 index 00000000..cc69229a --- /dev/null +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/commands/ParentAdd.java @@ -0,0 +1,81 @@ +/* + * 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.sponge.commands; + +import me.lucko.luckperms.api.context.ContextSet; +import me.lucko.luckperms.common.LuckPermsPlugin; +import me.lucko.luckperms.common.commands.Arg; +import me.lucko.luckperms.common.commands.CommandException; +import me.lucko.luckperms.common.commands.CommandResult; +import me.lucko.luckperms.common.commands.SubCommand; +import me.lucko.luckperms.common.commands.sender.Sender; +import me.lucko.luckperms.common.commands.utils.ArgumentUtils; +import me.lucko.luckperms.common.commands.utils.Util; +import me.lucko.luckperms.common.constants.Permission; +import me.lucko.luckperms.common.utils.Predicates; +import org.spongepowered.api.Sponge; +import org.spongepowered.api.service.permission.PermissionService; +import org.spongepowered.api.service.permission.Subject; +import org.spongepowered.api.service.permission.SubjectCollection; +import org.spongepowered.api.service.permission.SubjectData; + +import java.util.List; + +public class ParentAdd extends SubCommand { + public ParentAdd() { + super("add", "Adds a parent to the Subject", Permission.SPONGE_PARENT_ADD, Predicates.inRange(0, 1), + Arg.list( + Arg.create("collection", true, "the subject collection where the parent Subject is"), + Arg.create("subject", true, "the name of the parent Subject"), + Arg.create("contexts...", false, "the contexts to add the parent in") + ) + ); + } + + @Override + public CommandResult execute(LuckPermsPlugin plugin, Sender sender, SubjectData subjectData, List args, String label) throws CommandException { + String collection = args.get(0); + String name = args.get(1); + ContextSet contextSet = ArgumentUtils.handleContexts(2, args); + + PermissionService service = Sponge.getServiceManager().provideUnchecked(PermissionService.class); + if (service.getKnownSubjects().keySet().stream().map(String::toLowerCase).noneMatch(s -> s.equalsIgnoreCase(collection))) { + Util.sendPluginMessage(sender, "Warning: SubjectCollection '&4" + collection + "&c' doesn't already exist."); + } + + SubjectCollection c = service.getSubjects(collection); + if (!c.hasRegistered(name)) { + Util.sendPluginMessage(sender, "Warning: Subject '&4" + name + "&c' doesn't already exist."); + } + + Subject subject = c.get(name); + + if (subjectData.addParent(SpongeUtils.convertContexts(contextSet), subject)) { + Util.sendPluginMessage(sender, "&aAdded parent &b" + subject.getContainingCollection().getIdentifier() + + "&a/&b" + subject.getIdentifier() + "&a in context " + SpongeUtils.contextToString(contextSet)); + } else { + Util.sendPluginMessage(sender, "Unable to add parent. Does the Subject already have it added?"); + } + return CommandResult.SUCCESS; + } +} diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/commands/ParentClear.java b/sponge/src/main/java/me/lucko/luckperms/sponge/commands/ParentClear.java new file mode 100644 index 00000000..976f8e0b --- /dev/null +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/commands/ParentClear.java @@ -0,0 +1,61 @@ +/* + * 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.sponge.commands; + +import me.lucko.luckperms.api.context.ContextSet; +import me.lucko.luckperms.common.LuckPermsPlugin; +import me.lucko.luckperms.common.commands.Arg; +import me.lucko.luckperms.common.commands.CommandException; +import me.lucko.luckperms.common.commands.CommandResult; +import me.lucko.luckperms.common.commands.SubCommand; +import me.lucko.luckperms.common.commands.sender.Sender; +import me.lucko.luckperms.common.commands.utils.ArgumentUtils; +import me.lucko.luckperms.common.commands.utils.Util; +import me.lucko.luckperms.common.constants.Permission; +import me.lucko.luckperms.common.utils.Predicates; +import org.spongepowered.api.service.permission.SubjectData; + +import java.util.List; + +public class ParentClear extends SubCommand { + public ParentClear() { + super("clear", "Clears the Subjects parents", Permission.SPONGE_PARENT_CLEAR, Predicates.alwaysFalse(), + Arg.list( + Arg.create("contexts...", false, "the contexts to clear parents in") + ) + ); + } + + @Override + public CommandResult execute(LuckPermsPlugin plugin, Sender sender, SubjectData subjectData, List args, String label) throws CommandException { + ContextSet contextSet = ArgumentUtils.handleContexts(0, args); + if (contextSet.isEmpty()) { + subjectData.clearParents(); + Util.sendPluginMessage(sender, "&aCleared parents matching contexts &bANY&a."); + } else { + subjectData.clearParents(SpongeUtils.convertContexts(contextSet)); + Util.sendPluginMessage(sender, "&aCleared parents matching contexts &b" + SpongeUtils.contextToString(contextSet)); + } + return CommandResult.SUCCESS; + } +} diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/commands/ParentInfo.java b/sponge/src/main/java/me/lucko/luckperms/sponge/commands/ParentInfo.java new file mode 100644 index 00000000..425a48e6 --- /dev/null +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/commands/ParentInfo.java @@ -0,0 +1,80 @@ +/* + * 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.sponge.commands; + +import me.lucko.luckperms.api.context.ContextSet; +import me.lucko.luckperms.common.LuckPermsPlugin; +import me.lucko.luckperms.common.commands.Arg; +import me.lucko.luckperms.common.commands.CommandException; +import me.lucko.luckperms.common.commands.CommandResult; +import me.lucko.luckperms.common.commands.SubCommand; +import me.lucko.luckperms.common.commands.sender.Sender; +import me.lucko.luckperms.common.commands.utils.ArgumentUtils; +import me.lucko.luckperms.common.commands.utils.Util; +import me.lucko.luckperms.common.constants.Permission; +import me.lucko.luckperms.common.utils.Predicates; +import org.spongepowered.api.service.context.Context; +import org.spongepowered.api.service.permission.Subject; +import org.spongepowered.api.service.permission.SubjectData; + +import java.util.List; +import java.util.Map; +import java.util.Set; + +public class ParentInfo extends SubCommand { + public ParentInfo() { + super("info", "Shows info about the subject's parents", Permission.SPONGE_PARENT_INFO, Predicates.alwaysFalse(), + Arg.list(Arg.create("contexts...", false, "the contexts to filter by")) + ); + } + + @Override + public CommandResult execute(LuckPermsPlugin plugin, Sender sender, SubjectData subjectData, List args, String label) throws CommandException { + ContextSet contextSet = ArgumentUtils.handleContexts(0, args); + if (contextSet.isEmpty()) { + Util.sendPluginMessage(sender, "&aShowing parents matching contexts &bANY&a."); + Map, List> parents = subjectData.getAllParents(); + if (parents.isEmpty()) { + Util.sendPluginMessage(sender, "That subject does not have any parents defined."); + return CommandResult.SUCCESS; + } + + for (Map.Entry, List> e : parents.entrySet()) { + ContextSet set = SpongeUtils.convertContexts(e.getKey()); + Util.sendPluginMessage(sender, "&3>> &bContext: " + SpongeUtils.contextToString(set) + "\n" + SpongeUtils.parentsToString(e.getValue())); + } + + } else { + List parents = subjectData.getParents(SpongeUtils.convertContexts(contextSet)); + if (parents.isEmpty()) { + Util.sendPluginMessage(sender, "That subject does not have any parents defined in those contexts."); + return CommandResult.SUCCESS; + } + + Util.sendPluginMessage(sender, "&aShowing parents matching contexts &b" + + SpongeUtils.contextToString(contextSet) + "&a.\n" + SpongeUtils.parentsToString(parents)); + + } + return CommandResult.SUCCESS; + } +} diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/commands/ParentRemove.java b/sponge/src/main/java/me/lucko/luckperms/sponge/commands/ParentRemove.java new file mode 100644 index 00000000..60097af7 --- /dev/null +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/commands/ParentRemove.java @@ -0,0 +1,81 @@ +/* + * 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.sponge.commands; + +import me.lucko.luckperms.api.context.ContextSet; +import me.lucko.luckperms.common.LuckPermsPlugin; +import me.lucko.luckperms.common.commands.Arg; +import me.lucko.luckperms.common.commands.CommandException; +import me.lucko.luckperms.common.commands.CommandResult; +import me.lucko.luckperms.common.commands.SubCommand; +import me.lucko.luckperms.common.commands.sender.Sender; +import me.lucko.luckperms.common.commands.utils.ArgumentUtils; +import me.lucko.luckperms.common.commands.utils.Util; +import me.lucko.luckperms.common.constants.Permission; +import me.lucko.luckperms.common.utils.Predicates; +import org.spongepowered.api.Sponge; +import org.spongepowered.api.service.permission.PermissionService; +import org.spongepowered.api.service.permission.Subject; +import org.spongepowered.api.service.permission.SubjectCollection; +import org.spongepowered.api.service.permission.SubjectData; + +import java.util.List; + +public class ParentRemove extends SubCommand { + public ParentRemove() { + super("remove", "Removes a parent from the Subject", Permission.SPONGE_PARENT_REMOVE, Predicates.inRange(0, 1), + Arg.list( + Arg.create("collection", true, "the subject collection where the parent Subject is"), + Arg.create("subject", true, "the name of the parent Subject"), + Arg.create("contexts...", false, "the contexts to remove the parent in") + ) + ); + } + + @Override + public CommandResult execute(LuckPermsPlugin plugin, Sender sender, SubjectData subjectData, List args, String label) throws CommandException { + String collection = args.get(0); + String name = args.get(1); + ContextSet contextSet = ArgumentUtils.handleContexts(2, args); + + PermissionService service = Sponge.getServiceManager().provideUnchecked(PermissionService.class); + if (service.getKnownSubjects().keySet().stream().map(String::toLowerCase).noneMatch(s -> s.equalsIgnoreCase(collection))) { + Util.sendPluginMessage(sender, "Warning: SubjectCollection '&4" + collection + "&c' doesn't exist."); + } + + SubjectCollection c = service.getSubjects(collection); + if (!c.hasRegistered(name)) { + Util.sendPluginMessage(sender, "Warning: Subject '&4" + name + "&c' doesn't exist."); + } + + Subject subject = c.get(name); + + if (subjectData.removeParent(SpongeUtils.convertContexts(contextSet), subject)) { + Util.sendPluginMessage(sender, "&aRemoved parent &b" + subject.getContainingCollection().getIdentifier() + + "&a/&b" + subject.getIdentifier() + "&a in context " + SpongeUtils.contextToString(contextSet)); + } else { + Util.sendPluginMessage(sender, "Unable to remove parent. Are you sure the Subject has it added?"); + } + return CommandResult.SUCCESS; + } +} diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/commands/PermissionClear.java b/sponge/src/main/java/me/lucko/luckperms/sponge/commands/PermissionClear.java new file mode 100644 index 00000000..9d97227d --- /dev/null +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/commands/PermissionClear.java @@ -0,0 +1,61 @@ +/* + * 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.sponge.commands; + +import me.lucko.luckperms.api.context.ContextSet; +import me.lucko.luckperms.common.LuckPermsPlugin; +import me.lucko.luckperms.common.commands.Arg; +import me.lucko.luckperms.common.commands.CommandException; +import me.lucko.luckperms.common.commands.CommandResult; +import me.lucko.luckperms.common.commands.SubCommand; +import me.lucko.luckperms.common.commands.sender.Sender; +import me.lucko.luckperms.common.commands.utils.ArgumentUtils; +import me.lucko.luckperms.common.commands.utils.Util; +import me.lucko.luckperms.common.constants.Permission; +import me.lucko.luckperms.common.utils.Predicates; +import org.spongepowered.api.service.permission.SubjectData; + +import java.util.List; + +public class PermissionClear extends SubCommand { + public PermissionClear() { + super("clear", "Clears the Subjects permissions", Permission.SPONGE_PERMISSION_CLEAR, Predicates.alwaysFalse(), + Arg.list( + Arg.create("contexts...", false, "the contexts to clear permissions in") + ) + ); + } + + @Override + public CommandResult execute(LuckPermsPlugin plugin, Sender sender, SubjectData subjectData, List args, String label) throws CommandException { + ContextSet contextSet = ArgumentUtils.handleContexts(0, args); + if (contextSet.isEmpty()) { + subjectData.clearPermissions(); + Util.sendPluginMessage(sender, "&aCleared permissions matching contexts &bANY&a."); + } else { + subjectData.clearPermissions(SpongeUtils.convertContexts(contextSet)); + Util.sendPluginMessage(sender, "&aCleared permissions matching contexts &b" + SpongeUtils.contextToString(contextSet)); + } + return CommandResult.SUCCESS; + } +} diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/commands/PermissionInfo.java b/sponge/src/main/java/me/lucko/luckperms/sponge/commands/PermissionInfo.java new file mode 100644 index 00000000..ae08114d --- /dev/null +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/commands/PermissionInfo.java @@ -0,0 +1,79 @@ +/* + * 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.sponge.commands; + +import me.lucko.luckperms.api.context.ContextSet; +import me.lucko.luckperms.common.LuckPermsPlugin; +import me.lucko.luckperms.common.commands.Arg; +import me.lucko.luckperms.common.commands.CommandException; +import me.lucko.luckperms.common.commands.CommandResult; +import me.lucko.luckperms.common.commands.SubCommand; +import me.lucko.luckperms.common.commands.sender.Sender; +import me.lucko.luckperms.common.commands.utils.ArgumentUtils; +import me.lucko.luckperms.common.commands.utils.Util; +import me.lucko.luckperms.common.constants.Permission; +import me.lucko.luckperms.common.utils.Predicates; +import org.spongepowered.api.service.context.Context; +import org.spongepowered.api.service.permission.SubjectData; + +import java.util.List; +import java.util.Map; +import java.util.Set; + +public class PermissionInfo extends SubCommand { + public PermissionInfo() { + super("info", "Shows info about the subject's permissions", Permission.SPONGE_PERMISSION_INFO, Predicates.alwaysFalse(), + Arg.list(Arg.create("contexts...", false, "the contexts to filter by")) + ); + } + + @Override + public CommandResult execute(LuckPermsPlugin plugin, Sender sender, SubjectData subjectData, List args, String label) throws CommandException { + ContextSet contextSet = ArgumentUtils.handleContexts(0, args); + if (contextSet.isEmpty()) { + Util.sendPluginMessage(sender, "&aShowing permissions matching contexts &bANY&a."); + Map, Map> permissions = subjectData.getAllPermissions(); + if (permissions.isEmpty()) { + Util.sendPluginMessage(sender, "That subject does not have any permissions defined."); + return CommandResult.SUCCESS; + } + + for (Map.Entry, Map> e : permissions.entrySet()) { + ContextSet set = SpongeUtils.convertContexts(e.getKey()); + Util.sendPluginMessage(sender, "&3>> &bContext: " + SpongeUtils.contextToString(set) + "\n" + SpongeUtils.nodesToString(e.getValue())); + } + + } else { + Map permissions = subjectData.getPermissions(SpongeUtils.convertContexts(contextSet)); + if (permissions.isEmpty()) { + Util.sendPluginMessage(sender, "That subject does not have any permissions defined in those contexts."); + return CommandResult.SUCCESS; + } + + Util.sendPluginMessage(sender, "&aShowing permissions matching contexts &b" + + SpongeUtils.contextToString(contextSet) + "&a.\n" + SpongeUtils.nodesToString(permissions)); + + } + return CommandResult.SUCCESS; + } +} diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/commands/PermissionSet.java b/sponge/src/main/java/me/lucko/luckperms/sponge/commands/PermissionSet.java new file mode 100644 index 00000000..0f2f3d40 --- /dev/null +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/commands/PermissionSet.java @@ -0,0 +1,66 @@ +/* + * 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.sponge.commands; + +import me.lucko.luckperms.api.context.ContextSet; +import me.lucko.luckperms.common.LuckPermsPlugin; +import me.lucko.luckperms.common.commands.Arg; +import me.lucko.luckperms.common.commands.CommandException; +import me.lucko.luckperms.common.commands.CommandResult; +import me.lucko.luckperms.common.commands.SubCommand; +import me.lucko.luckperms.common.commands.sender.Sender; +import me.lucko.luckperms.common.commands.utils.ArgumentUtils; +import me.lucko.luckperms.common.commands.utils.Util; +import me.lucko.luckperms.common.constants.Permission; +import me.lucko.luckperms.common.utils.Predicates; +import org.spongepowered.api.service.permission.SubjectData; +import org.spongepowered.api.util.Tristate; + +import java.util.List; + +public class PermissionSet extends SubCommand { + public PermissionSet() { + super("set", "Sets a permission for the Subject", Permission.SPONGE_PERMISSION_SET, Predicates.inRange(0, 1), + Arg.list( + Arg.create("node", true, "the permission node to set"), + Arg.create("tristate", true, "the value to set the permission to"), + Arg.create("contexts...", false, "the contexts to set the permission in") + ) + ); + } + + @Override + public CommandResult execute(LuckPermsPlugin plugin, Sender sender, SubjectData subjectData, List args, String label) throws CommandException { + String node = args.get(0); + Tristate tristate = SpongeUtils.parseTristate(1, args); + ContextSet contextSet = ArgumentUtils.handleContexts(2, args); + + if (subjectData.setPermission(SpongeUtils.convertContexts(contextSet), node, tristate)) { + Util.sendPluginMessage(sender, "&aSet &b" + node + "&a to &b" + tristate.toString().toLowerCase() + "&a in context " + SpongeUtils.contextToString(contextSet)); + } else { + Util.sendPluginMessage(sender, "Unable to set permission. Does the Subject already have it set?"); + } + + return CommandResult.SUCCESS; + } +} diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/commands/SpongeMainCommand.java b/sponge/src/main/java/me/lucko/luckperms/sponge/commands/SpongeMainCommand.java new file mode 100644 index 00000000..0c0f6145 --- /dev/null +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/commands/SpongeMainCommand.java @@ -0,0 +1,231 @@ +/* + * 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.sponge.commands; + +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; +import me.lucko.luckperms.common.LuckPermsPlugin; +import me.lucko.luckperms.common.commands.*; +import me.lucko.luckperms.common.commands.sender.Sender; +import me.lucko.luckperms.common.commands.utils.Util; +import me.lucko.luckperms.common.constants.Message; +import me.lucko.luckperms.common.utils.ImmutableCollectors; +import me.lucko.luckperms.common.utils.Predicates; +import me.lucko.luckperms.sponge.LPSpongePlugin; +import me.lucko.luckperms.sponge.service.LuckPermsService; +import org.spongepowered.api.service.permission.Subject; +import org.spongepowered.api.service.permission.SubjectCollection; +import org.spongepowered.api.service.permission.SubjectData; + +import java.util.*; +import java.util.stream.Collectors; +import java.util.stream.StreamSupport; + +public class SpongeMainCommand extends BaseCommand { + private final LPSpongePlugin plugin; + + private final Map>> subCommands = ImmutableMap.>>builder() + .put("permission", ImmutableList.>builder() + .add(new PermissionInfo()) + .add(new PermissionSet()) + .add(new PermissionClear()) + .build() + ) + .put("parent", ImmutableList.>builder() + .add(new ParentInfo()) + .add(new ParentAdd()) + .add(new ParentRemove()) + .add(new ParentClear()) + .build() + ) + .put("option", ImmutableList.>builder() + .add(new OptionInfo()) + .add(new OptionSet()) + .add(new OptionUnset()) + .add(new OptionClear()) + .build() + ) + .build(); + + public SpongeMainCommand(LPSpongePlugin plugin) { + super( + "Sponge", + "Edit extra Sponge data", + null, + Predicates.alwaysFalse(), + Arg.list( + Arg.create("collection", true, "the collection to query"), + Arg.create("subject", true, "the subject to modify") + ), + null + ); + + this.plugin = plugin; + } + + @Override + public CommandResult execute(LuckPermsPlugin plugin, Sender sender, Void v, List args, String label) throws CommandException { + LuckPermsService service = this.plugin.getService(); + + if (args.size() < 1) { + Util.sendPluginMessage(sender, "&aCurrent Subject Collections:\n" + + Util.listToCommaSep(service.getKnownSubjects().keySet().stream() + .filter(s -> !s.equalsIgnoreCase("user") && !s.equalsIgnoreCase("group")) + .collect(Collectors.toList()) + ) + ); + return CommandResult.SUCCESS; + } + + String subjectCollection = args.get(0); + + if (subjectCollection.equalsIgnoreCase("user") || subjectCollection.equalsIgnoreCase("group")) { + Util.sendPluginMessage(sender, "Please use the main LuckPerms commands to edit users and groups."); + return CommandResult.STATE_ERROR; + } + + if (service.getKnownSubjects().keySet().stream().map(String::toLowerCase).noneMatch(s -> s.equalsIgnoreCase(subjectCollection))) { + Util.sendPluginMessage(sender, "Warning: SubjectCollection '&4" + subjectCollection + "&c' doesn't already exist. Creating it now."); + } + + SubjectCollection collection = service.getSubjects(subjectCollection); + + if (args.size() < 2) { + List subjects = StreamSupport.stream(collection.getAllSubjects().spliterator(), false) + .map(Subject::getIdentifier) + .collect(Collectors.toList()); + + if (subjects.size() > 50) { + List extra = subjects.subList(50, subjects.size()); + int overflow = extra.size(); + extra.clear(); + Util.sendPluginMessage(sender, "&aCurrent Subjects:\n" + Util.listToCommaSep(subjects) + "&b ... and &a" + overflow + " &bmore."); + } + + Util.sendPluginMessage(sender, "&aCurrent Subjects:\n" + Util.listToCommaSep(subjects)); + return CommandResult.SUCCESS; + } + + if (args.size() < 4) { + sendDetailedUsage(sender, label); + return CommandResult.SUCCESS; + } + + boolean persistent = true; + if (args.get(2).toLowerCase().startsWith("-t")) { + persistent = false; + args.remove(2); + } + + String type = args.get(2).toLowerCase(); + if (!type.equals("permission") && !type.equals("parent") && !type.equals("option")) { + sendDetailedUsage(sender, label); + return CommandResult.INVALID_ARGS; + } + + String cmd = args.get(3); + Optional> o = subCommands.get(type).stream() + .filter(s -> s.getName().equalsIgnoreCase(cmd)) + .findAny(); + + if (!o.isPresent()) { + sendDetailedUsage(sender, label); + return CommandResult.INVALID_ARGS; + } + + final Command sub = o.get(); + if (!sub.isAuthorized(sender)) { + Message.COMMAND_NO_PERMISSION.send(sender); + return CommandResult.NO_PERMISSION; + } + + List strippedArgs = new ArrayList<>(); + if (args.size() > 4) { + strippedArgs.addAll(args.subList(4, args.size())); + } + + if (sub.getArgumentCheck().test(strippedArgs.size())) { + sub.sendDetailedUsage(sender, label); + return CommandResult.INVALID_ARGS; + } + + String subjectId = args.get(1); + if (!collection.hasRegistered(subjectId)) { + Util.sendPluginMessage(sender, "Warning: Subject '&4" + subjectId + "&c' doesn't already exist. Creating it now."); + } + + Subject subject = collection.get(subjectId); + SubjectData subjectData = persistent ? subject.getSubjectData() : subject.getTransientSubjectData(); + + CommandResult result; + try { + result = sub.execute(plugin, sender, subjectData, strippedArgs, label); + } catch (CommandException e) { + result = CommandManager.handleException(e, sender, label, sub); + } + return result; + } + + // TODO tab completion + + @Override + public void sendUsage(Sender sender, String label) { + Util.sendPluginMessage(sender, "&3> &a" + String.format(getUsage(), label)); + } + + @Override + public void sendDetailedUsage(Sender sender, String label) { + Util.sendPluginMessage(sender, "&b" + getName() + " Sub Commands: &7(" + String.format("/%s sponge [-transient]", label) + " ...)"); + for (String s : Arrays.asList("Permission", "Parent", "Option")) { + List subs = subCommands.get(s.toLowerCase()).stream() + .filter(sub -> sub.isAuthorized(sender)) + .collect(Collectors.toList()); + + if (!subs.isEmpty()) { + Util.sendPluginMessage(sender, "&3>> &b" + s); + for (Command sub : subs) { + sub.sendUsage(sender, label); + } + } + } + } + + @Override + public boolean isAuthorized(Sender sender) { + return getSubCommands().stream().filter(sc -> sc.isAuthorized(sender)).count() != 0; + } + + @Override + public String getUsage() { + return "/%s sponge "; + } + + public List> getSubCommands() { + return subCommands.values().stream().flatMap(List::stream).collect(ImmutableCollectors.toImmutableList()); + } + + @Override + public Optional>> getChildren() { + return Optional.of(getSubCommands()); + } +} diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/commands/SpongeUtils.java b/sponge/src/main/java/me/lucko/luckperms/sponge/commands/SpongeUtils.java new file mode 100644 index 00000000..5e259db6 --- /dev/null +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/commands/SpongeUtils.java @@ -0,0 +1,110 @@ +/* + * 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.sponge.commands; + +import com.google.common.collect.Maps; +import lombok.experimental.UtilityClass; +import me.lucko.luckperms.api.context.ContextSet; +import me.lucko.luckperms.common.commands.utils.ArgumentUtils; +import org.spongepowered.api.service.context.Context; +import org.spongepowered.api.service.permission.Subject; +import org.spongepowered.api.util.Tristate; + +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; + +@UtilityClass +public class SpongeUtils { + + public static Tristate parseTristate(int index, List args) throws ArgumentUtils.ArgumentException { + String s = args.get(index).toLowerCase(); + if (s.equals("1") || s.equals("true") || s.equals("t")) { + return Tristate.TRUE; + } + if (s.equals("0") || s.equals("null") || s.equals("none") || s.equals("undefined") || s.equals("undef")) { + return Tristate.UNDEFINED; + } + if (s.equals("-1") || s.equals("false") || s.equals("f")) { + return Tristate.FALSE; + } + throw new ArgumentUtils.DetailedUsageException(); + } + + public static String nodesToString(Map nodes) { + StringBuilder sb = new StringBuilder(); + for (Map.Entry e : nodes.entrySet()) { + sb.append("&3> ") + .append(e.getValue() ? "&a" : "&c") + .append(e.getKey()) + .append("\n"); + } + return sb.toString(); + } + + public static String optionsToString(Map options) { + StringBuilder sb = new StringBuilder(); + for (Map.Entry e : options.entrySet()) { + sb.append("&3> &a") + .append(e.getKey()) + .append(" &f= \"") + .append(e.getKey()) + .append("&f\"\n"); + } + return sb.toString(); + } + + public static String parentsToString(List parents) { + StringBuilder sb = new StringBuilder(); + for (Subject s : parents) { + sb.append("&3> &a") + .append(s.getIdentifier()) + .append(" &bfrom collection &a") + .append(s.getContainingCollection().getIdentifier()) + .append("&b.\n"); + } + return sb.toString(); + } + + public static String contextToString(ContextSet set) { + if (set.isEmpty()) { + return "&bGLOBAL"; + } + + StringBuilder sb = new StringBuilder(); + for (Map.Entry e : set.toSet()) { + sb.append("&f").append(e.getKey()).append("&7=&f").append(e.getValue()).append("&7, "); + } + return sb.delete(sb.length() - 2, sb.length()).toString(); + } + + public static ContextSet convertContexts(Set contexts) { + return ContextSet.fromEntries(contexts.stream().map(c -> Maps.immutableEntry(c.getKey(), c.getValue())).collect(Collectors.toSet())); + } + + public static Set convertContexts(ContextSet contexts) { + return contexts.toSet().stream().map(e -> new Context(e.getKey(), e.getValue())).collect(Collectors.toSet()); + } + +}