Skip to content

Commit ded4480

Browse files
committed
Fix JsonUtils compatibility issue
Fix locking player object when executing command issue
1 parent 4b8eb49 commit ded4480

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

src/main/java/com/github/jie65535/opencommand/OpenCommandHandler.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ public void applyRoutes(Javalin javalin) {
4646
private static final Map<String, Integer> codes = new HashMap<>();
4747
private static final Int2ObjectMap<Date> codeExpireTime = new Int2ObjectOpenHashMap<>();
4848

49+
private static final Int2ObjectMap<MessageHandler> playerMessageHandlers = new Int2ObjectOpenHashMap<>();
50+
4951
public static void handle(Context context) {
5052
var plugin = OpenCommandPlugin.getInstance();
5153
var config = plugin.getConfig();
@@ -152,13 +154,18 @@ public static void handle(Context context) {
152154
return;
153155
}
154156
// Player MessageHandler do not support concurrency
157+
var handler = playerMessageHandlers.get(player.getUid());
158+
if (handler == null) {
159+
handler = new MessageHandler();
160+
playerMessageHandlers.put(player.getUid(), handler);
161+
}
155162
//noinspection SynchronizationOnLocalVariableOrMethodParameter
156-
synchronized (player) {
163+
synchronized (handler) {
157164
try {
158-
var resultCollector = new MessageHandler();
159-
player.setMessageHandler(resultCollector);
165+
handler.setMessage("");
166+
player.setMessageHandler(handler);
160167
CommandMap.getInstance().invoke(player, player, command);
161-
context.json(new JsonResponse(resultCollector.getMessage()));
168+
context.json(new JsonResponse(handler.getMessage()));
162169
} catch (Exception e) {
163170
plugin.getLogger().warn("Run command failed.", e);
164171
context.json(new JsonResponse(500, "error", e.getLocalizedMessage()));

src/main/java/com/github/jie65535/opencommand/OpenCommandPlugin.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
import emu.grasscutter.utils.JsonUtils;
3030

3131
import java.io.*;
32+
import java.nio.charset.StandardCharsets;
33+
import java.nio.file.Files;
3234

3335
public final class OpenCommandPlugin extends Plugin {
3436

@@ -105,8 +107,9 @@ private void loadConfig() {
105107
getLogger().error("[OpenCommand] Unable to save config file.");
106108
}
107109
} else {
108-
try (var fileReader = new InputStreamReader(new FileInputStream(configFile))) {
109-
config = JsonUtils.loadToClass(fileReader, OpenCommandConfig.class);
110+
try {
111+
config = JsonUtils.decode(Files.readString(configFile.toPath(), StandardCharsets.UTF_8),
112+
OpenCommandConfig.class);
110113
} catch (Exception exception) {
111114
config = new OpenCommandConfig();
112115
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() {
125128
data = new OpenCommandData();
126129
saveData();
127130
} else {
128-
try (var fileReader = new InputStreamReader(new FileInputStream(dataFile))) {
129-
data = JsonUtils.loadToClass(fileReader, OpenCommandData.class);
131+
try {
132+
data = JsonUtils.decode(Files.readString(dataFile.toPath(), StandardCharsets.UTF_8),
133+
OpenCommandData.class);
130134
} catch (Exception exception) {
131135
data = new OpenCommandData();
132136
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.");

0 commit comments

Comments
 (0)