package net.qbar.common.machine.typeadapter;

import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import net.qbar.QBar;
import net.qbar.common.machine.CraftingComponent;
import net.qbar.common.recipe.QBarRecipeHandler;
import org.apache.commons.lang3.ArrayUtils;

import java.io.IOException;

public class CraftingComponentTypeAdapter extends TypeAdapter<CraftingComponent>
{
    @Override
    public void write(JsonWriter out, CraftingComponent value) throws IOException
    {

    }

    @Override
    public CraftingComponent read(JsonReader in) throws IOException
    {
        CraftingComponent component = new CraftingComponent();

        int inventorySize = 0;

        in.beginObject();
        while (in.hasNext())
        {
            switch (in.nextName())
            {
                case "category":
                    String category = in.nextString();
                    if (!category.startsWith("qbar."))
                        category = "qbar." + category;
                    if (QBarRecipeHandler.RECIPES.containsKey(category))
                        component.setRecipeCategory(category);
                    else
                        QBar.logger.error("Unknown recipe category detected while parsing crafter (" + category + ")");
                    break;
                case "inventorySize":
                    inventorySize = in.nextInt();
                    break;
                case "speed":
                    component.setCraftingSpeed((float) in.nextDouble());
                    break;
                case "itemInput":
                    component.setInputs(new int[in.nextInt()]);
                    component.setBuffers(new int[component.getInputs().length]);
                    break;
                case "itemOutput":
                    component.setOutputs(new int[in.nextInt()]);
                    break;
                case "tankInput":
                    component.setInputTanks(new int[in.nextInt()]);
                    component.setBufferTanks(new int[component.getInputTanks().length]);
                    break;
                case "tankOutput":
                    component.setOutputTanks(new int[in.nextInt()]);
                    break;
                default:
                    break;
            }
        }
        in.endObject();

        component.setIoUnion(ArrayUtils.addAll(component.getInputs(), component.getOutputs()));
        if (inventorySize != 0)
            component.setInventorySize(inventorySize);
        else
            component.setInventorySize(component.getInputs().length +
                    component.getOutputs().length + component.getBuffers().length);
        return component;
    }
}

@@ -352,16 +352,67 @@
 ception;
+%0Aimport java.util.ArrayList;%0Aimport java.util.List;
 %0A%0Apublic
@@ -2038,53 +2038,410 @@
     
-component.setInputTanks(new int%5Bin.nextInt()%5D
+in.beginArray();%0A                    List%3CInteger%3E inputs = new ArrayList%3C%3E();%0A                    while (in.hasNext())%0A                        inputs.add(in.nextInt());%0A                    in.endArray();%0A%0A                    component.setInputTanks(new int%5Binputs.size()%5D);%0A                    for (int i = 0; i %3C inputs.size(); i++)%0A                        component.getInputTanks()%5Bi%5D = inputs.get(i
 );%0A 
@@ -2484,24 +2484,33 @@
 erTanks(
-new int%5B
+ArrayUtils.clone(
 componen
@@ -2526,24 +2526,17 @@
 tTanks()
-.length%5D
+)
 );%0A     
@@ -2616,184 +2616,916 @@
     
-component.setOutputTanks(new int%5Bin.nextInt()%5D);%0A                    break;%0A                default:%0A                    break;%0A            %7D%0A        %7D%0A        in.endObject();%0A
+in.beginArray();%0A                    List%3CInteger%3E outputs = new ArrayList%3C%3E();%0A                    while (in.hasNext())%0A                        outputs.add(in.nextInt());%0A                    in.endArray();%0A%0A                    component.setOutputTanks(new int%5Boutputs.size()%5D);%0A                    for (int i = 0; i %3C outputs.size(); i++)%0A                        component.getOutputTanks()%5Bi%5D = outputs.get(i);%0A                    break;%0A                default:%0A                    break;%0A            %7D%0A        %7D%0A        in.endObject();%0A%0A        for (int i = 0; i %3C component.getInputs().length; i++)%0A        %7B%0A            component.getInputs()%5Bi%5D = i;%0A            component.getBuffers()%5Bi%5D = i + component.getInputs().length + component.getOutputs().length;%0A        %7D%0A        for (int i = 0; i %3C component.getOutputs().length; i++)%0A            component.getOutputs()%5Bi%5D = i + component.getInputs().length;
 %0A   
@@ -3613,24 +3613,25 @@
 utputs()));%0A
+%0A
         if (

Fix craftingcomponent typeadapter