Advertisement
TechMage66

TileEntityPowerFurnace

Dec 7th, 2014
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.48 KB | None | 0 0
  1. package com.techmage.magetech.tileentity;
  2.  
  3. import com.techmage.magetech.crafting.RecipesCrusher;
  4. import com.techmage.magetech.reference.Names;
  5. import com.techmage.magetech.utility.LogHelper;
  6. import cpw.mods.fml.relauncher.Side;
  7. import cpw.mods.fml.relauncher.SideOnly;
  8. import net.minecraft.entity.player.EntityPlayer;
  9. import net.minecraft.inventory.IInventory;
  10. import net.minecraft.inventory.ISidedInventory;
  11. import net.minecraft.item.ItemStack;
  12. import net.minecraft.item.crafting.FurnaceRecipes;
  13. import net.minecraft.nbt.NBTTagCompound;
  14. import net.minecraft.nbt.NBTTagList;
  15. import net.minecraft.network.NetworkManager;
  16. import net.minecraft.network.Packet;
  17. import net.minecraft.network.play.server.S35PacketUpdateTileEntity;
  18.  
  19. public class TileEntityPowerFurnace extends TileEntityMageTech implements ISidedInventory
  20. {
  21.     public static final int INVENTORY_SIZE = 4;
  22.  
  23.     private static final int[] slotsTop = new int[] {0, 2};
  24.     private static final int[] slotsBottom = new int[] {1, 3};
  25.     private static final int[] slotsSides = new int[] {0, 1, 2, 3};
  26.  
  27.     /**
  28.      * The ItemStacks that hold the items currently being used in the Glass Bell
  29.      */
  30.     private ItemStack[] inventory;
  31.  
  32.     public int CurrentPower = 1000;
  33.  
  34.     public TileEntityPowerFurnace()
  35.     {
  36.         inventory = new ItemStack[INVENTORY_SIZE];
  37.     }
  38.  
  39.     public int CookingTime1;
  40.     public int CookingTime2;
  41.  
  42.     public boolean inSeries;
  43.  
  44.     public int transferTime;
  45.  
  46.     @Override
  47.     public int getSizeInventory()
  48.     {
  49.         return inventory.length;
  50.     }
  51.  
  52.     @Override
  53.     public ItemStack getStackInSlot(int slotIndex)
  54.     {
  55.         return inventory[slotIndex];
  56.     }
  57.  
  58.     @Override
  59.     public ItemStack decrStackSize(int slotIndex, int decrementAmount)
  60.     {
  61.         ItemStack itemStack = getStackInSlot(slotIndex);
  62.         if (itemStack != null)
  63.         {
  64.             if (itemStack.stackSize <= decrementAmount)
  65.             {
  66.                 setInventorySlotContents(slotIndex, null);
  67.             }
  68.             else
  69.             {
  70.                 itemStack = itemStack.splitStack(decrementAmount);
  71.                 if (itemStack.stackSize == 0)
  72.                 {
  73.                     setInventorySlotContents(slotIndex, null);
  74.                 }
  75.             }
  76.         }
  77.  
  78.         return itemStack;
  79.     }
  80.  
  81.     @Override
  82.     public ItemStack getStackInSlotOnClosing(int slotIndex)
  83.     {
  84.         ItemStack itemStack = getStackInSlot(slotIndex);
  85.         if (itemStack != null)
  86.         {
  87.             setInventorySlotContents(slotIndex, null);
  88.         }
  89.         return itemStack;
  90.     }
  91.  
  92.     @Override
  93.     public void setInventorySlotContents(int slotIndex, ItemStack itemStack)
  94.     {
  95.         inventory[slotIndex] = itemStack;
  96.         if (itemStack != null && itemStack.stackSize > getInventoryStackLimit())
  97.         {
  98.             itemStack.stackSize = getInventoryStackLimit();
  99.         }
  100.     }
  101.  
  102.     @Override
  103.     public String getInventoryName()
  104.     {
  105.         return this.hasCustomName() ? this.getCustomName() : Names.Containers.POWERFURNACE;
  106.     }
  107.  
  108.     @Override
  109.     public boolean hasCustomInventoryName()
  110.     {
  111.         return this.hasCustomName();
  112.     }
  113.  
  114.     @Override
  115.     public int getInventoryStackLimit()
  116.     {
  117.         return 64;
  118.     }
  119.  
  120.     @Override
  121.     public boolean isUseableByPlayer(EntityPlayer entityplayer)
  122.     {
  123.         return true;
  124.     }
  125.  
  126.     @Override
  127.     public void openInventory()
  128.     {
  129.         // NOOP
  130.     }
  131.  
  132.     @Override
  133.     public void closeInventory()
  134.     {
  135.         // NOOP
  136.     }
  137.  
  138.     @Override
  139.     public void writeToNBT(NBTTagCompound nbtTagCompound)
  140.     {
  141.         super.writeToNBT(nbtTagCompound);
  142.         nbtTagCompound.setShort("CookingTime1", (short)this.CookingTime1);
  143.         nbtTagCompound.setShort("CookingTime2", (short)this.CookingTime2);
  144.         nbtTagCompound.setShort("CurrentPower", (short)this.CurrentPower);
  145.         nbtTagCompound.setBoolean("inSeries", this.inSeries);
  146.  
  147.         // Write the ItemStacks in the inventory to NBT
  148.         NBTTagList tagList = new NBTTagList();
  149.         for (int currentIndex = 0; currentIndex < inventory.length; ++currentIndex)
  150.         {
  151.             if (inventory[currentIndex] != null)
  152.             {
  153.                 NBTTagCompound tagCompound = new NBTTagCompound();
  154.                 tagCompound.setByte("Slot", (byte) currentIndex);
  155.                 inventory[currentIndex].writeToNBT(tagCompound);
  156.                 tagList.appendTag(tagCompound);
  157.             }
  158.         }
  159.         nbtTagCompound.setTag(Names.NBT.ITEMS, tagList);
  160.     }
  161.  
  162.     @Override
  163.     public void readFromNBT(NBTTagCompound nbtTagCompound)
  164.     {
  165.         super.readFromNBT(nbtTagCompound);
  166.  
  167.         // Read in the ItemStacks in the inventory from NBT
  168.         NBTTagList tagList = nbtTagCompound.getTagList(Names.NBT.ITEMS, 10);
  169.         inventory = new ItemStack[this.getSizeInventory()];
  170.         for (int i = 0; i < tagList.tagCount(); ++i)
  171.         {
  172.             NBTTagCompound tagCompound = tagList.getCompoundTagAt(i);
  173.             byte slotIndex = tagCompound.getByte("Slot");
  174.             if (slotIndex >= 0 && slotIndex < inventory.length)
  175.             {
  176.                 inventory[slotIndex] = ItemStack.loadItemStackFromNBT(tagCompound);
  177.             }
  178.             this.CookingTime1 = nbtTagCompound.getShort("CookingTime1");
  179.             this.CookingTime2 = nbtTagCompound.getShort("CookingTime2");
  180.             this.CurrentPower = nbtTagCompound.getShort("CurrentPower");
  181.             this.inSeries = nbtTagCompound.getBoolean("inSeries");
  182.         }
  183.     }
  184.  
  185.     @Override
  186.     public Packet getDescriptionPacket()
  187.     {
  188.         NBTTagCompound tagCompound = new NBTTagCompound();
  189.         writeToNBT(tagCompound);
  190.         return new S35PacketUpdateTileEntity(xCoord, yCoord, zCoord, 1, tagCompound);
  191.     }
  192.  
  193.  
  194.     @Override
  195.     public void onDataPacket(NetworkManager net, S35PacketUpdateTileEntity pkt) {
  196.         NBTTagCompound tag = pkt.func_148857_g();
  197.         readFromNBT(tag);
  198.     }
  199.  
  200.     @SideOnly(Side.CLIENT)
  201.     public int getPowerScale(int var1) { return this.CurrentPower * var1 / 1000; }
  202.  
  203.     @SideOnly(Side.CLIENT)
  204.     public int getCookingProgressScaled1(int var1) { return this.CookingTime1 * var1 / 300; }
  205.  
  206.     @SideOnly(Side.CLIENT)
  207.     public int getCookingProgressScaled2(int var1) { return this.CookingTime2 * var1 / 300; }
  208.  
  209.     public boolean isCooking1()
  210.     {
  211.         return this.CookingTime1 > 0;
  212.     }
  213.     public boolean isCooking2()
  214.     {
  215.         return this.CookingTime2 > 0;
  216.     }
  217.  
  218.     @Override
  219.     public void updateEntity()
  220.     {
  221.  
  222.         boolean doUpdate = false;
  223.  
  224.         if (!this.worldObj.isRemote)
  225.         {
  226.             if (canCook1())
  227.             {
  228.                 if (this.CookingTime1 < 300)
  229.                 {
  230.                     this.CookingTime1++;
  231.                 }
  232.                 else
  233.                 {
  234.                     cookItem1();
  235.                     this.CookingTime1 = 0;
  236.                     doUpdate = true;
  237.                 }
  238.             }
  239.             else
  240.             {
  241.                 this.CookingTime1 = 0;
  242.             }
  243.  
  244.             if (canCook2())
  245.             {
  246.                 if (this.CookingTime2 < 300)
  247.                 {
  248.                     this.CookingTime2++;
  249.                 }
  250.                 else
  251.                 {
  252.                     cookItem2();
  253.                     this.CookingTime2 = 0;
  254.                     doUpdate = true;
  255.                 }
  256.             }
  257.             else
  258.             {
  259.                 this.CookingTime2 = 0;
  260.             }
  261.  
  262.             if (this.inSeries)
  263.             {
  264.                 if (canTransferItem())
  265.                 {
  266.                     if (this.transferTime < 100)
  267.                     {
  268.                         this.transferTime++;
  269.                     }
  270.                     else
  271.                     {
  272.                         this.transferItem();
  273.                         this.transferTime = 0;
  274.                     }
  275.                 }
  276.             }
  277.         }
  278.  
  279.         if (doUpdate)
  280.         {
  281.             this.markDirty();
  282.         }
  283.     }
  284.  
  285.     public boolean canCook1()
  286.     {
  287.  
  288.         if (inventory[0] != null)
  289.         {
  290.  
  291.             ItemStack result = FurnaceRecipes.smelting().getSmeltingResult(inventory[0]);
  292.  
  293.             if (result != null)
  294.             {
  295.                 if (this.CurrentPower >= 50)
  296.                 {
  297.                     if (inventory[1] == null)
  298.                     {
  299.                         return true;
  300.                     }
  301.                     else if (inventory[1].isItemEqual(result))
  302.                     {
  303.                         if (inventory[1].stackSize < 64)
  304.                         {
  305.                             return true;
  306.                         }
  307.                     }
  308.                 }
  309.             }
  310.         }
  311.  
  312.         return  false;
  313.  
  314.     }
  315.  
  316.     public boolean canCook2()
  317.     {
  318.  
  319.         if (inventory[2] != null)
  320.         {
  321.  
  322.             ItemStack result = FurnaceRecipes.smelting().getSmeltingResult(inventory[2]);
  323.  
  324.             if (result != null)
  325.             {
  326.                 if (this.CurrentPower >= 50)
  327.                 {
  328.                     if (inventory[3] == null)
  329.                     {
  330.                         return true;
  331.                     }
  332.                     else if (inventory[3].isItemEqual(result))
  333.                     {
  334.                         if (inventory[3].stackSize < 64)
  335.                         {
  336.                             return true;
  337.                         }
  338.                     }
  339.                 }
  340.             }
  341.         }
  342.  
  343.         return  false;
  344.  
  345.     }
  346.  
  347.     public void cookItem1()
  348.     {
  349.  
  350.         ItemStack result = FurnaceRecipes.smelting().getSmeltingResult(inventory[0]);
  351.  
  352.         this.CurrentPower = this.CurrentPower - 50;
  353.  
  354.         if (inventory[1] == null)
  355.         {
  356.             inventory[1] = new ItemStack(result.getItem(), 1, result.getItemDamage());
  357.         }
  358.         else
  359.         {
  360.             inventory[1].stackSize ++;
  361.         }
  362.  
  363.         if (inventory[0].stackSize > 1)
  364.         {
  365.             inventory[0].stackSize --;
  366.         }
  367.         else
  368.         {
  369.             inventory[0] = null;
  370.         }
  371.     }
  372.  
  373.     public void cookItem2()
  374.     {
  375.  
  376.         ItemStack result = FurnaceRecipes.smelting().getSmeltingResult(inventory[2]);
  377.  
  378.         this.CurrentPower = this.CurrentPower - 50;
  379.  
  380.         if (inventory[3] == null)
  381.         {
  382.             inventory[3] = new ItemStack(result.getItem(), 1, result.getItemDamage());
  383.         }
  384.         else
  385.         {
  386.             inventory[3].stackSize ++;
  387.         }
  388.  
  389.         if (inventory[2].stackSize > 1)
  390.         {
  391.             inventory[2].stackSize --;
  392.         }
  393.         else
  394.         {
  395.             inventory[2] = null;
  396.         }
  397.     }
  398.  
  399.     public void setMode()
  400.     {
  401.         if (this.inSeries == true)
  402.         {
  403.             this.inSeries = false;
  404.         }
  405.         else
  406.         {
  407.             this.inSeries = true;
  408.         }
  409.     }
  410.  
  411.     public boolean canTransferItem()
  412.     {
  413.         if (inventory[1] != null)
  414.         {
  415.             if (inventory[2] == null)
  416.             {
  417.                 return true;
  418.             }
  419.  
  420.             if (inventory[2].isItemEqual(inventory[1]))
  421.             {
  422.                 if (inventory[2].stackSize < 64)
  423.                 {
  424.                     return true;
  425.                 }
  426.             }
  427.         }
  428.         return false;
  429.     }
  430.  
  431.     public void transferItem()
  432.     {
  433.         if (inventory[1] != null)
  434.         {
  435.             if (inventory[1].stackSize > 1)
  436.             {
  437.                 inventory[1].stackSize --;
  438.             }
  439.             else
  440.             {
  441.                 inventory[1] = null;
  442.             }
  443.         }
  444.  
  445.         if (inventory[2] == null)
  446.         {
  447.             inventory[2] = new ItemStack(inventory[1].getItem(), 1, inventory[1].getItemDamage());
  448.         }
  449.         else
  450.         {
  451.             inventory[2].stackSize++;
  452.         }
  453.     }
  454.  
  455.     public boolean isItemValidForSlot(int slotIndex, ItemStack itemStack)
  456.     {
  457.         return slotIndex == 1 ? false : slotIndex == 3 ? false : true;
  458.     }
  459.  
  460.     public int[] getAccessibleSlotsFromSide(int side)
  461.     {
  462.         return side == 0 ? slotsBottom : (side == 1 ? slotsTop : slotsSides);
  463.     }
  464.  
  465.     public boolean canInsertItem(int slot, ItemStack itemStack, int side)
  466.     {
  467.         return this.isItemValidForSlot(side, itemStack);
  468.     }
  469.     public boolean canExtractItem(int slot, ItemStack itemStack, int side)
  470.     {
  471.         return side != 0 && side != 1 ? slot == 0 || slot == 2 ? false : true : true;
  472.     }
  473. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement