Skip to content

Commit

Permalink
修复world/stats不存在时报错导致一些功能无法正常使用
Browse files Browse the repository at this point in the history
Fixes #38
  • Loading branch information
Xujiayao committed Jun 28, 2024
1 parent a666396 commit b226e4b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ public void run() {
properties.load(new FileInputStream("server.properties"));

Set<String> uniquePlayers = new HashSet<>();
FileUtils.listFiles(new File((properties.getProperty("level-name") + "/stats/")), null, false).forEach(file -> uniquePlayers.add(file.getName()));
try {
FileUtils.listFiles(new File((properties.getProperty("level-name") + "/stats/")), null, false).forEach(file -> uniquePlayers.add(file.getName()));
} catch (Exception ignored) {
}
channelTopicInfo.add("uniquePlayers", new Gson().fromJson(Arrays.toString(uniquePlayers.toArray()), JsonArray.class));

channelTopicInfo.addProperty("serverName", CONFIG.multiServer.name);
Expand Down
45 changes: 27 additions & 18 deletions src/main/java/com/xujiayao/discord_mc_chat/utils/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import java.nio.charset.StandardCharsets;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -468,26 +469,28 @@ public static String getStatsCommandMessage(String type, String name) {
Properties properties = new Properties();
properties.load(new FileInputStream("server.properties"));

FileUtils.listFiles(new File((properties.getProperty("level-name") + "/stats/")), null, false).forEach(file -> {
try {
for (JsonElement player : players) {
if (player.getAsJsonObject().get("uuid").getAsString().equals(file.getName().replace(".json", ""))) {
JsonObject json = new Gson().fromJson(IOUtils.toString(file.toURI(), StandardCharsets.UTF_8), JsonObject.class);

try {
stats.put(player.getAsJsonObject().get("name").getAsString(), json
.getAsJsonObject("stats")
.getAsJsonObject("minecraft:" + type)
.get("minecraft:" + name)
.getAsInt());
} catch (NullPointerException ignored) {
}
Collection<File> files = new ArrayList<>();
try {
files = FileUtils.listFiles(new File((properties.getProperty("level-name") + "/stats/")), null, false);
} catch (Exception ignored) {
}

for (File file : files) {
for (JsonElement player : players) {
if (player.getAsJsonObject().get("uuid").getAsString().equals(file.getName().replace(".json", ""))) {
JsonObject json = new Gson().fromJson(IOUtils.toString(file.toURI(), StandardCharsets.UTF_8), JsonObject.class);

try {
stats.put(player.getAsJsonObject().get("name").getAsString(), json
.getAsJsonObject("stats")
.getAsJsonObject("minecraft:" + type)
.get("minecraft:" + name)
.getAsInt());
} catch (NullPointerException ignored) {
}
}
} catch (Exception e) {
LOGGER.error(ExceptionUtils.getStackTrace(e));
}
});
}
} catch (Exception e) {
LOGGER.error(ExceptionUtils.getStackTrace(e));
}
Expand Down Expand Up @@ -573,10 +576,16 @@ public void run() {
Properties properties = new Properties();
properties.load(new FileInputStream("server.properties"));

int uniquePlayerCount = 0;
try {
uniquePlayerCount = FileUtils.listFiles(new File((properties.getProperty("level-name") + "/stats/")), null, false).size();
} catch (Exception ignored) {
}

String topic = Translations.translateMessage("message.onlineChannelTopic")
.replace("%onlinePlayerCount%", Integer.toString(SERVER.getPlayerCount()))
.replace("%maxPlayerCount%", Integer.toString(SERVER.getMaxPlayers()))
.replace("%uniquePlayerCount%", Integer.toString(FileUtils.listFiles(new File((properties.getProperty("level-name") + "/stats/")), null, false).size()))
.replace("%uniquePlayerCount%", Integer.toString(uniquePlayerCount))
.replace("%serverStartedTime%", SERVER_STARTED_TIME)
.replace("%lastUpdateTime%", Long.toString(epochSecond))
.replace("%nextUpdateTime%", Long.toString(epochSecond + CONFIG.generic.channelTopicUpdateInterval / 1000));
Expand Down

0 comments on commit b226e4b

Please sign in to comment.