Search in sources :

Example 1 with ArgumentStack

use of revxrsal.commands.command.ArgumentStack in project MC-Auth-with-VK by U61vashka.

the class MessageListener method onMessage.

@EventHandler
public void onMessage(VKMessageEvent event) {
    VkHandler.getInstances().forEach((commandHandler) -> {
        CommandActor commandActor = new VKCommandActorWrapper(new BaseVkActor(event.getMessage(), commandHandler));
        ArgumentStack argumentStack = ArgumentStack.fromString(event.getMessage().getText());
        commandHandler.dispatch(commandActor, argumentStack);
    });
}
Also used : BaseVkActor(com.ubivashka.lamp.commands.vk.core.BaseVkActor) CommandActor(revxrsal.commands.command.CommandActor) VKCommandActorWrapper(me.mastercapexd.auth.link.vk.VKCommandActorWrapper) ArgumentStack(revxrsal.commands.command.ArgumentStack) EventHandler(net.md_5.bungee.event.EventHandler)

Example 2 with ArgumentStack

use of revxrsal.commands.command.ArgumentStack in project MC-Auth-with-VK by U61vashka.

the class BungeeCommandsRegistry method registerCommandContexts.

private void registerCommandContexts() {
    BungeePluginConfig config = PLUGIN.getConfig();
    BUNGEE_COMMAND_HANDLER.registerValueResolver(DoublePassword.class, (context) -> {
        ArgumentStack arguments = context.arguments();
        String oldPassword = arguments.pop();
        if (arguments.isEmpty())
            throw new SendMessageException(config.getBungeeMessages().getStringMessage("enter-new-password"));
        String newPassword = arguments.pop();
        DoublePassword password = new DoublePassword(oldPassword, newPassword);
        if (oldPassword.equals(newPassword))
            throw new SendMessageException(config.getBungeeMessages().getStringMessage("nothing-to-change"));
        if (newPassword.length() < config.getPasswordMinLength())
            throw new SendMessageException(config.getBungeeMessages().getStringMessage("password-too-short"));
        if (newPassword.length() > config.getPasswordMaxLength())
            throw new SendMessageException(config.getBungeeMessages().getStringMessage("password-too-long"));
        return password;
    });
    BUNGEE_COMMAND_HANDLER.registerValueResolver(NewPassword.class, context -> {
        String newRawPassword = context.pop();
        if (newRawPassword.length() < config.getPasswordMinLength())
            throw new SendMessageException(config.getBungeeMessages().getStringMessage("password-too-short"));
        if (newRawPassword.length() > config.getPasswordMaxLength())
            throw new SendMessageException(config.getBungeeMessages().getStringMessage("password-too-long"));
        return new NewPassword(newRawPassword);
    });
    BUNGEE_COMMAND_HANDLER.registerCondition((actor, command, arguments) -> {
        if (!actor.as(BungeeCommandActor.class).isPlayer())
            return;
        ProxiedPlayer player = actor.as(BungeeCommandActor.class).asPlayer();
        String accountId = config.getActiveIdentifierType().getId(player);
        if (!Auth.hasAccount(accountId))
            return;
        if (!command.hasAnnotation(AuthenticationStepCommand.class))
            return;
        Account account = Auth.getAccount(accountId);
        if (account.getCurrentAuthenticationStep() == null)
            return;
        String stepName = command.getAnnotation(AuthenticationStepCommand.class).stepName();
        if (account.getCurrentAuthenticationStep().getStepName().equals(stepName))
            return;
        throw new SendMessageException(config.getBungeeMessages().getSubMessages("authentication-step-usage").getStringMessage(account.getCurrentAuthenticationStep().getStepName()));
    });
    BUNGEE_COMMAND_HANDLER.registerCondition((actor, command, arguments) -> {
        if (!command.hasAnnotation(GoogleUse.class))
            return;
        if (!config.getGoogleAuthenticatorSettings().isEnabled())
            throw new SendMessageException(config.getBungeeMessages().getStringMessage("google-disabled"));
    });
    BUNGEE_COMMAND_HANDLER.registerCondition((actor, command, arguments) -> {
        if (!command.hasAnnotation(VkUse.class))
            return;
        if (!config.getVKSettings().isEnabled())
            throw new SendMessageException(config.getBungeeMessages().getStringMessage("vk-disabled"));
    });
    BUNGEE_COMMAND_HANDLER.registerValueResolver(CommandSender.class, context -> context.actor().as(BungeeCommandActor.class).getSender());
    BUNGEE_COMMAND_HANDLER.registerValueResolver(ProxiedPlayer.class, (context) -> {
        if (!context.parameter().hasAnnotation(OtherPlayer.class)) {
            ProxiedPlayer selfPlayer = context.actor().as(BungeeCommandActor.class).asPlayer();
            if (selfPlayer == null)
                throw new SendMessageException(config.getBungeeMessages().getStringMessage("players-only"));
            return selfPlayer;
        }
        String value = context.pop();
        ProxiedPlayer player = ProxyServer.getInstance().getPlayer(value);
        if (player == null)
            throw new SendMessageException(config.getBungeeMessages().getStringMessage("player-offline"));
        return player;
    });
    BUNGEE_COMMAND_HANDLER.registerContextResolver(Account.class, (context) -> {
        ProxiedPlayer player = context.actor().as(BungeeCommandActor.class).asPlayer();
        if (player == null)
            throw new SendMessageException(config.getBungeeMessages().getStringMessage("players-only"));
        String id = config.getActiveIdentifierType().getId(player);
        if (!Auth.hasAccount(id))
            throw new SendMessageException(config.getBungeeMessages().getStringMessage("already-logged-in"));
        Account account = Auth.getAccount(id);
        if (!account.isRegistered() && !context.parameter().hasAnnotation(AuthenticationAccount.class))
            throw new SendMessageException(config.getBungeeMessages().getStringMessage("account-not-found"));
        if (account.isRegistered() && context.parameter().hasAnnotation(AuthenticationAccount.class))
            throw new SendMessageException(config.getBungeeMessages().getStringMessage("account-exists"));
        return account;
    });
}
Also used : NewPassword(me.mastercapexd.auth.bungee.commands.parameters.NewPassword) ProxiedPlayer(net.md_5.bungee.api.connection.ProxiedPlayer) Account(me.mastercapexd.auth.account.Account) AuthenticationAccount(me.mastercapexd.auth.bungee.commands.annotations.AuthenticationAccount) AuthenticationAccount(me.mastercapexd.auth.bungee.commands.annotations.AuthenticationAccount) AuthenticationStepCommand(me.mastercapexd.auth.bungee.commands.annotations.AuthenticationStepCommand) SendMessageException(revxrsal.commands.exception.SendMessageException) BungeePluginConfig(me.mastercapexd.auth.config.BungeePluginConfig) BungeeCommandActor(revxrsal.commands.bungee.BungeeCommandActor) OtherPlayer(me.mastercapexd.auth.bungee.commands.annotations.OtherPlayer) DoublePassword(me.mastercapexd.auth.bungee.commands.parameters.DoublePassword) GoogleUse(me.mastercapexd.auth.bungee.commands.annotations.GoogleUse) ArgumentStack(revxrsal.commands.command.ArgumentStack) VkUse(me.mastercapexd.auth.bungee.commands.annotations.VkUse)

