2022-08-19 19:08:54 +02:00
|
|
|
package net.knarcraft.blacksmith.util;
|
|
|
|
|
2022-11-05 04:21:47 +01:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
2022-08-19 19:08:54 +02:00
|
|
|
/**
|
|
|
|
* A helper class for getting an object value as the correct type
|
|
|
|
*/
|
|
|
|
public final class ConfigHelper {
|
|
|
|
|
|
|
|
private ConfigHelper() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-11-05 04:21:47 +01:00
|
|
|
/**
|
|
|
|
* Gets the given value as a string list
|
|
|
|
*
|
|
|
|
* @param value <p>The raw string list value</p>
|
2023-01-16 17:42:54 +01:00
|
|
|
* @return <p>The value as a string list, or null if not compatible</p>
|
2022-11-05 04:21:47 +01:00
|
|
|
*/
|
|
|
|
public static List<String> asStringList(Object value) {
|
|
|
|
if (value instanceof String) {
|
|
|
|
return List.of(((String) value).split(","));
|
2023-01-16 17:42:54 +01:00
|
|
|
} else if (value instanceof List<?> list) {
|
2022-11-05 04:21:47 +01:00
|
|
|
List<String> strings = new ArrayList<>();
|
|
|
|
for (Object object : list) {
|
|
|
|
strings.add(String.valueOf(object));
|
|
|
|
}
|
|
|
|
return strings;
|
2023-01-16 17:42:54 +01:00
|
|
|
} else {
|
|
|
|
return null;
|
2022-11-05 04:21:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-19 19:08:54 +02:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|