remove config deadlock

This commit is contained in:
Pyr 2023-04-20 01:04:24 +01:00
parent 575f435bbc
commit ddb24c3047

@ -34,77 +34,42 @@ public class Configuration {
} }
private Configuration(String name, Path path) { private Configuration(String name, Path path) {
if (!path.getFileName().toString().endsWith(".json")) { if (!path.getFileName().toString().endsWith(".json")) throw new IllegalStateException("invalid configuration format for: " + path.getFileName());
throw new IllegalStateException("invalid configuration format for: " + path.getFileName());
} else {
this.name = name; this.name = name;
this.path = path; this.path = path;
this.configurationValues = ConfigurationValue.VALUES_BY_NAME.get(name).stream().collect(Collectors.toMap((c) -> c, ConfigurationValue::getValue)); this.configurationValues = ConfigurationValue.VALUES_BY_NAME.get(name).stream().collect(Collectors.toMap((c) -> c, ConfigurationValue::getValue));
this.onLoad(); this.onLoad();
} }
}
protected void onLoad() { protected void onLoad() {
synchronized(this.path) { try (Reader reader = Files.newBufferedReader(this.path, CHARSET)) {
try {
Reader reader = Files.newBufferedReader(this.path, CHARSET);
try {
JsonElement data = JsonParser.parseReader(reader); JsonElement data = JsonParser.parseReader(reader);
if (data != null) { if (data == null) return;
for(ConfigurationValue configValue : this.configurationValues.keySet()) { for(ConfigurationValue configValue : this.configurationValues.keySet()) {
boolean single = this.configurationValues.size() == 1; boolean single = this.configurationValues.size() == 1;
JsonElement jsonElement = single ? data : (data.isJsonObject() ? data.getAsJsonObject().get(configValue.name()) : null); JsonElement jsonElement = single ? data : (data.isJsonObject() ? data.getAsJsonObject().get(configValue.name()) : null);
if (jsonElement != null && !jsonElement.isJsonNull()) { if (jsonElement == null || jsonElement.isJsonNull()) continue;
if (!single && configValue.getPrimitiveType().isEnum()) { if (!single && configValue.getPrimitiveType().isEnum()) this.configurationValues.put(configValue, ZNPCsPlus.GSON.fromJson(jsonElement, configValue.getPrimitiveType()));
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())));
} 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 (NoSuchFileException ignored) {
} catch (IOException var19) { } catch (IOException ex) {
throw new IllegalStateException("Failed to read config: " + this.name); throw new IllegalStateException("Failed to read config: " + this.name);
} finally {
this.save();
}
} }
} }
public void save() { public void save() {
synchronized(this.path) {
try (Writer writer = Files.newBufferedWriter(this.path, CHARSET)) { try (Writer writer = Files.newBufferedWriter(this.path, CHARSET)) {
ZNPCsPlus.GSON.toJson(this.configurationValues.size() == 1 ? this.configurationValues.values().iterator().next() : this.configurationValues, writer); ZNPCsPlus.GSON.toJson(this.configurationValues.size() == 1 ? this.configurationValues.values().iterator().next() : this.configurationValues, writer);
} catch (IOException ex) { } catch (IOException ex) {
throw new IllegalStateException("Failed to save config: " + this.name); throw new IllegalStateException("Failed to save config: " + this.name);
} }
} }
}
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public <T> T getValue(ConfigurationValue configValue) { public <T> 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) { public void sendMessage(org.bukkit.command.CommandSender sender, ConfigurationValue configValue, Object... replaces) {
sender.sendMessage(Utils.toColor(String.format(this.getValue(configValue), replaces))); sender.sendMessage(Utils.toColor(String.format(this.getValue(configValue), replaces)));