package org.cyclops.integrateddynamics.core.evaluate.variable;

import net.minecraft.nbt.JsonToNBT;
import net.minecraft.nbt.NBTException;
import net.minecraft.nbt.NBTTagCompound;
import org.cyclops.cyclopscore.persist.nbt.INBTProvider;
import org.cyclops.integrateddynamics.api.evaluate.variable.IValue;
import org.cyclops.integrateddynamics.api.evaluate.variable.IValueType;
import org.cyclops.integrateddynamics.api.evaluate.variable.IValueTypeListProxy;
import org.cyclops.integrateddynamics.api.evaluate.variable.IValueTypeListProxyFactoryTypeRegistry;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * Factory for list proxies that implement {@link org.cyclops.cyclopscore.persist.nbt.INBTProvider}.
 * @author rubensworks
 */
public class ValueTypeListProxyNBTFactory<T extends IValueType<V>, V extends IValue, P extends IValueTypeListProxy<T, V> & INBTProvider> implements IValueTypeListProxyFactoryTypeRegistry.IProxyFactory<T, V, P> {

    private final String name;
    private final Class<P> proxyClass;

    public ValueTypeListProxyNBTFactory(String name, Class<P> proxyClass) {
        this.name = name;
        this.proxyClass = proxyClass;
    }

    @Override
    public String getName() {
        return this.name;
    }

    protected Class<P> getProxyClass() {
        return this.proxyClass;
    }

    @Override
    public String serialize(P values) throws IValueTypeListProxyFactoryTypeRegistry.SerializationException {
        NBTTagCompound tag = new NBTTagCompound();
        values.writeGeneratedFieldsToNBT(tag);
        return tag.toString();
    }

    @Override
    public P deserialize(String value) throws IValueTypeListProxyFactoryTypeRegistry.SerializationException {
        try {
            Constructor<P> constructor = getProxyClass().getConstructor();
            P proxy = constructor.newInstance();
            NBTTagCompound tag = JsonToNBT.getTagFromJson(value);
            proxy.readGeneratedFieldsFromNBT(tag);
            return proxy;
        } catch (NoSuchMethodException | InvocationTargetException | InstantiationException | NBTException | IllegalAccessException e) {
            e.printStackTrace();
            throw new IValueTypeListProxyFactoryTypeRegistry.SerializationException(e.getMessage());
        }
    }
}

@@ -1057,16 +1057,72 @@
 xyClass;
+%0A    private final Constructor%3CP%3E proxyClassConstructor;
 %0A%0A    pu
@@ -1246,32 +1246,423 @@
 ss = proxyClass;
+%0A%0A        try %7B%0A            this.proxyClassConstructor = this.proxyClass.getConstructor();%0A        %7D catch (NoSuchMethodException e) %7B%0A            e.printStackTrace();%0A            throw new RuntimeException(String.format(%0A                    %22Could not find a default constructor for %25s, while this is required for list proxies. This is a developer error.%22, proxyClass.getName()));%0A        %7D
 %0A    %7D%0A%0A    @Ove
@@ -2223,94 +2223,34 @@
     
-Constructor%3CP%3E constructor = getProxyClass().getConstructor();%0A            P proxy = c
+P proxy = this.proxyClassC
 onst
@@ -2414,16 +2414,16 @@
  proxy;%0A
+
         
@@ -2435,32 +2435,8 @@
 ch (
-NoSuchMethodException %7C 
 Invo

Add check for list proxies that have a missing default constructor