TechMage66

TileEntityInfuser

Nov 21st, 2014
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.25 KB | None | 0 0
  1. package com.techmage.magetech.tileentity;
  2.  
  3. import com.techmage.magetech.init.ModItems;
  4. import com.techmage.magetech.item.crafting.RecipesInfuser;
  5. import com.techmage.magetech.reference.Names;
  6. import com.techmage.magetech.utility.LogHelper;
  7. import cpw.mods.fml.relauncher.Side;
  8. import cpw.mods.fml.relauncher.SideOnly;
  9. import net.minecraft.entity.player.EntityPlayer;
  10. import net.minecraft.inventory.IInventory;
  11. import net.minecraft.item.ItemStack;
  12. import net.minecraft.nbt.NBTTagCompound;
  13. import net.minecraft.nbt.NBTTagList;
  14.  
  15. public class TileEntityInfuser extends TileEntityMageTech implements IInventory
  16. {
  17.     public static final int INVENTORY_SIZE = 4;
  18.  
  19.     /**
  20.      * The ItemStacks that hold the items currently being used in the Glass Bell
  21.      */
  22.     private ItemStack[] inventory;
  23.  
  24.     int CurrentEssence = 1000;
  25.  
  26.     public TileEntityInfuser()
  27.     {
  28.         inventory = new ItemStack[INVENTORY_SIZE];
  29.     }
  30.  
  31.     public int InfusionTime;
  32.  
  33.     @Override
  34.     public int getSizeInventory()
  35.     {
  36.         return inventory.length;
  37.     }
  38.  
  39.     @Override
  40.     public ItemStack getStackInSlot(int slotIndex)
  41.     {
  42.         return inventory[slotIndex];
  43.     }
  44.  
  45.     @Override
  46.     public ItemStack decrStackSize(int slotIndex, int decrementAmount)
  47.     {
  48.         ItemStack itemStack = getStackInSlot(slotIndex);
  49.         if (itemStack != null)
  50.         {
  51.             if (itemStack.stackSize <= decrementAmount)
  52.             {
  53.                 setInventorySlotContents(slotIndex, null);
  54.             }
  55.             else
  56.             {
  57.                 itemStack = itemStack.splitStack(decrementAmount);
  58.                 if (itemStack.stackSize == 0)
  59.                 {
  60.                     setInventorySlotContents(slotIndex, null);
  61.                 }
  62.             }
  63.         }
  64.  
  65.         return itemStack;
  66.     }
  67.  
  68.     @Override
  69.     public ItemStack getStackInSlotOnClosing(int slotIndex)
  70.     {
  71.         ItemStack itemStack = getStackInSlot(slotIndex);
  72.         if (itemStack != null)
  73.         {
  74.             setInventorySlotContents(slotIndex, null);
  75.         }
  76.         return itemStack;
  77.     }
  78.  
  79.     @Override
  80.     public void setInventorySlotContents(int slotIndex, ItemStack itemStack)
  81.     {
  82.         inventory[slotIndex] = itemStack;
  83.         if (itemStack != null && itemStack.stackSize > getInventoryStackLimit())
  84.         {
  85.             itemStack.stackSize = getInventoryStackLimit();
  86.         }
  87.     }
  88.  
  89.     @Override
  90.     public String getInventoryName()
  91.     {
  92.         return this.hasCustomName() ? this.getCustomName() : Names.Containers.INFUSER;
  93.     }
  94.  
  95.     @Override
  96.     public boolean hasCustomInventoryName()
  97.     {
  98.         return this.hasCustomName();
  99.     }
  100.  
  101.     @Override
  102.     public int getInventoryStackLimit()
  103.     {
  104.         return 64;
  105.     }
  106.  
  107.     @Override
  108.     public boolean isUseableByPlayer(EntityPlayer entityplayer)
  109.     {
  110.         return true;
  111.     }
  112.  
  113.     @Override
  114.     public void openInventory()
  115.     {
  116.         // NOOP
  117.     }
  118.  
  119.     @Override
  120.     public void closeInventory()
  121.     {
  122.         // NOOP
  123.     }
  124.  
  125.     @Override
  126.     public boolean isItemValidForSlot(int slotIndex, ItemStack itemStack)
  127.     {
  128.         return false;
  129.     }
  130.  
  131.     @Override
  132.     public void writeToNBT(NBTTagCompound nbtTagCompound)
  133.     {
  134.         super.writeToNBT(nbtTagCompound);
  135.  
  136.         // Write the ItemStacks in the inventory to NBT
  137.         NBTTagList tagList = new NBTTagList();
  138.         for (int currentIndex = 0; currentIndex < inventory.length; ++currentIndex)
  139.         {
  140.             if (inventory[currentIndex] != null)
  141.             {
  142.                 NBTTagCompound tagCompound = new NBTTagCompound();
  143.                 tagCompound.setByte("Slot", (byte) currentIndex);
  144.                 inventory[currentIndex].writeToNBT(tagCompound);
  145.                 tagList.appendTag(tagCompound);
  146.             }
  147.         }
  148.         nbtTagCompound.setTag(Names.NBT.ITEMS, tagList);
  149.     }
  150.  
  151.     @Override
  152.     public void readFromNBT(NBTTagCompound nbtTagCompound)
  153.     {
  154.         super.readFromNBT(nbtTagCompound);
  155.  
  156.         // Read in the ItemStacks in the inventory from NBT
  157.         NBTTagList tagList = nbtTagCompound.getTagList(Names.NBT.ITEMS, 10);
  158.         inventory = new ItemStack[this.getSizeInventory()];
  159.         for (int i = 0; i < tagList.tagCount(); ++i)
  160.         {
  161.             NBTTagCompound tagCompound = tagList.getCompoundTagAt(i);
  162.             byte slotIndex = tagCompound.getByte("Slot");
  163.             if (slotIndex >= 0 && slotIndex < inventory.length)
  164.             {
  165.                 inventory[slotIndex] = ItemStack.loadItemStackFromNBT(tagCompound);
  166.             }
  167.         }
  168.     }
  169.  
  170.     @SideOnly(Side.CLIENT)
  171.     public int getEssenceScale(int var1) { return this.CurrentEssence * var1 / 1000; }
  172.  
  173.     @SideOnly(Side.CLIENT)
  174.     public int getInfusionProgressScale(int var1) { return this.InfusionTime * var1 / 500; }
  175.  
  176.     public boolean isInfusing()
  177.     {
  178.         if (this.InfusionTime > 0)
  179.         {
  180.             return true;
  181.         }
  182.  
  183.         return  false;
  184.     }
  185.  
  186.     @Override
  187.     public void updateEntity()
  188.     {
  189.  
  190.         if (canInfuse())
  191.         {
  192.             if (this.InfusionTime < 500)
  193.             {
  194.                 this.InfusionTime ++;
  195.             }
  196.             else
  197.             {
  198.                 infuseItem();
  199.                 this.InfusionTime = 0;
  200.             }
  201.         }
  202.         else
  203.         {
  204.             this.InfusionTime = 0;
  205.         }
  206.     }
  207.  
  208.     public boolean canInfuse()
  209.     {
  210.  
  211.         if (inventory[0] != null && inventory[1] != null && inventory[2] != null)
  212.         {
  213.  
  214.             ItemStack result = RecipesInfuser.infusing().getCraftingResult(inventory[0], inventory[1], inventory[2]);
  215.  
  216.             if (result != null)
  217.             {
  218.                 if (RecipesInfuser.infusing().getEssenceCost(inventory[0], inventory[1], inventory[2]) <= this.CurrentEssence)
  219.                 {
  220.                     if (inventory[3] == null)
  221.                     {
  222.                         return true;
  223.                     }
  224.                     else if (inventory[3] == result)
  225.                     {
  226.                         if (inventory[3].stackSize < 64)
  227.                         {
  228.                             return true;
  229.                         }
  230.                     }
  231.                 }
  232.             }
  233.         }
  234.  
  235.         return  false;
  236.  
  237.     }
  238.  
  239.     public void infuseItem()
  240.     {
  241.  
  242.         ItemStack result = RecipesInfuser.infusing().getCraftingResult(inventory[0], inventory[1], inventory[2]);
  243.  
  244.         this.CurrentEssence = this.CurrentEssence - RecipesInfuser.infusing().getEssenceCost(inventory[0], inventory[1], inventory[2]);
  245.  
  246.         if (inventory[3] == null)
  247.         {
  248.             inventory[3] = result;
  249.         }
  250.         else
  251.         {
  252.             inventory[3].stackSize ++;
  253.         }
  254.  
  255.         if (inventory[0].stackSize > 1)
  256.         {
  257.             inventory[0].stackSize --;
  258.         }
  259.         else
  260.         {
  261.             inventory[0] = null;
  262.         }
  263.  
  264.         if (inventory[1].stackSize > 1)
  265.         {
  266.             inventory[1].stackSize --;
  267.         }
  268.         else
  269.         {
  270.             inventory[1] = null;
  271.         }
  272.  
  273.         if (inventory[2].stackSize > 1)
  274.         {
  275.             inventory[2].stackSize --;
  276.         }
  277.         else
  278.         {
  279.             inventory[2] = null;
  280.         }
  281.     }
  282. }
Advertisement
Add Comment
Please, Sign In to add comment