EpicKnarvik97 e5cb3b4a30
All checks were successful
EpicKnarvik97/Blacksmith/pipeline/head This commit looks good
A lot of tests, and some improvements
Adds a lot more tests for some utility classes
Adds a new option to disable the hard-coded limitation "EnchantmentTarget.BREAKABLE" which can be useful for servers with custom items or similar.
Makes enchantment name matching case-insensitive.
Prevents an exception is enchantment name contains invalid characters.
Makes invalid objects supplied to asStringList return null instead of throwing an exception.
Uses isBlank instead of trim.isEmpty in the isEmpty method
Re-uses the random generator if calculating salvage several times
2023-01-16 17:42:54 +01:00

86 lines
2.3 KiB
Java

package net.knarcraft.blacksmith.util;
import java.util.ArrayList;
import java.util.List;
/**
* A helper class for getting an object value as the correct type
*/
public final class ConfigHelper {
private ConfigHelper() {
}
/**
* Gets the given value as a string list
*
* @param value <p>The raw string list value</p>
* @return <p>The value as a string list, or null if not compatible</p>
*/
public static List<String> asStringList(Object value) {
if (value instanceof String) {
return List.of(((String) value).split(","));
} else if (value instanceof List<?> list) {
List<String> strings = new ArrayList<>();
for (Object object : list) {
strings.add(String.valueOf(object));
}
return strings;
} else {
return null;
}
}
/**
* Gets the given value as a double
*
* <p>This will throw an exception if used for a non-double setting</p>
*
* @param value <p>The object value to get</p>
* @return <p>The value of the given object as a double</p>
*/
public static double asDouble(Object value) {
if (value instanceof String) {
return Double.parseDouble((String) value);
} else if (value instanceof Integer) {
return (Integer) value;
} else {
return (Double) value;
}
}
/**
* Gets the given value as a boolean
*
* <p>This will throw an exception if used for a non-boolean value</p>
*
* @param value <p>The object value to get</p>
* @return <p>The value of the given object as a boolean</p>
*/
public static boolean asBoolean(Object value) {
if (value instanceof String) {
return Boolean.parseBoolean((String) value);
} else {
return (Boolean) value;
}
}
/**
* Gets the given value as an integer
*
* <p>This will throw an exception if used for a non-integer setting</p>
*
* @param value <p>The object value to get</p>
* @return <p>The value of the given object as an integer</p>
*/
public static int asInt(Object value) {
if (value instanceof String) {
return Integer.parseInt((String) value);
} else {
return (Integer) value;
}
}
}