Fix arrays in map values for config system

This commit is contained in:
LexManos 2017-04-06 13:15:26 -07:00
parent d850ca890f
commit ae96fd08db
3 changed files with 59 additions and 39 deletions

View File

@ -50,6 +50,7 @@ public class ConfigManager
{
private static Map<String, Multimap<Config.Type, ASMData>> asm_data = Maps.newHashMap();
static Map<Class<?>, ITypeAdapter> ADAPTERS = Maps.newHashMap();
static Map<Class<?>, Class<?>> ARRAY_REMAP = Maps.newHashMap();
private static Map<String, Configuration> CONFIGS = Maps.newHashMap();
private static Map<String, Set<Class<?>>> MOD_CONFIG_CLASSES = Maps.newHashMap();
@ -85,6 +86,16 @@ public class ConfigManager
register(Integer[].class, TypeAdapters.IntA);
register(String.class, TypeAdapters.Str);
register(String[].class, TypeAdapters.StrA);
ARRAY_REMAP.put(Boolean.class, Boolean[].class );
ARRAY_REMAP.put(Float.class, Float[].class );
ARRAY_REMAP.put(Double.class, Double[].class );
ARRAY_REMAP.put(Byte.class, Byte[].class );
ARRAY_REMAP.put(Character.class, Character[].class);
ARRAY_REMAP.put(Short.class, Short[].class );
ARRAY_REMAP.put(Integer.class, Integer[].class );
ARRAY_REMAP.put(String.class, String[].class );
}
private static void register(Class<?> cls, ITypeAdapter adpt)
{

View File

@ -1,6 +1,7 @@
package net.minecraftforge.common.config;
import java.lang.reflect.Field;
import java.lang.reflect.GenericArrayType;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Iterator;
@ -67,6 +68,7 @@ public abstract class FieldWrapper implements IFieldWrapper
{
private Map<String, Object> theMap = null;
private Type mType;
ITypeAdapter adapter;
@SuppressWarnings("unchecked")
private MapWrapper(String category, Field field, Object instance)
@ -90,18 +92,25 @@ public abstract class FieldWrapper implements IFieldWrapper
ParameterizedType type = (ParameterizedType) field.getGenericType();
mType = type.getActualTypeArguments()[1];
if (ADAPTERS.get(mType) == null && !Enum.class.isAssignableFrom((Class<?>) mType))
this.adapter = ADAPTERS.get(mType);
if (this.adapter == null && mType instanceof GenericArrayType)
{
this.adapter = ADAPTERS.get(ARRAY_REMAP.get(((GenericArrayType)mType).getGenericComponentType())); //J6 seems to have issues, Need to find a better way to translate this. We don't have access to array depth.
}
if (mType instanceof Class && Enum.class.isAssignableFrom((Class<?>)mType))
{
this.adapter = TypeAdapters.Str;
}
if (this.adapter == null)
throw new IllegalArgumentException(String.format("The map '%s' of class '%s' has target values which are neither primitive nor an enum!",
field.getName(), field.getDeclaringClass().getCanonicalName()));
}
@Override
public ITypeAdapter getTypeAdapter()
{
ITypeAdapter adapter = ADAPTERS.get(mType);
if (adapter == null && Enum.class.isAssignableFrom((Class<?>) mType))
adapter = TypeAdapters.Str;
return adapter;
}