Fixed crashing with forge-config-api-port-fabric

This commit is contained in:
coolGi
2023-08-07 21:54:41 +09:30
parent 9f9c542a6c
commit fc54e0f893
3 changed files with 24 additions and 10 deletions
@@ -1100,6 +1100,7 @@ public class Config
.build();
/** This class is used to debug the different features of the config GUI */
// FIXME: WARNING: Some of the options in this class dont get show n in the default UI
public static class ExampleConfigScreen
{
// Defined in the lang, just a note about this screen
@@ -1128,8 +1129,7 @@ public class Config
public static ConfigEntry<Long> longTest = new ConfigEntry.Builder<Long>()
.set(42069L)
.build();
// doesn't show up in the UI right now
public static ConfigEntry<Float> floatTest = new ConfigEntry.Builder<Float>()
.set(0.42069f)
.build();
@@ -1142,6 +1142,10 @@ public class Config
.set(new ArrayList<String>(Arrays.asList("option 1", "option 2", "option 3")))
.build();
public static ConfigEntry<Map<String, String>> mapTest = new ConfigEntry.Builder<Map<String, String>>()
.set(new HashMap<String, String>())
.build();
public static ConfigUIButton uiButtonTest = new ConfigUIButton(() -> {
System.setProperty("java.awt.headless", "false"); // Required to make it work
JOptionPane.showMessageDialog(null, "Button pressed!", "UITester dialog", JOptionPane.INFORMATION_MESSAGE);
@@ -115,8 +115,9 @@ public class ConfigFileHandling {
if (entry.getTrueValue() == null)
throw new IllegalArgumentException("Entry ["+ entry.getNameWCategory() +"] is null, this may be a problem with ["+ configBase.modName +"]. Please contact the authors");
if (ConfigTypeConverters.convertObjects.containsKey(entry.getType())) {
workConfig.set(entry.getNameWCategory(), ConfigTypeConverters.convertToString(entry.getType(), entry.getTrueValue()));
Class<?> originalClass = ConfigTypeConverters.isClassConvertable(entry.getType());
if (originalClass != null) {
workConfig.set(entry.getNameWCategory(), ConfigTypeConverters.convertToString(originalClass, entry.getTrueValue()));
return;
}
workConfig.set(entry.getNameWCategory(), entry.getTrueValue());
@@ -141,8 +142,9 @@ public class ConfigFileHandling {
entry.setWithoutSaving((T) ( workConfig.getEnum(entry.getNameWCategory(), (Class<? extends Enum>) entry.getType())));
return;
}
if (ConfigTypeConverters.convertObjects.containsKey(entry.getType())) {
entry.setWithoutSaving((T) ConfigTypeConverters.convertFromString(entry.getType(), workConfig.get(entry.getNameWCategory())));
Class<?> originalClass = ConfigTypeConverters.isClassConvertable(entry.getType());
if (originalClass != null) {
entry.setWithoutSaving((T) ConfigTypeConverters.convertFromString(originalClass, workConfig.get(entry.getNameWCategory())));
return;
}
@@ -15,17 +15,25 @@ import java.util.Map;
*/
public class ConfigTypeConverters {
// Once you've made a converter add it to here where the first value is the type you want to convert and the 2nd value is the converter
public static final Map<Class, ConverterBase> convertObjects = new HashMap<Class, ConverterBase>() {{
public static final Map<Class<?>, ConverterBase> convertObjects = new HashMap<Class<?>, ConverterBase>() {{
put(Short.class, new ShortConverter());
put(Long.class, new LongConverter());
put(Float.class, new FloatConverter());
put(Byte.class, new ByteConverter());
put(Map.class, new MapConverter());
put(HashMap.class, new MapConverter());
}};
public static String convertToString(Class clazz, Object value) {
public static Class<?> isClassConvertable(Class<?> clazz) {
for (int i = 0; i < convertObjects.size(); i++) {
Class<?> selectedClass = (Class<?>) convertObjects.keySet().toArray()[i];
if (selectedClass.isAssignableFrom(clazz))
return selectedClass;
}
return null;
}
public static String convertToString(Class<?> clazz, Object value) {
try {
return convertObjects.get(clazz).convertToString(value);
} catch (Exception e) {
@@ -33,7 +41,7 @@ public class ConfigTypeConverters {
return null;
}
}
public static Object convertFromString(Class clazz, String value) {
public static Object convertFromString(Class<?> clazz, String value) {
try {
return convertObjects.get(clazz).convertFromString(value);
} catch (Exception e) {