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);
});
}
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;
});
}
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;
}
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);
}
}
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();
}
}
Aggregations