Загрузка данных


package blusunrize.immersiveengineering.common.gui;

import blusunrize.immersiveengineering.ImmersiveEngineering;
import blusunrize.immersiveengineering.api.crafting.BlueprintCraftingRecipe;
import blusunrize.immersiveengineering.api.shader.CapabilityShader;
import blusunrize.immersiveengineering.api.tool.IConfigurableTool;
import blusunrize.immersiveengineering.api.tool.IUpgradeableTool;
import blusunrize.immersiveengineering.common.blocks.wooden.TileEntityModWorkbench;
import blusunrize.immersiveengineering.common.items.ItemEngineersBlueprint;
import blusunrize.immersiveengineering.common.util.inventory.IEItemStackHandler;
import javax.annotation.Nonnull;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.ClickType;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
import net.minecraftforge.fml.common.FMLCommonHandler;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler;

public class ContainerModWorkbench extends ContainerIEBase<TileEntityModWorkbench> {
  private final World world;
  
  public InventoryPlayer inventoryPlayer;
  
  private InventoryBlueprint inventoryBPoutput;
  
  public InventoryShader shaderInv;
  
  public ContainerModWorkbench(InventoryPlayer inventoryPlayer, World world, TileEntityModWorkbench tile) {
    super(inventoryPlayer, tile);
    this.world = world;
    this.inventoryPlayer = inventoryPlayer;
    rebindSlots();
  }
  
  private void bindPlayerInv(InventoryPlayer inventoryPlayer) {
    int i;
    for (i = 0; i < 3; i++) {
      for (int j = 0; j < 9; j++)
        func_75146_a(new Slot((IInventory)inventoryPlayer, j + i * 9 + 9, 8 + j * 18, 87 + i * 18)); 
    } 
    for (i = 0; i < 9; i++)
      func_75146_a(new Slot((IInventory)inventoryPlayer, i, 8 + i * 18, 145)); 
  }
  
  public void rebindSlots() {
    if (FMLCommonHandler.instance().getEffectiveSide() == Side.CLIENT)
      for (Slot slot : this.field_75151_b) {
        if (slot instanceof IESlot.Upgrades && 
          ItemStack.func_179545_c(((IESlot.Upgrades)slot).upgradeableTool, this.inv.func_70301_a(0)))
          return; 
      }  
    this.field_75151_b.clear();
    this.field_75153_a.clear();
    func_75146_a(new IESlot.ModWorkbench(this, this.inv, 0, 24, 22, 1));
    this.slotCount = 1;
    ItemStack tool = func_75139_a(0).func_75211_c();
    if (tool.func_77973_b() instanceof IUpgradeableTool) {
      IItemHandler handler = (IItemHandler)tool.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null);
      if (handler instanceof IEItemStackHandler)
        ((IEItemStackHandler)handler).setTile((TileEntity)this.tile); 
      Slot[] slots = ((IUpgradeableTool)tool.func_77973_b()).getWorkbenchSlots(this, tool);
      if (slots != null)
        for (Slot s : slots) {
          func_75146_a(s);
          this.slotCount++;
        }  
      if (tool.hasCapability(CapabilityShader.SHADER_CAPABILITY, null)) {
        CapabilityShader.ShaderWrapper wrapper = (CapabilityShader.ShaderWrapper)tool.getCapability(CapabilityShader.SHADER_CAPABILITY, null);
        if (wrapper != null) {
          this.shaderInv = new InventoryShader(this, wrapper);
          func_75146_a(new IESlot.Shader(this, this.shaderInv, 0, 130, 32, tool));
          this.slotCount++;
          this.shaderInv.shader = wrapper.getShaderItem();
        } 
      } 
    } else if (!(tool.func_77973_b() instanceof IConfigurableTool)) {
      boolean blueprint = false;
      if (tool.func_77973_b() instanceof ItemEngineersBlueprint) {
        blueprint = true;
        BlueprintCraftingRecipe[] recipes = ((ItemEngineersBlueprint)tool.func_77973_b()).getRecipes(tool);
        this.inventoryBPoutput = new InventoryBlueprint(this, recipes);
        for (int j = 0; j < recipes.length; j++) {
          int y = 21 + ((j < 9) ? (j / 3) : (-(j - 6) / 3)) * 18;
          func_75146_a(new IESlot.BlueprintOutput(this, this.inventoryBPoutput, this.inv, j, 118 + j % 3 * 18, y, recipes[j]));
          this.slotCount++;
        } 
      } 
      for (int i = 0; i < 6; i++) {
        if (blueprint) {
          func_75146_a(new IESlot.BlueprintInput(this, this.inv, this.inventoryBPoutput, i + 1, (i % 2 == 0) ? 74 : 92, 21 + i / 2 * 18));
        } else {
          func_75146_a(new Slot(this.inv, i + 1, (i % 2 == 0) ? 74 : 92, 21 + i / 2 * 18));
        } 
        this.slotCount++;
      } 
    } 
    bindPlayerInv(this.inventoryPlayer);
    ImmersiveEngineering.proxy.reInitGui();
  }
  
  @Nonnull
  public ItemStack func_82846_b(EntityPlayer player, int slot) {
    ItemStack stack = ItemStack.field_190927_a;
    Slot slotObject = this.field_75151_b.get(slot);
    if (slotObject != null && slotObject.func_75216_d()) {
      ItemStack stackInSlot = slotObject.func_75211_c();
      stack = stackInSlot.func_77946_l();
      if (slot < this.slotCount) {
        if (!func_75135_a(stackInSlot, this.slotCount, this.slotCount + 36, true))
          return ItemStack.field_190927_a; 
      } else if (!stackInSlot.func_190926_b()) {
        if (stackInSlot.func_77973_b() instanceof ItemEngineersBlueprint) {
          if (!func_75135_a(stackInSlot, 0, 1, true))
            return ItemStack.field_190927_a; 
        } else if (stackInSlot.func_77973_b() instanceof IUpgradeableTool && ((IUpgradeableTool)stackInSlot.func_77973_b()).canModify(stackInSlot)) {
          if (!func_75135_a(stackInSlot, 0, 1, true))
            return ItemStack.field_190927_a; 
        } else if (stackInSlot.func_77973_b() instanceof IConfigurableTool && ((IConfigurableTool)stackInSlot.func_77973_b()).canConfigure(stackInSlot)) {
          if (!func_75135_a(stackInSlot, 0, 1, true))
            return ItemStack.field_190927_a; 
        } else if (this.slotCount > 1) {
          boolean b = true;
          for (int i = 1; i < this.slotCount; i++) {
            Slot s = this.field_75151_b.get(i);
            if (s != null && s.func_75214_a(stackInSlot) && 
              func_75135_a(stackInSlot, i, i + 1, true)) {
              b = false;
              break;
            } 
          } 
          if (b)
            return ItemStack.field_190927_a; 
        } 
      } 
      if (stackInSlot.func_190916_E() == 0) {
        slotObject.func_75215_d(ItemStack.field_190927_a);
      } else {
        slotObject.func_75218_e();
      } 
      if (stackInSlot.func_190916_E() == stack.func_190916_E())
        return ItemStack.field_190927_a; 
      slotObject.func_190901_a(player, stack);
    } 
    return stack;
  }
  
  @Nonnull
  public ItemStack func_184996_a(int id, int button, ClickType clickType, EntityPlayer player) {
    ItemStack ret = super.func_184996_a(id, button, clickType, player);
    this.tile.markContainingBlockForUpdate(null);
    if (FMLCommonHandler.instance().getEffectiveSide().isServer())
      func_75142_b(); 
    return ret;
  }
}