Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
150 changes: 53 additions & 97 deletions common/src/main/java/net/sweenus/simplyswords/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,122 +64,78 @@ public static JsonObject getJsonObject(String json) {
}
}



// -- Safe Config Fetching --
// Allows safely fetching config values in a scenario where we do not know if they exist.
// EG. Addon mod for Simply Swords attempting to load config values before Simply Swords has initialised.

private static final HashMap<String, Boolean> BOOLEAN = new LinkedHashMap<>();
private static final HashMap<String, Float> FLOAT = new LinkedHashMap<>();
private static final HashMap<String, Double> DOUBLE = new LinkedHashMap<>();
private static final HashMap<String, Integer> INT = new LinkedHashMap<>();
private static boolean isLoaded = false;
private static final HashMap<String, JsonObject> JSONS = new HashMap<>();

public static boolean getBoolean(String key, String parent, boolean defaultValue) {
//System.out.println("Trying to fetch config value for " + key + " from " + parent);
safeValueFetch("boolean", parent);
if (!BOOLEAN.isEmpty()) {
if (BOOLEAN.containsKey(key)) {
//System.out.println("Successfully fetched value for " + key + " : " + BOOLEAN.get(key));
return BOOLEAN.get(key);
}
var e = getElement(parent, key);
if (e == null) return defaultValue;

try {
return e.getAsBoolean();
}catch (Exception ignored) {
System.out.println("Failed to fetch config value for " + key + ". Loading default value.\nIt is recommended that you restart your game.");
return defaultValue;
}
System.out.println("Failed to fetch config value for " + key + ". Loading default value.\nIt is recommended that you restart your game.");
return defaultValue;
}

public static float getFloat(String key, String parent, float defaultValue) {
//System.out.println("Trying to fetch config value for " + key + " from " + parent);
safeValueFetch("float", parent);
if (!FLOAT.isEmpty()) {
if (FLOAT.containsKey(key)) {
//System.out.println("Successfully fetched value for " + key + " : " + FLOAT.get(key));
return FLOAT.get(key);
}
var e = getElement(parent, key);
if (e == null) return defaultValue;

try {
return e.getAsFloat();
}catch (Exception ignored) {
System.out.println("Failed to fetch config value for " + key + ". Loading default value.\nIt is recommended that you restart your game.");
return defaultValue;
}
System.out.println("Failed to fetch config value for " + key + ". Loading default value.\nIt is recommended that you restart your game.");
//System.out.print(FLOAT);
return defaultValue;
}

public static double getDouble(String key, String parent, double defaultValue) {
safeValueFetch("double", parent);
if (!DOUBLE.isEmpty()) {
if (DOUBLE.containsKey(key))
return DOUBLE.get(key);
var e = getElement(parent, key);
if (e == null) return defaultValue;

try {
return e.getAsDouble();
}catch (Exception ignored) {
System.out.println("Failed to fetch config value for " + key + ". Loading default value.\nIt is recommended that you restart your game.");
return defaultValue;
}
System.out.println("Failed to fetch config value for " + key + ". Loading default value.\nIt is recommended that you restart your game.");
return defaultValue;
}

public static int getInt(String key, String parent, int defaultValue) {
safeValueFetch("int", parent);
if (!INT.isEmpty()) {
if (INT.containsKey(key))
return INT.get(key);
var e = getElement(parent, key);
if (e == null) return defaultValue;

try {
return e.getAsInt();
}catch (Exception ignored) {
System.out.println("Failed to fetch config value for " + key + ". Loading default value.\nIt is recommended that you restart your game.");
return defaultValue;
}
System.out.println("Failed to fetch config value for " + key + ". Loading default value.\nIt is recommended that you restart your game.");
return defaultValue;
}

public static void safeValueFetch(String type, String parent) {
Path path = Paths.get("config/simplyswords_main/");
JsonObject json = null;
if (Files.exists(path)) {
json = switch (parent) {
case "GemEffects" -> Config.getJsonObject(Config.readFile(new File("config/simplyswords_main/gem_effects.json5")));
case "General" -> Config.getJsonObject(Config.readFile(new File("config/simplyswords_main/general.json5")));
case "Loot" -> Config.getJsonObject(Config.readFile(new File("config/simplyswords_main/loot.json5")));
case "RunicEffects" -> Config.getJsonObject(Config.readFile(new File("config/simplyswords_main/runic_effects.json5")));
case "StatusEffects" -> Config.getJsonObject(Config.readFile(new File("config/simplyswords_main/status_effects.json5")));
case "UniqueEffects" -> Config.getJsonObject(Config.readFile(new File("config/simplyswords_main/unique_effects.json5")));
case "WeaponAttributes" -> Config.getJsonObject(Config.readFile(new File("config/simplyswords_main/weapon_attributes.json5")));
default -> null;
};
}
private static JsonObject getJson(String key) {
if (!isLoaded) {
Path path = Paths.get("config/simplyswords_main/");
if (!Files.exists(path)) return null;

JSONS.put("GemEffects", Config.getJsonObject(Config.readFile(new File("config/simplyswords_main/gem_effects.json5"))));
JSONS.put("General", Config.getJsonObject(Config.readFile(new File("config/simplyswords_main/general.json5"))));
JSONS.put("Loot", Config.getJsonObject(Config.readFile(new File("config/simplyswords_main/loot.json5"))));
JSONS.put("RunicEffects", Config.getJsonObject(Config.readFile(new File("config/simplyswords_main/runic_effects.json5"))));
JSONS.put("StatusEffects", Config.getJsonObject(Config.readFile(new File("config/simplyswords_main/status_effects.json5"))));
JSONS.put("UniqueEffects", Config.getJsonObject(Config.readFile(new File("config/simplyswords_main/unique_effects.json5"))));
JSONS.put("WeaponAttributes", Config.getJsonObject(Config.readFile(new File("config/simplyswords_main/weapon_attributes.json5"))));
isLoaded = true;

if (json != null) {

switch (type) {
case "boolean" -> {
for (Map.Entry<String, JsonElement> entry : json.entrySet()) {
try {
BOOLEAN.put(entry.getKey(), entry.getValue().getAsBoolean());
} catch (Exception e) {
//System.out.println(entry.getKey() + ": " + entry.getValue() + " is not a valid value. Skipping this entry.");
}
}
}
case "float" -> {
for (Map.Entry<String, JsonElement> entry : json.entrySet()) {
try {
FLOAT.put(entry.getKey(), entry.getValue().getAsFloat());
} catch (Exception e) {
//System.out.println(entry.getKey() + ": " + entry.getValue() + " is not a valid value. Skipping this entry.");
}
}
}
case "double" -> {
for (Map.Entry<String, JsonElement> entry : json.entrySet()) {
try {
DOUBLE.put(entry.getKey(), entry.getValue().getAsDouble());
} catch (Exception e) {
//System.out.println(entry.getKey() + ": " + entry.getValue() + " is not a valid value. Skipping this entry.");
}
}
}
case "int" -> {
for (Map.Entry<String, JsonElement> entry : json.entrySet()) {
try {
INT.put(entry.getKey(), entry.getValue().getAsInt());
} catch (Exception e) {
//System.out.println(entry.getKey() + ": " + entry.getValue() + " is not a valid value. Skipping this entry.");
}
}
}
}
}
return JSONS.get(key);
}


private static JsonElement getElement(String parent, String key) {
var json = getJson(parent);
if (json == null) return null;
return json.get(key);
}
}