remove config deadlock
This commit is contained in:
parent
575f435bbc
commit
ddb24c3047
1 changed files with 22 additions and 57 deletions
|
@ -34,76 +34,41 @@ 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());
|
this.name = name;
|
||||||
} else {
|
this.path = path;
|
||||||
this.name = name;
|
this.configurationValues = ConfigurationValue.VALUES_BY_NAME.get(name).stream().collect(Collectors.toMap((c) -> c, ConfigurationValue::getValue));
|
||||||
this.path = path;
|
this.onLoad();
|
||||||
this.configurationValues = ConfigurationValue.VALUES_BY_NAME.get(name).stream().collect(Collectors.toMap((c) -> c, ConfigurationValue::getValue));
|
|
||||||
this.onLoad();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void onLoad() {
|
protected void onLoad() {
|
||||||
synchronized(this.path) {
|
try (Reader reader = Files.newBufferedReader(this.path, CHARSET)) {
|
||||||
try {
|
JsonElement data = JsonParser.parseReader(reader);
|
||||||
Reader reader = Files.newBufferedReader(this.path, CHARSET);
|
if (data == null) return;
|
||||||
|
for(ConfigurationValue configValue : this.configurationValues.keySet()) {
|
||||||
try {
|
boolean single = this.configurationValues.size() == 1;
|
||||||
JsonElement data = JsonParser.parseReader(reader);
|
JsonElement jsonElement = single ? data : (data.isJsonObject() ? data.getAsJsonObject().get(configValue.name()) : null);
|
||||||
if (data != null) {
|
if (jsonElement == null || jsonElement.isJsonNull()) continue;
|
||||||
for(ConfigurationValue configValue : this.configurationValues.keySet()) {
|
if (!single && configValue.getPrimitiveType().isEnum()) this.configurationValues.put(configValue, ZNPCsPlus.GSON.fromJson(jsonElement, configValue.getPrimitiveType()));
|
||||||
boolean single = this.configurationValues.size() == 1;
|
else this.configurationValues.put(configValue, ZNPCsPlus.GSON.fromJson(jsonElement, $Gson$Types.newParameterizedTypeWithOwner(null, configValue.getValue().getClass(), configValue.getPrimitiveType())));
|
||||||
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();
|
|
||||||
}
|
}
|
||||||
|
} catch (NoSuchFileException ignored) {
|
||||||
|
} catch (IOException ex) {
|
||||||
|
throw new IllegalStateException("Failed to read config: " + this.name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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) {
|
||||||
|
|
Loading…
Reference in a new issue