Example 3 with ArgumentStack

use of revxrsal.commands.command.ArgumentStack in project Lamp by Revxrsal.

the class BaseCommandDispatcher method handleFlag.

@SneakyThrows
private void handleFlag(List<String> input, CommandActor actor, ArgumentStack args, Object[] values, CommandParameter parameter) {
    String lookup = handler.getFlagPrefix() + parameter.getFlagName();
    int index = args.indexOf(lookup);
    ArgumentStack flagArguments;
    if (index == -1) {
        // flag isn't specified, use default value or throw an MPE.
        if (parameter.isOptional()) {
            if (parameter.getDefaultValue() != null) {
                args.add(lookup);
                args.add(parameter.getDefaultValue());
                index = args.indexOf(lookup);
                // remove the flag prefix + flag name
                args.remove(index);
                // put the actual value in a separate argument stack
                flagArguments = ArgumentStack.of(args.remove(index));
            } else {
                for (ParameterValidator<Object> v : parameter.getValidators()) {
                    v.validate(null, parameter, actor);
                }
                values[parameter.getMethodIndex()] = null;
                return;
            }
        } else {
            throw new MissingArgumentException(parameter);
        }
    } else {
        // remove the flag prefix + flag name
        args.remove(index);
        if (args.isEmpty())
            throw new MissingArgumentException(parameter);
        // put the actual value in a separate argument stack
        flagArguments = ArgumentStack.of(args.remove(index));
    }
    ValueContextR contextR = new ValueContextR(input, actor, parameter, values, flagArguments);
    Object value = parameter.getResolver().resolve(contextR);
    for (ParameterValidator<Object> v : parameter.getValidators()) {
        v.validate(value, parameter, actor);
    }
    values[parameter.getMethodIndex()] = value;
}
Also used : ArgumentStack(revxrsal.commands.command.ArgumentStack) SneakyThrows(lombok.SneakyThrows)

