Add Customizability to SystemCommandMap #84

Merged
delvh merged 2 commits from f/enhanced-system-commands into develop 2020-10-07 22:12:58 +02:00
2 changed files with 32 additions and 28 deletions
Showing only changes of commit 6f9982bbc3 - Show all commits

View File

@ -1,11 +1,16 @@
package envoy.client.data.commands; package envoy.client.data.commands;
import java.util.*; import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.logging.*; import java.util.logging.*;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import javafx.application.Platform;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import envoy.util.EnvoyLog; import envoy.util.EnvoyLog;
/** /**
@ -27,26 +32,9 @@ public final class SystemCommandMap {
private static final Logger logger = EnvoyLog.getLogger(SystemCommandMap.class); private static final Logger logger = EnvoyLog.getLogger(SystemCommandMap.class);
/** /**
* The default char to be used as activator. * Creates a new {@code SystemCommandMap} with the given char as activator.
* <p> * If this Character is null, any text used as input will be treated as a system
* Value: '/'. * command.
*
* @since Envoy Client v0.3-beta
*/
public static final char defaultActivator = '/';
/**
* The Character to use if every String should be considered as a possible
* {@link SystemCommand}.
* <p>
* Value: null.
*
* @since Envoy Client v0.3-beta
*/
public static final Character noActivator = null;
/**
* Creates a new {@link SystemCommandMap} with the given activator.
* *
* @param activator the char to use as activator for commands * @param activator the char to use as activator for commands
* @since Envoy Client v0.3-beta * @since Envoy Client v0.3-beta
@ -54,11 +42,11 @@ public final class SystemCommandMap {
public SystemCommandMap(Character activator) { this.activator = activator; } public SystemCommandMap(Character activator) { this.activator = activator; }
/** /**
* Creates a new {@link SystemCommandMap} with default activator. * Creates a new {@code SystemCommandMap} with '/' as activator.
* *
* @since Envoy Client v0.3-beta * @since Envoy Client v0.3-beta
*/ */
public SystemCommandMap() { activator = defaultActivator; } public SystemCommandMap() { activator = '/'; }
/** /**
* Adds a new command to the map if the command name is valid. * Adds a new command to the map if the command name is valid.
@ -101,7 +89,7 @@ public final class SystemCommandMap {
* input. * input.
* It returns the command as (most likely) entered as key in the map for the * It returns the command as (most likely) entered as key in the map for the
* first word of the text.<br> * first word of the text.<br>
* Activators in the middle of the wod will be disregarded. * Activators in the middle of the word will be disregarded.
* *
* @param raw the input * @param raw the input
* @return the command as entered in the map * @return the command as entered in the map
@ -149,7 +137,7 @@ public final class SystemCommandMap {
* activator. If that is the case, it will be executed. * activator. If that is the case, it will be executed.
* *
* @param raw the raw input string * @param raw the raw input string
* @return whether a command could be found * @return whether a command could be found and successfully executed
* @since Envoy Client v0.2-beta * @since Envoy Client v0.2-beta
*/ */
public boolean executeIfPresent(String raw) { public boolean executeIfPresent(String raw) {
@ -204,12 +192,13 @@ public final class SystemCommandMap {
* result: {@code button.getText()=="xyz"} * result: {@code button.getText()=="xyz"}
* *
* @param input the input string given by the user * @param input the input string given by the user
* @return whether a command could be found * @return whether a command could be found and successfully executed
* @since Envoy Client v0.2-beta * @since Envoy Client v0.2-beta
*/ */
private boolean executeAvailableCommand(String input) { private boolean executeAvailableCommand(String input) {
final var command = getCommand(input); final var command = getCommand(input);
final var value = get(command); final var value = get(command);
final var commandExecuted = new AtomicBoolean(value.isPresent());
value.ifPresent(systemCommand -> { value.ifPresent(systemCommand -> {
// Splitting the String so that the leading command including the first " " is // Splitting the String so that the leading command including the first " " is
@ -225,11 +214,23 @@ public final class SystemCommandMap {
String.format( String.format(
"System command %s could not be performed correctly because the user is a dumbass and could not write a parseable number.", "System command %s could not be performed correctly because the user is a dumbass and could not write a parseable number.",
command)); command));
Platform.runLater(() -> {
final var alert = new Alert(AlertType.ERROR);
alert.setContentText("Please enter a readable number as argument.");
alert.showAndWait();
});
commandExecuted.set(false);
} catch (final Exception e) { } catch (final Exception e) {
logger.log(Level.WARNING, "System command " + command + " threw an exception: ", e); logger.log(Level.WARNING, "System command " + command + " threw an exception: ", e);
Platform.runLater(() -> {
final var alert = new Alert(AlertType.ERROR);
alert.setContentText("Could not execute system command: Internal error. Please insult the responsible programmer.");
alert.showAndWait();
});
commandExecuted.set(false);
} }
}); });
return value.isPresent(); return commandExecuted.get();
} }
/** /**

View File

@ -40,6 +40,9 @@ public final class ChatSceneCommands {
public ChatSceneCommands(ListView<Message> messageList, ChatScene chatScene) { public ChatSceneCommands(ListView<Message> messageList, ChatScene chatScene) {
this.messageList = messageList; this.messageList = messageList;
// Error message initialization
builder.setAction(text -> { throw new RuntimeException(); }).setDescription("Shows an error message.").buildNoArg("error");
// Do A Barrel roll initialization // Do A Barrel roll initialization
final var random = new Random(); final var random = new Random();
builder.setAction(text -> chatScene.doABarrelRoll(Integer.parseInt(text.get(0)), Double.parseDouble(text.get(1)))) builder.setAction(text -> chatScene.doABarrelRoll(Integer.parseInt(text.get(0)), Double.parseDouble(text.get(1))))