This commit is contained in:
2023-01-11 23:09:21 +08:00
parent 560a5e25f0
commit ec347e2eab
2 changed files with 11 additions and 6 deletions

View File

@ -46,7 +46,7 @@ public final class OpenChatPlugin extends Plugin {
var configFile = new File(getDataFolder(), "config.json");
if (!configFile.exists()) {
config = new OpenChatConfig();
try (var file = new FileWriter(configFile)) {
try (var file = new FileWriter(configFile, StandardCharsets.UTF_8)) {
file.write(JsonUtils.encode(config));
} catch (IOException e) {
getLogger().error("[OpenChat] Unable to write to config file.");
@ -58,7 +58,7 @@ public final class OpenChatPlugin extends Plugin {
config = JsonUtils.decode(Files.readString(configFile.toPath(), StandardCharsets.UTF_8), OpenChatConfig.class);
} catch (Exception exception) {
config = new OpenChatConfig();
getLogger().error("[OpenChat] 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.");
getLogger().error("[OpenChat] 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.", exception);
}
}
}
@ -68,7 +68,6 @@ public final class OpenChatPlugin extends Plugin {
return data;
}
private void loadData() {
data = new OpenChatData();
var dataFile = new File(getDataFolder(), "data.json");
if (!dataFile.exists()) {
data = new OpenChatData();
@ -78,12 +77,12 @@ public final class OpenChatPlugin extends Plugin {
data = JsonUtils.decode(Files.readString(dataFile.toPath(), StandardCharsets.UTF_8), OpenChatData.class);
} catch (Exception exception) {
data = new OpenChatData();
getLogger().error("[OpenChat] 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.");
getLogger().error("[OpenChat] 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.", exception);
}
}
}
public void saveData() {
try (var file = new FileWriter(new File(getDataFolder(), "data.json"))) {
try (var file = new FileWriter(new File(getDataFolder(), "data.json"), StandardCharsets.UTF_8)) {
file.write(JsonUtils.encode(data));
} catch (IOException e) {
getLogger().error("[OpenChat] Unable to write to data file.");
@ -112,6 +111,7 @@ public final class OpenChatPlugin extends Plugin {
getHandle().registerCommand(new ChatServerCommands());
getHandle().registerCommand(new ChatPlayerCommands());
// Set my chat system.
getServer().setChatSystem(new OpenChatSystem(getServer(), this));
// Log a plugin status message.

View File

@ -14,6 +14,8 @@ public class OpenChatSystem extends ChatSystem {
@Override
public void sendPrivateMessage(Player player, int targetUid, String message) {
plugin.getLogger().info(String.format("onSendPrivateMessage: player=%s(%d) targetUid=%d message=%s",
player.getNickname(), player.getUid(), targetUid, message));
// Sanity checks.
if (message == null || message.length() == 0) {
return;
@ -33,6 +35,7 @@ public class OpenChatSystem extends ChatSystem {
* @param message 消息内容
*/
private void handlePlayerMessage(Player player, String message) {
plugin.getLogger().info("handlePlayerMessage enter");
if (!plugin.getConfig().serverChatEnabled) {
return;
}
@ -45,6 +48,8 @@ public class OpenChatSystem extends ChatSystem {
// 处理发言频率限制与发言内容审查
if (!checkMessageFre(player) || !checkMessageModeration(message)) {
// 可提示也可忽略忽略可让玩家以为自己发送成功其实别人看不到
plugin.getLogger().warn(String.format("Message blocked: player=%s(%d): \"%s\"",
player.getNickname(), player.getUid(), message));
return;
}
// 格式化消息
@ -55,7 +60,7 @@ public class OpenChatSystem extends ChatSystem {
// 转发给其它玩家
for (Player p : getServer().getPlayers().values()) {
// 将消息发送给除了自己以外所有未关闭聊天的玩家
if (p != player && plugin.getData().offChatPlayers.contains(p.getUid())) {
if (p != player && !plugin.getData().offChatPlayers.contains(p.getUid())) {
p.dropMessage(message);
}
}