Advertisement
dragonfreak1000

TileEntity3DPrinter

Jan 28th, 2015
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 13.44 KB | None | 0 0
  1. package net.codepixl.BitsAndBobs.TileEntity;
  2.  
  3. import net.codepixl.BitsAndBobs.Main;
  4. import net.codepixl.BitsAndBobs.Block.Block3DPrinter;
  5. import net.minecraft.block.Block;
  6. import net.minecraft.block.material.Material;
  7. import net.minecraft.entity.player.EntityPlayer;
  8. import net.minecraft.init.Blocks;
  9. import net.minecraft.init.Items;
  10. import net.minecraft.inventory.ISidedInventory;
  11. import net.minecraft.item.Item;
  12. import net.minecraft.item.ItemBlock;
  13. import net.minecraft.item.ItemHoe;
  14. import net.minecraft.item.ItemStack;
  15. import net.minecraft.item.ItemSword;
  16. import net.minecraft.item.ItemTool;
  17. import net.minecraft.item.crafting.FurnaceRecipes;
  18. import net.minecraft.nbt.NBTTagCompound;
  19. import net.minecraft.nbt.NBTTagList;
  20. import net.minecraft.tileentity.TileEntity;
  21. import cpw.mods.fml.common.registry.GameRegistry;
  22. import cpw.mods.fml.relauncher.Side;
  23. import cpw.mods.fml.relauncher.SideOnly;
  24.  
  25. public class TileEntity3DPrinter extends TileEntity implements ISidedInventory
  26. {
  27.     private int furnaceSpeed = 100;
  28.     private static final int[] slotsTop = new int[] {0};
  29.     private static final int[] slotsBottom = new int[] {2, 1};
  30.     private static final int[] slotsSides = new int[] {1};
  31.     /** The ItemStacks that hold the items currently being used in the furnace */
  32.     private ItemStack[] furnaceItemStacks = new ItemStack[3];
  33.     /** The number of ticks that the furnace will keep burning */
  34.     public int burnTime;
  35.     /** The number of ticks that a fresh copy of the currently-burning item would keep the furnace burning for */
  36.     public int currentBurnTime;
  37.     /** The number of ticks that the current item has been cooking for */
  38.     public int cookTime;
  39.     private String field_145958_o;
  40.     private static final String __OBFID = "CL_00000357";
  41.  
  42.     /**
  43.      * Returns the number of slots in the inventory.
  44.      */
  45.     public int getSizeInventory()
  46.     {
  47.         return this.furnaceItemStacks.length;
  48.     }
  49.  
  50.     /**
  51.      * Returns the stack in slot i
  52.      */
  53.     public ItemStack getStackInSlot(int p_70301_1_)
  54.     {
  55.         return this.furnaceItemStacks[p_70301_1_];
  56.     }
  57.  
  58.     /**
  59.      * Removes from an inventory slot (first arg) up to a specified number (second arg) of items and returns them in a
  60.      * new stack.
  61.      */
  62.     public ItemStack decrStackSize(int p_70298_1_, int p_70298_2_)
  63.     {
  64.         if (this.furnaceItemStacks[p_70298_1_] != null)
  65.         {
  66.             ItemStack itemstack;
  67.  
  68.             if (this.furnaceItemStacks[p_70298_1_].stackSize <= p_70298_2_)
  69.             {
  70.                 itemstack = this.furnaceItemStacks[p_70298_1_];
  71.                 this.furnaceItemStacks[p_70298_1_] = null;
  72.                 return itemstack;
  73.             }
  74.             else
  75.             {
  76.                 itemstack = this.furnaceItemStacks[p_70298_1_].splitStack(p_70298_2_);
  77.  
  78.                 if (this.furnaceItemStacks[p_70298_1_].stackSize == 0)
  79.                 {
  80.                     this.furnaceItemStacks[p_70298_1_] = null;
  81.                 }
  82.  
  83.                 return itemstack;
  84.             }
  85.         }
  86.         else
  87.         {
  88.             return null;
  89.         }
  90.     }
  91.  
  92.     /**
  93.      * When some containers are closed they call this on each slot, then drop whatever it returns as an EntityItem -
  94.      * like when you close a workbench GUI.
  95.      */
  96.     public ItemStack getStackInSlotOnClosing(int p_70304_1_)
  97.     {
  98.         if (this.furnaceItemStacks[p_70304_1_] != null)
  99.         {
  100.             ItemStack itemstack = this.furnaceItemStacks[p_70304_1_];
  101.             this.furnaceItemStacks[p_70304_1_] = null;
  102.             return itemstack;
  103.         }
  104.         else
  105.         {
  106.             return null;
  107.         }
  108.     }
  109.  
  110.     /**
  111.      * Sets the given item stack to the specified slot in the inventory (can be crafting or armor sections).
  112.      */
  113.     public void setInventorySlotContents(int p_70299_1_, ItemStack p_70299_2_)
  114.     {
  115.         this.furnaceItemStacks[p_70299_1_] = p_70299_2_;
  116.  
  117.         if (p_70299_2_ != null && p_70299_2_.stackSize > this.getInventoryStackLimit())
  118.         {
  119.             p_70299_2_.stackSize = this.getInventoryStackLimit();
  120.         }
  121.     }
  122.  
  123.     public void readFromNBT(NBTTagCompound p_145839_1_)
  124.     {
  125.         super.readFromNBT(p_145839_1_);
  126.         NBTTagList nbttaglist = p_145839_1_.getTagList("Items", 10);
  127.         this.furnaceItemStacks = new ItemStack[this.getSizeInventory()];
  128.  
  129.         for (int i = 0; i < nbttaglist.tagCount(); ++i)
  130.         {
  131.             NBTTagCompound nbttagcompound1 = nbttaglist.getCompoundTagAt(i);
  132.             byte b0 = nbttagcompound1.getByte("Slot");
  133.  
  134.             if (b0 >= 0 && b0 < this.furnaceItemStacks.length)
  135.             {
  136.                 this.furnaceItemStacks[b0] = ItemStack.loadItemStackFromNBT(nbttagcompound1);
  137.             }
  138.         }
  139.  
  140.         this.burnTime = p_145839_1_.getShort("BurnTime");
  141.         this.cookTime = p_145839_1_.getShort("CookTime");
  142.         this.currentBurnTime = getItemBurnTime(this.furnaceItemStacks[1]);
  143.  
  144.         if (p_145839_1_.hasKey("CustomName", 8))
  145.         {
  146.             this.field_145958_o = p_145839_1_.getString("CustomName");
  147.         }
  148.     }
  149.  
  150.     public void writeToNBT(NBTTagCompound p_145841_1_)
  151.     {
  152.         super.writeToNBT(p_145841_1_);
  153.         p_145841_1_.setShort("BurnTime", (short)this.burnTime);
  154.         p_145841_1_.setShort("CookTime", (short)this.cookTime);
  155.         NBTTagList nbttaglist = new NBTTagList();
  156.  
  157.         for (int i = 0; i < this.furnaceItemStacks.length; ++i)
  158.         {
  159.             if (this.furnaceItemStacks[i] != null)
  160.             {
  161.                 NBTTagCompound nbttagcompound1 = new NBTTagCompound();
  162.                 nbttagcompound1.setByte("Slot", (byte)i);
  163.                 this.furnaceItemStacks[i].writeToNBT(nbttagcompound1);
  164.                 nbttaglist.appendTag(nbttagcompound1);
  165.             }
  166.         }
  167.  
  168.         p_145841_1_.setTag("Items", nbttaglist);
  169.  
  170.         if (this.hasCustomInventoryName())
  171.         {
  172.             p_145841_1_.setString("CustomName", this.field_145958_o);
  173.         }
  174.     }
  175.  
  176.     /**
  177.      * Returns the maximum stack size for a inventory slot.
  178.      */
  179.     public int getInventoryStackLimit()
  180.     {
  181.         return 64;
  182.     }
  183.  
  184.     /**
  185.      * Returns an integer between 0 and the passed value representing how close the current item is to being completely
  186.      * cooked
  187.      */
  188.     @SideOnly(Side.CLIENT)
  189.     public int getCookProgressScaled(int p_145953_1_)
  190.     {
  191.         return this.cookTime * p_145953_1_ / this.furnaceSpeed;
  192.     }
  193.  
  194.     /**
  195.      * Returns an integer between 0 and the passed value representing how much burn time is left on the current fuel
  196.      * item, where 0 means that the item is exhausted and the passed value means that the item is fresh
  197.      */
  198.     @SideOnly(Side.CLIENT)
  199.     public int getBurnTimeRemainingScaled(int p_145955_1_)
  200.     {
  201.         if (this.currentBurnTime == 0)
  202.         {
  203.             this.currentBurnTime = this.furnaceSpeed;
  204.         }
  205.  
  206.         return this.burnTime * p_145955_1_ / this.currentBurnTime;
  207.     }
  208.  
  209.     /**
  210.      * Furnace isBurning
  211.      */
  212.     public boolean isBurning()
  213.     {
  214.         return this.burnTime > 0;
  215.     }
  216.  
  217.     public void updateEntity()
  218.     {
  219.         boolean flag = this.burnTime > 0;
  220.         boolean flag1 = false;
  221.  
  222.         if (this.burnTime > 0 && furnaceItemStacks[0] != null)
  223.         {
  224.             --this.burnTime;
  225.         }
  226.  
  227.         if (!this.worldObj.isRemote)
  228.         {
  229.             if (this.burnTime != 0 || this.furnaceItemStacks[1] != null && this.furnaceItemStacks[0] != null)
  230.             {
  231.                 if (this.burnTime == 0 && this.canSmelt())
  232.                 {
  233.                     this.currentBurnTime = this.burnTime = getItemBurnTime(this.furnaceItemStacks[1]);
  234.  
  235.                     if (this.burnTime > 0)
  236.                     {
  237.                         flag1 = true;
  238.  
  239.                         if (this.furnaceItemStacks[1] != null)
  240.                         {
  241.                             --this.furnaceItemStacks[1].stackSize;
  242.  
  243.                             if (this.furnaceItemStacks[1].stackSize == 0)
  244.                             {
  245.                                 this.furnaceItemStacks[1] = furnaceItemStacks[1].getItem().getContainerItem(furnaceItemStacks[1]);
  246.                             }
  247.                         }
  248.                     }
  249.                 }
  250.  
  251.                 if (this.isBurning() && this.canSmelt())
  252.                 {
  253.                     ++this.cookTime;
  254.  
  255.                     if (this.cookTime == this.furnaceSpeed)
  256.                     {
  257.                         this.cookTime = 0;
  258.                         this.smeltItem();
  259.                         flag1 = true;
  260.                     }
  261.                 }
  262.                 else
  263.                 {
  264.                     this.cookTime = 0;
  265.                 }
  266.             }
  267.  
  268.             if (flag != this.burnTime > 0)
  269.             {
  270.                 flag1 = true;
  271.                 Block3DPrinter.update3DPrinterState(this.burnTime > 0, this.worldObj, this.xCoord, this.yCoord, this.zCoord);
  272.             }
  273.         }
  274.  
  275.         if (flag1)
  276.         {
  277.             this.markDirty();
  278.         }
  279.     }
  280.  
  281.     /**
  282.      * Returns true if the furnace can smelt an item, i.e. has a source item, destination stack isn't full, etc.
  283.      */
  284.     private boolean canSmelt()
  285.     {
  286.         if (this.furnaceItemStacks[0] == null)
  287.         {
  288.             return false;
  289.         }
  290.         else
  291.         {
  292.             ItemStack itemstack = p3DRecipies.smelting().getSmeltingResult(this.furnaceItemStacks[0]);
  293.             if (itemstack == null) return false;
  294.             if (this.furnaceItemStacks[2] == null) return true;
  295.             if (!this.furnaceItemStacks[2].isItemEqual(itemstack)) return false;
  296.             int result = furnaceItemStacks[2].stackSize + itemstack.stackSize;
  297.             return result <= getInventoryStackLimit() && result <= this.furnaceItemStacks[2].getMaxStackSize(); //Forge BugFix: Make it respect stack sizes properly.
  298.         }
  299.     }
  300.  
  301.     /**
  302.      * Turn one item from the furnace source stack into the appropriate smelted item in the furnace result stack
  303.      */
  304.     public void smeltItem()
  305.     {
  306.         if (this.canSmelt())
  307.         {
  308.             ItemStack itemstack = p3DRecipies.smelting().getSmeltingResult(this.furnaceItemStacks[0]);
  309.             if (this.furnaceItemStacks[2] == null)
  310.             {
  311.                 this.furnaceItemStacks[2] = itemstack.copy();
  312.             }
  313.             else if (this.furnaceItemStacks[2].getItem() == itemstack.getItem())
  314.             {
  315.                 this.furnaceItemStacks[2].stackSize += itemstack.stackSize; // Forge BugFix: Results may have multiple items
  316.             }
  317.  
  318.             --this.furnaceItemStacks[0].stackSize;
  319.  
  320.             if (this.furnaceItemStacks[0].stackSize <= 0)
  321.             {
  322.                 this.furnaceItemStacks[0] = null;
  323.             }
  324.         }
  325.     }
  326.  
  327.     /**
  328.      * Returns the number of ticks that the supplied fuel item will keep the furnace burning, or 0 if the item isn't
  329.      * fuel
  330.      */
  331.     public static int getItemBurnTime(ItemStack is)
  332.     {
  333.         if(is != null){
  334.             if(is.getItem() == Main.Plastic){
  335.                 return 100;
  336.             }
  337.             return 0;
  338.         }else{
  339.             return 0;
  340.         }
  341.     }
  342.  
  343.     public static boolean isItemFuel(ItemStack p_145954_0_)
  344.     {
  345.         /**
  346.          * Returns the number of ticks that the supplied fuel item will keep the furnace burning, or 0 if the item isn't
  347.          * fuel
  348.          */
  349.         return getItemBurnTime(p_145954_0_) > 0;
  350.     }
  351.  
  352.     /**
  353.      * Do not make give this method the name canInteractWith because it clashes with Container
  354.      */
  355.     public boolean isUseableByPlayer(EntityPlayer p_70300_1_)
  356.     {
  357.         return this.worldObj.getTileEntity(this.xCoord, this.yCoord, this.zCoord) != this ? false : p_70300_1_.getDistanceSq((double)this.xCoord + 0.5D, (double)this.yCoord + 0.5D, (double)this.zCoord + 0.5D) <= 64.0D;
  358.     }
  359.  
  360.     public void openInventory() {}
  361.  
  362.     public void closeInventory() {}
  363.  
  364.     /**
  365.      * Returns true if automation is allowed to insert the given stack (ignoring stack size) into the given slot.
  366.      */
  367.     public boolean isItemValidForSlot(int p_94041_1_, ItemStack p_94041_2_)
  368.     {
  369.         return p_94041_1_ == 2 ? false : (p_94041_1_ == 1 ? isItemFuel(p_94041_2_) : true);
  370.     }
  371.  
  372.     /**
  373.      * Returns an array containing the indices of the slots that can be accessed by automation on the given side of this
  374.      * block.
  375.      */
  376.     public int[] getAccessibleSlotsFromSide(int p_94128_1_)
  377.     {
  378.         return p_94128_1_ == 0 ? slotsBottom : (p_94128_1_ == 1 ? slotsTop : slotsSides);
  379.     }
  380.  
  381.     /**
  382.      * Returns true if automation can insert the given item in the given slot from the given side. Args: Slot, item,
  383.      * side
  384.      */
  385.     public boolean canInsertItem(int p_102007_1_, ItemStack p_102007_2_, int p_102007_3_)
  386.     {
  387.         return this.isItemValidForSlot(p_102007_1_, p_102007_2_);
  388.     }
  389.  
  390.     /**
  391.      * Returns true if automation can extract the given item in the given slot from the given side. Args: Slot, item,
  392.      * side
  393.      */
  394.     public boolean canExtractItem(int p_102008_1_, ItemStack p_102008_2_, int p_102008_3_)
  395.     {
  396.         return p_102008_3_ != 0 || p_102008_1_ != 1 || p_102008_2_.getItem() == Items.bucket;
  397.     }
  398.  
  399.     @Override
  400.     public String getInventoryName() {
  401.         // TODO Auto-generated method stub
  402.         return null;
  403.     }
  404.  
  405.     @Override
  406.     public boolean hasCustomInventoryName() {
  407.         // TODO Auto-generated method stub
  408.         return false;
  409.     }
  410. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement