Move SystemComandMap From ChatScene to Its Own Component (#74)

Move SystemComandMap from ChatScene to its own component.
Create message specific commands with their own parser.
Fix separators not shown correctly in TextInputContextMenu.
Reviewed-on: https://git.kske.dev/zdm/envoy/pulls/74
Reviewed-by: kske <kai@kske.dev>
Reviewed-by: DieGurke <maxi@kske.dev>
This commit is contained in:
2020-10-02 15:23:21 +02:00
parent 85d0aa37d2
commit 9934eefd41
10 changed files with 218 additions and 132 deletions

View File

@ -0,0 +1,23 @@
package envoy.client.data.commands;
import java.util.List;
/**
* This interface defines an action that should be performed when a system
* command gets called.
*
* @author Leon Hofmeister
* @since Envoy Client v0.2-beta
*/
public interface Callable {
/**
* Performs the instance specific action when a {@link SystemCommand} has been
* called.
*
* @param arguments the arguments that should be passed to the
* {@link SystemCommand}
* @since Envoy Client v0.2-beta
*/
void call(List<String> arguments);
}

View File

@ -1,30 +0,0 @@
package envoy.client.data.commands;
import java.util.function.Supplier;
/**
* This interface defines an action that should be performed when a system
* command gets called.
*
* @author Leon Hofmeister
* @since Envoy Client v0.2-beta
*/
public interface OnCall {
/**
* Performs class specific actions when a {@link SystemCommand} has been called.
*
* @since Envoy Client v0.2-beta
*/
void onCall();
/**
* Performs actions that can only be performed by classes that are not
* {@link SystemCommand}s when a SystemCommand has been called.
*
* @param consumer the action to perform when this {@link SystemCommand} has
* been called
* @since Envoy Client v0.2-beta
*/
void onCall(Supplier<Void> consumer);
}

View File

@ -1,15 +1,15 @@
package envoy.client.data.commands;
import java.util.*;
import java.util.function.*;
import java.util.function.Consumer;
/**
* This class is the base class of all {@code SystemCommands} and contains an
* action and a number of arguments that should be used as input for this
* function.
* No {@code SystemCommand} can return anything.
* Every {@code SystemCommand} must have as argument type {@code List<String>} so
* that the words following the indicator String can be used as input of the
* Every {@code SystemCommand} must have as argument type {@code List<String>}
* so that the words following the indicator String can be used as input of the
* function. This approach has one limitation:<br>
* <b>Order matters!</b> Changing the order of arguments will likely result in
* unexpected behavior.
@ -17,7 +17,7 @@ import java.util.function.*;
* @author Leon Hofmeister
* @since Envoy Client v0.2-beta
*/
public final class SystemCommand implements OnCall {
public final class SystemCommand implements Callable {
protected int relevance;
@ -55,12 +55,6 @@ public final class SystemCommand implements OnCall {
this.description = description;
}
/**
* @return the action that should be performed
* @since Envoy Client v0.2-beta
*/
public Consumer<List<String>> getAction() { return action; }
/**
* @return the argument count of the command
* @since Envoy Client v0.2-beta
@ -85,20 +79,10 @@ public final class SystemCommand implements OnCall {
*/
public void setRelevance(int relevance) { this.relevance = relevance; }
/**
* Increments the relevance of this {@code SystemCommand}.
*/
@Override
public void onCall() { relevance++; }
/**
* Increments the relevance of this {@code SystemCommand} and executes the
* supplier.
*/
@Override
public void onCall(Supplier<Void> consumer) {
onCall();
consumer.get();
public void call(List<String> arguments) {
action.accept(arguments);
++relevance;
}
/**
@ -115,14 +99,13 @@ public final class SystemCommand implements OnCall {
if (this == obj) return true;
if (obj == null) return false;
if (getClass() != obj.getClass()) return false;
final SystemCommand other = (SystemCommand) obj;
final var other = (SystemCommand) obj;
return Objects.equals(action, other.action);
}
@Override
public String toString() {
return "SystemCommand [relevance=" + relevance + ", numberOfArguments=" + numberOfArguments + ", "
+ (action != null ? "action=" + action + ", " : "") + (description != null ? "description=" + description + ", " : "")
+ (defaults != null ? "defaults=" + defaults : "") + "]";
+ (description != null ? "description=" + description + ", " : "") + (defaults != null ? "defaults=" + defaults : "") + "]";
}
}

View File

@ -73,9 +73,14 @@ public final class SystemCommandMap {
* exception: for recommendation purposes.
*/
public String getCommand(String raw) {
final var trimmed = raw.stripLeading();
final var index = trimmed.indexOf(' ');
return trimmed.substring(trimmed.charAt(0) == '/' ? 1 : 0, index < 1 ? trimmed.length() : index);
final var trimmed = raw.stripLeading();
// Entering only a slash should not throw an error
if (trimmed.length() == 1 && trimmed.charAt(0) == '/') return "";
else {
final var index = trimmed.indexOf(' ');
return trimmed.substring(trimmed.charAt(0) == '/' ? 1 : 0, index < 1 ? trimmed.length() : index);
}
}
/**
@ -92,7 +97,7 @@ public final class SystemCommandMap {
* @since Envoy Client v0.2-beta
*/
public boolean isValidKey(String command) {
final boolean valid = commandPattern.matcher(command).matches();
final var valid = commandPattern.matcher(command).matches();
if (!valid) logger.log(Level.WARNING,
"The command \"" + command
+ "\" is not valid. As it will cause problems in execution, it will not be entered into the map. Only the characters "
@ -150,10 +155,14 @@ public final class SystemCommandMap {
final var arguments = extractArguments(input, systemCommand);
// Executing the function
try {
systemCommand.getAction().accept(arguments);
systemCommand.onCall();
systemCommand.call(arguments);
} catch (final NumberFormatException e) {
logger.log(Level.INFO,
String.format(
"System command %s could not be performed correctly because the user is a dumbass and could not write a parseable number.",
command));
} catch (final Exception e) {
logger.log(Level.WARNING, "The system command " + command + " threw an exception: ", e);
logger.log(Level.WARNING, "System command " + command + " threw an exception: ", e);
}
});
return value.isPresent();
@ -241,7 +250,7 @@ public final class SystemCommandMap {
final var numberOfArguments = toEvaluate.getNumberOfArguments();
final List<String> result = new ArrayList<>();
if (toEvaluate.getNumberOfArguments() > 0) for (int index = 0; index < numberOfArguments; index++) {
if (toEvaluate.getNumberOfArguments() > 0) for (var index = 0; index < numberOfArguments; index++) {
String textArg = null;
if (index < textArguments.length) textArg = textArguments[index];
// Set the argument at position index to the current argument of the text, if it