|
20 | 20 | import java.nio.charset.StandardCharsets; |
21 | 21 | import java.nio.file.Files; |
22 | 22 | import java.nio.file.Path; |
23 | | -import java.nio.file.StandardOpenOption; |
24 | 23 | import java.time.Duration; |
25 | 24 | import java.util.HashSet; |
26 | 25 | import java.util.Optional; |
|
30 | 29 | import java.util.concurrent.Executors; |
31 | 30 | import java.util.concurrent.ScheduledExecutorService; |
32 | 31 | import java.util.concurrent.TimeUnit; |
| 32 | +import java.util.concurrent.atomic.AtomicBoolean; |
33 | 33 | import java.util.zip.GZIPOutputStream; |
34 | 34 |
|
35 | 35 | public abstract class SimpleMetrics implements Metrics { |
@@ -265,13 +265,23 @@ protected static final class Config implements Metrics.Config { |
265 | 265 | @Contract(mutates = "io") |
266 | 266 | protected Config(Path file) throws IOException { |
267 | 267 | 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); |
268 | 280 |
|
269 | | - this.serverId = properties.map(object -> UUID.fromString(object.getProperty("serverId"))).orElseGet(UUID::randomUUID); |
270 | 281 | this.enabled = properties.map(object -> object.getProperty("enabled")).map(Boolean::parseBoolean).orElse(true); |
271 | 282 | this.debug = properties.map(object -> object.getProperty("debug")).map(Boolean::parseBoolean).orElse(false); |
272 | 283 |
|
273 | | - this.firstRun = properties.isEmpty(); |
274 | | - if (this.firstRun) create(file, serverId); |
| 284 | + if (saveConfig.get()) save(file, serverId, enabled, debug); |
275 | 285 | } |
276 | 286 |
|
277 | 287 | @VisibleForTesting |
@@ -305,15 +315,15 @@ private static Optional<Properties> readOrEmpty(Path file) throws IOException { |
305 | 315 | } |
306 | 316 | } |
307 | 317 |
|
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 { |
309 | 319 | Files.createDirectories(file.getParent()); |
310 | | - try (var out = Files.newOutputStream(file, StandardOpenOption.CREATE_NEW); |
| 320 | + try (var out = Files.newOutputStream(file); |
311 | 321 | var writer = new OutputStreamWriter(out, StandardCharsets.UTF_8)) { |
312 | 322 | var properties = new Properties(); |
313 | 323 |
|
314 | 324 | 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)); |
317 | 327 |
|
318 | 328 | var comment = """ |
319 | 329 | FastStats (https://faststats.dev) gathers basic information for plugin developers, |
|
0 commit comments