From b3784d39d864386b6973b87133bc5e05802b9fad Mon Sep 17 00:00:00 2001 From: LostLuma Date: Fri, 10 Nov 2023 12:13:40 +0100 Subject: [PATCH] General cleanup / organization --- .../mixin/common/PlayerEntityMixin.java | 5 ++- .../server_stats/stats/ServerPlayerStats.java | 30 +++++++----------- .../{server => common}/EntitiesMixin.java | 2 +- .../mixin/common}/PlayerEntityMixin.java | 7 ++--- .../PlayerManagerMixin.java | 2 +- .../server_stats/stats/ServerPlayerStats.java | 30 +++++++----------- .../main/resources/server_stats.mixins.json | 9 +++--- .../mixin/common/PlayerEntityMixin.java | 5 ++- .../server_stats/stats/ServerPlayerStats.java | 30 +++++++----------- .../mixin/common}/EntitiesMixin.java | 2 +- .../mixin/common}/PlayerEntityMixin.java | 7 ++--- .../mixin/common}/PlayerManagerMixin.java | 2 +- .../server_stats/stats/ServerPlayerStats.java | 31 ++++++++----------- .../main/resources/server_stats.mixins.json | 9 +++--- .../mixin/common/PlayerEntityMixin.java | 5 ++- .../server_stats/stats/ServerPlayerStats.java | 30 +++++++----------- .../mixin/common}/EntitiesMixin.java | 2 +- .../{server => common}/PlayerEntityMixin.java | 7 ++--- .../mixin/common}/PlayerManagerMixin.java | 2 +- .../server_stats/stats/ServerPlayerStats.java | 30 +++++++----------- .../main/resources/server_stats.mixins.json | 9 +++--- .../mixin/common/PlayerEntityMixin.java | 5 ++- .../server_stats/stats/ServerPlayerStats.java | 28 +++++++---------- .../mixin/common/PlayerEntityMixin.java | 5 ++- .../server_stats/stats/ServerPlayerStats.java | 30 +++++++----------- .../mixin/common/PlayerEntityMixin.java | 5 ++- .../server_stats/stats/ServerPlayerStats.java | 30 +++++++----------- .../mixin/common/PlayerEntityMixin.java | 5 ++- .../server_stats/stats/ServerPlayerStats.java | 30 +++++++----------- 29 files changed, 164 insertions(+), 230 deletions(-) rename versions/1.0.1-server/src/main/java/net/lostluma/server_stats/mixin/{server => common}/EntitiesMixin.java (92%) rename versions/{1.1.0-server/src/main/java/net/lostluma/server_stats/mixin/server => 1.0.1-server/src/main/java/net/lostluma/server_stats/mixin/common}/PlayerEntityMixin.java (89%) rename versions/1.0.1-server/src/main/java/net/lostluma/server_stats/mixin/{server => common}/PlayerManagerMixin.java (96%) rename versions/{1.2.5-server/src/main/java/net/lostluma/server_stats/mixin/server => 1.1.0-server/src/main/java/net/lostluma/server_stats/mixin/common}/EntitiesMixin.java (92%) rename versions/{1.0.1-server/src/main/java/net/lostluma/server_stats/mixin/server => 1.1.0-server/src/main/java/net/lostluma/server_stats/mixin/common}/PlayerEntityMixin.java (89%) rename versions/{1.2.5-server/src/main/java/net/lostluma/server_stats/mixin/server => 1.1.0-server/src/main/java/net/lostluma/server_stats/mixin/common}/PlayerManagerMixin.java (97%) rename versions/{1.1.0-server/src/main/java/net/lostluma/server_stats/mixin/server => 1.2.5-server/src/main/java/net/lostluma/server_stats/mixin/common}/EntitiesMixin.java (92%) rename versions/1.2.5-server/src/main/java/net/lostluma/server_stats/mixin/{server => common}/PlayerEntityMixin.java (89%) rename versions/{1.1.0-server/src/main/java/net/lostluma/server_stats/mixin/server => 1.2.5-server/src/main/java/net/lostluma/server_stats/mixin/common}/PlayerManagerMixin.java (97%) diff --git a/versions/1.0.0-client/src/main/java/net/lostluma/server_stats/mixin/common/PlayerEntityMixin.java b/versions/1.0.0-client/src/main/java/net/lostluma/server_stats/mixin/common/PlayerEntityMixin.java index 386add1..e96b17b 100644 --- a/versions/1.0.0-client/src/main/java/net/lostluma/server_stats/mixin/common/PlayerEntityMixin.java +++ b/versions/1.0.0-client/src/main/java/net/lostluma/server_stats/mixin/common/PlayerEntityMixin.java @@ -28,10 +28,9 @@ private PlayerEntity getPlayer() { @Override public void server_stats$incrementStat(Stat stat, int amount) { var stats = this.server_stats$getStats(); - var player = (PlayerEntity)(Object)this; if (stats != null) { - stats.increment(player, stat, amount); + stats.increment(this.getPlayer(), stat, amount); } } @@ -46,7 +45,7 @@ private PlayerEntity getPlayer() { @Override public @Nullable ServerPlayerStats server_stats$getStats() { - var player = (PlayerEntity)(Object)this; + var player = this.getPlayer(); // Unmapped method returns true when the server is multiplayer if (Minecraft.INSTANCE.m_2812472()) { diff --git a/versions/1.0.0-client/src/main/java/net/lostluma/server_stats/stats/ServerPlayerStats.java b/versions/1.0.0-client/src/main/java/net/lostluma/server_stats/stats/ServerPlayerStats.java index 9f92288..d426b8b 100644 --- a/versions/1.0.0-client/src/main/java/net/lostluma/server_stats/stats/ServerPlayerStats.java +++ b/versions/1.0.0-client/src/main/java/net/lostluma/server_stats/stats/ServerPlayerStats.java @@ -60,11 +60,7 @@ public int get(Stat stat) { } public void load() { - if (Objects.isNull(STATS)) { - throw new RuntimeException("Stats directory unset."); - } - - Path path = STATS.resolve(this.name + ".json"); + Path path = getStatsDirectory().resolve(this.name + ".json"); if (Files.exists(path)) { try { @@ -78,16 +74,13 @@ public void load() { } public void save() { - if (Objects.isNull(STATS)) { - throw new RuntimeException("Stats directory unset."); - } - - Path path = STATS.resolve(this.name + ".json"); + Path base = getStatsDirectory(); + Path path = base.resolve(this.name + ".json"); try { - Files.createDirectories(STATS); + Files.createDirectories(base); - Path temp = Files.createTempFile(STATS, this.name, ".json", DEFAULT_ATTRIBUTES); + Path temp = Files.createTempFile(base, this.name, ".json", DEFAULT_ATTRIBUTES); Files.writeString(temp, this.serialize(), StandardCharsets.UTF_8); Files.move(temp, path, StandardCopyOption.ATOMIC_MOVE); // Prevent issues on server crash @@ -97,11 +90,7 @@ public void save() { } public void deserialize() throws IOException { - if (Objects.isNull(STATS)) { - throw new RuntimeException("Stats directory unset."); - } - - Path path = STATS.resolve(this.name + ".json"); + Path path = getStatsDirectory().resolve(this.name + ".json"); JsonElement root = JsonParser.parseString(Files.readString(path, StandardCharsets.UTF_8)); if (!root.isJsonObject()) { @@ -118,7 +107,7 @@ public void deserialize() throws IOException { this.counters.put(stat, value.getAsInt()); } else { LOGGER.warning(String.format("Failed to read stat %s in %s! It's either unknown or has invalid data.", - entry.getKey(), path)); + entry.getKey(), path)); } } } @@ -133,6 +122,11 @@ public String serialize() { return result.toString(); } + private static Path getStatsDirectory() { + Objects.requireNonNull(STATS, "Stats directory unset."); + return STATS; + } + private static FileAttribute[] getDefaultFileAttributes() { if (!System.getProperty("os.name").toLowerCase(Locale.ROOT).contains("linux")) { return new FileAttribute[0]; diff --git a/versions/1.0.1-server/src/main/java/net/lostluma/server_stats/mixin/server/EntitiesMixin.java b/versions/1.0.1-server/src/main/java/net/lostluma/server_stats/mixin/common/EntitiesMixin.java similarity index 92% rename from versions/1.0.1-server/src/main/java/net/lostluma/server_stats/mixin/server/EntitiesMixin.java rename to versions/1.0.1-server/src/main/java/net/lostluma/server_stats/mixin/common/EntitiesMixin.java index 45d8cbd..49cff01 100644 --- a/versions/1.0.1-server/src/main/java/net/lostluma/server_stats/mixin/server/EntitiesMixin.java +++ b/versions/1.0.1-server/src/main/java/net/lostluma/server_stats/mixin/common/EntitiesMixin.java @@ -1,4 +1,4 @@ -package net.lostluma.server_stats.mixin.server; +package net.lostluma.server_stats.mixin.common; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; diff --git a/versions/1.1.0-server/src/main/java/net/lostluma/server_stats/mixin/server/PlayerEntityMixin.java b/versions/1.0.1-server/src/main/java/net/lostluma/server_stats/mixin/common/PlayerEntityMixin.java similarity index 89% rename from versions/1.1.0-server/src/main/java/net/lostluma/server_stats/mixin/server/PlayerEntityMixin.java rename to versions/1.0.1-server/src/main/java/net/lostluma/server_stats/mixin/common/PlayerEntityMixin.java index 5da4c5b..f494131 100644 --- a/versions/1.1.0-server/src/main/java/net/lostluma/server_stats/mixin/server/PlayerEntityMixin.java +++ b/versions/1.0.1-server/src/main/java/net/lostluma/server_stats/mixin/common/PlayerEntityMixin.java @@ -1,4 +1,4 @@ -package net.lostluma.server_stats.mixin.server; +package net.lostluma.server_stats.mixin.common; import net.lostluma.server_stats.stats.Stats; import net.lostluma.server_stats.types.StatsPlayer; @@ -26,10 +26,9 @@ private PlayerEntity getPlayer() { @Override public void server_stats$incrementStat(Stat stat, int amount) { var stats = this.server_stats$getStats(); - var player = (PlayerEntity)(Object)this; if (stats != null) { - stats.increment(player, stat, amount); + stats.increment(this.getPlayer(), stat, amount); } } @@ -44,7 +43,7 @@ private PlayerEntity getPlayer() { @Override public @Nullable ServerPlayerStats server_stats$getStats() { - var player = (PlayerEntity)(Object)this; + var player = this.getPlayer(); if (this.server_stats$serverPlayerStats == null) { this.server_stats$serverPlayerStats = new ServerPlayerStats(player); diff --git a/versions/1.0.1-server/src/main/java/net/lostluma/server_stats/mixin/server/PlayerManagerMixin.java b/versions/1.0.1-server/src/main/java/net/lostluma/server_stats/mixin/common/PlayerManagerMixin.java similarity index 96% rename from versions/1.0.1-server/src/main/java/net/lostluma/server_stats/mixin/server/PlayerManagerMixin.java rename to versions/1.0.1-server/src/main/java/net/lostluma/server_stats/mixin/common/PlayerManagerMixin.java index 7f56e00..6be65a9 100644 --- a/versions/1.0.1-server/src/main/java/net/lostluma/server_stats/mixin/server/PlayerManagerMixin.java +++ b/versions/1.0.1-server/src/main/java/net/lostluma/server_stats/mixin/common/PlayerManagerMixin.java @@ -1,4 +1,4 @@ -package net.lostluma.server_stats.mixin.server; +package net.lostluma.server_stats.mixin.common; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; diff --git a/versions/1.0.1-server/src/main/java/net/lostluma/server_stats/stats/ServerPlayerStats.java b/versions/1.0.1-server/src/main/java/net/lostluma/server_stats/stats/ServerPlayerStats.java index 9f92288..d426b8b 100644 --- a/versions/1.0.1-server/src/main/java/net/lostluma/server_stats/stats/ServerPlayerStats.java +++ b/versions/1.0.1-server/src/main/java/net/lostluma/server_stats/stats/ServerPlayerStats.java @@ -60,11 +60,7 @@ public int get(Stat stat) { } public void load() { - if (Objects.isNull(STATS)) { - throw new RuntimeException("Stats directory unset."); - } - - Path path = STATS.resolve(this.name + ".json"); + Path path = getStatsDirectory().resolve(this.name + ".json"); if (Files.exists(path)) { try { @@ -78,16 +74,13 @@ public void load() { } public void save() { - if (Objects.isNull(STATS)) { - throw new RuntimeException("Stats directory unset."); - } - - Path path = STATS.resolve(this.name + ".json"); + Path base = getStatsDirectory(); + Path path = base.resolve(this.name + ".json"); try { - Files.createDirectories(STATS); + Files.createDirectories(base); - Path temp = Files.createTempFile(STATS, this.name, ".json", DEFAULT_ATTRIBUTES); + Path temp = Files.createTempFile(base, this.name, ".json", DEFAULT_ATTRIBUTES); Files.writeString(temp, this.serialize(), StandardCharsets.UTF_8); Files.move(temp, path, StandardCopyOption.ATOMIC_MOVE); // Prevent issues on server crash @@ -97,11 +90,7 @@ public void save() { } public void deserialize() throws IOException { - if (Objects.isNull(STATS)) { - throw new RuntimeException("Stats directory unset."); - } - - Path path = STATS.resolve(this.name + ".json"); + Path path = getStatsDirectory().resolve(this.name + ".json"); JsonElement root = JsonParser.parseString(Files.readString(path, StandardCharsets.UTF_8)); if (!root.isJsonObject()) { @@ -118,7 +107,7 @@ public void deserialize() throws IOException { this.counters.put(stat, value.getAsInt()); } else { LOGGER.warning(String.format("Failed to read stat %s in %s! It's either unknown or has invalid data.", - entry.getKey(), path)); + entry.getKey(), path)); } } } @@ -133,6 +122,11 @@ public String serialize() { return result.toString(); } + private static Path getStatsDirectory() { + Objects.requireNonNull(STATS, "Stats directory unset."); + return STATS; + } + private static FileAttribute[] getDefaultFileAttributes() { if (!System.getProperty("os.name").toLowerCase(Locale.ROOT).contains("linux")) { return new FileAttribute[0]; diff --git a/versions/1.0.1-server/src/main/resources/server_stats.mixins.json b/versions/1.0.1-server/src/main/resources/server_stats.mixins.json index bdadd17..fedf6f9 100644 --- a/versions/1.0.1-server/src/main/resources/server_stats.mixins.json +++ b/versions/1.0.1-server/src/main/resources/server_stats.mixins.json @@ -3,13 +3,14 @@ "minVersion": "0.8", "package": "net.lostluma.server_stats.mixin", "compatibilityLevel": "JAVA_17", - "mixins": [], + "mixins": [ + "server.EntitiesMixin", + "server.PlayerEntityMixin", + "server.PlayerManagerMixin" + ], "client": [], "server": [ - "server.EntitiesMixin", "server.MinecraftServerMixin", - "server.PlayerEntityMixin", - "server.PlayerManagerMixin", "server.ServerPlayerEntityMixin" ], "injectors": { diff --git a/versions/1.1.0-client/src/main/java/net/lostluma/server_stats/mixin/common/PlayerEntityMixin.java b/versions/1.1.0-client/src/main/java/net/lostluma/server_stats/mixin/common/PlayerEntityMixin.java index 5181565..bb082f6 100644 --- a/versions/1.1.0-client/src/main/java/net/lostluma/server_stats/mixin/common/PlayerEntityMixin.java +++ b/versions/1.1.0-client/src/main/java/net/lostluma/server_stats/mixin/common/PlayerEntityMixin.java @@ -28,10 +28,9 @@ private PlayerEntity getPlayer() { @Override public void server_stats$incrementStat(Stat stat, int amount) { var stats = this.server_stats$getStats(); - var player = (PlayerEntity)(Object)this; if (stats != null) { - stats.increment(player, stat, amount); + stats.increment(this.getPlayer(), stat, amount); } } @@ -46,7 +45,7 @@ private PlayerEntity getPlayer() { @Override public @Nullable ServerPlayerStats server_stats$getStats() { - var player = (PlayerEntity)(Object)this; + var player = this.getPlayer(); // Unmapped method returns true when the server is multiplayer if (Minecraft.INSTANCE.m_2812472()) { diff --git a/versions/1.1.0-client/src/main/java/net/lostluma/server_stats/stats/ServerPlayerStats.java b/versions/1.1.0-client/src/main/java/net/lostluma/server_stats/stats/ServerPlayerStats.java index 9f92288..d426b8b 100644 --- a/versions/1.1.0-client/src/main/java/net/lostluma/server_stats/stats/ServerPlayerStats.java +++ b/versions/1.1.0-client/src/main/java/net/lostluma/server_stats/stats/ServerPlayerStats.java @@ -60,11 +60,7 @@ public int get(Stat stat) { } public void load() { - if (Objects.isNull(STATS)) { - throw new RuntimeException("Stats directory unset."); - } - - Path path = STATS.resolve(this.name + ".json"); + Path path = getStatsDirectory().resolve(this.name + ".json"); if (Files.exists(path)) { try { @@ -78,16 +74,13 @@ public void load() { } public void save() { - if (Objects.isNull(STATS)) { - throw new RuntimeException("Stats directory unset."); - } - - Path path = STATS.resolve(this.name + ".json"); + Path base = getStatsDirectory(); + Path path = base.resolve(this.name + ".json"); try { - Files.createDirectories(STATS); + Files.createDirectories(base); - Path temp = Files.createTempFile(STATS, this.name, ".json", DEFAULT_ATTRIBUTES); + Path temp = Files.createTempFile(base, this.name, ".json", DEFAULT_ATTRIBUTES); Files.writeString(temp, this.serialize(), StandardCharsets.UTF_8); Files.move(temp, path, StandardCopyOption.ATOMIC_MOVE); // Prevent issues on server crash @@ -97,11 +90,7 @@ public void save() { } public void deserialize() throws IOException { - if (Objects.isNull(STATS)) { - throw new RuntimeException("Stats directory unset."); - } - - Path path = STATS.resolve(this.name + ".json"); + Path path = getStatsDirectory().resolve(this.name + ".json"); JsonElement root = JsonParser.parseString(Files.readString(path, StandardCharsets.UTF_8)); if (!root.isJsonObject()) { @@ -118,7 +107,7 @@ public void deserialize() throws IOException { this.counters.put(stat, value.getAsInt()); } else { LOGGER.warning(String.format("Failed to read stat %s in %s! It's either unknown or has invalid data.", - entry.getKey(), path)); + entry.getKey(), path)); } } } @@ -133,6 +122,11 @@ public String serialize() { return result.toString(); } + private static Path getStatsDirectory() { + Objects.requireNonNull(STATS, "Stats directory unset."); + return STATS; + } + private static FileAttribute[] getDefaultFileAttributes() { if (!System.getProperty("os.name").toLowerCase(Locale.ROOT).contains("linux")) { return new FileAttribute[0]; diff --git a/versions/1.2.5-server/src/main/java/net/lostluma/server_stats/mixin/server/EntitiesMixin.java b/versions/1.1.0-server/src/main/java/net/lostluma/server_stats/mixin/common/EntitiesMixin.java similarity index 92% rename from versions/1.2.5-server/src/main/java/net/lostluma/server_stats/mixin/server/EntitiesMixin.java rename to versions/1.1.0-server/src/main/java/net/lostluma/server_stats/mixin/common/EntitiesMixin.java index 45d8cbd..49cff01 100644 --- a/versions/1.2.5-server/src/main/java/net/lostluma/server_stats/mixin/server/EntitiesMixin.java +++ b/versions/1.1.0-server/src/main/java/net/lostluma/server_stats/mixin/common/EntitiesMixin.java @@ -1,4 +1,4 @@ -package net.lostluma.server_stats.mixin.server; +package net.lostluma.server_stats.mixin.common; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; diff --git a/versions/1.0.1-server/src/main/java/net/lostluma/server_stats/mixin/server/PlayerEntityMixin.java b/versions/1.1.0-server/src/main/java/net/lostluma/server_stats/mixin/common/PlayerEntityMixin.java similarity index 89% rename from versions/1.0.1-server/src/main/java/net/lostluma/server_stats/mixin/server/PlayerEntityMixin.java rename to versions/1.1.0-server/src/main/java/net/lostluma/server_stats/mixin/common/PlayerEntityMixin.java index 5da4c5b..f494131 100644 --- a/versions/1.0.1-server/src/main/java/net/lostluma/server_stats/mixin/server/PlayerEntityMixin.java +++ b/versions/1.1.0-server/src/main/java/net/lostluma/server_stats/mixin/common/PlayerEntityMixin.java @@ -1,4 +1,4 @@ -package net.lostluma.server_stats.mixin.server; +package net.lostluma.server_stats.mixin.common; import net.lostluma.server_stats.stats.Stats; import net.lostluma.server_stats.types.StatsPlayer; @@ -26,10 +26,9 @@ private PlayerEntity getPlayer() { @Override public void server_stats$incrementStat(Stat stat, int amount) { var stats = this.server_stats$getStats(); - var player = (PlayerEntity)(Object)this; if (stats != null) { - stats.increment(player, stat, amount); + stats.increment(this.getPlayer(), stat, amount); } } @@ -44,7 +43,7 @@ private PlayerEntity getPlayer() { @Override public @Nullable ServerPlayerStats server_stats$getStats() { - var player = (PlayerEntity)(Object)this; + var player = this.getPlayer(); if (this.server_stats$serverPlayerStats == null) { this.server_stats$serverPlayerStats = new ServerPlayerStats(player); diff --git a/versions/1.2.5-server/src/main/java/net/lostluma/server_stats/mixin/server/PlayerManagerMixin.java b/versions/1.1.0-server/src/main/java/net/lostluma/server_stats/mixin/common/PlayerManagerMixin.java similarity index 97% rename from versions/1.2.5-server/src/main/java/net/lostluma/server_stats/mixin/server/PlayerManagerMixin.java rename to versions/1.1.0-server/src/main/java/net/lostluma/server_stats/mixin/common/PlayerManagerMixin.java index 3ca37ab..588a702 100644 --- a/versions/1.2.5-server/src/main/java/net/lostluma/server_stats/mixin/server/PlayerManagerMixin.java +++ b/versions/1.1.0-server/src/main/java/net/lostluma/server_stats/mixin/common/PlayerManagerMixin.java @@ -1,4 +1,4 @@ -package net.lostluma.server_stats.mixin.server; +package net.lostluma.server_stats.mixin.common; import net.lostluma.server_stats.Constants; import net.minecraft.network.Connection; diff --git a/versions/1.1.0-server/src/main/java/net/lostluma/server_stats/stats/ServerPlayerStats.java b/versions/1.1.0-server/src/main/java/net/lostluma/server_stats/stats/ServerPlayerStats.java index c760bde..d426b8b 100644 --- a/versions/1.1.0-server/src/main/java/net/lostluma/server_stats/stats/ServerPlayerStats.java +++ b/versions/1.1.0-server/src/main/java/net/lostluma/server_stats/stats/ServerPlayerStats.java @@ -31,6 +31,7 @@ public class ServerPlayerStats { // Use Minecraft logger as it is already properly set up private static final Logger LOGGER = Logger.getLogger("Minecraft"); + private static final FileAttribute[] DEFAULT_ATTRIBUTES = getDefaultFileAttributes(); public ServerPlayerStats(PlayerEntity player) { @@ -59,11 +60,7 @@ public int get(Stat stat) { } public void load() { - if (Objects.isNull(STATS)) { - throw new RuntimeException("Stats directory unset."); - } - - Path path = STATS.resolve(this.name + ".json"); + Path path = getStatsDirectory().resolve(this.name + ".json"); if (Files.exists(path)) { try { @@ -77,16 +74,13 @@ public void load() { } public void save() { - if (Objects.isNull(STATS)) { - throw new RuntimeException("Stats directory unset."); - } - - Path path = STATS.resolve(this.name + ".json"); + Path base = getStatsDirectory(); + Path path = base.resolve(this.name + ".json"); try { - Files.createDirectories(STATS); + Files.createDirectories(base); - Path temp = Files.createTempFile(STATS, this.name, ".json", DEFAULT_ATTRIBUTES); + Path temp = Files.createTempFile(base, this.name, ".json", DEFAULT_ATTRIBUTES); Files.writeString(temp, this.serialize(), StandardCharsets.UTF_8); Files.move(temp, path, StandardCopyOption.ATOMIC_MOVE); // Prevent issues on server crash @@ -96,11 +90,7 @@ public void save() { } public void deserialize() throws IOException { - if (Objects.isNull(STATS)) { - throw new RuntimeException("Stats directory unset."); - } - - Path path = STATS.resolve(this.name + ".json"); + Path path = getStatsDirectory().resolve(this.name + ".json"); JsonElement root = JsonParser.parseString(Files.readString(path, StandardCharsets.UTF_8)); if (!root.isJsonObject()) { @@ -117,7 +107,7 @@ public void deserialize() throws IOException { this.counters.put(stat, value.getAsInt()); } else { LOGGER.warning(String.format("Failed to read stat %s in %s! It's either unknown or has invalid data.", - entry.getKey(), path)); + entry.getKey(), path)); } } } @@ -132,6 +122,11 @@ public String serialize() { return result.toString(); } + private static Path getStatsDirectory() { + Objects.requireNonNull(STATS, "Stats directory unset."); + return STATS; + } + private static FileAttribute[] getDefaultFileAttributes() { if (!System.getProperty("os.name").toLowerCase(Locale.ROOT).contains("linux")) { return new FileAttribute[0]; diff --git a/versions/1.1.0-server/src/main/resources/server_stats.mixins.json b/versions/1.1.0-server/src/main/resources/server_stats.mixins.json index bdadd17..fedf6f9 100644 --- a/versions/1.1.0-server/src/main/resources/server_stats.mixins.json +++ b/versions/1.1.0-server/src/main/resources/server_stats.mixins.json @@ -3,13 +3,14 @@ "minVersion": "0.8", "package": "net.lostluma.server_stats.mixin", "compatibilityLevel": "JAVA_17", - "mixins": [], + "mixins": [ + "server.EntitiesMixin", + "server.PlayerEntityMixin", + "server.PlayerManagerMixin" + ], "client": [], "server": [ - "server.EntitiesMixin", "server.MinecraftServerMixin", - "server.PlayerEntityMixin", - "server.PlayerManagerMixin", "server.ServerPlayerEntityMixin" ], "injectors": { diff --git a/versions/1.2.5-client/src/main/java/net/lostluma/server_stats/mixin/common/PlayerEntityMixin.java b/versions/1.2.5-client/src/main/java/net/lostluma/server_stats/mixin/common/PlayerEntityMixin.java index 5181565..bb082f6 100644 --- a/versions/1.2.5-client/src/main/java/net/lostluma/server_stats/mixin/common/PlayerEntityMixin.java +++ b/versions/1.2.5-client/src/main/java/net/lostluma/server_stats/mixin/common/PlayerEntityMixin.java @@ -28,10 +28,9 @@ private PlayerEntity getPlayer() { @Override public void server_stats$incrementStat(Stat stat, int amount) { var stats = this.server_stats$getStats(); - var player = (PlayerEntity)(Object)this; if (stats != null) { - stats.increment(player, stat, amount); + stats.increment(this.getPlayer(), stat, amount); } } @@ -46,7 +45,7 @@ private PlayerEntity getPlayer() { @Override public @Nullable ServerPlayerStats server_stats$getStats() { - var player = (PlayerEntity)(Object)this; + var player = this.getPlayer(); // Unmapped method returns true when the server is multiplayer if (Minecraft.INSTANCE.m_2812472()) { diff --git a/versions/1.2.5-client/src/main/java/net/lostluma/server_stats/stats/ServerPlayerStats.java b/versions/1.2.5-client/src/main/java/net/lostluma/server_stats/stats/ServerPlayerStats.java index 9f92288..d426b8b 100644 --- a/versions/1.2.5-client/src/main/java/net/lostluma/server_stats/stats/ServerPlayerStats.java +++ b/versions/1.2.5-client/src/main/java/net/lostluma/server_stats/stats/ServerPlayerStats.java @@ -60,11 +60,7 @@ public int get(Stat stat) { } public void load() { - if (Objects.isNull(STATS)) { - throw new RuntimeException("Stats directory unset."); - } - - Path path = STATS.resolve(this.name + ".json"); + Path path = getStatsDirectory().resolve(this.name + ".json"); if (Files.exists(path)) { try { @@ -78,16 +74,13 @@ public void load() { } public void save() { - if (Objects.isNull(STATS)) { - throw new RuntimeException("Stats directory unset."); - } - - Path path = STATS.resolve(this.name + ".json"); + Path base = getStatsDirectory(); + Path path = base.resolve(this.name + ".json"); try { - Files.createDirectories(STATS); + Files.createDirectories(base); - Path temp = Files.createTempFile(STATS, this.name, ".json", DEFAULT_ATTRIBUTES); + Path temp = Files.createTempFile(base, this.name, ".json", DEFAULT_ATTRIBUTES); Files.writeString(temp, this.serialize(), StandardCharsets.UTF_8); Files.move(temp, path, StandardCopyOption.ATOMIC_MOVE); // Prevent issues on server crash @@ -97,11 +90,7 @@ public void save() { } public void deserialize() throws IOException { - if (Objects.isNull(STATS)) { - throw new RuntimeException("Stats directory unset."); - } - - Path path = STATS.resolve(this.name + ".json"); + Path path = getStatsDirectory().resolve(this.name + ".json"); JsonElement root = JsonParser.parseString(Files.readString(path, StandardCharsets.UTF_8)); if (!root.isJsonObject()) { @@ -118,7 +107,7 @@ public void deserialize() throws IOException { this.counters.put(stat, value.getAsInt()); } else { LOGGER.warning(String.format("Failed to read stat %s in %s! It's either unknown or has invalid data.", - entry.getKey(), path)); + entry.getKey(), path)); } } } @@ -133,6 +122,11 @@ public String serialize() { return result.toString(); } + private static Path getStatsDirectory() { + Objects.requireNonNull(STATS, "Stats directory unset."); + return STATS; + } + private static FileAttribute[] getDefaultFileAttributes() { if (!System.getProperty("os.name").toLowerCase(Locale.ROOT).contains("linux")) { return new FileAttribute[0]; diff --git a/versions/1.1.0-server/src/main/java/net/lostluma/server_stats/mixin/server/EntitiesMixin.java b/versions/1.2.5-server/src/main/java/net/lostluma/server_stats/mixin/common/EntitiesMixin.java similarity index 92% rename from versions/1.1.0-server/src/main/java/net/lostluma/server_stats/mixin/server/EntitiesMixin.java rename to versions/1.2.5-server/src/main/java/net/lostluma/server_stats/mixin/common/EntitiesMixin.java index 45d8cbd..49cff01 100644 --- a/versions/1.1.0-server/src/main/java/net/lostluma/server_stats/mixin/server/EntitiesMixin.java +++ b/versions/1.2.5-server/src/main/java/net/lostluma/server_stats/mixin/common/EntitiesMixin.java @@ -1,4 +1,4 @@ -package net.lostluma.server_stats.mixin.server; +package net.lostluma.server_stats.mixin.common; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; diff --git a/versions/1.2.5-server/src/main/java/net/lostluma/server_stats/mixin/server/PlayerEntityMixin.java b/versions/1.2.5-server/src/main/java/net/lostluma/server_stats/mixin/common/PlayerEntityMixin.java similarity index 89% rename from versions/1.2.5-server/src/main/java/net/lostluma/server_stats/mixin/server/PlayerEntityMixin.java rename to versions/1.2.5-server/src/main/java/net/lostluma/server_stats/mixin/common/PlayerEntityMixin.java index 5da4c5b..f494131 100644 --- a/versions/1.2.5-server/src/main/java/net/lostluma/server_stats/mixin/server/PlayerEntityMixin.java +++ b/versions/1.2.5-server/src/main/java/net/lostluma/server_stats/mixin/common/PlayerEntityMixin.java @@ -1,4 +1,4 @@ -package net.lostluma.server_stats.mixin.server; +package net.lostluma.server_stats.mixin.common; import net.lostluma.server_stats.stats.Stats; import net.lostluma.server_stats.types.StatsPlayer; @@ -26,10 +26,9 @@ private PlayerEntity getPlayer() { @Override public void server_stats$incrementStat(Stat stat, int amount) { var stats = this.server_stats$getStats(); - var player = (PlayerEntity)(Object)this; if (stats != null) { - stats.increment(player, stat, amount); + stats.increment(this.getPlayer(), stat, amount); } } @@ -44,7 +43,7 @@ private PlayerEntity getPlayer() { @Override public @Nullable ServerPlayerStats server_stats$getStats() { - var player = (PlayerEntity)(Object)this; + var player = this.getPlayer(); if (this.server_stats$serverPlayerStats == null) { this.server_stats$serverPlayerStats = new ServerPlayerStats(player); diff --git a/versions/1.1.0-server/src/main/java/net/lostluma/server_stats/mixin/server/PlayerManagerMixin.java b/versions/1.2.5-server/src/main/java/net/lostluma/server_stats/mixin/common/PlayerManagerMixin.java similarity index 97% rename from versions/1.1.0-server/src/main/java/net/lostluma/server_stats/mixin/server/PlayerManagerMixin.java rename to versions/1.2.5-server/src/main/java/net/lostluma/server_stats/mixin/common/PlayerManagerMixin.java index 3ca37ab..588a702 100644 --- a/versions/1.1.0-server/src/main/java/net/lostluma/server_stats/mixin/server/PlayerManagerMixin.java +++ b/versions/1.2.5-server/src/main/java/net/lostluma/server_stats/mixin/common/PlayerManagerMixin.java @@ -1,4 +1,4 @@ -package net.lostluma.server_stats.mixin.server; +package net.lostluma.server_stats.mixin.common; import net.lostluma.server_stats.Constants; import net.minecraft.network.Connection; diff --git a/versions/1.2.5-server/src/main/java/net/lostluma/server_stats/stats/ServerPlayerStats.java b/versions/1.2.5-server/src/main/java/net/lostluma/server_stats/stats/ServerPlayerStats.java index 9f92288..d426b8b 100644 --- a/versions/1.2.5-server/src/main/java/net/lostluma/server_stats/stats/ServerPlayerStats.java +++ b/versions/1.2.5-server/src/main/java/net/lostluma/server_stats/stats/ServerPlayerStats.java @@ -60,11 +60,7 @@ public int get(Stat stat) { } public void load() { - if (Objects.isNull(STATS)) { - throw new RuntimeException("Stats directory unset."); - } - - Path path = STATS.resolve(this.name + ".json"); + Path path = getStatsDirectory().resolve(this.name + ".json"); if (Files.exists(path)) { try { @@ -78,16 +74,13 @@ public void load() { } public void save() { - if (Objects.isNull(STATS)) { - throw new RuntimeException("Stats directory unset."); - } - - Path path = STATS.resolve(this.name + ".json"); + Path base = getStatsDirectory(); + Path path = base.resolve(this.name + ".json"); try { - Files.createDirectories(STATS); + Files.createDirectories(base); - Path temp = Files.createTempFile(STATS, this.name, ".json", DEFAULT_ATTRIBUTES); + Path temp = Files.createTempFile(base, this.name, ".json", DEFAULT_ATTRIBUTES); Files.writeString(temp, this.serialize(), StandardCharsets.UTF_8); Files.move(temp, path, StandardCopyOption.ATOMIC_MOVE); // Prevent issues on server crash @@ -97,11 +90,7 @@ public void save() { } public void deserialize() throws IOException { - if (Objects.isNull(STATS)) { - throw new RuntimeException("Stats directory unset."); - } - - Path path = STATS.resolve(this.name + ".json"); + Path path = getStatsDirectory().resolve(this.name + ".json"); JsonElement root = JsonParser.parseString(Files.readString(path, StandardCharsets.UTF_8)); if (!root.isJsonObject()) { @@ -118,7 +107,7 @@ public void deserialize() throws IOException { this.counters.put(stat, value.getAsInt()); } else { LOGGER.warning(String.format("Failed to read stat %s in %s! It's either unknown or has invalid data.", - entry.getKey(), path)); + entry.getKey(), path)); } } } @@ -133,6 +122,11 @@ public String serialize() { return result.toString(); } + private static Path getStatsDirectory() { + Objects.requireNonNull(STATS, "Stats directory unset."); + return STATS; + } + private static FileAttribute[] getDefaultFileAttributes() { if (!System.getProperty("os.name").toLowerCase(Locale.ROOT).contains("linux")) { return new FileAttribute[0]; diff --git a/versions/1.2.5-server/src/main/resources/server_stats.mixins.json b/versions/1.2.5-server/src/main/resources/server_stats.mixins.json index bdadd17..fedf6f9 100644 --- a/versions/1.2.5-server/src/main/resources/server_stats.mixins.json +++ b/versions/1.2.5-server/src/main/resources/server_stats.mixins.json @@ -3,13 +3,14 @@ "minVersion": "0.8", "package": "net.lostluma.server_stats.mixin", "compatibilityLevel": "JAVA_17", - "mixins": [], + "mixins": [ + "server.EntitiesMixin", + "server.PlayerEntityMixin", + "server.PlayerManagerMixin" + ], "client": [], "server": [ - "server.EntitiesMixin", "server.MinecraftServerMixin", - "server.PlayerEntityMixin", - "server.PlayerManagerMixin", "server.ServerPlayerEntityMixin" ], "injectors": { diff --git a/versions/1.3.2/src/main/java/net/lostluma/server_stats/mixin/common/PlayerEntityMixin.java b/versions/1.3.2/src/main/java/net/lostluma/server_stats/mixin/common/PlayerEntityMixin.java index b36867c..4e9b953 100644 --- a/versions/1.3.2/src/main/java/net/lostluma/server_stats/mixin/common/PlayerEntityMixin.java +++ b/versions/1.3.2/src/main/java/net/lostluma/server_stats/mixin/common/PlayerEntityMixin.java @@ -26,10 +26,9 @@ private PlayerEntity getPlayer() { @Override public void server_stats$incrementStat(Stat stat, int amount) { var stats = this.server_stats$getStats(); - var player = (PlayerEntity)(Object)this; if (stats != null) { - stats.increment(player, stat, amount); + stats.increment(this.getPlayer(), stat, amount); } } @@ -44,7 +43,7 @@ private PlayerEntity getPlayer() { @Override public @Nullable ServerPlayerStats server_stats$getStats() { - var player = (PlayerEntity)(Object)this; + var player = this.getPlayer(); if (player.world.isClient) { return null; diff --git a/versions/1.3.2/src/main/java/net/lostluma/server_stats/stats/ServerPlayerStats.java b/versions/1.3.2/src/main/java/net/lostluma/server_stats/stats/ServerPlayerStats.java index 92996e1..ae10b72 100644 --- a/versions/1.3.2/src/main/java/net/lostluma/server_stats/stats/ServerPlayerStats.java +++ b/versions/1.3.2/src/main/java/net/lostluma/server_stats/stats/ServerPlayerStats.java @@ -60,11 +60,7 @@ public int get(Stat stat) { } public void load() { - if (Objects.isNull(STATS)) { - throw new RuntimeException("Stats directory unset."); - } - - Path path = STATS.resolve(this.name + ".json"); + Path path = getStatsDirectory().resolve(this.name + ".json"); if (Files.exists(path)) { try { @@ -78,16 +74,13 @@ public void load() { } public void save() { - if (Objects.isNull(STATS)) { - throw new RuntimeException("Stats directory unset."); - } - - Path path = STATS.resolve(this.name + ".json"); + Path base = getStatsDirectory(); + Path path = base.resolve(this.name + ".json"); try { - Files.createDirectories(STATS); + Files.createDirectories(base); - Path temp = Files.createTempFile(STATS, this.name, ".json", DEFAULT_ATTRIBUTES); + Path temp = Files.createTempFile(base, this.name, ".json", DEFAULT_ATTRIBUTES); Files.writeString(temp, this.serialize(), StandardCharsets.UTF_8); Files.move(temp, path, StandardCopyOption.ATOMIC_MOVE); // Prevent issues on server crash @@ -97,11 +90,7 @@ public void save() { } public void deserialize() throws IOException { - if (Objects.isNull(STATS)) { - throw new RuntimeException("Stats directory unset."); - } - - Path path = STATS.resolve(this.name + ".json"); + Path path = getStatsDirectory().resolve(this.name + ".json"); JsonElement root = JsonParser.parseString(Files.readString(path, StandardCharsets.UTF_8)); if (!root.isJsonObject()) { @@ -133,6 +122,11 @@ public String serialize() { return result.toString(); } + private static Path getStatsDirectory() { + Objects.requireNonNull(STATS, "Stats directory unset."); + return STATS; + } + private static FileAttribute[] getDefaultFileAttributes() { if (!System.getProperty("os.name").toLowerCase(Locale.ROOT).contains("linux")) { return new FileAttribute[0]; diff --git a/versions/1.4.7/src/main/java/net/lostluma/server_stats/mixin/common/PlayerEntityMixin.java b/versions/1.4.7/src/main/java/net/lostluma/server_stats/mixin/common/PlayerEntityMixin.java index b36867c..4e9b953 100644 --- a/versions/1.4.7/src/main/java/net/lostluma/server_stats/mixin/common/PlayerEntityMixin.java +++ b/versions/1.4.7/src/main/java/net/lostluma/server_stats/mixin/common/PlayerEntityMixin.java @@ -26,10 +26,9 @@ private PlayerEntity getPlayer() { @Override public void server_stats$incrementStat(Stat stat, int amount) { var stats = this.server_stats$getStats(); - var player = (PlayerEntity)(Object)this; if (stats != null) { - stats.increment(player, stat, amount); + stats.increment(this.getPlayer(), stat, amount); } } @@ -44,7 +43,7 @@ private PlayerEntity getPlayer() { @Override public @Nullable ServerPlayerStats server_stats$getStats() { - var player = (PlayerEntity)(Object)this; + var player = this.getPlayer(); if (player.world.isClient) { return null; diff --git a/versions/1.4.7/src/main/java/net/lostluma/server_stats/stats/ServerPlayerStats.java b/versions/1.4.7/src/main/java/net/lostluma/server_stats/stats/ServerPlayerStats.java index 92996e1..6d3280b 100644 --- a/versions/1.4.7/src/main/java/net/lostluma/server_stats/stats/ServerPlayerStats.java +++ b/versions/1.4.7/src/main/java/net/lostluma/server_stats/stats/ServerPlayerStats.java @@ -60,11 +60,7 @@ public int get(Stat stat) { } public void load() { - if (Objects.isNull(STATS)) { - throw new RuntimeException("Stats directory unset."); - } - - Path path = STATS.resolve(this.name + ".json"); + Path path = getStatsDirectory().resolve(this.name + ".json"); if (Files.exists(path)) { try { @@ -78,16 +74,13 @@ public void load() { } public void save() { - if (Objects.isNull(STATS)) { - throw new RuntimeException("Stats directory unset."); - } - - Path path = STATS.resolve(this.name + ".json"); + Path base = getStatsDirectory(); + Path path = base.resolve(this.name + ".json"); try { - Files.createDirectories(STATS); + Files.createDirectories(base); - Path temp = Files.createTempFile(STATS, this.name, ".json", DEFAULT_ATTRIBUTES); + Path temp = Files.createTempFile(base, this.name, ".json", DEFAULT_ATTRIBUTES); Files.writeString(temp, this.serialize(), StandardCharsets.UTF_8); Files.move(temp, path, StandardCopyOption.ATOMIC_MOVE); // Prevent issues on server crash @@ -97,11 +90,7 @@ public void save() { } public void deserialize() throws IOException { - if (Objects.isNull(STATS)) { - throw new RuntimeException("Stats directory unset."); - } - - Path path = STATS.resolve(this.name + ".json"); + Path path = getStatsDirectory().resolve(this.name + ".json"); JsonElement root = JsonParser.parseString(Files.readString(path, StandardCharsets.UTF_8)); if (!root.isJsonObject()) { @@ -118,7 +107,7 @@ public void deserialize() throws IOException { this.counters.put(stat, value.getAsInt()); } else { LOGGER.warning(String.format("Failed to read stat %s in %s! It's either unknown or has invalid data.", - entry.getKey(), path)); + entry.getKey(), path)); } } } @@ -133,6 +122,11 @@ public String serialize() { return result.toString(); } + private static Path getStatsDirectory() { + Objects.requireNonNull(STATS, "Stats directory unset."); + return STATS; + } + private static FileAttribute[] getDefaultFileAttributes() { if (!System.getProperty("os.name").toLowerCase(Locale.ROOT).contains("linux")) { return new FileAttribute[0]; diff --git a/versions/1.5.2/src/main/java/net/lostluma/server_stats/mixin/common/PlayerEntityMixin.java b/versions/1.5.2/src/main/java/net/lostluma/server_stats/mixin/common/PlayerEntityMixin.java index b36867c..4e9b953 100644 --- a/versions/1.5.2/src/main/java/net/lostluma/server_stats/mixin/common/PlayerEntityMixin.java +++ b/versions/1.5.2/src/main/java/net/lostluma/server_stats/mixin/common/PlayerEntityMixin.java @@ -26,10 +26,9 @@ private PlayerEntity getPlayer() { @Override public void server_stats$incrementStat(Stat stat, int amount) { var stats = this.server_stats$getStats(); - var player = (PlayerEntity)(Object)this; if (stats != null) { - stats.increment(player, stat, amount); + stats.increment(this.getPlayer(), stat, amount); } } @@ -44,7 +43,7 @@ private PlayerEntity getPlayer() { @Override public @Nullable ServerPlayerStats server_stats$getStats() { - var player = (PlayerEntity)(Object)this; + var player = this.getPlayer(); if (player.world.isClient) { return null; diff --git a/versions/1.5.2/src/main/java/net/lostluma/server_stats/stats/ServerPlayerStats.java b/versions/1.5.2/src/main/java/net/lostluma/server_stats/stats/ServerPlayerStats.java index 92996e1..6d3280b 100644 --- a/versions/1.5.2/src/main/java/net/lostluma/server_stats/stats/ServerPlayerStats.java +++ b/versions/1.5.2/src/main/java/net/lostluma/server_stats/stats/ServerPlayerStats.java @@ -60,11 +60,7 @@ public int get(Stat stat) { } public void load() { - if (Objects.isNull(STATS)) { - throw new RuntimeException("Stats directory unset."); - } - - Path path = STATS.resolve(this.name + ".json"); + Path path = getStatsDirectory().resolve(this.name + ".json"); if (Files.exists(path)) { try { @@ -78,16 +74,13 @@ public void load() { } public void save() { - if (Objects.isNull(STATS)) { - throw new RuntimeException("Stats directory unset."); - } - - Path path = STATS.resolve(this.name + ".json"); + Path base = getStatsDirectory(); + Path path = base.resolve(this.name + ".json"); try { - Files.createDirectories(STATS); + Files.createDirectories(base); - Path temp = Files.createTempFile(STATS, this.name, ".json", DEFAULT_ATTRIBUTES); + Path temp = Files.createTempFile(base, this.name, ".json", DEFAULT_ATTRIBUTES); Files.writeString(temp, this.serialize(), StandardCharsets.UTF_8); Files.move(temp, path, StandardCopyOption.ATOMIC_MOVE); // Prevent issues on server crash @@ -97,11 +90,7 @@ public void save() { } public void deserialize() throws IOException { - if (Objects.isNull(STATS)) { - throw new RuntimeException("Stats directory unset."); - } - - Path path = STATS.resolve(this.name + ".json"); + Path path = getStatsDirectory().resolve(this.name + ".json"); JsonElement root = JsonParser.parseString(Files.readString(path, StandardCharsets.UTF_8)); if (!root.isJsonObject()) { @@ -118,7 +107,7 @@ public void deserialize() throws IOException { this.counters.put(stat, value.getAsInt()); } else { LOGGER.warning(String.format("Failed to read stat %s in %s! It's either unknown or has invalid data.", - entry.getKey(), path)); + entry.getKey(), path)); } } } @@ -133,6 +122,11 @@ public String serialize() { return result.toString(); } + private static Path getStatsDirectory() { + Objects.requireNonNull(STATS, "Stats directory unset."); + return STATS; + } + private static FileAttribute[] getDefaultFileAttributes() { if (!System.getProperty("os.name").toLowerCase(Locale.ROOT).contains("linux")) { return new FileAttribute[0]; diff --git a/versions/1.6.4/src/main/java/net/lostluma/server_stats/mixin/common/PlayerEntityMixin.java b/versions/1.6.4/src/main/java/net/lostluma/server_stats/mixin/common/PlayerEntityMixin.java index fb46196..4119f3d 100644 --- a/versions/1.6.4/src/main/java/net/lostluma/server_stats/mixin/common/PlayerEntityMixin.java +++ b/versions/1.6.4/src/main/java/net/lostluma/server_stats/mixin/common/PlayerEntityMixin.java @@ -26,10 +26,9 @@ private PlayerEntity getPlayer() { @Override public void server_stats$incrementStat(Stat stat, int amount) { var stats = this.server_stats$getStats(); - var player = (PlayerEntity)(Object)this; if (stats != null) { - stats.increment(player, stat, amount); + stats.increment(this.getPlayer(), stat, amount); } } @@ -44,7 +43,7 @@ private PlayerEntity getPlayer() { @Override public @Nullable ServerPlayerStats server_stats$getStats() { - var player = (PlayerEntity)(Object)this; + var player = this.getPlayer(); if (player.world.isClient) { return null; diff --git a/versions/1.6.4/src/main/java/net/lostluma/server_stats/stats/ServerPlayerStats.java b/versions/1.6.4/src/main/java/net/lostluma/server_stats/stats/ServerPlayerStats.java index 92996e1..6d3280b 100644 --- a/versions/1.6.4/src/main/java/net/lostluma/server_stats/stats/ServerPlayerStats.java +++ b/versions/1.6.4/src/main/java/net/lostluma/server_stats/stats/ServerPlayerStats.java @@ -60,11 +60,7 @@ public int get(Stat stat) { } public void load() { - if (Objects.isNull(STATS)) { - throw new RuntimeException("Stats directory unset."); - } - - Path path = STATS.resolve(this.name + ".json"); + Path path = getStatsDirectory().resolve(this.name + ".json"); if (Files.exists(path)) { try { @@ -78,16 +74,13 @@ public void load() { } public void save() { - if (Objects.isNull(STATS)) { - throw new RuntimeException("Stats directory unset."); - } - - Path path = STATS.resolve(this.name + ".json"); + Path base = getStatsDirectory(); + Path path = base.resolve(this.name + ".json"); try { - Files.createDirectories(STATS); + Files.createDirectories(base); - Path temp = Files.createTempFile(STATS, this.name, ".json", DEFAULT_ATTRIBUTES); + Path temp = Files.createTempFile(base, this.name, ".json", DEFAULT_ATTRIBUTES); Files.writeString(temp, this.serialize(), StandardCharsets.UTF_8); Files.move(temp, path, StandardCopyOption.ATOMIC_MOVE); // Prevent issues on server crash @@ -97,11 +90,7 @@ public void save() { } public void deserialize() throws IOException { - if (Objects.isNull(STATS)) { - throw new RuntimeException("Stats directory unset."); - } - - Path path = STATS.resolve(this.name + ".json"); + Path path = getStatsDirectory().resolve(this.name + ".json"); JsonElement root = JsonParser.parseString(Files.readString(path, StandardCharsets.UTF_8)); if (!root.isJsonObject()) { @@ -118,7 +107,7 @@ public void deserialize() throws IOException { this.counters.put(stat, value.getAsInt()); } else { LOGGER.warning(String.format("Failed to read stat %s in %s! It's either unknown or has invalid data.", - entry.getKey(), path)); + entry.getKey(), path)); } } } @@ -133,6 +122,11 @@ public String serialize() { return result.toString(); } + private static Path getStatsDirectory() { + Objects.requireNonNull(STATS, "Stats directory unset."); + return STATS; + } + private static FileAttribute[] getDefaultFileAttributes() { if (!System.getProperty("os.name").toLowerCase(Locale.ROOT).contains("linux")) { return new FileAttribute[0];