diff --git a/src/main/java/com/github/jie65535/opencommand/OpenCommandHandler.java b/src/main/java/com/github/jie65535/opencommand/OpenCommandHandler.java index c06d007..71c6dd3 100644 --- a/src/main/java/com/github/jie65535/opencommand/OpenCommandHandler.java +++ b/src/main/java/com/github/jie65535/opencommand/OpenCommandHandler.java @@ -46,6 +46,8 @@ public void applyRoutes(Javalin javalin) { private static final Map codes = new HashMap<>(); private static final Int2ObjectMap codeExpireTime = new Int2ObjectOpenHashMap<>(); + private static final Int2ObjectMap playerMessageHandlers = new Int2ObjectOpenHashMap<>(); + public static void handle(Context context) { var plugin = OpenCommandPlugin.getInstance(); var config = plugin.getConfig(); @@ -152,13 +154,18 @@ public static void handle(Context context) { return; } // Player MessageHandler do not support concurrency + var handler = playerMessageHandlers.get(player.getUid()); + if (handler == null) { + handler = new MessageHandler(); + playerMessageHandlers.put(player.getUid(), handler); + } //noinspection SynchronizationOnLocalVariableOrMethodParameter - synchronized (player) { + synchronized (handler) { try { - var resultCollector = new MessageHandler(); - player.setMessageHandler(resultCollector); + handler.setMessage(""); + player.setMessageHandler(handler); CommandMap.getInstance().invoke(player, player, command); - context.json(new JsonResponse(resultCollector.getMessage())); + context.json(new JsonResponse(handler.getMessage())); } catch (Exception e) { plugin.getLogger().warn("Run command failed.", e); context.json(new JsonResponse(500, "error", e.getLocalizedMessage())); diff --git a/src/main/java/com/github/jie65535/opencommand/OpenCommandPlugin.java b/src/main/java/com/github/jie65535/opencommand/OpenCommandPlugin.java index 1e2aadd..7b9649b 100644 --- a/src/main/java/com/github/jie65535/opencommand/OpenCommandPlugin.java +++ b/src/main/java/com/github/jie65535/opencommand/OpenCommandPlugin.java @@ -29,6 +29,8 @@ import emu.grasscutter.utils.JsonUtils; import java.io.*; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; public final class OpenCommandPlugin extends Plugin { @@ -105,8 +107,9 @@ private void loadConfig() { getLogger().error("[OpenCommand] Unable to save config file."); } } else { - try (var fileReader = new InputStreamReader(new FileInputStream(configFile))) { - config = JsonUtils.loadToClass(fileReader, OpenCommandConfig.class); + 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."); @@ -125,8 +128,9 @@ private void loadData() { data = new OpenCommandData(); saveData(); } else { - try (var fileReader = new InputStreamReader(new FileInputStream(dataFile))) { - data = JsonUtils.loadToClass(fileReader, OpenCommandData.class); + try { + data = JsonUtils.decode(Files.readString(dataFile.toPath(), StandardCharsets.UTF_8), + OpenCommandData.class); } catch (Exception exception) { data = new OpenCommandData(); getLogger().error("[OpenCommand] There was an error while trying to load the data from data.json. Please make sure that there are no syntax errors. If you want to start with a default data, delete your existing data.json.");