Skip to content

Commit 3e833e2

Browse files
authored
Merge pull request #45 from faststats-dev/fix/config-saving
Improve server ID handling and config save logic
2 parents 59c0585 + 5f444cb commit 3e833e2

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

core/src/main/java/dev/faststats/core/SimpleMetrics.java

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import java.nio.charset.StandardCharsets;
2121
import java.nio.file.Files;
2222
import java.nio.file.Path;
23-
import java.nio.file.StandardOpenOption;
2423
import java.time.Duration;
2524
import java.util.HashSet;
2625
import java.util.Optional;
@@ -30,6 +29,7 @@
3029
import java.util.concurrent.Executors;
3130
import java.util.concurrent.ScheduledExecutorService;
3231
import java.util.concurrent.TimeUnit;
32+
import java.util.concurrent.atomic.AtomicBoolean;
3333
import java.util.zip.GZIPOutputStream;
3434

3535
public abstract class SimpleMetrics implements Metrics {
@@ -265,13 +265,23 @@ protected static final class Config implements Metrics.Config {
265265
@Contract(mutates = "io")
266266
protected Config(Path file) throws IOException {
267267
var properties = readOrEmpty(file);
268+
this.firstRun = properties.isEmpty();
269+
var saveConfig = new AtomicBoolean(this.firstRun);
270+
271+
this.serverId = properties.map(object -> object.getProperty("serverId")).map(string -> {
272+
try {
273+
var trimmed = string.trim();
274+
return UUID.fromString(trimmed.length() > 36 ? trimmed.substring(0, 36) : trimmed);
275+
} catch (IllegalArgumentException e) {
276+
saveConfig.set(true);
277+
return UUID.randomUUID();
278+
}
279+
}).orElseGet(UUID::randomUUID);
268280

269-
this.serverId = properties.map(object -> UUID.fromString(object.getProperty("serverId"))).orElseGet(UUID::randomUUID);
270281
this.enabled = properties.map(object -> object.getProperty("enabled")).map(Boolean::parseBoolean).orElse(true);
271282
this.debug = properties.map(object -> object.getProperty("debug")).map(Boolean::parseBoolean).orElse(false);
272283

273-
this.firstRun = properties.isEmpty();
274-
if (this.firstRun) create(file, serverId);
284+
if (saveConfig.get()) save(file, serverId, enabled, debug);
275285
}
276286

277287
@VisibleForTesting
@@ -305,15 +315,15 @@ private static Optional<Properties> readOrEmpty(Path file) throws IOException {
305315
}
306316
}
307317

308-
private static void create(Path file, UUID serverId) throws IOException {
318+
private static void save(Path file, UUID serverId, boolean enabled, boolean debug) throws IOException {
309319
Files.createDirectories(file.getParent());
310-
try (var out = Files.newOutputStream(file, StandardOpenOption.CREATE_NEW);
320+
try (var out = Files.newOutputStream(file);
311321
var writer = new OutputStreamWriter(out, StandardCharsets.UTF_8)) {
312322
var properties = new Properties();
313323

314324
properties.setProperty("serverId", serverId.toString());
315-
properties.setProperty("enabled", Boolean.toString(true));
316-
properties.setProperty("debug", Boolean.toString(false));
325+
properties.setProperty("enabled", Boolean.toString(enabled));
326+
properties.setProperty("debug", Boolean.toString(debug));
317327

318328
var comment = """
319329
FastStats (https://faststats.dev) gathers basic information for plugin developers,

0 commit comments

Comments
 (0)