Advertisement
Guest User

TileEntityCulinary

a guest
Sep 21st, 2012
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.56 KB | None | 0 0
  1. package KBI.agricrafture.api;
  2.  
  3. import KBI.agricrafture.core.*;
  4. import net.minecraft.src.*;
  5. import java.util.LinkedList;
  6. import java.util.List;
  7. import java.util.Iterator;
  8.  
  9. import net.minecraftforge.common.ISidedInventory;
  10. import net.minecraftforge.common.ForgeDirection;
  11. import cpw.mods.fml.common.Side;
  12. import cpw.mods.fml.common.asm.SideOnly;
  13.  
  14. public class TileEntityCulinary extends TileEntity implements IInventory, ISidedInventory
  15. {
  16.  
  17.         private ItemStack[] inv;
  18.        
  19.         public int workTime = 0;
  20.        
  21.         public int recipeClicks = 1;
  22.  
  23.         public TileEntityCulinary()
  24.         {
  25.                 inv = new ItemStack[11];
  26.         }
  27.        
  28.         public String getInvName()
  29.         {
  30.             return "tile.blockCulinary.name";
  31.         }
  32.        
  33.         @Override
  34.         public int getSizeInventory()
  35.         {
  36.                 return inv.length;
  37.         }
  38.  
  39.         @Override
  40.         public ItemStack getStackInSlot(int slot)
  41.         {
  42.                 return inv[slot];
  43.         }
  44.        
  45.         @Override
  46.         public void setInventorySlotContents(int slot, ItemStack stack)
  47.         {
  48.                 inv[slot] = stack;
  49.                 if (stack != null && stack.stackSize > getInventoryStackLimit()) {
  50.                         stack.stackSize = getInventoryStackLimit();
  51.                 }              
  52.         }
  53.  
  54.         @Override
  55.         public ItemStack decrStackSize(int slot, int amt)
  56.         {
  57.                 ItemStack stack = getStackInSlot(slot);
  58.                 if (stack != null) {
  59.                         if (stack.stackSize <= amt) {
  60.                                 setInventorySlotContents(slot, null);
  61.                         } else {
  62.                                 stack = stack.splitStack(amt);
  63.                                 if (stack.stackSize == 0) {
  64.                                         setInventorySlotContents(slot, null);
  65.                                 }
  66.                         }
  67.                 }
  68.                 return stack;
  69.         }
  70.  
  71.         @Override
  72.         public ItemStack getStackInSlotOnClosing(int slot)
  73.         {
  74.                 ItemStack stack = getStackInSlot(slot);
  75.                 if (stack != null) {
  76.                         setInventorySlotContents(slot, null);
  77.                 }
  78.                 return stack;
  79.         }
  80.        
  81.         @Override
  82.         public int getInventoryStackLimit()
  83.         {
  84.                 return 64;
  85.         }
  86.  
  87.         @Override
  88.         public boolean isUseableByPlayer(EntityPlayer player)
  89.         {
  90.                 return worldObj.getBlockTileEntity(xCoord, yCoord, zCoord) == this &&
  91.                 player.getDistanceSq(xCoord + 0.5, yCoord + 0.5, zCoord + 0.5) < 64;
  92.         }
  93.        
  94.         @Override
  95.         public int getStartInventorySide(ForgeDirection side)
  96.         {
  97.             if (side == ForgeDirection.EAST) return 0;
  98.             if (side == ForgeDirection.UP) return 6;
  99.             if (side == ForgeDirection.DOWN) return 7;
  100.             return 8;
  101.         }
  102.  
  103.         @Override
  104.         public int getSizeInventorySide(ForgeDirection side)
  105.         {
  106.             if (side == ForgeDirection.EAST) return 6;
  107.             if (side == ForgeDirection.DOWN) return 1;
  108.             if (side == ForgeDirection.UP) return 1;
  109.             return 3;
  110.         }
  111.  
  112.         @Override
  113.         public void openChest() {}
  114.  
  115.         @Override
  116.         public void closeChest() {}
  117.        
  118.         @Override
  119.         public void readFromNBT(NBTTagCompound tagCompound) {
  120.                 super.readFromNBT(tagCompound);
  121.                
  122.                 NBTTagList tagList = tagCompound.getTagList("Inventory");
  123.                 for (int i = 0; i < tagList.tagCount(); i++) {
  124.                         NBTTagCompound tag = (NBTTagCompound) tagList.tagAt(i);
  125.                         byte slot = tag.getByte("Slot");
  126.                         if (slot >= 0 && slot < inv.length) {
  127.                                 inv[slot] = ItemStack.loadItemStackFromNBT(tag);
  128.                         }
  129.                 }
  130.                
  131.                 this.workTime = tagCompound.getShort("ClicksLeft");
  132.         }
  133.  
  134.         @Override
  135.         public void writeToNBT(NBTTagCompound tagCompound)
  136.         {
  137.                 tagCompound.setShort("ClicksLeft", (short)this.workTime);
  138.                
  139.                 NBTTagList itemList = new NBTTagList();
  140.                 for (int i = 0; i < inv.length; i++) {
  141.                         ItemStack stack = inv[i];
  142.                         if (stack != null) {
  143.                                 NBTTagCompound tag = new NBTTagCompound();
  144.                                 tag.setByte("Slot", (byte) i);
  145.                                 stack.writeToNBT(tag);
  146.                                 itemList.appendTag(tag);
  147.                         }
  148.                 }
  149.                 tagCompound.setTag("Inventory", itemList);
  150.                
  151.                 super.writeToNBT(tagCompound);
  152.         }
  153.        
  154.         @SideOnly(Side.CLIENT)
  155.  
  156.         /**
  157.          * Returns an integer between 0 and the passed value representing how close the current item is to being completely
  158.          * done
  159.          */
  160.         public int getWorkProgressScaled(int par1)
  161.         {
  162.             return this.workTime * par1 / this.recipeClicks;
  163.         }
  164.        
  165.         public boolean isWorking()
  166.         {
  167.             return this.workTime > 0;
  168.         }
  169.        
  170.         /**
  171.          * Check to see if the current recipe can be completed
  172.          *
  173.          */
  174.         private boolean canWork()
  175.         {
  176.             //makes sure input is not empty
  177.             if (this.inv[0] == null && this.inv[1] == null && this.inv[2] == null && this.inv[3] == null && this.inv[4] == null && this.inv[5] == null)
  178.             {
  179.                 return false;
  180.             }
  181.            
  182.             //makes sure there is a tool in the slot; if not, then it would be a crafting recipe instead of culinary.
  183.             //There is no check on the dish slot as not all recipes may require them.
  184.             if (this.inv[6] == null)
  185.             {
  186.                 return false;
  187.             }
  188.            
  189.             LinkedList recipes = CulinaryRecipe.recipes;
  190.             Iterator iterate = recipes.iterator();
  191.             boolean hasfound = false;
  192.             LinkedList<ItemStack> fail = new LinkedList<ItemStack>();
  193.             fail.add(new ItemStack(0, 0, 0));
  194.             CulinaryRecipe recipematch = new CulinaryRecipe(fail, new ItemStack(0, 0, 0), new ItemStack(0, 0, 0));
  195.  
  196.             while (iterate.hasNext())
  197.             {
  198.                 CulinaryRecipe thisrecipe = (CulinaryRecipe)iterate.next();
  199.                 if (thisrecipe.matches(this))
  200.                 {
  201.                     hasfound = true;
  202.                     recipematch = thisrecipe;
  203.                 }
  204.             }
  205.            
  206.             if (hasfound == false)
  207.             {
  208.                 return false;
  209.             }
  210.            
  211.             if (this.inv[8] != null && this.inv[8].itemID != recipematch.result.itemID)
  212.             {
  213.                 return false;
  214.             }
  215.            
  216.             if (this.inv[8] != null && this.inv[8].stackSize >= recipematch.result.getMaxStackSize())
  217.             {
  218.                 return false;
  219.             }
  220.            
  221.             if (recipematch.output1 != null)
  222.             {
  223.                 if (recipematch.output2 != null)
  224.                 {
  225.                     if (this.inv[9] != null && this.inv[9].itemID != recipematch.output1.itemID && this.inv[9].itemID != recipematch.output2.itemID)
  226.                     {
  227.                         return false;
  228.                     }
  229.                    
  230.                     if (this.inv[9] != null && this.inv[9].itemID == recipematch.output1.itemID && this.inv[9].stackSize >= recipematch.output1.getMaxStackSize())
  231.                     {
  232.                         return false;
  233.                     }
  234.                    
  235.                     if (this.inv[9] != null && this.inv[9].itemID == recipematch.output2.itemID && this.inv[9].stackSize >= recipematch.output2.getMaxStackSize())
  236.                     {
  237.                         return false;
  238.                     }
  239.                    
  240.                     if (this.inv[10] != null && this.inv[10].itemID != recipematch.output1.itemID && this.inv[10].itemID != recipematch.output2.itemID)
  241.                     {
  242.                         return false;
  243.                     }
  244.                    
  245.                     if (this.inv[10] != null && this.inv[10].itemID == recipematch.output1.itemID && this.inv[10].stackSize >= recipematch.output1.getMaxStackSize())
  246.                     {
  247.                         return false;
  248.                     }
  249.                    
  250.                     if (this.inv[10] != null && this.inv[10].itemID == recipematch.output2.itemID && this.inv[10].stackSize >= recipematch.output2.getMaxStackSize())
  251.                     {
  252.                         return false;
  253.                     }
  254.                 }
  255.                
  256.                 if (this.inv[9] != null && this.inv[9].itemID != recipematch.output1.itemID)
  257.                 {
  258.                     return false;
  259.                 }
  260.                
  261.                 if (this.inv[9] != null && this.inv[9].itemID == recipematch.output1.itemID && this.inv[9].stackSize >= recipematch.output1.getMaxStackSize())
  262.                 {
  263.                     return false;
  264.                 }
  265.                
  266.                 if (this.inv[10] != null && this.inv[10].itemID != recipematch.output1.itemID)
  267.                 {
  268.                     return false;
  269.                 }
  270.                
  271.                 if (this.inv[10] != null && this.inv[10].itemID == recipematch.output1.itemID && this.inv[10].stackSize >= recipematch.output1.getMaxStackSize())
  272.                 {
  273.                     return false;
  274.                 }
  275.             }
  276.            
  277.             return true;
  278.         }
  279.        
  280.         public void workItem()
  281.         {
  282.             if(this.canWork())
  283.             {
  284.                 LinkedList recipes = CulinaryRecipe.recipes;
  285.                 Iterator iterate = recipes.iterator();
  286.                 boolean hasfound = false;
  287.                 LinkedList<ItemStack> fail = new LinkedList<ItemStack>();
  288.                 fail.add(new ItemStack(0, 0, 0));
  289.                 CulinaryRecipe recipematch = new CulinaryRecipe(fail, new ItemStack(0, 0, 0), new ItemStack(0, 0, 0));
  290.  
  291.                 while (iterate.hasNext() && hasfound == false)
  292.                 {
  293.                     CulinaryRecipe thisrecipe = (CulinaryRecipe)iterate.next();
  294.                     if (thisrecipe.matches(this))
  295.                     {
  296.                         hasfound = true;
  297.                         recipematch = thisrecipe;
  298.                     }
  299.                 }
  300.                
  301.                 //we know that the recipematch exists because we used the same code in canWork.
  302.                
  303.                 if(!isWorking())
  304.                 {
  305.                     this.recipeClicks = recipematch.clicks;
  306.                 }
  307.                
  308.                 this.workTime = this.workTime + 1;
  309.                
  310.                 if(this.workTime >= this.recipeClicks)
  311.                 {
  312.                     if(this.inv[8] == null)
  313.                     {
  314.                         this.inv[8] = recipematch.result.copy();
  315.                     }
  316.                     else if (this.inv[8].isItemEqual(recipematch.result))
  317.                     {
  318.                         this.inv[8].stackSize += recipematch.result.stackSize;
  319.                     }
  320.                    
  321.                     if(recipematch.output1 != null)
  322.                     {
  323.                         //if top stack matches
  324.                         if (this.inv[9].isItemEqual(recipematch.output1))
  325.                         {
  326.                             this.inv[9].stackSize += recipematch.output1.stackSize;
  327.                         }
  328.                         //if bottom stack matches
  329.                         else if (this.inv[10].isItemEqual(recipematch.output1))
  330.                         {
  331.                             this.inv[10].stackSize += recipematch.output1.stackSize;
  332.                         }
  333.                         //if neither stack matches but top is empty
  334.                         else if (this.inv[9] == null)
  335.                         {
  336.                             this.inv[9] = recipematch.output1.copy();
  337.                         }
  338.                         //if neither stack matches and top is full but bottom is empty
  339.                         else if (this.inv[10] == null)
  340.                         {
  341.                             this.inv[10] = recipematch.output1.copy();
  342.                         }
  343.                         //the recipe will not work if both stacks are not empty and don't match, so no code is needed here.
  344.                     }
  345.                    
  346.                     if(recipematch.output2 != null)
  347.                     {
  348.                         //if top stack matches
  349.                         if (this.inv[9].isItemEqual(recipematch.output2))
  350.                         {
  351.                             this.inv[9].stackSize += recipematch.output2.stackSize;
  352.                         }
  353.                         //if bottom stack matches
  354.                         else if (this.inv[10].isItemEqual(recipematch.output2))
  355.                         {
  356.                             this.inv[10].stackSize += recipematch.output2.stackSize;
  357.                         }
  358.                         //if neither stack matches but top is empty
  359.                         else if (this.inv[9] == null)
  360.                         {
  361.                             this.inv[9] = recipematch.output2.copy();
  362.                         }
  363.                         //if neither stack matches and top is full but bottom is empty
  364.                         else if (this.inv[10] == null)
  365.                         {
  366.                             this.inv[10] = recipematch.output2.copy();
  367.                         }
  368.                         //the recipe will not work if both stacks are not empty and don't match, so no code is needed here.
  369.                     }
  370.                    
  371.                     for (int q = 0; q < 6; q++)
  372.                     {
  373.                         if(this.inv[q] != null)
  374.                         {
  375.                             --this.inv[q].stackSize;
  376.                             if (this.inv[q].stackSize <= 0)
  377.                             {
  378.                                 this.inv[q] = null;
  379.                             }
  380.                         }
  381.                     }
  382.                    
  383.                     //If tools are going to take damage, put that code here. But for now, they won't.
  384.                    
  385.                     if(this.inv[7] != null)
  386.                     {
  387.                         --this.inv[7].stackSize;
  388.                         if(this.inv[7].stackSize <= 0)
  389.                         {
  390.                             this.inv[7] = null;
  391.                         }
  392.                     }
  393.                 }
  394.             }
  395.         }
  396. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement