Search in sources :

Example 1 with Command

use of me.lucko.luckperms.common.command.abstraction.Command in project LuckPerms by lucko.

the class SpongeMainCommand method execute.

@Override
public CommandResult execute(LuckPermsPlugin plugin, Sender sender, Void v, List<String> args, String label) {
    LuckPermsService service = this.plugin.getService();
    if (args.size() < 1) {
        MessageUtils.sendPluginMessage(sender, "&aCurrent Subject Collections:\n" + MessageUtils.toCommaSep(service.getLoadedCollections().keySet().stream().filter(s -> !s.equalsIgnoreCase("user") && !s.equalsIgnoreCase("group")).sorted().collect(Collectors.toList())));
        return CommandResult.SUCCESS;
    }
    String subjectCollection = args.get(0);
    if (subjectCollection.equalsIgnoreCase("user") || subjectCollection.equalsIgnoreCase("group")) {
        MessageUtils.sendPluginMessage(sender, "Please use the main LuckPerms commands to edit users and groups.");
        return CommandResult.STATE_ERROR;
    }
    if (service.getLoadedCollections().keySet().stream().map(String::toLowerCase).noneMatch(s -> s.equalsIgnoreCase(subjectCollection))) {
        MessageUtils.sendPluginMessage(sender, "Warning: SubjectCollection '&4" + subjectCollection + "&c' doesn't already exist. Creating it now.");
    }
    LPSubjectCollection collection = service.getCollection(subjectCollection);
    if (args.size() < 2) {
        List<String> subjects = collection.getLoadedSubjects().stream().map(LPSubject::getIdentifier).collect(Collectors.toList());
        if (subjects.size() > 50) {
            List<String> extra = subjects.subList(50, subjects.size());
            int overflow = extra.size();
            extra.clear();
            MessageUtils.sendPluginMessage(sender, "&aCurrent Subjects:\n" + MessageUtils.toCommaSep(subjects) + "&b ... and &a" + overflow + " &bmore.");
        } else {
            MessageUtils.sendPluginMessage(sender, "&aCurrent Subjects:\n" + MessageUtils.toCommaSep(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<LPSubjectData, ?>> o = this.subCommands.get(type).stream().filter(s -> s.getName().equalsIgnoreCase(cmd)).findAny();
    if (!o.isPresent()) {
        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())) {
        sub.sendDetailedUsage(sender, label);
        return CommandResult.INVALID_ARGS;
    }
    String subjectId = args.get(1);
    if (!collection.hasRegistered(subjectId).join()) {
        MessageUtils.sendPluginMessage(sender, "Warning: Subject '&4" + subjectId + "&c' doesn't already exist. Creating it now.");
    }
    LPSubject subject = collection.loadSubject(subjectId).join();
    LPSubjectData 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;
}
Also used : CommandException(me.lucko.luckperms.common.command.abstraction.CommandException) Arrays(java.util.Arrays) LuckPermsService(me.lucko.luckperms.sponge.service.LuckPermsService) LocaleManager(me.lucko.luckperms.common.locale.LocaleManager) LPSubject(me.lucko.luckperms.sponge.service.model.LPSubject) Command(me.lucko.luckperms.common.command.abstraction.Command) ArrayList(java.util.ArrayList) CommandSpec(me.lucko.luckperms.common.locale.command.CommandSpec) ImmutableList(com.google.common.collect.ImmutableList) Map(java.util.Map) Message(me.lucko.luckperms.common.locale.message.Message) LuckPermsPlugin(me.lucko.luckperms.common.plugin.LuckPermsPlugin) Nonnull(javax.annotation.Nonnull) CommandManager(me.lucko.luckperms.common.command.CommandManager) ImmutableMap(com.google.common.collect.ImmutableMap) MessageUtils(me.lucko.luckperms.common.command.utils.MessageUtils) Sender(me.lucko.luckperms.common.sender.Sender) Collectors(java.util.stream.Collectors) CommandResult(me.lucko.luckperms.common.command.CommandResult) LPSubjectCollection(me.lucko.luckperms.sponge.service.model.LPSubjectCollection) LPSubjectData(me.lucko.luckperms.sponge.service.model.LPSubjectData) ImmutableCollectors(me.lucko.luckperms.common.utils.ImmutableCollectors) List(java.util.List) Optional(java.util.Optional) LPSpongePlugin(me.lucko.luckperms.sponge.LPSpongePlugin) Predicates(me.lucko.luckperms.common.utils.Predicates) LPSubjectData(me.lucko.luckperms.sponge.service.model.LPSubjectData) ArrayList(java.util.ArrayList) CommandException(me.lucko.luckperms.common.command.abstraction.CommandException) LPSubjectCollection(me.lucko.luckperms.sponge.service.model.LPSubjectCollection) CommandResult(me.lucko.luckperms.common.command.CommandResult) LuckPermsService(me.lucko.luckperms.sponge.service.LuckPermsService) Command(me.lucko.luckperms.common.command.abstraction.Command) LPSubject(me.lucko.luckperms.sponge.service.model.LPSubject)

Example 2 with Command

use of me.lucko.luckperms.common.command.abstraction.Command in project LuckPerms by lucko.

the class CommandManager method sendCommandUsage.

private void sendCommandUsage(Sender sender, String label) {
    MessageUtils.sendPluginMessage(sender, "&2Running &bLuckPerms v" + this.plugin.getBootstrap().getVersion() + "&2.");
    this.mainCommands.stream().filter(Command::shouldDisplay).filter(c -> c.isAuthorized(sender)).forEach(c -> {
        @SuppressWarnings("unchecked") String permission = (String) c.getPermission().map(p -> ((CommandPermission) p).getPermission()).orElse("None");
        TextComponent component = TextUtils.fromLegacy("&3> &a" + String.format(c.getUsage(), label), AMPERSAND_CHAR).toBuilder().applyDeep(comp -> {
            comp.hoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, TextUtils.fromLegacy(TextUtils.joinNewline("&bCommand: &2" + c.getName(), "&bDescription: &2" + c.getDescription(), "&bUsage: &2" + String.format(c.getUsage(), label), "&bPermission: &2" + permission, " ", "&7Click to auto-complete."), AMPERSAND_CHAR)));
            comp.clickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, String.format(c.getUsage(), label)));
        }).build();
        sender.sendMessage(component);
    });
}
Also used : CommandException(me.lucko.luckperms.common.command.abstraction.CommandException) InfoCommand(me.lucko.luckperms.common.commands.misc.InfoCommand) BulkUpdateCommand(me.lucko.luckperms.common.commands.misc.BulkUpdateCommand) ListGroups(me.lucko.luckperms.common.commands.group.ListGroups) ListIterator(java.util.ListIterator) TextUtils(me.lucko.luckperms.common.utils.TextUtils) ImportCommand(me.lucko.luckperms.common.commands.misc.ImportCommand) HoverEvent(net.kyori.text.event.HoverEvent) ExportCommand(me.lucko.luckperms.common.commands.misc.ExportCommand) CheckCommand(me.lucko.luckperms.common.commands.misc.CheckCommand) EditorCommand(me.lucko.luckperms.common.commands.misc.EditorCommand) Message(me.lucko.luckperms.common.locale.message.Message) LuckPermsPlugin(me.lucko.luckperms.common.plugin.LuckPermsPlugin) VerboseCommand(me.lucko.luckperms.common.commands.misc.VerboseCommand) MessageUtils(me.lucko.luckperms.common.command.utils.MessageUtils) ListTracks(me.lucko.luckperms.common.commands.track.ListTracks) UUID(java.util.UUID) TextComponent(net.kyori.text.TextComponent) Collectors(java.util.stream.Collectors) Executors(java.util.concurrent.Executors) List(java.util.List) CreateTrack(me.lucko.luckperms.common.commands.track.CreateTrack) Optional(java.util.Optional) TreeCommand(me.lucko.luckperms.common.commands.misc.TreeCommand) Pattern(java.util.regex.Pattern) DeleteGroup(me.lucko.luckperms.common.commands.group.DeleteGroup) MigrationMainCommand(me.lucko.luckperms.common.commands.migration.MigrationMainCommand) TrackMainCommand(me.lucko.luckperms.common.commands.track.TrackMainCommand) LocaleManager(me.lucko.luckperms.common.locale.LocaleManager) CompletableFuture(java.util.concurrent.CompletableFuture) LogMainCommand(me.lucko.luckperms.common.commands.log.LogMainCommand) Command(me.lucko.luckperms.common.command.abstraction.Command) ArgumentParser(me.lucko.luckperms.common.command.utils.ArgumentParser) ArrayList(java.util.ArrayList) SearchCommand(me.lucko.luckperms.common.commands.misc.SearchCommand) DebugCommand(me.lucko.luckperms.common.commands.misc.DebugCommand) ImmutableList(com.google.common.collect.ImmutableList) UserMainCommand(me.lucko.luckperms.common.commands.user.UserMainCommand) ClickEvent(net.kyori.text.event.ClickEvent) SyncCommand(me.lucko.luckperms.common.commands.misc.SyncCommand) ExecutorService(java.util.concurrent.ExecutorService) CreateGroup(me.lucko.luckperms.common.commands.group.CreateGroup) ApplyEditsCommand(me.lucko.luckperms.common.commands.misc.ApplyEditsCommand) NetworkSyncCommand(me.lucko.luckperms.common.commands.misc.NetworkSyncCommand) Executor(java.util.concurrent.Executor) Sender(me.lucko.luckperms.common.sender.Sender) ReloadConfigCommand(me.lucko.luckperms.common.commands.misc.ReloadConfigCommand) CommandPermission(me.lucko.luckperms.common.command.access.CommandPermission) GroupMainCommand(me.lucko.luckperms.common.commands.group.GroupMainCommand) DeleteTrack(me.lucko.luckperms.common.commands.track.DeleteTrack) Collections(java.util.Collections) TextComponent(net.kyori.text.TextComponent) HoverEvent(net.kyori.text.event.HoverEvent) InfoCommand(me.lucko.luckperms.common.commands.misc.InfoCommand) BulkUpdateCommand(me.lucko.luckperms.common.commands.misc.BulkUpdateCommand) ImportCommand(me.lucko.luckperms.common.commands.misc.ImportCommand) ExportCommand(me.lucko.luckperms.common.commands.misc.ExportCommand) CheckCommand(me.lucko.luckperms.common.commands.misc.CheckCommand) EditorCommand(me.lucko.luckperms.common.commands.misc.EditorCommand) VerboseCommand(me.lucko.luckperms.common.commands.misc.VerboseCommand) TreeCommand(me.lucko.luckperms.common.commands.misc.TreeCommand) MigrationMainCommand(me.lucko.luckperms.common.commands.migration.MigrationMainCommand) TrackMainCommand(me.lucko.luckperms.common.commands.track.TrackMainCommand) LogMainCommand(me.lucko.luckperms.common.commands.log.LogMainCommand) Command(me.lucko.luckperms.common.command.abstraction.Command) SearchCommand(me.lucko.luckperms.common.commands.misc.SearchCommand) DebugCommand(me.lucko.luckperms.common.commands.misc.DebugCommand) UserMainCommand(me.lucko.luckperms.common.commands.user.UserMainCommand) SyncCommand(me.lucko.luckperms.common.commands.misc.SyncCommand) ApplyEditsCommand(me.lucko.luckperms.common.commands.misc.ApplyEditsCommand) NetworkSyncCommand(me.lucko.luckperms.common.commands.misc.NetworkSyncCommand) ReloadConfigCommand(me.lucko.luckperms.common.commands.misc.ReloadConfigCommand) GroupMainCommand(me.lucko.luckperms.common.commands.group.GroupMainCommand) ClickEvent(net.kyori.text.event.ClickEvent)

Example 3 with Command

use of me.lucko.luckperms.common.command.abstraction.Command in project LuckPerms by lucko.

the class CommandManager method execute.

@SuppressWarnings("unchecked")
private CommandResult execute(Sender sender, String label, List<String> args) {
    List<String> arguments = new ArrayList<>(args);
    handleRewrites(arguments, true);
    // Handle no arguments
    if (arguments.isEmpty() || (arguments.size() == 1 && arguments.get(0).trim().isEmpty())) {
        MessageUtils.sendPluginMessage(sender, "&2Running &bLuckPerms v" + this.plugin.getBootstrap().getVersion() + "&2.");
        if (this.mainCommands.stream().anyMatch(c -> c.shouldDisplay() && c.isAuthorized(sender))) {
            Message.VIEW_AVAILABLE_COMMANDS_PROMPT.send(sender, label);
        } else {
            Message.NO_PERMISSION_FOR_SUBCOMMANDS.send(sender);
        }
        return CommandResult.INVALID_ARGS;
    }
    // Look for the main command.
    Optional<Command> o = this.mainCommands.stream().filter(m -> m.getName().equalsIgnoreCase(arguments.get(0))).limit(1).findAny();
    // Main command not found
    if (!o.isPresent()) {
        sendCommandUsage(sender, label);
        return CommandResult.INVALID_ARGS;
    }
    // Check the Sender has permission to use the main command.
    final Command main = o.get();
    if (!main.isAuthorized(sender)) {
        sendCommandUsage(sender, label);
        return CommandResult.NO_PERMISSION;
    }
    // remove the main command arg.
    arguments.remove(0);
    // Check the correct number of args were given for the main command
    if (main.getArgumentCheck().test(arguments.size())) {
        main.sendDetailedUsage(sender, label);
        return CommandResult.INVALID_ARGS;
    }
    // Try to execute the command.
    CommandResult result;
    try {
        result = main.execute(this.plugin, sender, null, arguments, label);
    } catch (CommandException e) {
        result = handleException(e, sender, label, main);
    } catch (Throwable e) {
        e.printStackTrace();
        result = CommandResult.FAILURE;
    }
    return result;
}
Also used : InfoCommand(me.lucko.luckperms.common.commands.misc.InfoCommand) BulkUpdateCommand(me.lucko.luckperms.common.commands.misc.BulkUpdateCommand) ImportCommand(me.lucko.luckperms.common.commands.misc.ImportCommand) ExportCommand(me.lucko.luckperms.common.commands.misc.ExportCommand) CheckCommand(me.lucko.luckperms.common.commands.misc.CheckCommand) EditorCommand(me.lucko.luckperms.common.commands.misc.EditorCommand) VerboseCommand(me.lucko.luckperms.common.commands.misc.VerboseCommand) TreeCommand(me.lucko.luckperms.common.commands.misc.TreeCommand) MigrationMainCommand(me.lucko.luckperms.common.commands.migration.MigrationMainCommand) TrackMainCommand(me.lucko.luckperms.common.commands.track.TrackMainCommand) LogMainCommand(me.lucko.luckperms.common.commands.log.LogMainCommand) Command(me.lucko.luckperms.common.command.abstraction.Command) SearchCommand(me.lucko.luckperms.common.commands.misc.SearchCommand) DebugCommand(me.lucko.luckperms.common.commands.misc.DebugCommand) UserMainCommand(me.lucko.luckperms.common.commands.user.UserMainCommand) SyncCommand(me.lucko.luckperms.common.commands.misc.SyncCommand) ApplyEditsCommand(me.lucko.luckperms.common.commands.misc.ApplyEditsCommand) NetworkSyncCommand(me.lucko.luckperms.common.commands.misc.NetworkSyncCommand) ReloadConfigCommand(me.lucko.luckperms.common.commands.misc.ReloadConfigCommand) GroupMainCommand(me.lucko.luckperms.common.commands.group.GroupMainCommand) ArrayList(java.util.ArrayList) CommandException(me.lucko.luckperms.common.command.abstraction.CommandException)

