Cleanup command abstraction classes

This commit is contained in:
Luck committed 2020-01-15 23:54:43 +00:00
1 parent 4c83d439af
commit 2ccb475aa0
139 files changed
+933 -997

No files matched your search

@@ -43,7 +43,7 @@ import me.lucko.luckperms.common.tasks.CacheHousekeepingTask;
import me.lucko.luckperms.common.tasks.ExpireTemporaryTask;
import me.lucko.luckperms.common.util.MoreFiles;
import me.lucko.luckperms.sponge.calculator.SpongeCalculatorFactory;
import me.lucko.luckperms.sponge.commands.SpongeMainCommand;
import me.lucko.luckperms.sponge.commands.SpongeParentCommand;
import me.lucko.luckperms.sponge.context.SpongeContextManager;
import me.lucko.luckperms.sponge.context.WorldCalculator;
import me.lucko.luckperms.sponge.listeners.SpongeConnectionListener;
@@ -280,8 +280,8 @@ public class LPSpongePlugin extends AbstractLuckPermsPlugin {
}
@Override
public List<Command<?, ?>> getExtraCommands() {
return Collections.singletonList(new SpongeMainCommand(this));
public List<Command<?>> getExtraCommands() {
return Collections.singletonList(new SpongeParentCommand(this));
}
public SpongeSenderFactory getSenderFactory() {
@@ -25,9 +25,8 @@
package me.lucko.luckperms.sponge;
import com.google.common.base.Splitter;
import me.lucko.luckperms.common.command.CommandManager;
import me.lucko.luckperms.common.command.utils.ArgumentTokenizer;
import me.lucko.luckperms.common.sender.Sender;
import org.checkerframework.checker.nullness.qual.NonNull;
@@ -46,9 +45,6 @@ import java.util.ListIterator;
import java.util.Optional;
public class SpongeCommandExecutor extends CommandManager implements CommandCallable {
private static final Splitter TAB_COMPLETE_ARGUMENT_SPLITTER = Splitter.on(COMMAND_SEPARATOR_PATTERN);
private static final Splitter ARGUMENT_SPLITTER = Splitter.on(COMMAND_SEPARATOR_PATTERN).omitEmptyStrings();
private final LPSpongePlugin plugin;
public SpongeCommandExecutor(LPSpongePlugin plugin) {
@@ -57,20 +53,18 @@ public class SpongeCommandExecutor extends CommandManager implements CommandCall
}
@Override
public @NonNull CommandResult process(@NonNull CommandSource source, @NonNull String s) {
Sender lpSender = this.plugin.getSenderFactory().wrap(source);
List<String> arguments = processSelectors(source, CommandManager.stripQuotes(ARGUMENT_SPLITTER.splitToList(s)));
onCommand(lpSender, "lp", arguments);
public @NonNull CommandResult process(@NonNull CommandSource source, @NonNull String args) {
Sender wrapped = this.plugin.getSenderFactory().wrap(source);
List<String> arguments = processSelectors(source, ArgumentTokenizer.EXECUTE.tokenizeInput(args));
executeCommand(wrapped, "lp", arguments);
return CommandResult.success();
}
@Override
public @NonNull List<String> getSuggestions(@NonNull CommandSource source, @NonNull String s, @Nullable Location<World> location) {
Sender lpSender = this.plugin.getSenderFactory().wrap(source);
List<String> arguments = processSelectors(source, CommandManager.stripQuotes(TAB_COMPLETE_ARGUMENT_SPLITTER.splitToList(s)));
return onTabComplete(lpSender, arguments);
public @NonNull List<String> getSuggestions(@NonNull CommandSource source, @NonNull String args, @Nullable Location<World> location) {
Sender wrapped = this.plugin.getSenderFactory().wrap(source);
List<String> arguments = processSelectors(source, ArgumentTokenizer.TAB_COMPLETE.tokenizeInput(args));
return tabCompleteCommand(wrapped, arguments);
}
@Override
@@ -26,7 +26,7 @@
package me.lucko.luckperms.sponge.commands;
import me.lucko.luckperms.common.command.CommandResult;
import me.lucko.luckperms.common.command.abstraction.SubCommand;
import me.lucko.luckperms.common.command.abstraction.ChildCommand;
import me.lucko.luckperms.common.command.access.CommandPermission;
import me.lucko.luckperms.common.command.utils.ArgumentParser;
import me.lucko.luckperms.common.locale.LocaleManager;
@@ -41,7 +41,7 @@ import net.luckperms.api.context.ImmutableContextSet;
import java.util.List;
public class OptionClear extends SubCommand<LPSubjectData> {
public class OptionClear extends ChildCommand<LPSubjectData> {
public OptionClear(LocaleManager locale) {
super(CommandSpec.SPONGE_OPTION_CLEAR.localize(locale), "clear", CommandPermission.SPONGE_OPTION_CLEAR, Predicates.alwaysFalse());
}
@@ -28,7 +28,7 @@ package me.lucko.luckperms.sponge.commands;
import com.google.common.collect.ImmutableMap;
import me.lucko.luckperms.common.command.CommandResult;
import me.lucko.luckperms.common.command.abstraction.SubCommand;
import me.lucko.luckperms.common.command.abstraction.ChildCommand;
import me.lucko.luckperms.common.command.access.CommandPermission;
import me.lucko.luckperms.common.command.utils.ArgumentParser;
import me.lucko.luckperms.common.locale.LocaleManager;
@@ -44,7 +44,7 @@ import net.luckperms.api.context.ImmutableContextSet;
import java.util.List;
import java.util.Map;
public class OptionInfo extends SubCommand<LPSubjectData> {
public class OptionInfo extends ChildCommand<LPSubjectData> {
public OptionInfo(LocaleManager locale) {
super(CommandSpec.SPONGE_OPTION_INFO.localize(locale), "info", CommandPermission.SPONGE_OPTION_INFO, Predicates.alwaysFalse());
}
@@ -26,7 +26,7 @@
package me.lucko.luckperms.sponge.commands;
import me.lucko.luckperms.common.command.CommandResult;
import me.lucko.luckperms.common.command.abstraction.SubCommand;
import me.lucko.luckperms.common.command.abstraction.ChildCommand;
import me.lucko.luckperms.common.command.access.CommandPermission;
import me.lucko.luckperms.common.command.utils.ArgumentParser;
import me.lucko.luckperms.common.locale.LocaleManager;
@@ -41,7 +41,7 @@ import net.luckperms.api.context.ImmutableContextSet;
import java.util.List;
public class OptionSet extends SubCommand<LPSubjectData> {
public class OptionSet extends ChildCommand<LPSubjectData> {
public OptionSet(LocaleManager locale) {
super(CommandSpec.SPONGE_OPTION_SET.localize(locale), "set", CommandPermission.SPONGE_OPTION_SET, Predicates.inRange(0, 1));
}
@@ -26,7 +26,7 @@
package me.lucko.luckperms.sponge.commands;
import me.lucko.luckperms.common.command.CommandResult;
import me.lucko.luckperms.common.command.abstraction.SubCommand;
import me.lucko.luckperms.common.command.abstraction.ChildCommand;
import me.lucko.luckperms.common.command.access.CommandPermission;
import me.lucko.luckperms.common.command.utils.ArgumentParser;
import me.lucko.luckperms.common.locale.LocaleManager;
@@ -41,7 +41,7 @@ import net.luckperms.api.context.ImmutableContextSet;
import java.util.List;
public class OptionUnset extends SubCommand<LPSubjectData> {
public class OptionUnset extends ChildCommand<LPSubjectData> {
public OptionUnset(LocaleManager locale) {
super(CommandSpec.SPONGE_OPTION_UNSET.localize(locale), "unset", CommandPermission.SPONGE_OPTION_UNSET, Predicates.is(0));
}
@@ -26,7 +26,7 @@
package me.lucko.luckperms.sponge.commands;
import me.lucko.luckperms.common.command.CommandResult;
import me.lucko.luckperms.common.command.abstraction.SubCommand;
import me.lucko.luckperms.common.command.abstraction.ChildCommand;
import me.lucko.luckperms.common.command.access.CommandPermission;
import me.lucko.luckperms.common.command.utils.ArgumentParser;
import me.lucko.luckperms.common.locale.LocaleManager;
@@ -46,7 +46,7 @@ import org.spongepowered.api.Sponge;
import java.util.List;
public class ParentAdd extends SubCommand<LPSubjectData> {
public class ParentAdd extends ChildCommand<LPSubjectData> {
public ParentAdd(LocaleManager locale) {
super(CommandSpec.SPONGE_PARENT_ADD.localize(locale), "add", CommandPermission.SPONGE_PARENT_ADD, Predicates.inRange(0, 1));
}
@@ -26,7 +26,7 @@
package me.lucko.luckperms.sponge.commands;
import me.lucko.luckperms.common.command.CommandResult;
import me.lucko.luckperms.common.command.abstraction.SubCommand;
import me.lucko.luckperms.common.command.abstraction.ChildCommand;
import me.lucko.luckperms.common.command.access.CommandPermission;
import me.lucko.luckperms.common.command.utils.ArgumentParser;
import me.lucko.luckperms.common.locale.LocaleManager;
@@ -41,7 +41,7 @@ import net.luckperms.api.context.ImmutableContextSet;
import java.util.List;
public class ParentClear extends SubCommand<LPSubjectData> {
public class ParentClear extends ChildCommand<LPSubjectData> {
public ParentClear(LocaleManager locale) {
super(CommandSpec.SPONGE_PARENT_CLEAR.localize(locale), "clear", CommandPermission.SPONGE_PARENT_CLEAR, Predicates.alwaysFalse());
}
@@ -28,7 +28,7 @@ package me.lucko.luckperms.sponge.commands;
import com.google.common.collect.ImmutableList;
import me.lucko.luckperms.common.command.CommandResult;
import me.lucko.luckperms.common.command.abstraction.SubCommand;
import me.lucko.luckperms.common.command.abstraction.ChildCommand;
import me.lucko.luckperms.common.command.access.CommandPermission;
import me.lucko.luckperms.common.command.utils.ArgumentParser;
import me.lucko.luckperms.common.locale.LocaleManager;
@@ -45,7 +45,7 @@ import net.luckperms.api.context.ImmutableContextSet;
import java.util.List;
import java.util.Map;
public class ParentInfo extends SubCommand<LPSubjectData> {
public class ParentInfo extends ChildCommand<LPSubjectData> {
public ParentInfo(LocaleManager locale) {
super(CommandSpec.SPONGE_PARENT_INFO.localize(locale), "info", CommandPermission.SPONGE_PARENT_INFO, Predicates.alwaysFalse());
}
@@ -26,7 +26,7 @@
package me.lucko.luckperms.sponge.commands;
import me.lucko.luckperms.common.command.CommandResult;
import me.lucko.luckperms.common.command.abstraction.SubCommand;
import me.lucko.luckperms.common.command.abstraction.ChildCommand;
import me.lucko.luckperms.common.command.access.CommandPermission;
import me.lucko.luckperms.common.command.utils.ArgumentParser;
import me.lucko.luckperms.common.locale.LocaleManager;
@@ -46,7 +46,7 @@ import org.spongepowered.api.Sponge;
import java.util.List;
public class ParentRemove extends SubCommand<LPSubjectData> {
public class ParentRemove extends ChildCommand<LPSubjectData> {
public ParentRemove(LocaleManager locale) {
super(CommandSpec.SPONGE_PARENT_REMOVE.localize(locale), "remove", CommandPermission.SPONGE_PARENT_REMOVE, Predicates.inRange(0, 1));
}
@@ -26,7 +26,7 @@
package me.lucko.luckperms.sponge.commands;
import me.lucko.luckperms.common.command.CommandResult;
import me.lucko.luckperms.common.command.abstraction.SubCommand;
import me.lucko.luckperms.common.command.abstraction.ChildCommand;
import me.lucko.luckperms.common.command.access.CommandPermission;
import me.lucko.luckperms.common.command.utils.ArgumentParser;
import me.lucko.luckperms.common.locale.LocaleManager;
@@ -41,7 +41,7 @@ import net.luckperms.api.context.ImmutableContextSet;
import java.util.List;
public class PermissionClear extends SubCommand<LPSubjectData> {
public class PermissionClear extends ChildCommand<LPSubjectData> {
public PermissionClear(LocaleManager locale) {
super(CommandSpec.SPONGE_PERMISSION_CLEAR.localize(locale), "clear", CommandPermission.SPONGE_PERMISSION_CLEAR, Predicates.alwaysFalse());
}
@@ -28,7 +28,7 @@ package me.lucko.luckperms.sponge.commands;
import com.google.common.collect.ImmutableMap;
import me.lucko.luckperms.common.command.CommandResult;
import me.lucko.luckperms.common.command.abstraction.SubCommand;
import me.lucko.luckperms.common.command.abstraction.ChildCommand;
import me.lucko.luckperms.common.command.access.CommandPermission;
import me.lucko.luckperms.common.command.utils.ArgumentParser;
import me.lucko.luckperms.common.locale.LocaleManager;
@@ -44,7 +44,7 @@ import net.luckperms.api.context.ImmutableContextSet;
import java.util.List;
import java.util.Map;
public class PermissionInfo extends SubCommand<LPSubjectData> {
public class PermissionInfo extends ChildCommand<LPSubjectData> {
public PermissionInfo(LocaleManager locale) {
super(CommandSpec.SPONGE_PERMISSION_INFO.localize(locale), "info", CommandPermission.SPONGE_PERMISSION_INFO, Predicates.alwaysFalse());
}
@@ -26,8 +26,8 @@
package me.lucko.luckperms.sponge.commands;
import me.lucko.luckperms.common.command.CommandResult;
import me.lucko.luckperms.common.command.abstraction.ChildCommand;
import me.lucko.luckperms.common.command.abstraction.CommandException;
import me.lucko.luckperms.common.command.abstraction.SubCommand;
import me.lucko.luckperms.common.command.access.CommandPermission;
import me.lucko.luckperms.common.command.utils.ArgumentParser;
import me.lucko.luckperms.common.locale.LocaleManager;
@@ -43,7 +43,7 @@ import net.luckperms.api.util.Tristate;
import java.util.List;
public class PermissionSet extends SubCommand<LPSubjectData> {
public class PermissionSet extends ChildCommand<LPSubjectData> {
public PermissionSet(LocaleManager locale) {
super(CommandSpec.SPONGE_PERMISSION_SET.localize(locale), "set", CommandPermission.SPONGE_PERMISSION_SET, Predicates.inRange(0, 1));
}
@@ -28,8 +28,8 @@ package me.lucko.luckperms.sponge.commands;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import me.lucko.luckperms.common.command.CommandManager;
import me.lucko.luckperms.common.command.CommandResult;
import me.lucko.luckperms.common.command.abstraction.ChildCommand;
import me.lucko.luckperms.common.command.abstraction.Command;
import me.lucko.luckperms.common.command.abstraction.CommandException;
import me.lucko.luckperms.common.command.utils.MessageUtils;
@@ -38,7 +38,6 @@ import me.lucko.luckperms.common.locale.command.CommandSpec;
import me.lucko.luckperms.common.locale.message.Message;
import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
import me.lucko.luckperms.common.sender.Sender;
import me.lucko.luckperms.common.util.ImmutableCollectors;
import me.lucko.luckperms.common.util.Predicates;
import me.lucko.luckperms.sponge.LPSpongePlugin;
import me.lucko.luckperms.sponge.service.LuckPermsService;
@@ -46,40 +45,36 @@ import me.lucko.luckperms.sponge.service.model.LPSubject;
import me.lucko.luckperms.sponge.service.model.LPSubjectCollection;
import me.lucko.luckperms.sponge.service.model.LPSubjectData;
import org.checkerframework.checker.nullness.qual.NonNull;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
public class SpongeMainCommand extends Command<Void, LPSubjectData> {
public class SpongeParentCommand extends Command<Void> {
private final LPSpongePlugin plugin;
private final Map<String, List<Command<LPSubjectData, ?>>> subCommands;
private final Map<String, List<ChildCommand<LPSubjectData>>> children;
public SpongeMainCommand(LPSpongePlugin plugin) {
public SpongeParentCommand(LPSpongePlugin plugin) {
super(CommandSpec.SPONGE.localize(plugin.getLocaleManager()), "Sponge", null, Predicates.alwaysFalse());
LocaleManager locale = plugin.getLocaleManager();
this.subCommands = ImmutableMap.<String, List<Command<LPSubjectData, ?>>>builder()
.put("permission", ImmutableList.<Command<LPSubjectData, ?>>builder()
this.children = ImmutableMap.<String, List<ChildCommand<LPSubjectData>>>builder()
.put("permission", ImmutableList.<ChildCommand<LPSubjectData>>builder()
.add(new PermissionInfo(locale))
.add(new PermissionSet(locale))
.add(new PermissionClear(locale))
.build()
)
.put("parent", ImmutableList.<Command<LPSubjectData, ?>>builder()
.put("parent", ImmutableList.<ChildCommand<LPSubjectData>>builder()
.add(new ParentInfo(locale))
.add(new ParentAdd(locale))
.add(new ParentRemove(locale))
.add(new ParentClear(locale))
.build()
)
.put("option", ImmutableList.<Command<LPSubjectData, ?>>builder()
.put("option", ImmutableList.<ChildCommand<LPSubjectData>>builder()
.add(new OptionInfo(locale))
.add(new OptionSet(locale))
.add(new OptionUnset(locale))
@@ -153,27 +148,22 @@ public class SpongeMainCommand extends Command<Void, LPSubjectData> {
}
String cmd = args.get(3);
Optional<Command<LPSubjectData, ?>> o = this.subCommands.get(type).stream()
ChildCommand<LPSubjectData> sub = this.children.get(type).stream()
.filter(s -> s.getName().equalsIgnoreCase(cmd))
.findAny();
.findAny()
.orElse(null);
if (!o.isPresent()) {
if (sub == null) {
sendDetailedUsage(sender, label);
return CommandResult.INVALID_ARGS;
}
final Command<LPSubjectData, ?> 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())) {
if (sub.getArgumentCheck().test(args.size() - 4)) {
sub.sendDetailedUsage(sender, label);
return CommandResult.INVALID_ARGS;
}
@@ -188,9 +178,9 @@ public class SpongeMainCommand extends Command<Void, LPSubjectData> {
CommandResult result;
try {
result = sub.execute(plugin, sender, subjectData, strippedArgs, label);
result = sub.execute(plugin, sender, subjectData, args.subList(4, args.size()), label);
} catch (CommandException e) {
result = CommandManager.handleException(e, sender, label, sub);
result = e.handle(sender, label, sub);
}
return result;
}
@@ -204,13 +194,13 @@ public class SpongeMainCommand extends Command<Void, LPSubjectData> {
public void sendDetailedUsage(Sender sender, String label) {
Message.BLANK.send(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 = this.subCommands.get(s.toLowerCase()).stream()
List<Command<?>> subs = this.children.get(s.toLowerCase()).stream()
.filter(sub -> sub.isAuthorized(sender))
.collect(Collectors.toList());
if (!subs.isEmpty()) {
Message.BLANK.send(sender, "&3>> &b" + s);
for (Command sub : subs) {
for (Command<?> sub : subs) {
sub.sendUsage(sender, label);
}
}
@@ -219,15 +209,7 @@ public class SpongeMainCommand extends Command<Void, LPSubjectData> {
@Override
public boolean isAuthorized(Sender sender) {
return getSubCommands().stream().anyMatch(sc -> sc.isAuthorized(sender));
return this.children.values().stream().flatMap(List::stream).anyMatch(sc -> sc.isAuthorized(sender));
}
public List<Command<LPSubjectData, ?>> getSubCommands() {
return this.subCommands.values().stream().flatMap(List::stream).collect(ImmutableCollectors.toList());
}
@Override
public @NonNull Optional<List<Command<LPSubjectData, ?>>> getChildren() {
return Optional.of(getSubCommands());
}
}
@@ -81,7 +81,7 @@ public final class SimplePermissionDescription implements LPPermissionDescriptio
return Optional.ofNullable(this.owner);
}
@SuppressWarnings("unchecked")
@SuppressWarnings({"unchecked", "rawtypes"})
@Override
public CompletableFuture<Map<LPSubjectReference, Boolean>> findAssignedSubjects(String id) {
LPSubjectCollection collection = this.service.getCollection(id);