package net.blay09.mods.craftingtweaks.addon;

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.blay09.mods.craftingtweaks.api.CraftingTweaksAPI;
import net.blay09.mods.craftingtweaks.api.DefaultProvider;
import net.blay09.mods.craftingtweaks.api.TweakProvider;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.IInventory;

import java.lang.reflect.Field;
import java.util.List;

public class BackpacksTweakProvider implements TweakProvider {

    private final DefaultProvider defaultProvider = CraftingTweaksAPI.createDefaultProvider();
    private boolean isLoaded;
    private Field craftingGridField;

    public BackpacksTweakProvider() {
        try {
            Class clazz = Class.forName("de.eydamos.backpack.inventory.container.ContainerWorkbenchBackpack");
            craftingGridField = clazz.getDeclaredField("craftingGrid");
            craftingGridField.setAccessible(true);
            isLoaded = true;
        } catch (ClassNotFoundException ignored) {
        } catch (NoSuchFieldException ignored) {}
    }

    @Override
    public boolean isLoaded() {
        return isLoaded;
    }

    @Override
    public void clearGrid(EntityPlayer entityPlayer, Container container, int id) {
        try {
            IInventory craftMatrix = (IInventory) craftingGridField.get(container);
            defaultProvider.clearGrid(entityPlayer, container, craftMatrix);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void rotateGrid(EntityPlayer entityPlayer, Container container, int id) {
        try {
            IInventory craftMatrix = (IInventory) craftingGridField.get(container);
            defaultProvider.rotateGrid(entityPlayer, container, craftMatrix);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void balanceGrid(EntityPlayer entityPlayer, Container container, int id) {
        try {
            IInventory craftMatrix = (IInventory) craftingGridField.get(container);
            defaultProvider.balanceGrid(entityPlayer, container, craftMatrix);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    @Override
    @SideOnly(Side.CLIENT)
    public void initGui(GuiContainer guiContainer, List buttonList) {
        final int paddingTop = 16;
        buttonList.add(CraftingTweaksAPI.createRotateButton(0, guiContainer.guiLeft - 16, guiContainer.guiTop + paddingTop));
        buttonList.add(CraftingTweaksAPI.createBalanceButton(0, guiContainer.guiLeft - 16, guiContainer.guiTop + paddingTop + 18));
        buttonList.add(CraftingTweaksAPI.createClearButton(0, guiContainer.guiLeft - 16, guiContainer.guiTop + paddingTop + 36));
    }

    @Override
    @SideOnly(Side.CLIENT)
    public boolean areHotkeysEnabled(EntityPlayer entityPlayer, Container container) {
        return true;
    }

    @Override
    public String getModId() {
        return "Backpack";
    }

}

@@ -1482,70 +1482,157 @@
     
-defaultProvider.clearGrid(entityPlayer, container, craftMatrix
+for(int i = 0; i %3C 9; i++) %7B%0A                craftMatrix.setInventorySlotContents(i, null);%0A            %7D%0A            container.detectAndSendChanges(
 );%0A 
@@ -2198,811 +2198,284 @@
 d) %7B
-%0A        try %7B%0A            IInventory craftMatrix = (IInventory) craftingGridField.get(container);%0A            defaultProvider.balanceGrid(entityPlayer, container, craftMatrix);%0A        %7D catch (IllegalAccessException e) %7B%0A            e.printStackTrace();%0A        %7D%0A    %7D%0A%0A    @Override%0A    @SideOnly(Side.CLIENT)%0A    public void initGui(GuiContainer guiContainer, List buttonList) %7B%0A        final int paddingTop = 16;%0A        buttonList.add(CraftingTweaksAPI.createRotateButton(0, guiContainer.guiLeft - 16, guiContainer.guiTop + paddingTop));%0A        buttonList.add(CraftingTweaksAPI.createBalanceButton(0, guiContainer.guiLeft - 16, guiContainer.guiTop + paddingTop + 18));%0A        buttonList.add(CraftingTweaksAPI.createClearButton(0, guiContainer.guiLeft - 16, guiContainer.guiTop + paddingTop + 36
+%7D%0A%0A    @Override%0A    @SideOnly(Side.CLIENT)%0A    public void initGui(GuiContainer guiContainer, List buttonList) %7B%0A        final int paddingTop = 16;%0A        buttonList.add(CraftingTweaksAPI.createRotateButton(0, guiContainer.guiLeft + 16, guiContainer.guiTop + paddingTop + 18
 ));%0A

Fix Backpacks support.