Skip to content

Commit b3784d3

Browse files
committed
General cleanup / organization
1 parent 6eb1419 commit b3784d3

File tree

29 files changed

+164
-230
lines changed

29 files changed

+164
-230
lines changed

versions/1.0.0-client/src/main/java/net/lostluma/server_stats/mixin/common/PlayerEntityMixin.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,9 @@ private PlayerEntity getPlayer() {
2828
@Override
2929
public void server_stats$incrementStat(Stat stat, int amount) {
3030
var stats = this.server_stats$getStats();
31-
var player = (PlayerEntity)(Object)this;
3231

3332
if (stats != null) {
34-
stats.increment(player, stat, amount);
33+
stats.increment(this.getPlayer(), stat, amount);
3534
}
3635
}
3736

@@ -46,7 +45,7 @@ private PlayerEntity getPlayer() {
4645

4746
@Override
4847
public @Nullable ServerPlayerStats server_stats$getStats() {
49-
var player = (PlayerEntity)(Object)this;
48+
var player = this.getPlayer();
5049

5150
// Unmapped method returns true when the server is multiplayer
5251
if (Minecraft.INSTANCE.m_2812472()) {

versions/1.0.0-client/src/main/java/net/lostluma/server_stats/stats/ServerPlayerStats.java

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,7 @@ public int get(Stat stat) {
6060
}
6161

6262
public void load() {
63-
if (Objects.isNull(STATS)) {
64-
throw new RuntimeException("Stats directory unset.");
65-
}
66-
67-
Path path = STATS.resolve(this.name + ".json");
63+
Path path = getStatsDirectory().resolve(this.name + ".json");
6864

6965
if (Files.exists(path)) {
7066
try {
@@ -78,16 +74,13 @@ public void load() {
7874
}
7975

8076
public void save() {
81-
if (Objects.isNull(STATS)) {
82-
throw new RuntimeException("Stats directory unset.");
83-
}
84-
85-
Path path = STATS.resolve(this.name + ".json");
77+
Path base = getStatsDirectory();
78+
Path path = base.resolve(this.name + ".json");
8679

8780
try {
88-
Files.createDirectories(STATS);
81+
Files.createDirectories(base);
8982

90-
Path temp = Files.createTempFile(STATS, this.name, ".json", DEFAULT_ATTRIBUTES);
83+
Path temp = Files.createTempFile(base, this.name, ".json", DEFAULT_ATTRIBUTES);
9184
Files.writeString(temp, this.serialize(), StandardCharsets.UTF_8);
9285

9386
Files.move(temp, path, StandardCopyOption.ATOMIC_MOVE); // Prevent issues on server crash
@@ -97,11 +90,7 @@ public void save() {
9790
}
9891

9992
public void deserialize() throws IOException {
100-
if (Objects.isNull(STATS)) {
101-
throw new RuntimeException("Stats directory unset.");
102-
}
103-
104-
Path path = STATS.resolve(this.name + ".json");
93+
Path path = getStatsDirectory().resolve(this.name + ".json");
10594
JsonElement root = JsonParser.parseString(Files.readString(path, StandardCharsets.UTF_8));
10695

10796
if (!root.isJsonObject()) {
@@ -118,7 +107,7 @@ public void deserialize() throws IOException {
118107
this.counters.put(stat, value.getAsInt());
119108
} else {
120109
LOGGER.warning(String.format("Failed to read stat %s in %s! It's either unknown or has invalid data.",
121-
entry.getKey(), path));
110+
entry.getKey(), path));
122111
}
123112
}
124113
}
@@ -133,6 +122,11 @@ public String serialize() {
133122
return result.toString();
134123
}
135124

125+
private static Path getStatsDirectory() {
126+
Objects.requireNonNull(STATS, "Stats directory unset.");
127+
return STATS;
128+
}
129+
136130
private static FileAttribute<?>[] getDefaultFileAttributes() {
137131
if (!System.getProperty("os.name").toLowerCase(Locale.ROOT).contains("linux")) {
138132
return new FileAttribute[0];

versions/1.0.1-server/src/main/java/net/lostluma/server_stats/mixin/server/EntitiesMixin.java renamed to versions/1.0.1-server/src/main/java/net/lostluma/server_stats/mixin/common/EntitiesMixin.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package net.lostluma.server_stats.mixin.server;
1+
package net.lostluma.server_stats.mixin.common;
22

33
import org.spongepowered.asm.mixin.Mixin;
44
import org.spongepowered.asm.mixin.injection.At;

versions/1.1.0-server/src/main/java/net/lostluma/server_stats/mixin/server/PlayerEntityMixin.java renamed to versions/1.0.1-server/src/main/java/net/lostluma/server_stats/mixin/common/PlayerEntityMixin.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package net.lostluma.server_stats.mixin.server;
1+
package net.lostluma.server_stats.mixin.common;
22

33
import net.lostluma.server_stats.stats.Stats;
44
import net.lostluma.server_stats.types.StatsPlayer;
@@ -26,10 +26,9 @@ private PlayerEntity getPlayer() {
2626
@Override
2727
public void server_stats$incrementStat(Stat stat, int amount) {
2828
var stats = this.server_stats$getStats();
29-
var player = (PlayerEntity)(Object)this;
3029

3130
if (stats != null) {
32-
stats.increment(player, stat, amount);
31+
stats.increment(this.getPlayer(), stat, amount);
3332
}
3433
}
3534

@@ -44,7 +43,7 @@ private PlayerEntity getPlayer() {
4443

4544
@Override
4645
public @Nullable ServerPlayerStats server_stats$getStats() {
47-
var player = (PlayerEntity)(Object)this;
46+
var player = this.getPlayer();
4847

4948
if (this.server_stats$serverPlayerStats == null) {
5049
this.server_stats$serverPlayerStats = new ServerPlayerStats(player);

versions/1.0.1-server/src/main/java/net/lostluma/server_stats/mixin/server/PlayerManagerMixin.java renamed to versions/1.0.1-server/src/main/java/net/lostluma/server_stats/mixin/common/PlayerManagerMixin.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package net.lostluma.server_stats.mixin.server;
1+
package net.lostluma.server_stats.mixin.common;
22

33
import org.spongepowered.asm.mixin.Mixin;
44
import org.spongepowered.asm.mixin.Shadow;

versions/1.0.1-server/src/main/java/net/lostluma/server_stats/stats/ServerPlayerStats.java

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,7 @@ public int get(Stat stat) {
6060
}
6161

6262
public void load() {
63-
if (Objects.isNull(STATS)) {
64-
throw new RuntimeException("Stats directory unset.");
65-
}
66-
67-
Path path = STATS.resolve(this.name + ".json");
63+
Path path = getStatsDirectory().resolve(this.name + ".json");
6864

6965
if (Files.exists(path)) {
7066
try {
@@ -78,16 +74,13 @@ public void load() {
7874
}
7975

8076
public void save() {
81-
if (Objects.isNull(STATS)) {
82-
throw new RuntimeException("Stats directory unset.");
83-
}
84-
85-
Path path = STATS.resolve(this.name + ".json");
77+
Path base = getStatsDirectory();
78+
Path path = base.resolve(this.name + ".json");
8679

8780
try {
88-
Files.createDirectories(STATS);
81+
Files.createDirectories(base);
8982

90-
Path temp = Files.createTempFile(STATS, this.name, ".json", DEFAULT_ATTRIBUTES);
83+
Path temp = Files.createTempFile(base, this.name, ".json", DEFAULT_ATTRIBUTES);
9184
Files.writeString(temp, this.serialize(), StandardCharsets.UTF_8);
9285

9386
Files.move(temp, path, StandardCopyOption.ATOMIC_MOVE); // Prevent issues on server crash
@@ -97,11 +90,7 @@ public void save() {
9790
}
9891

9992
public void deserialize() throws IOException {
100-
if (Objects.isNull(STATS)) {
101-
throw new RuntimeException("Stats directory unset.");
102-
}
103-
104-
Path path = STATS.resolve(this.name + ".json");
93+
Path path = getStatsDirectory().resolve(this.name + ".json");
10594
JsonElement root = JsonParser.parseString(Files.readString(path, StandardCharsets.UTF_8));
10695

10796
if (!root.isJsonObject()) {
@@ -118,7 +107,7 @@ public void deserialize() throws IOException {
118107
this.counters.put(stat, value.getAsInt());
119108
} else {
120109
LOGGER.warning(String.format("Failed to read stat %s in %s! It's either unknown or has invalid data.",
121-
entry.getKey(), path));
110+
entry.getKey(), path));
122111
}
123112
}
124113
}
@@ -133,6 +122,11 @@ public String serialize() {
133122
return result.toString();
134123
}
135124

125+
private static Path getStatsDirectory() {
126+
Objects.requireNonNull(STATS, "Stats directory unset.");
127+
return STATS;
128+
}
129+
136130
private static FileAttribute<?>[] getDefaultFileAttributes() {
137131
if (!System.getProperty("os.name").toLowerCase(Locale.ROOT).contains("linux")) {
138132
return new FileAttribute[0];

versions/1.0.1-server/src/main/resources/server_stats.mixins.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
"minVersion": "0.8",
44
"package": "net.lostluma.server_stats.mixin",
55
"compatibilityLevel": "JAVA_17",
6-
"mixins": [],
6+
"mixins": [
7+
"server.EntitiesMixin",
8+
"server.PlayerEntityMixin",
9+
"server.PlayerManagerMixin"
10+
],
711
"client": [],
812
"server": [
9-
"server.EntitiesMixin",
1013
"server.MinecraftServerMixin",
11-
"server.PlayerEntityMixin",
12-
"server.PlayerManagerMixin",
1314
"server.ServerPlayerEntityMixin"
1415
],
1516
"injectors": {

versions/1.1.0-client/src/main/java/net/lostluma/server_stats/mixin/common/PlayerEntityMixin.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,9 @@ private PlayerEntity getPlayer() {
2828
@Override
2929
public void server_stats$incrementStat(Stat stat, int amount) {
3030
var stats = this.server_stats$getStats();
31-
var player = (PlayerEntity)(Object)this;
3231

3332
if (stats != null) {
34-
stats.increment(player, stat, amount);
33+
stats.increment(this.getPlayer(), stat, amount);
3534
}
3635
}
3736

@@ -46,7 +45,7 @@ private PlayerEntity getPlayer() {
4645

4746
@Override
4847
public @Nullable ServerPlayerStats server_stats$getStats() {
49-
var player = (PlayerEntity)(Object)this;
48+
var player = this.getPlayer();
5049

5150
// Unmapped method returns true when the server is multiplayer
5251
if (Minecraft.INSTANCE.m_2812472()) {

versions/1.1.0-client/src/main/java/net/lostluma/server_stats/stats/ServerPlayerStats.java

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,7 @@ public int get(Stat stat) {
6060
}
6161

6262
public void load() {
63-
if (Objects.isNull(STATS)) {
64-
throw new RuntimeException("Stats directory unset.");
65-
}
66-
67-
Path path = STATS.resolve(this.name + ".json");
63+
Path path = getStatsDirectory().resolve(this.name + ".json");
6864

6965
if (Files.exists(path)) {
7066
try {
@@ -78,16 +74,13 @@ public void load() {
7874
}
7975

8076
public void save() {
81-
if (Objects.isNull(STATS)) {
82-
throw new RuntimeException("Stats directory unset.");
83-
}
84-
85-
Path path = STATS.resolve(this.name + ".json");
77+
Path base = getStatsDirectory();
78+
Path path = base.resolve(this.name + ".json");
8679

8780
try {
88-
Files.createDirectories(STATS);
81+
Files.createDirectories(base);
8982

90-
Path temp = Files.createTempFile(STATS, this.name, ".json", DEFAULT_ATTRIBUTES);
83+
Path temp = Files.createTempFile(base, this.name, ".json", DEFAULT_ATTRIBUTES);
9184
Files.writeString(temp, this.serialize(), StandardCharsets.UTF_8);
9285

9386
Files.move(temp, path, StandardCopyOption.ATOMIC_MOVE); // Prevent issues on server crash
@@ -97,11 +90,7 @@ public void save() {
9790
}
9891

9992
public void deserialize() throws IOException {
100-
if (Objects.isNull(STATS)) {
101-
throw new RuntimeException("Stats directory unset.");
102-
}
103-
104-
Path path = STATS.resolve(this.name + ".json");
93+
Path path = getStatsDirectory().resolve(this.name + ".json");
10594
JsonElement root = JsonParser.parseString(Files.readString(path, StandardCharsets.UTF_8));
10695

10796
if (!root.isJsonObject()) {
@@ -118,7 +107,7 @@ public void deserialize() throws IOException {
118107
this.counters.put(stat, value.getAsInt());
119108
} else {
120109
LOGGER.warning(String.format("Failed to read stat %s in %s! It's either unknown or has invalid data.",
121-
entry.getKey(), path));
110+
entry.getKey(), path));
122111
}
123112
}
124113
}
@@ -133,6 +122,11 @@ public String serialize() {
133122
return result.toString();
134123
}
135124

125+
private static Path getStatsDirectory() {
126+
Objects.requireNonNull(STATS, "Stats directory unset.");
127+
return STATS;
128+
}
129+
136130
private static FileAttribute<?>[] getDefaultFileAttributes() {
137131
if (!System.getProperty("os.name").toLowerCase(Locale.ROOT).contains("linux")) {
138132
return new FileAttribute[0];

versions/1.2.5-server/src/main/java/net/lostluma/server_stats/mixin/server/EntitiesMixin.java renamed to versions/1.1.0-server/src/main/java/net/lostluma/server_stats/mixin/common/EntitiesMixin.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package net.lostluma.server_stats.mixin.server;
1+
package net.lostluma.server_stats.mixin.common;
22

33
import org.spongepowered.asm.mixin.Mixin;
44
import org.spongepowered.asm.mixin.injection.At;

versions/1.0.1-server/src/main/java/net/lostluma/server_stats/mixin/server/PlayerEntityMixin.java renamed to versions/1.1.0-server/src/main/java/net/lostluma/server_stats/mixin/common/PlayerEntityMixin.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package net.lostluma.server_stats.mixin.server;
1+
package net.lostluma.server_stats.mixin.common;
22

33
import net.lostluma.server_stats.stats.Stats;
44
import net.lostluma.server_stats.types.StatsPlayer;
@@ -26,10 +26,9 @@ private PlayerEntity getPlayer() {
2626
@Override
2727
public void server_stats$incrementStat(Stat stat, int amount) {
2828
var stats = this.server_stats$getStats();
29-
var player = (PlayerEntity)(Object)this;
3029

3130
if (stats != null) {
32-
stats.increment(player, stat, amount);
31+
stats.increment(this.getPlayer(), stat, amount);
3332
}
3433
}
3534

@@ -44,7 +43,7 @@ private PlayerEntity getPlayer() {
4443

4544
@Override
4645
public @Nullable ServerPlayerStats server_stats$getStats() {
47-
var player = (PlayerEntity)(Object)this;
46+
var player = this.getPlayer();
4847

4948
if (this.server_stats$serverPlayerStats == null) {
5049
this.server_stats$serverPlayerStats = new ServerPlayerStats(player);

versions/1.2.5-server/src/main/java/net/lostluma/server_stats/mixin/server/PlayerManagerMixin.java renamed to versions/1.1.0-server/src/main/java/net/lostluma/server_stats/mixin/common/PlayerManagerMixin.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package net.lostluma.server_stats.mixin.server;
1+
package net.lostluma.server_stats.mixin.common;
22

33
import net.lostluma.server_stats.Constants;
44
import net.minecraft.network.Connection;

0 commit comments

Comments
 (0)