Notify user about unsuccessful system command execution

Additionally added error system command.
Fixes #75
This commit is contained in:
Leon Hofmeister 2020-10-06 22:20:11 +02:00
parent 5e1b9a9d5b
commit 6f9982bbc3
Signed by: delvh
GPG Key ID: 3DECE05F6D9A647C
2 changed files with 32 additions and 28 deletions

View File

@ -1,11 +1,16 @@
package envoy.client.data.commands;
import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Consumer;
import java.util.logging.*;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import javafx.application.Platform;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import envoy.util.EnvoyLog;
/**
@ -27,26 +32,9 @@ public final class SystemCommandMap {
private static final Logger logger = EnvoyLog.getLogger(SystemCommandMap.class);
/**
* The default char to be used as activator.
* <p>
* Value: '/'.
*
* @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.
* Creates a new {@code SystemCommandMap} with the given char as activator.
* If this Character is null, any text used as input will be treated as a system
* command.
*
* @param activator the char to use as activator for commands
* @since Envoy Client v0.3-beta
@ -54,11 +42,11 @@ public final class SystemCommandMap {
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
*/
public SystemCommandMap() { activator = defaultActivator; }
public SystemCommandMap() { activator = '/'; }
/**
* Adds a new command to the map if the command name is valid.
@ -101,7 +89,7 @@ public final class SystemCommandMap {
* input.
* It returns the command as (most likely) entered as key in the map for the
* 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
* @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.
*
* @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
*/
public boolean executeIfPresent(String raw) {
@ -204,12 +192,13 @@ public final class SystemCommandMap {
* result: {@code button.getText()=="xyz"}
*
* @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
*/
private boolean executeAvailableCommand(String input) {
final var command = getCommand(input);
final var value = get(command);
final var command = getCommand(input);
final var value = get(command);
final var commandExecuted = new AtomicBoolean(value.isPresent());
value.ifPresent(systemCommand -> {
// Splitting the String so that the leading command including the first " " is
@ -225,11 +214,23 @@ public final class SystemCommandMap {
String.format(
"System command %s could not be performed correctly because the user is a dumbass and could not write a parseable number.",
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) {
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) {
this.messageList = messageList;
// Error message initialization
builder.setAction(text -> { throw new RuntimeException(); }).setDescription("Shows an error message.").buildNoArg("error");
// Do A Barrel roll initialization
final var random = new Random();
builder.setAction(text -> chatScene.doABarrelRoll(Integer.parseInt(text.get(0)), Double.parseDouble(text.get(1))))