Aggregations

ArrayList (java.util.ArrayList)3 Command (me.lucko.luckperms.common.command.abstraction.Command)3 CommandException (me.lucko.luckperms.common.command.abstraction.CommandException)3 ImmutableList (com.google.common.collect.ImmutableList)2 List (java.util.List)2 Optional (java.util.Optional)2 Collectors (java.util.stream.Collectors)2 MessageUtils (me.lucko.luckperms.common.command.utils.MessageUtils)2 GroupMainCommand (me.lucko.luckperms.common.commands.group.GroupMainCommand)2 LogMainCommand (me.lucko.luckperms.common.commands.log.LogMainCommand)2 MigrationMainCommand (me.lucko.luckperms.common.commands.migration.MigrationMainCommand)2 ApplyEditsCommand (me.lucko.luckperms.common.commands.misc.ApplyEditsCommand)2 BulkUpdateCommand (me.lucko.luckperms.common.commands.misc.BulkUpdateCommand)2 CheckCommand (me.lucko.luckperms.common.commands.misc.CheckCommand)2 DebugCommand (me.lucko.luckperms.common.commands.misc.DebugCommand)2 EditorCommand (me.lucko.luckperms.common.commands.misc.EditorCommand)2 ExportCommand (me.lucko.luckperms.common.commands.misc.ExportCommand)2 ImportCommand (me.lucko.luckperms.common.commands.misc.ImportCommand)2 InfoCommand (me.lucko.luckperms.common.commands.misc.InfoCommand)2 NetworkSyncCommand (me.lucko.luckperms.common.commands.misc.NetworkSyncCommand)2