From fc54e0f8939bc46cd27b43c7d3389450d0a35bbd Mon Sep 17 00:00:00 2001 From: coolGi Date: Mon, 7 Aug 2023 21:54:41 +0930 Subject: [PATCH] Fixed crashing with forge-config-api-port-fabric --- .../distanthorizons/core/config/Config.java | 8 ++++++-- .../core/config/file/ConfigFileHandling.java | 10 ++++++---- .../core/config/file/ConfigTypeConverters.java | 16 ++++++++++++---- 3 files changed, 24 insertions(+), 10 deletions(-) diff --git a/core/src/main/java/com/seibel/distanthorizons/core/config/Config.java b/core/src/main/java/com/seibel/distanthorizons/core/config/Config.java index 11a65cc44..1ab41cbe1 100644 --- a/core/src/main/java/com/seibel/distanthorizons/core/config/Config.java +++ b/core/src/main/java/com/seibel/distanthorizons/core/config/Config.java @@ -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 longTest = new ConfigEntry.Builder() .set(42069L) .build(); - - // doesn't show up in the UI right now + public static ConfigEntry floatTest = new ConfigEntry.Builder() .set(0.42069f) .build(); @@ -1142,6 +1142,10 @@ public class Config .set(new ArrayList(Arrays.asList("option 1", "option 2", "option 3"))) .build(); + public static ConfigEntry> mapTest = new ConfigEntry.Builder>() + .set(new HashMap()) + .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); diff --git a/core/src/main/java/com/seibel/distanthorizons/core/config/file/ConfigFileHandling.java b/core/src/main/java/com/seibel/distanthorizons/core/config/file/ConfigFileHandling.java index 83910c4ee..e8ef1fef5 100644 --- a/core/src/main/java/com/seibel/distanthorizons/core/config/file/ConfigFileHandling.java +++ b/core/src/main/java/com/seibel/distanthorizons/core/config/file/ConfigFileHandling.java @@ -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) 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; } diff --git a/core/src/main/java/com/seibel/distanthorizons/core/config/file/ConfigTypeConverters.java b/core/src/main/java/com/seibel/distanthorizons/core/config/file/ConfigTypeConverters.java index b2da5f8a7..0ac53df7b 100644 --- a/core/src/main/java/com/seibel/distanthorizons/core/config/file/ConfigTypeConverters.java +++ b/core/src/main/java/com/seibel/distanthorizons/core/config/file/ConfigTypeConverters.java @@ -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 convertObjects = new HashMap() {{ + public static final Map, ConverterBase> convertObjects = new HashMap, 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) {