Support multi-line commands

Update auto-generate console token
This commit is contained in:
2023-09-02 18:38:59 +08:00
parent e9e2805738
commit 1b86226951
4 changed files with 111 additions and 76 deletions

View File

@@ -83,7 +83,7 @@ public final class OpenCommandHandler implements Router {
}
return;
} else if (req.action.equals("ping")) {
context.json(new JsonResponse());
context.json(new JsonResponse(plugin.getVersion()));
return;
} else if (req.action.equals("online")) {
var p = new ArrayList<String>();
@@ -147,7 +147,7 @@ public final class OpenCommandHandler implements Router {
// update token expire time
client.tokenExpireTime = new Date(now.getTime() + config.tokenLastUseExpirationTime_H * 60L * 60L * 1000L);
var player = plugin.getServer().getPlayerByUid(client.playerId);
var command = req.data.toString();
var rawMessage = req.data.toString();
if (player == null) {
context.json(new JsonResponse(404, "Player not found"));
return;
@@ -158,7 +158,12 @@ public final class OpenCommandHandler implements Router {
synchronized (handler) {
try {
handler.setLength(0);
CommandMap.getInstance().invoke(player, player, command);
for (var command : rawMessage.split("\n[/!]|\\|")) {
if (command.charAt(0) == '/' || command.charAt(0) == '!') {
command = command.substring(1);
}
CommandMap.getInstance().invoke(player, player, command);
}
context.json(new JsonResponse(handler.toString()));
} catch (Exception e) {
plugin.getLogger().warn("Run command failed.", e);

View File

@@ -26,7 +26,9 @@ import emu.grasscutter.server.event.HandlerPriority;
import emu.grasscutter.server.event.game.ReceiveCommandFeedbackEvent;
import emu.grasscutter.server.event.player.PlayerJoinEvent;
import emu.grasscutter.server.event.player.PlayerQuitEvent;
import emu.grasscutter.utils.Crypto;
import emu.grasscutter.utils.JsonUtils;
import emu.grasscutter.utils.Utils;
import java.io.*;
import java.nio.charset.StandardCharsets;
@@ -105,24 +107,24 @@ public final class OpenCommandPlugin extends Plugin {
var configFile = new File(getDataFolder(), "config.json");
if (!configFile.exists()) {
config = new OpenCommandConfig();
try (var file = new FileWriter(configFile)) {
file.write(JsonUtils.encode(config));
} catch (IOException e) {
getLogger().error("[OpenCommand] Unable to write to config file.");
} catch (Exception e) {
getLogger().error("[OpenCommand] Unable to save config file.");
}
saveConfig();
} else {
try {
config = JsonUtils.decode(Files.readString(configFile.toPath(), StandardCharsets.UTF_8),
OpenCommandConfig.class);
} catch (Exception exception) {
config = new OpenCommandConfig();
getLogger().error("[OpenCommand] There was an error while trying to load the configuration from config.json. Please make sure that there are no syntax errors. If you want to start with a default configuration, delete your existing config.json.");
}
if (config == null) {
config = new OpenCommandConfig();
}
}
// 检查控制台Token
if (config.consoleToken == null || config.consoleToken.isEmpty()) {
config.consoleToken = Utils.base64Encode(Crypto.createSessionKey(24));
saveConfig();
getLogger().warn("Detected that consoleToken is empty, automatically generated Token for you as follows: {}", config.consoleToken);
}
try {
runMode = Grasscutter.getConfig().server.runMode;
} catch (Exception ex) {
@@ -130,6 +132,17 @@ public final class OpenCommandPlugin extends Plugin {
}
}
private void saveConfig() {
var configFile = new File(getDataFolder(), "config.json");
try (var file = new FileWriter(configFile)) {
file.write(JsonUtils.encode(config));
} catch (IOException e) {
getLogger().error("[OpenCommand] Unable to write to config file.");
} catch (Exception e) {
getLogger().error("[OpenCommand] Unable to save config file.");
}
}
private void loadData() {
var dataFile = new File(getDataFolder(), "data.json");
if (!dataFile.exists()) {