From ddb24c30477c20139f37b3e526c837680bb4f54b Mon Sep 17 00:00:00 2001 From: Pyr Date: Thu, 20 Apr 2023 01:04:24 +0100 Subject: [PATCH] remove config deadlock --- .../configuration/Configuration.java | 79 ++++++------------- 1 file changed, 22 insertions(+), 57 deletions(-) diff --git a/src/main/java/io/github/znetworkw/znpcservers/configuration/Configuration.java b/src/main/java/io/github/znetworkw/znpcservers/configuration/Configuration.java index a57722f..3e8366a 100644 --- a/src/main/java/io/github/znetworkw/znpcservers/configuration/Configuration.java +++ b/src/main/java/io/github/znetworkw/znpcservers/configuration/Configuration.java @@ -34,76 +34,41 @@ public class Configuration { } private Configuration(String name, Path path) { - if (!path.getFileName().toString().endsWith(".json")) { - throw new IllegalStateException("invalid configuration format for: " + path.getFileName()); - } else { - this.name = name; - this.path = path; - this.configurationValues = ConfigurationValue.VALUES_BY_NAME.get(name).stream().collect(Collectors.toMap((c) -> c, ConfigurationValue::getValue)); - this.onLoad(); - } + if (!path.getFileName().toString().endsWith(".json")) throw new IllegalStateException("invalid configuration format for: " + path.getFileName()); + this.name = name; + this.path = path; + this.configurationValues = ConfigurationValue.VALUES_BY_NAME.get(name).stream().collect(Collectors.toMap((c) -> c, ConfigurationValue::getValue)); + this.onLoad(); } protected void onLoad() { - synchronized(this.path) { - try { - Reader reader = Files.newBufferedReader(this.path, CHARSET); - - try { - JsonElement data = JsonParser.parseReader(reader); - if (data != null) { - for(ConfigurationValue configValue : this.configurationValues.keySet()) { - boolean single = this.configurationValues.size() == 1; - JsonElement jsonElement = single ? data : (data.isJsonObject() ? data.getAsJsonObject().get(configValue.name()) : null); - if (jsonElement != null && !jsonElement.isJsonNull()) { - if (!single && configValue.getPrimitiveType().isEnum()) { - this.configurationValues.put(configValue, ZNPCsPlus.GSON.fromJson(jsonElement, configValue.getPrimitiveType())); - } else { - this.configurationValues.put(configValue, ZNPCsPlus.GSON.fromJson(jsonElement, $Gson$Types.newParameterizedTypeWithOwner(null, configValue.getValue().getClass(), configValue.getPrimitiveType()))); - } - } - } - - reader.close(); - - return; - } - - reader.close(); - } catch (Throwable var17) { - try { - reader.close(); - } catch (Throwable var16) { - var17.addSuppressed(var16); - } - - throw var17; - } - } catch (NoSuchFileException ignored) { - } catch (IOException var19) { - throw new IllegalStateException("Failed to read config: " + this.name); - } finally { - this.save(); + try (Reader reader = Files.newBufferedReader(this.path, CHARSET)) { + JsonElement data = JsonParser.parseReader(reader); + if (data == null) return; + for(ConfigurationValue configValue : this.configurationValues.keySet()) { + boolean single = this.configurationValues.size() == 1; + JsonElement jsonElement = single ? data : (data.isJsonObject() ? data.getAsJsonObject().get(configValue.name()) : null); + if (jsonElement == null || jsonElement.isJsonNull()) continue; + if (!single && configValue.getPrimitiveType().isEnum()) this.configurationValues.put(configValue, ZNPCsPlus.GSON.fromJson(jsonElement, configValue.getPrimitiveType())); + else this.configurationValues.put(configValue, ZNPCsPlus.GSON.fromJson(jsonElement, $Gson$Types.newParameterizedTypeWithOwner(null, configValue.getValue().getClass(), configValue.getPrimitiveType()))); } - + } catch (NoSuchFileException ignored) { + } catch (IOException ex) { + throw new IllegalStateException("Failed to read config: " + this.name); } } public void save() { - synchronized(this.path) { - try (Writer writer = Files.newBufferedWriter(this.path, CHARSET)) { - ZNPCsPlus.GSON.toJson(this.configurationValues.size() == 1 ? this.configurationValues.values().iterator().next() : this.configurationValues, writer); - } catch (IOException ex) { - throw new IllegalStateException("Failed to save config: " + this.name); - } + try (Writer writer = Files.newBufferedWriter(this.path, CHARSET)) { + ZNPCsPlus.GSON.toJson(this.configurationValues.size() == 1 ? this.configurationValues.values().iterator().next() : this.configurationValues, writer); + } catch (IOException ex) { + throw new IllegalStateException("Failed to save config: " + this.name); } } @SuppressWarnings("unchecked") public T getValue(ConfigurationValue configValue) { - synchronized(this.path) { - return (T)this.configurationValues.get(configValue); - } + return (T)this.configurationValues.get(configValue); } public void sendMessage(org.bukkit.command.CommandSender sender, ConfigurationValue configValue, Object... replaces) {