Guest User

ContainerAutoCraftingTable

a guest
Jul 20th, 2015
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.18 KB | None | 0 0
  1. package com.earthcomputer.farmersheaven;
  2.  
  3. import net.minecraft.entity.player.EntityPlayer;
  4. import net.minecraft.entity.player.InventoryPlayer;
  5. import net.minecraft.inventory.Container;
  6. import net.minecraft.inventory.ICrafting;
  7. import net.minecraft.inventory.Slot;
  8. import net.minecraft.inventory.SlotCrafting;
  9. import net.minecraft.inventory.SlotFurnaceFuel;
  10. import net.minecraft.item.ItemStack;
  11. import net.minecraft.tileentity.TileEntity;
  12. import net.minecraft.tileentity.TileEntityFurnace;
  13. import net.minecraft.util.BlockPos;
  14. import net.minecraft.world.World;
  15. import net.minecraftforge.fml.common.FMLCommonHandler;
  16. import net.minecraftforge.fml.relauncher.Side;
  17. import net.minecraftforge.fml.relauncher.SideOnly;
  18.  
  19. public class ContainerAutoCraftingTable extends Container
  20. {
  21.    
  22.     private TileEntityAutoCraftingTable craftingTable;
  23.    
  24.     private int craftTime = 0;
  25.     private int burnTime = 0;
  26.     private int maxBurnTime = 200;
  27.    
  28.     public ContainerAutoCraftingTable(EntityPlayer player, World world, BlockPos pos)
  29.     {
  30.         TileEntity tileEntity = world.getTileEntity(pos);
  31.         if(tileEntity instanceof TileEntityAutoCraftingTable)
  32.             craftingTable = (TileEntityAutoCraftingTable) tileEntity;
  33.         else craftingTable = null;
  34.         for(int j = 0; j < 3; j++)
  35.         {
  36.             for(int i = 0; i < 3; i++)
  37.             {
  38.                 addSlotToContainer(new Slot(craftingTable, i + j * 3, 30 + i * 18, 17 + j * 18));
  39.             }
  40.         }
  41.         addSlotToContainer(new SlotCrafting(player, craftingTable.getInventoryCrafting(), craftingTable, 9, 124, 35) {
  42.             @Override
  43.             public void onPickupFromSlot(EntityPlayer player, ItemStack stack)
  44.             {
  45.                 FMLCommonHandler.instance()
  46.                     .firePlayerCraftingEvent(player, stack, craftingTable.getInventoryCrafting());
  47.                 onCrafting(stack);
  48.             }
  49.         });
  50.         addSlotToContainer(new SlotFurnaceFuel(craftingTable, 10, 93, 55));
  51.        
  52.         bindPlayerInventory(player.inventory);
  53.     }
  54.    
  55.     public TileEntityAutoCraftingTable getCraftingTable()
  56.     {
  57.         return craftingTable;
  58.     }
  59.    
  60.     private void bindPlayerInventory(InventoryPlayer invPlayer)
  61.     {
  62.         // main inventory
  63.         for(int j = 0; j < 3; j++)
  64.         {
  65.             for(int i = 0; i < 9; i++)
  66.             {
  67.                 addSlotToContainer(new Slot(invPlayer, i + j * 9 + 9, 8 + i * 18, 84 + j * 18));
  68.             }
  69.         }
  70.         // hotbar
  71.         for(int i = 0; i < 9; i++)
  72.         {
  73.             addSlotToContainer(new Slot(invPlayer, i, 8 + i * 18, 142));
  74.         }
  75.     }
  76.    
  77.     @Override
  78.     public boolean canInteractWith(EntityPlayer player)
  79.     {
  80.         return craftingTable.isUseableByPlayer(player);
  81.     }
  82.    
  83.     @Override
  84.     public ItemStack transferStackInSlot(EntityPlayer player, int index)
  85.     {
  86.         ItemStack itemstack = null;
  87.         Slot slot = (Slot) inventorySlots.get(index);
  88.        
  89.         if(slot != null && slot.getHasStack())
  90.         {
  91.             ItemStack itemstack1 = slot.getStack();
  92.             itemstack = itemstack1.copy();
  93.            
  94.             boolean flag = index >= craftingTable.getSizeInventory() && index < craftingTable.getSizeInventory() + 36;
  95.             if(index == 9) // result
  96.             {
  97.                 if(!mergeItemStack(itemstack1, craftingTable.getSizeInventory(), craftingTable.getSizeInventory() + 36,
  98.                     true)){ return null; }
  99.             }
  100.             else if(!flag) // craft matrix or fuel slot
  101.             {
  102.                 if(!this.mergeItemStack(itemstack1, craftingTable.getSizeInventory(),
  103.                     craftingTable.getSizeInventory() + 27, false)){ return null; }
  104.             }
  105.             else if(TileEntityFurnace.isItemFuel(itemstack1))
  106.             {
  107.                 if(mergeItemStack(itemstack1, 10, 11, false)) flag = false;
  108.             }
  109.             if(flag)
  110.             {
  111.                 if(index >= craftingTable.getSizeInventory() && index < craftingTable.getSizeInventory() + 27) // main inventory
  112.                 {
  113.                     if(!mergeItemStack(itemstack1, craftingTable.getSizeInventory() + 27,
  114.                         craftingTable.getSizeInventory() + 36, false)){ return null; }
  115.                 }
  116.                 else if(index >= craftingTable.getSizeInventory() + 27 && index < craftingTable.getSizeInventory() + 36) // hotbar
  117.                 {
  118.                     if(!this.mergeItemStack(itemstack1, craftingTable.getSizeInventory(),
  119.                         craftingTable.getSizeInventory() + 27, false)){ return null; }
  120.                 }
  121.             }
  122.             if(itemstack1.stackSize == 0)
  123.             {
  124.                 slot.putStack((ItemStack) null);
  125.             }
  126.             else
  127.             {
  128.                 slot.onSlotChanged();
  129.             }
  130.            
  131.             if(itemstack1.stackSize == itemstack.stackSize){ return null; }
  132.            
  133.             slot.onPickupFromSlot(player, itemstack1);
  134.         }
  135.        
  136.         return itemstack;
  137.     }
  138.    
  139.     @Override
  140.     public void addCraftingToCrafters(ICrafting listener)
  141.     {
  142.         super.addCraftingToCrafters(listener);
  143.         listener.func_175173_a(this, craftingTable);
  144.     }
  145.    
  146.     @Override
  147.     public void detectAndSendChanges()
  148.     {
  149.         super.detectAndSendChanges();
  150.         for(Object object : crafters)
  151.         {
  152.             ICrafting crafting = (ICrafting) object;
  153.             if(craftTime != craftingTable.getCraftTime())
  154.                 crafting.sendProgressBarUpdate(this, 0, craftingTable.getCraftTime());
  155.             if(burnTime != craftingTable.getBurnTime())
  156.                 crafting.sendProgressBarUpdate(this, 1, craftingTable.getBurnTime());
  157.             if(maxBurnTime != craftingTable.getMaxBurnTime())
  158.                 crafting.sendProgressBarUpdate(this, 2, craftingTable.getMaxBurnTime());
  159.         }
  160.         craftTime = craftingTable.getCraftTime();
  161.         burnTime = craftingTable.getBurnTime();
  162.         maxBurnTime = craftingTable.getMaxBurnTime();
  163.     }
  164.    
  165.     @Override
  166.     @SideOnly(Side.CLIENT)
  167.     public void updateProgressBar(int id, int newValue)
  168.     {
  169.         craftingTable.setField(id, newValue);
  170.     }
  171.    
  172. }
Advertisement
Add Comment
Please, Sign In to add comment