Skip to content

Commit

Permalink
Merge branch '1.16' into 1.17
Browse files Browse the repository at this point in the history
  • Loading branch information
deirn committed Mar 25, 2021
2 parents 11ce028 + d15ba21 commit 9b29ac3
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions common/src/main/java/mcp/mobius/waila/utils/JsonConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,12 @@ public JsonConfig(String fileName, Class<T> configClass, Supplier<T> defaultFact
this(Waila.configDir.resolve(fileName + (fileName.endsWith(".json") ? "" : ".json")).toFile(), configClass, defaultFactory);
}

public JsonConfig(File file, Class<T> configClass) {
this(file, configClass, defaultFactory(configClass));
}

public JsonConfig(String fileName, Class<T> configClass) {
this(fileName, configClass, () -> {
try {
return configClass.getConstructor().newInstance();
} catch (Exception e) {
throw new RuntimeException("Failed to create new config instance", e);
}
});
this(fileName, configClass, defaultFactory(configClass));
}

public JsonConfig<T> withGson(Gson gson) {
Expand Down Expand Up @@ -77,6 +75,16 @@ public void invalidate() {
configGetter.invalidate();
}

private static <T> Supplier<T> defaultFactory(Class<T> configClass) {
return () -> {
try {
return configClass.getConstructor().newInstance();
} catch (Exception e) {
throw new RuntimeException("Failed to create new config instance", e);
}
};
}

static class CachedSupplier<T> {

private final Supplier<T> supplier;
Expand Down

0 comments on commit 9b29ac3

Please sign in to comment.