11package com .zetaplugins .zetacore .services .config ;
22
3+ import com .zetaplugins .zetacore .annotations .ConfigAttribute ;
34import com .zetaplugins .zetacore .annotations .PluginConfig ;
45import com .zetaplugins .zetacore .annotations .NestedConfig ;
56import org .bukkit .configuration .ConfigurationSection ;
@@ -54,10 +55,11 @@ private static <T> void mapSection(ConfigurationSection section, T instance) thr
5455 for (Field field : configClass .getDeclaredFields ()) {
5556 field .setAccessible (true );
5657 Class <?> fieldType = field .getType ();
58+ String fieldName = getFieldName (field );
5759
5860 // Nested objects annotated with @NestedConfig
5961 if (fieldType .isAnnotationPresent (NestedConfig .class )) {
60- ConfigurationSection nestedSection = section .getConfigurationSection (field . getName () );
62+ ConfigurationSection nestedSection = section .getConfigurationSection (fieldName );
6163 if (nestedSection == null ) continue ;
6264 Object nestedInstance = fieldType .getConstructor ().newInstance ();
6365 mapSection (nestedSection , nestedInstance );
@@ -67,9 +69,9 @@ private static <T> void mapSection(ConfigurationSection section, T instance) thr
6769
6870 // Lists
6971 if (isListType (fieldType )) {
70- Object rawListObj = section .get (field . getName () );
72+ Object rawListObj = section .get (fieldName );
7173 if (rawListObj instanceof List <?> rawList ) {
72- List <Object > mappedList = convertList (field .getGenericType (), rawList , field . getName () );
74+ List <Object > mappedList = convertList (field .getGenericType (), rawList , fieldName );
7375 field .set (instance , mappedList );
7476 } else if (rawListObj == null ) {
7577 // nothing present - skip
@@ -82,13 +84,13 @@ private static <T> void mapSection(ConfigurationSection section, T instance) thr
8284 // Maps
8385 if (isMapType (fieldType )) {
8486 // raw can be a ConfigurationSection or a Map/LinkedHashMap
85- Object rawMap = section .get (field . getName () );
86- ConfigurationSection rawSection = section .getConfigurationSection (field . getName () );
87+ Object rawMap = section .get (fieldName );
88+ ConfigurationSection rawSection = section .getConfigurationSection (fieldName );
8789 if (rawSection != null ) {
88- Map <?, ?> mapped = convertMap (field .getGenericType (), rawSection , field . getName () );
90+ Map <?, ?> mapped = convertMap (field .getGenericType (), rawSection , fieldName );
8991 field .set (instance , mapped );
9092 } else if (rawMap instanceof LinkedHashMap <?, ?> || rawMap instanceof Map <?, ?>) {
91- Map <?, ?> mapped = convertMap (field .getGenericType (), rawMap , field . getName () );
93+ Map <?, ?> mapped = convertMap (field .getGenericType (), rawMap , fieldName );
9294 field .set (instance , mapped );
9395 } else if (rawMap == null ) {
9496 // nothing present - skip
@@ -100,7 +102,7 @@ private static <T> void mapSection(ConfigurationSection section, T instance) thr
100102
101103 // Enum types
102104 if (fieldType .isEnum ()) {
103- String enumName = section .getString (field . getName () );
105+ String enumName = section .getString (fieldName );
104106 if (enumName == null ) continue ;
105107 String upperEnumName = enumName .toUpperCase ();
106108 Object enumValue = Enum .valueOf ((Class <Enum >) fieldType , upperEnumName );
@@ -110,7 +112,7 @@ private static <T> void mapSection(ConfigurationSection section, T instance) thr
110112
111113 // Primitive / String / basic types
112114 if (isSimpleType (fieldType )) {
113- Object value = section .get (field . getName () );
115+ Object value = section .get (fieldName );
114116 if (value != null ) {
115117 field .set (instance , value );
116118 }
@@ -135,7 +137,7 @@ private static void mapFromMap(LinkedHashMap<?, ?> map, Object instance) throws
135137 Class <?> clazz = instance .getClass ();
136138 for (Field field : clazz .getDeclaredFields ()) {
137139 field .setAccessible (true );
138- Object value = map .get (field . getName ( ));
140+ Object value = map .get (getFieldName ( field ));
139141 if (value == null ) continue ;
140142
141143 Class <?> fieldType = field .getType ();
@@ -146,11 +148,11 @@ private static void mapFromMap(LinkedHashMap<?, ?> map, Object instance) throws
146148 field .set (instance , nestedInstance );
147149
148150 } else if (isListType (fieldType ) && value instanceof List <?> rawList ) {
149- List <Object > mappedList = convertList (field .getGenericType (), rawList , field . getName ( ));
151+ List <Object > mappedList = convertList (field .getGenericType (), rawList , getFieldName ( field ));
150152 field .set (instance , mappedList );
151153
152154 } else if (isMapType (fieldType ) && value instanceof LinkedHashMap <?, ?> rawLinkedMap ) {
153- Map <?, ?> mappedMap = convertMap (field .getGenericType (), rawLinkedMap , field . getName ( ));
155+ Map <?, ?> mappedMap = convertMap (field .getGenericType (), rawLinkedMap , getFieldName ( field ));
154156 field .set (instance , mappedMap );
155157
156158 } else if (isSimpleType (fieldType )) {
@@ -348,4 +350,12 @@ private static boolean isListType(Class<?> type) {
348350 private static boolean isMapType (Class <?> type ) {
349351 return Map .class .isAssignableFrom (type );
350352 }
353+
354+ private static String getFieldName (Field field ) {
355+ if (field .isAnnotationPresent (ConfigAttribute .class )) {
356+ ConfigAttribute annotation = field .getAnnotation (ConfigAttribute .class );
357+ return annotation .name ();
358+ }
359+ return field .getName ();
360+ }
351361}
0 commit comments