Sponge: Implement support for editing more than just users/groups with commands

This commit is contained in:
Luck 2016-11-14 18:45:48 +00:00
parent 18a3dfd604
commit 9c8097ecb9
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
18 changed files with 1203 additions and 25 deletions

View File

@ -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<Contexts> getPreProcessContexts(boolean op);
default List<BaseCommand> getExtraCommands() {
return Collections.emptyList();
}
/**
* Gets a set of players ignoring logging output
* @return a {@link Set} of {@link UUID}s

View File

@ -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<BaseCommand> mainCommands = ImmutableList.<BaseCommand>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<BaseCommand> mainCommands;
public CommandManager(LuckPermsPlugin plugin) {
this.plugin = plugin;
ImmutableList.Builder<BaseCommand> 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

View File

@ -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<String> args) {
if (args.size() <= fromIndex) {
return ContextSet.empty();
}
MutableContextSet contextSet = new MutableContextSet();
List<String> 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 {}

View File

@ -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;

View File

@ -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<BaseCommand> getExtraCommands() {
return Collections.singletonList(new SpongeMainCommand(this));
}
private void registerPermission(PermissionService p, String node) {
Optional<PermissionDescription.Builder> builder = p.newDescriptionBuilder(this);
if (!builder.isPresent()) return;

View File

@ -0,0 +1,61 @@
/*
* Copyright (c) 2016 Lucko (Luck) <luck@lucko.me>
*
* 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<SubjectData> {
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<String> 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;
}
}

View File

@ -0,0 +1,79 @@
/*
* Copyright (c) 2016 Lucko (Luck) <luck@lucko.me>
*
* 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<SubjectData> {
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<String> args, String label) throws CommandException {
ContextSet contextSet = ArgumentUtils.handleContexts(0, args);
if (contextSet.isEmpty()) {
Util.sendPluginMessage(sender, "&aShowing options matching contexts &bANY&a.");
Map<Set<Context>, Map<String, String>> options = subjectData.getAllOptions();
if (options.isEmpty()) {
Util.sendPluginMessage(sender, "That subject does not have any options defined.");
return CommandResult.SUCCESS;
}
for (Map.Entry<Set<Context>, Map<String, String>> e : options.entrySet()) {
ContextSet set = SpongeUtils.convertContexts(e.getKey());
Util.sendPluginMessage(sender, "&3>> &bContext: " + SpongeUtils.contextToString(set) + "\n" + SpongeUtils.optionsToString(e.getValue()));
}
} else {
Map<String, String> 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;
}
}

View File

@ -0,0 +1,65 @@
/*
* Copyright (c) 2016 Lucko (Luck) <luck@lucko.me>
*
* 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<SubjectData> {
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<String> 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;
}
}

View File

@ -0,0 +1,62 @@
/*
* Copyright (c) 2016 Lucko (Luck) <luck@lucko.me>
*
* 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<SubjectData> {
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<String> 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;
}
}

View File

@ -0,0 +1,81 @@
/*
* Copyright (c) 2016 Lucko (Luck) <luck@lucko.me>
*
* 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<SubjectData> {
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<String> 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;
}
}

View File

@ -0,0 +1,61 @@
/*
* Copyright (c) 2016 Lucko (Luck) <luck@lucko.me>
*
* 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<SubjectData> {
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<String> 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;
}
}

View File

@ -0,0 +1,80 @@
/*
* Copyright (c) 2016 Lucko (Luck) <luck@lucko.me>
*
* 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<SubjectData> {
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<String> args, String label) throws CommandException {
ContextSet contextSet = ArgumentUtils.handleContexts(0, args);
if (contextSet.isEmpty()) {
Util.sendPluginMessage(sender, "&aShowing parents matching contexts &bANY&a.");
Map<Set<Context>, List<Subject>> parents = subjectData.getAllParents();
if (parents.isEmpty()) {
Util.sendPluginMessage(sender, "That subject does not have any parents defined.");
return CommandResult.SUCCESS;
}
for (Map.Entry<Set<Context>, List<Subject>> e : parents.entrySet()) {
ContextSet set = SpongeUtils.convertContexts(e.getKey());
Util.sendPluginMessage(sender, "&3>> &bContext: " + SpongeUtils.contextToString(set) + "\n" + SpongeUtils.parentsToString(e.getValue()));
}
} else {
List<Subject> 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;
}
}

View File

@ -0,0 +1,81 @@
/*
* Copyright (c) 2016 Lucko (Luck) <luck@lucko.me>
*
* 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<SubjectData> {
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<String> 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;
}
}

View File

@ -0,0 +1,61 @@
/*
* Copyright (c) 2016 Lucko (Luck) <luck@lucko.me>
*
* 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<SubjectData> {
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<String> 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;
}
}

View File

@ -0,0 +1,79 @@
/*
* Copyright (c) 2016 Lucko (Luck) <luck@lucko.me>
*
* 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<SubjectData> {
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<String> args, String label) throws CommandException {
ContextSet contextSet = ArgumentUtils.handleContexts(0, args);
if (contextSet.isEmpty()) {
Util.sendPluginMessage(sender, "&aShowing permissions matching contexts &bANY&a.");
Map<Set<Context>, Map<String, Boolean>> permissions = subjectData.getAllPermissions();
if (permissions.isEmpty()) {
Util.sendPluginMessage(sender, "That subject does not have any permissions defined.");
return CommandResult.SUCCESS;
}
for (Map.Entry<Set<Context>, Map<String, Boolean>> e : permissions.entrySet()) {
ContextSet set = SpongeUtils.convertContexts(e.getKey());
Util.sendPluginMessage(sender, "&3>> &bContext: " + SpongeUtils.contextToString(set) + "\n" + SpongeUtils.nodesToString(e.getValue()));
}
} else {
Map<String, Boolean> 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;
}
}

View File

@ -0,0 +1,66 @@
/*
* Copyright (c) 2016 Lucko (Luck) <luck@lucko.me>
*
* 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<SubjectData> {
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<String> 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;
}
}

View File

@ -0,0 +1,231 @@
/*
* Copyright (c) 2016 Lucko (Luck) <luck@lucko.me>
*
* 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<Void, SubjectData> {
private final LPSpongePlugin plugin;
private final Map<String, List<Command<SubjectData, ?>>> subCommands = ImmutableMap.<String, List<Command<SubjectData, ?>>>builder()
.put("permission", ImmutableList.<Command<SubjectData, ?>>builder()
.add(new PermissionInfo())
.add(new PermissionSet())
.add(new PermissionClear())
.build()
)
.put("parent", ImmutableList.<Command<SubjectData, ?>>builder()
.add(new ParentInfo())
.add(new ParentAdd())
.add(new ParentRemove())
.add(new ParentClear())
.build()
)
.put("option", ImmutableList.<Command<SubjectData, ?>>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<String> 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<String> subjects = StreamSupport.stream(collection.getAllSubjects().spliterator(), false)
.map(Subject::getIdentifier)
.collect(Collectors.toList());
if (subjects.size() > 50) {
List<String> 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<Command<SubjectData, ?>> o = subCommands.get(type).stream()
.filter(s -> s.getName().equalsIgnoreCase(cmd))
.findAny();
if (!o.isPresent()) {
sendDetailedUsage(sender, label);
return CommandResult.INVALID_ARGS;
}
final Command<SubjectData, ?> sub = o.get();
if (!sub.isAuthorized(sender)) {
Message.COMMAND_NO_PERMISSION.send(sender);
return CommandResult.NO_PERMISSION;
}
List<String> 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 <collection> <subject> [-transient]", label) + " ...)");
for (String s : Arrays.asList("Permission", "Parent", "Option")) {
List<Command> 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 <collection> <subject>";
}
public List<Command<SubjectData, ?>> getSubCommands() {
return subCommands.values().stream().flatMap(List::stream).collect(ImmutableCollectors.toImmutableList());
}
@Override
public Optional<List<Command<SubjectData, ?>>> getChildren() {
return Optional.of(getSubCommands());
}
}

View File

@ -0,0 +1,110 @@
/*
* Copyright (c) 2016 Lucko (Luck) <luck@lucko.me>
*
* 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<String> 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<String, Boolean> nodes) {
StringBuilder sb = new StringBuilder();
for (Map.Entry<String, Boolean> e : nodes.entrySet()) {
sb.append("&3> ")
.append(e.getValue() ? "&a" : "&c")
.append(e.getKey())
.append("\n");
}
return sb.toString();
}
public static String optionsToString(Map<String, String> options) {
StringBuilder sb = new StringBuilder();
for (Map.Entry<String, String> 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<Subject> 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<String, String> 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<Context> contexts) {
return ContextSet.fromEntries(contexts.stream().map(c -> Maps.immutableEntry(c.getKey(), c.getValue())).collect(Collectors.toSet()));
}
public static Set<Context> convertContexts(ContextSet contexts) {
return contexts.toSet().stream().map(e -> new Context(e.getKey(), e.getValue())).collect(Collectors.toSet());
}
}