Example 4 with ArgumentStack

use of revxrsal.commands.command.ArgumentStack in project Lamp by Revxrsal.

the class VelocitySimpleCommand method execute.

@Override
public void execute(Invocation invocation) {
    CommandSource source = invocation.source();
    VelocityCommandActor actor = new VelocityActor(source, handler.getServer(), handler);
    try {
        ArgumentStack arguments = ArgumentStack.of(invocation.arguments());
        arguments.addFirst(invocation.alias());
        handler.dispatch(actor, arguments);
    } catch (Throwable t) {
        handler.getExceptionHandler().handleException(t, actor);
    }
}
Also used : VelocityCommandActor(revxrsal.commands.velocity.VelocityCommandActor) CommandSource(com.velocitypowered.api.command.CommandSource) ArgumentStack(revxrsal.commands.command.ArgumentStack)

Example 5 with ArgumentStack

use of revxrsal.commands.command.ArgumentStack in project Lamp by Revxrsal.

the class VelocitySimpleCommand method suggest.

@Override
public List<String> suggest(Invocation invocation) {
    try {
        VelocityCommandActor actor = new VelocityActor(invocation.source(), handler.getServer(), handler);
        ArgumentStack arguments;
        if (invocation.arguments().length == 0)
            arguments = ArgumentStack.forAutoCompletion("");
        else
            arguments = ArgumentStack.forAutoCompletion(invocation.arguments());
        arguments.addFirst(invocation.alias());
        return handler.getAutoCompleter().complete(actor, arguments);
    } catch (ArgumentParseException e) {
        return Collections.emptyList();
    }
}
Also used : VelocityCommandActor(revxrsal.commands.velocity.VelocityCommandActor) ArgumentParseException(revxrsal.commands.exception.ArgumentParseException) ArgumentStack(revxrsal.commands.command.ArgumentStack)

Aggregations

ArgumentStack (revxrsal.commands.command.ArgumentStack)12 ArgumentParseException (revxrsal.commands.exception.ArgumentParseException)4 BungeeCommandActor (revxrsal.commands.bungee.BungeeCommandActor)3 CommandActor (revxrsal.commands.command.CommandActor)3 NotNull (org.jetbrains.annotations.NotNull)2 BukkitCommandActor (revxrsal.commands.bukkit.BukkitCommandActor)2 VelocityCommandActor (revxrsal.commands.velocity.VelocityCommandActor)2 BaseVkActor (com.ubivashka.lamp.commands.vk.core.BaseVkActor)1 CommandSource (com.velocitypowered.api.command.CommandSource)1 SneakyThrows (lombok.SneakyThrows)1 Account (me.mastercapexd.auth.account.Account)1 AuthenticationAccount (me.mastercapexd.auth.bungee.commands.annotations.AuthenticationAccount)1 AuthenticationStepCommand (me.mastercapexd.auth.bungee.commands.annotations.AuthenticationStepCommand)1 GoogleUse (me.mastercapexd.auth.bungee.commands.annotations.GoogleUse)1 OtherPlayer (me.mastercapexd.auth.bungee.commands.annotations.OtherPlayer)1 VkUse (me.mastercapexd.auth.bungee.commands.annotations.VkUse)1 DoublePassword (me.mastercapexd.auth.bungee.commands.parameters.DoublePassword)1 NewPassword (me.mastercapexd.auth.bungee.commands.parameters.NewPassword)1 BungeePluginConfig (me.mastercapexd.auth.config.BungeePluginConfig)1 VKCommandActorWrapper (me.mastercapexd.auth.link.vk.VKCommandActorWrapper)1