Advertisement
TechMage66

TileEntityPowerFurnace

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