@@ -60,11 +60,7 @@ public int get(Stat stat) {
60
60
}
61
61
62
62
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" );
68
64
69
65
if (Files .exists (path )) {
70
66
try {
@@ -78,16 +74,13 @@ public void load() {
78
74
}
79
75
80
76
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" );
86
79
87
80
try {
88
- Files .createDirectories (STATS );
81
+ Files .createDirectories (base );
89
82
90
- Path temp = Files .createTempFile (STATS , this .name , ".json" , DEFAULT_ATTRIBUTES );
83
+ Path temp = Files .createTempFile (base , this .name , ".json" , DEFAULT_ATTRIBUTES );
91
84
Files .writeString (temp , this .serialize (), StandardCharsets .UTF_8 );
92
85
93
86
Files .move (temp , path , StandardCopyOption .ATOMIC_MOVE ); // Prevent issues on server crash
@@ -97,11 +90,7 @@ public void save() {
97
90
}
98
91
99
92
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" );
105
94
JsonElement root = JsonParser .parseString (Files .readString (path , StandardCharsets .UTF_8 ));
106
95
107
96
if (!root .isJsonObject ()) {
@@ -118,7 +107,7 @@ public void deserialize() throws IOException {
118
107
this .counters .put (stat , value .getAsInt ());
119
108
} else {
120
109
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 ));
122
111
}
123
112
}
124
113
}
@@ -133,6 +122,11 @@ public String serialize() {
133
122
return result .toString ();
134
123
}
135
124
125
+ private static Path getStatsDirectory () {
126
+ Objects .requireNonNull (STATS , "Stats directory unset." );
127
+ return STATS ;
128
+ }
129
+
136
130
private static FileAttribute <?>[] getDefaultFileAttributes () {
137
131
if (!System .getProperty ("os.name" ).toLowerCase (Locale .ROOT ).contains ("linux" )) {
138
132
return new FileAttribute [0 ];
0 commit comments