Skip to content

Commit

Permalink
General cleanup / organization
Browse files Browse the repository at this point in the history
  • Loading branch information
LostLuma committed Nov 10, 2023
1 parent 6eb1419 commit b3784d3
Show file tree
Hide file tree
Showing 29 changed files with 164 additions and 230 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand All @@ -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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -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()) {
Expand All @@ -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));
}
}
}
Expand All @@ -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];
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
}
}

Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -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()) {
Expand All @@ -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));
}
}
}
Expand All @@ -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];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand All @@ -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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -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()) {
Expand All @@ -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));
}
}
}
Expand All @@ -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];
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
}
}

Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Loading

0 comments on commit b3784d3

Please sign in to comment.