use of net.robinfriedli.aiode.entities.xml.CommandContribution in project aiode by robinfriedli.
the class StoredScript method asCommand.
public ScriptCommand asCommand(CommandManager commandManager, CommandContext context, String input) {
CommandContribution scriptCommandContribution = commandManager.getCommandContribution("script");
String commandBody = input.substring(identifier.length()).trim();
ScriptCommand scriptCommand = (ScriptCommand) scriptCommandContribution.instantiate(commandManager, context, commandBody);
scriptCommand.setScript(this);
return scriptCommand;
}
use of net.robinfriedli.aiode.entities.xml.CommandContribution in project aiode by robinfriedli.
the class PermissionCommand method getSelectedTargets.
private Set<? extends PermissionTarget> getSelectedTargets() {
Set<? extends PermissionTarget> selectedRootTargets = getSelectedRootTargets();
if (!argumentSet("argument")) {
return selectedRootTargets;
}
String argumentValue;
if (argumentSet("all")) {
argumentValue = getArgumentValueOrElse("argument", getCommandInput());
} else {
argumentValue = getArgumentValueWithTypeOrElse("argument", String.class, null);
}
if (argumentValue == null) {
return getAllArguments(selectedRootTargets);
}
List<String> argumentIdentifiers = COMMA_SPLITTER.splitToList(argumentValue);
if (argumentIdentifiers.isEmpty()) {
return getAllArguments(selectedRootTargets);
}
Set<CommandArgument> selectedArguments = Sets.newHashSet();
for (String argumentIdentifier : argumentIdentifiers) {
for (PermissionTarget selectedRootTarget : selectedRootTargets) {
if (selectedRootTarget instanceof CommandContribution) {
CommandContribution commandContribution = (CommandContribution) selectedRootTarget;
CommandArgument argument = commandContribution.getArgument(argumentIdentifier);
if (argument != null) {
selectedArguments.add(argument);
} else {
throw new InvalidCommandException(String.format("No such argument '%s' on command '%s'.", argumentIdentifier, commandContribution.getIdentifier()));
}
} else {
throw new InvalidCommandException(String.format("Cannot find argument '%s' on permission target '%s' as it is not a command.", argumentIdentifier, selectedRootTarget.getFullPermissionTargetIdentifier()));
}
}
}
return selectedArguments;
}
use of net.robinfriedli.aiode.entities.xml.CommandContribution in project aiode by robinfriedli.
the class CommandManager method instantiateCommandForContext.
public Optional<AbstractCommand> instantiateCommandForContext(CommandContext context, Session session, boolean includeScripts) {
String commandBody = context.getCommandBody();
if (commandBody.isBlank()) {
return Optional.empty();
}
String formattedCommandInput = commandBody.toLowerCase();
CommandContribution commandContribution = getCommandContributionForInput(commandBody);
AbstractCommand commandInstance;
// find a preset where the preset name matches the beginning of the command, find the longest matching preset name
// corresponds to lower(name) = substring(lower('" + commandBody.replaceAll("'", "''") + "'), 0, length(name) + 1)
Optional<Preset> optionalPreset = queryBuilderFactory.find(Preset.class).where((cb, root) -> cb.equal(cb.lower(root.get("name")), cb.substring(cb.literal(formattedCommandInput), cb.literal(1), cb.length(root.get("name"))))).orderBy((root, cb) -> cb.desc(cb.length(root.get("name")))).build(session).setMaxResults(1).setCacheable(true).uniqueResultOptional();
Optional<StoredScript> optionalStoredScript;
if (includeScripts) {
optionalStoredScript = queryBuilderFactory.find(StoredScript.class).where(((cb, root, subQueryFactory) -> cb.and(cb.equal(cb.lower(root.get("identifier")), cb.substring(cb.literal(formattedCommandInput), cb.literal(1), cb.length(root.get("identifier")))), cb.equal(root.get("scriptUsage"), subQueryFactory.createUncorrelatedSubQuery(StoredScript.ScriptUsage.class, "pk").where((cb1, root1) -> cb1.equal(root1.get("uniqueId"), "script")).build(session))))).orderBy((root, cb) -> cb.desc(cb.length(root.get("identifier")))).build(session).setMaxResults(1).setCacheable(true).uniqueResultOptional();
} else {
optionalStoredScript = Optional.empty();
}
// A ∧ B ∧ C
if (commandContribution != null && optionalPreset.isPresent() && optionalStoredScript.isPresent()) {
Preset preset = optionalPreset.get();
String identifier = commandContribution.getIdentifier();
StoredScript storedScript = optionalStoredScript.get();
if (preset.getName().length() > identifier.length() && preset.getName().length() >= storedScript.getIdentifier().length()) {
commandInstance = preset.instantiateCommand(this, context, commandBody);
} else if (storedScript.getIdentifier().length() > identifier.length() && storedScript.getIdentifier().length() > preset.getName().length()) {
commandInstance = storedScript.asCommand(this, context, commandBody);
} else {
String commandInput = commandBody.substring(identifier.length()).trim();
commandInstance = commandContribution.instantiate(this, context, commandInput);
}
} else if (commandContribution != null && optionalPreset.isPresent()) {
// A ∧ B ∧ !C
String identifier = commandContribution.getIdentifier();
Preset preset = optionalPreset.get();
if (preset.getName().length() > identifier.length()) {
commandInstance = preset.instantiateCommand(this, context, commandBody);
} else {
String commandInput = commandBody.substring(identifier.length()).trim();
commandInstance = commandContribution.instantiate(this, context, commandInput);
}
} else if (optionalPreset.isPresent() && optionalStoredScript.isPresent()) {
// !A ∧ B ∧ C
Preset preset = optionalPreset.get();
StoredScript storedScript = optionalStoredScript.get();
if (storedScript.getIdentifier().length() > preset.getName().length()) {
commandInstance = storedScript.asCommand(this, context, commandBody);
} else {
commandInstance = preset.instantiateCommand(this, context, commandBody);
}
} else if (commandContribution != null && optionalStoredScript.isPresent()) {
// A ∧ !B ∧ C
StoredScript storedScript = optionalStoredScript.get();
String identifier = commandContribution.getIdentifier();
if (storedScript.getIdentifier().length() > identifier.length()) {
commandInstance = storedScript.asCommand(this, context, commandBody);
} else {
String commandInput = commandBody.substring(identifier.length()).trim();
commandInstance = commandContribution.instantiate(this, context, commandInput);
}
} else if (optionalStoredScript.isPresent()) {
// !A ∧ !B ∧ C
commandInstance = optionalStoredScript.get().asCommand(this, context, commandBody);
} else if (optionalPreset.isPresent()) {
// !A ∧ B ∧ !C
commandInstance = optionalPreset.get().instantiateCommand(this, context, commandBody);
} else if (commandContribution != null) {
// A ∧ !B ∧ !C
String commandInput = commandBody.substring(commandContribution.getIdentifier().length()).trim();
commandInstance = commandContribution.instantiate(this, context, commandInput);
} else {
// !A ∧ !B ∧ !C
return Optional.empty();
}
return Optional.of(commandInstance);
}
use of net.robinfriedli.aiode.entities.xml.CommandContribution in project aiode by robinfriedli.
the class Preset method instantiateCommand.
public AbstractCommand instantiateCommand(CommandManager commandManager, CommandContext context, String input) {
CommandContribution presetContribution = commandManager.getCommandContributionForInput(preset);
if (presetContribution == null) {
throw new InvalidCommandException("Invalid preset, no command found");
}
String commandBody = preset.substring(presetContribution.getIdentifier().length()).trim();
String var = input.substring(name.length()).trim();
if (commandBody.contains("%s")) {
commandBody = String.format(commandBody, var);
} else if (!var.isBlank()) {
throw new InvalidCommandException("This preset does not have a variable");
}
return presetContribution.instantiate(commandManager, context, commandBody);
}
Aggregations