Guest User

TileEntityGemcutter

a guest
May 7th, 2013
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 16.38 KB | None | 0 0
  1. package Mod_Ores.Blocks.Gemcutter;
  2.  
  3. import Mod_Ores.mod_Ores;
  4. import cpw.mods.fml.common.registry.GameRegistry;
  5. import cpw.mods.fml.relauncher.Side;
  6. import cpw.mods.fml.relauncher.SideOnly;
  7. import net.minecraft.block.Block;
  8. import net.minecraft.block.BlockFurnace;
  9. import net.minecraft.block.material.Material;
  10. import net.minecraft.entity.player.EntityPlayer;
  11. import net.minecraft.inventory.ISidedInventory;
  12. import net.minecraft.item.Item;
  13. import net.minecraft.item.ItemBlock;
  14. import net.minecraft.item.ItemHoe;
  15. import net.minecraft.item.ItemStack;
  16. import net.minecraft.item.ItemSword;
  17. import net.minecraft.item.ItemTool;
  18. import net.minecraft.item.crafting.FurnaceRecipes;
  19. import net.minecraft.nbt.NBTTagCompound;
  20. import net.minecraft.nbt.NBTTagList;
  21. import net.minecraft.tileentity.TileEntity;
  22. import net.minecraftforge.common.ForgeDirection;
  23. import net.minecraftforge.common.ForgeDummyContainer;
  24.  
  25. public class TileEntityGemcutter extends TileEntity implements net.minecraftforge.common.ISidedInventory
  26. {
  27.     private static final int[] field_102010_d = new int[] {0};
  28.     private static final int[] field_102011_e = new int[] {2, 1};
  29.     private static final int[] field_102009_f = new int[] {1};
  30.  
  31.     /**
  32.      * The ItemStacks that hold the items currently being used in the furnace
  33.      */
  34.     private ItemStack[] gemcutterItemStacks = new ItemStack[4];
  35.  
  36.     /** The number of ticks that the furnace will keep burning */
  37.     public int gemcutterBurnTime = 0;
  38.     /**
  39.      * The number of ticks that a fresh copy of the currently-burning item would keep the furnace burning for
  40.      */
  41.     public int currentGemItemBurnTime = 0;
  42.  
  43.     /** The number of ticks that the current item has been cooking for */
  44.     public int gemcutterCookTime = 0;
  45.     public int gemcutterCookTime2 = 0;
  46.     private String field_94130_e;
  47.  
  48.     /**
  49.      * Returns the number of slots in the inventory.
  50.      */
  51.     @Override
  52.     public int getSizeInventory()
  53.     {
  54.         return this.gemcutterItemStacks.length;
  55.     }
  56.  
  57.     /**
  58.      * Returns the stack in slot i
  59.      */
  60.     @Override
  61.     public ItemStack getStackInSlot(int par1)
  62.     {
  63.         return this.gemcutterItemStacks[par1];
  64.     }
  65.  
  66.     /**
  67.      * Removes from an inventory slot (first arg) up to a specified number (second arg) of items and returns them in a
  68.      * new stack.
  69.      */
  70.     @Override
  71.     public ItemStack decrStackSize(int par1, int par2)
  72.     {
  73.         if (this.gemcutterItemStacks[par1] != null)
  74.         {
  75.             ItemStack itemstack;
  76.  
  77.             if (this.gemcutterItemStacks[par1].stackSize <= par2)
  78.             {
  79.                 itemstack = this.gemcutterItemStacks[par1];
  80.                 this.gemcutterItemStacks[par1] = null;
  81.                 return itemstack;
  82.             }
  83.             else
  84.             {
  85.                 itemstack = this.gemcutterItemStacks[par1].splitStack(par2);
  86.  
  87.                 if (this.gemcutterItemStacks[par1].stackSize == 0)
  88.                 {
  89.                     this.gemcutterItemStacks[par1] = null;
  90.                 }
  91.  
  92.                 return itemstack;
  93.             }
  94.         }
  95.         else
  96.         {
  97.             return null;
  98.         }
  99.     }
  100.  
  101.     /**
  102.      * When some containers are closed they call this on each slot, then drop whatever it returns as an EntityItem -
  103.      * like when you close a workbench GUI.
  104.      */
  105.     @Override
  106.     public ItemStack getStackInSlotOnClosing(int par1)
  107.     {
  108.         if (this.gemcutterItemStacks[par1] != null)
  109.         {
  110.             ItemStack itemstack = this.gemcutterItemStacks[par1];
  111.             this.gemcutterItemStacks[par1] = null;
  112.             return itemstack;
  113.         }
  114.         else
  115.         {
  116.             return null;
  117.         }
  118.     }
  119.  
  120.     /**
  121.      * Sets the given item stack to the specified slot in the inventory (can be crafting or armor sections).
  122.      */
  123.     @Override
  124.     public void setInventorySlotContents(int par1, ItemStack par2ItemStack)
  125.     {
  126.         this.gemcutterItemStacks[par1] = par2ItemStack;
  127.  
  128.         if (par2ItemStack != null && par2ItemStack.stackSize > this.getInventoryStackLimit())
  129.         {
  130.             par2ItemStack.stackSize = this.getInventoryStackLimit();
  131.         }
  132.     }
  133.  
  134.     /**
  135.      * Returns the name of the inventory.
  136.      */
  137.     public String getInvName()
  138.     {
  139.         return this.isInvNameLocalized() ? this.field_94130_e : "container.gemcutter";
  140.     }
  141.  
  142.     /**
  143.      * If this returns false, the inventory name will be used as an unlocalized name, and translated into the player's
  144.      * language. Otherwise it will be used directly.
  145.      */
  146.     public boolean isInvNameLocalized()
  147.     {
  148.         return this.field_94130_e != null && this.field_94130_e.length() > 0;
  149.     }
  150.  
  151.     public void func_94129_a(String par1Str)
  152.     {
  153.         this.field_94130_e = par1Str;
  154.     }
  155.  
  156.     /**
  157.      * Reads a tile entity from NBT.
  158.      */
  159.     @Override
  160.     public void readFromNBT(NBTTagCompound par1NBTTagCompound)
  161.     {
  162.         super.readFromNBT(par1NBTTagCompound);
  163.         NBTTagList nbttaglist = par1NBTTagCompound.getTagList("Items");
  164.         this.gemcutterItemStacks = new ItemStack[this.getSizeInventory()];
  165.  
  166.         for (int i = 0; i < nbttaglist.tagCount(); ++i)
  167.         {
  168.             NBTTagCompound nbttagcompound1 = (NBTTagCompound)nbttaglist.tagAt(i);
  169.             byte b0 = nbttagcompound1.getByte("Slot");
  170.  
  171.             if (b0 >= 0 && b0 < this.gemcutterItemStacks.length)
  172.             {
  173.                 this.gemcutterItemStacks[b0] = ItemStack.loadItemStackFromNBT(nbttagcompound1);
  174.             }
  175.         }
  176.  
  177.         this.gemcutterBurnTime = par1NBTTagCompound.getShort("BurnTime");
  178.         this.gemcutterCookTime = par1NBTTagCompound.getShort("CookTime");
  179.         this.currentGemItemBurnTime = getItemBurnTime(this.gemcutterItemStacks[1]);
  180.  
  181.         if (par1NBTTagCompound.hasKey("CustomName"))
  182.         {
  183.             this.field_94130_e = par1NBTTagCompound.getString("CustomName");
  184.         }
  185.     }
  186.  
  187.     /**
  188.      * Writes a tile entity to NBT.
  189.      */
  190.     @Override
  191.     public void writeToNBT(NBTTagCompound par1NBTTagCompound)
  192.     {
  193.         super.writeToNBT(par1NBTTagCompound);
  194.         par1NBTTagCompound.setShort("BurnTime", (short)this.gemcutterBurnTime);
  195.         par1NBTTagCompound.setShort("CookTime", (short)this.gemcutterCookTime);
  196.         NBTTagList nbttaglist = new NBTTagList();
  197.  
  198.         for (int i = 0; i < this.gemcutterItemStacks.length; ++i)
  199.         {
  200.             if (this.gemcutterItemStacks[i] != null)
  201.             {
  202.                 NBTTagCompound nbttagcompound1 = new NBTTagCompound();
  203.                 nbttagcompound1.setByte("Slot", (byte)i);
  204.                 this.gemcutterItemStacks[i].writeToNBT(nbttagcompound1);
  205.                 nbttaglist.appendTag(nbttagcompound1);
  206.             }
  207.         }
  208.  
  209.         par1NBTTagCompound.setTag("Items", nbttaglist);
  210.  
  211.         if (this.isInvNameLocalized())
  212.         {
  213.             par1NBTTagCompound.setString("CustomName", this.field_94130_e);
  214.         }
  215.     }
  216.  
  217.     /**
  218.      * Returns the maximum stack size for a inventory slot. Seems to always be 64, possibly will be extended. *Isn't
  219.      * this more of a set than a get?*
  220.      */
  221.     public int getInventoryStackLimit()
  222.     {
  223.         return 64;
  224.     }
  225.  
  226.     @SideOnly(Side.CLIENT)
  227.  
  228.     /**
  229.      * Returns an integer between 0 and the passed value representing how close the current item is to being completely
  230.      * cooked
  231.      */
  232.     public int getCookProgressScaled(int par1)
  233.     {
  234.         return this.gemcutterCookTime * par1 / 200;
  235.     }
  236.  
  237.     @SideOnly(Side.CLIENT)
  238.  
  239.     /**
  240.      * Returns an integer between 0 and the passed value representing how much burn time is left on the current fuel
  241.      * item, where 0 means that the item is exhausted and the passed value means that the item is fresh
  242.      */
  243.     public int getBurnTimeRemainingScaled(int par1)
  244.     {
  245.         if (this.currentGemItemBurnTime == 0)
  246.         {
  247.             this.currentGemItemBurnTime = 200;
  248.         }
  249.  
  250.         return this.gemcutterBurnTime * par1 / this.currentGemItemBurnTime;
  251.     }
  252.  
  253.     /**
  254.      * Returns true if the furnace is currently burning
  255.      */
  256.     public boolean isBurning()
  257.     {
  258.         return this.gemcutterBurnTime > 0;
  259.     }
  260.  
  261.     /**
  262.      * Allows the entity to update its state. Overridden in most subclasses, e.g. the mob spawner uses this to count
  263.      * ticks and creates a new spawn inside its implementation.
  264.      */
  265.     public void updateEntity()
  266.     {
  267.         boolean flag = this.gemcutterBurnTime > 0;
  268.         boolean flag1 = false;
  269.  
  270.         if (this.gemcutterBurnTime > 0)
  271.         {
  272.             --this.gemcutterBurnTime;
  273.         }
  274.  
  275.         if (!this.worldObj.isRemote)
  276.         {
  277.             if (this.gemcutterBurnTime == 0 && this.canSmelt())
  278.             {
  279.                 this.currentGemItemBurnTime = this.gemcutterBurnTime = getItemBurnTime(this.gemcutterItemStacks[1]);
  280.  
  281.                 if (this.gemcutterBurnTime > 0)
  282.                 {
  283.                     flag1 = true;
  284.  
  285.                     if (this.gemcutterItemStacks[1] != null)
  286.                     {
  287.                         --this.gemcutterItemStacks[1].stackSize;
  288.  
  289.                         if (this.gemcutterItemStacks[1].stackSize == 0)
  290.                         {
  291.                             this.gemcutterItemStacks[1] = this.gemcutterItemStacks[1].getItem().getContainerItemStack(gemcutterItemStacks[1]);
  292.                         }
  293.                     }
  294.                 }
  295.             }
  296.  
  297.             if (this.isBurning() && this.canSmelt())
  298.             {
  299.                 ++this.gemcutterCookTime;
  300.  
  301.                 if (this.gemcutterCookTime == 200)
  302.                 {
  303.                     this.gemcutterCookTime = 0;
  304.                     this.smeltItem();
  305.                     flag1 = true;
  306.                 }
  307.             }
  308.             else
  309.             {
  310.                 this.gemcutterCookTime = 0;
  311.             }
  312.  
  313.             if (flag != this.gemcutterBurnTime > 0)
  314.             {
  315.                 flag1 = true;
  316.                 BlockGemcutterBench.updateGemcutterBlockState(this.gemcutterBurnTime > 0, this.worldObj, this.xCoord, this.yCoord, this.zCoord);
  317.             }
  318.         }
  319.  
  320.         if (flag1)
  321.         {
  322.             this.onInventoryChanged();
  323.         }
  324.     }
  325.  
  326.     /**
  327.      * Returns true if the furnace can smelt an item, i.e. has a source item, destination stack isn't full, etc.
  328.      */
  329.     private boolean canSmelt()
  330.     {
  331.         if (this.gemcutterItemStacks[0] == null)
  332.         {
  333.             return false;
  334.         }
  335. //also changed bits down here
  336.         if (this.gemcutterItemStacks[3] == null)
  337.         {
  338.             return false;
  339.         }
  340.         else
  341.         {
  342.             ItemStack itemstack = GemcutterRecipes.smelting().getSmeltingResult(this.gemcutterItemStacks[0], this.gemcutterItemStacks[3]);
  343.             if (itemstack == null) return false;
  344.             if (this.gemcutterItemStacks[2] == null) return true;
  345.             if (!this.gemcutterItemStacks[2].isItemEqual(itemstack)) return false;
  346.             int result = gemcutterItemStacks[2].stackSize + itemstack.stackSize;
  347.             return (result <= getInventoryStackLimit() && result <= itemstack.getMaxStackSize());
  348.         }
  349.     }
  350.  
  351.     /**
  352.      * Turn one item from the furnace source stack into the appropriate smelted item in the furnace result stack
  353.      */
  354.     public void smeltItem()
  355.     {
  356.         if (this.canSmelt())
  357.         {
  358. //Not sure if this is correct
  359.             ItemStack itemstack = GemcutterRecipes.smelting().getSmeltingResult(this.gemcutterItemStacks[0], this.gemcutterItemStacks[3]);
  360.            
  361.             if (this.gemcutterItemStacks[2] == null)
  362.             {
  363.                 this.gemcutterItemStacks[2] = itemstack.copy();
  364.             }
  365.             else if (this.gemcutterItemStacks[2].isItemEqual(itemstack))
  366.             {
  367.                 gemcutterItemStacks[2].stackSize += itemstack.stackSize;
  368.             }
  369.  
  370.             --this.gemcutterItemStacks[0].stackSize;
  371.  
  372.             if (this.gemcutterItemStacks[0].stackSize <= 0)
  373.             {
  374.                 this.gemcutterItemStacks[0] = null;
  375.             }
  376.            
  377. //Added this bit down here
  378.             --this.gemcutterItemStacks[3].stackSize;
  379.  
  380.             if (this.gemcutterItemStacks[3].stackSize <= 0)
  381.             {
  382.                 this.gemcutterItemStacks[3] = null;
  383.             }
  384.         }
  385.     }
  386.  
  387.     /**
  388.      * Returns the number of ticks that the supplied fuel item will keep the furnace burning, or 0 if the item isn't
  389.      * fuel
  390.      */
  391.     public static int getItemBurnTime(ItemStack par0ItemStack)
  392.     {
  393.         if (par0ItemStack == null)
  394.         {
  395.             return 0;
  396.         }
  397.         else
  398.         {
  399.             int i = par0ItemStack.getItem().itemID;
  400.             Item item = par0ItemStack.getItem();
  401.  
  402.             if (par0ItemStack.getItem() instanceof ItemBlock && Block.blocksList[i] != null)
  403.             {
  404.                 Block block = Block.blocksList[i];
  405.  
  406.                 /*if (block == Block.woodSingleSlab)
  407.                 {
  408.                     return 150;
  409.                 }
  410.  
  411.                 if (block.blockMaterial == Material.wood)
  412.                 {
  413.                     return 300;
  414.                 }*/
  415.             }
  416.  
  417.             //if (i == mod_Ores.PolisherTowel.itemID) return 800;
  418.             if (i == mod_Ores.Polisher.itemID) return 1600;
  419.             //if (i == mod_Ores.UraniumLiquid.itemID) return 1600;
  420.             //if (i == Item.coal.itemID) return 1600;
  421.             return GameRegistry.getFuelValue(par0ItemStack);
  422.         }
  423.     }
  424.  
  425.     /**
  426.      * Return true if item is a fuel source (getItemBurnTime() > 0).
  427.      */
  428.     public static boolean isItemFuel(ItemStack par0ItemStack)
  429.     {
  430.         return getItemBurnTime(par0ItemStack) > 0;
  431.     }
  432.  
  433.     /**
  434.      * Do not make give this method the name canInteractWith because it clashes with Container
  435.      */
  436.     @Override
  437.     public boolean isUseableByPlayer(EntityPlayer par1EntityPlayer)
  438.     {
  439.         return this.worldObj.getBlockTileEntity(this.xCoord, this.yCoord, this.zCoord) != this ? false : par1EntityPlayer.getDistanceSq((double)this.xCoord + 0.5D, (double)this.yCoord + 0.5D, (double)this.zCoord + 0.5D) <= 64.0D;
  440.     }
  441.  
  442.     public void openChest() {}
  443.  
  444.     public void closeChest() {}
  445.  
  446.     /**
  447.      * Returns true if automation is allowed to insert the given stack (ignoring stack size) into the given slot.
  448.      */
  449.     public boolean isStackValidForSlot(int par1, ItemStack par2ItemStack)
  450.     {
  451.         return par1 == 2 ? false : (par1 == 1 ? isItemFuel(par2ItemStack) : true);
  452.     }
  453.  
  454.     /**
  455.      * Get the size of the side inventory.
  456.      */
  457.     public int[] getSizeInventorySide(int par1)
  458.     {
  459.         return par1 == 0 ? field_102011_e : (par1 == 1 ? field_102010_d : field_102009_f);
  460.     }
  461.  
  462.     public boolean func_102007_a(int par1, ItemStack par2ItemStack, int par3)
  463.     {
  464.         return this.isStackValidForSlot(par1, par2ItemStack);
  465.     }
  466.  
  467.     public boolean func_102008_b(int par1, ItemStack par2ItemStack, int par3)
  468.     {
  469.         return par3 != 0 || par1 != 1 || par2ItemStack.itemID == Item.bucketEmpty.itemID;
  470.     }
  471.  
  472.     /***********************************************************************************
  473.      * This function is here for compatibilities sake, Modders should Check for
  474.      * Sided before ContainerWorldly, Vanilla Minecraft does not follow the sided standard
  475.      * that Modding has for a while.
  476.      *
  477.      * In vanilla:
  478.      *
  479.      *   Top: Ores
  480.      *   Sides: Fuel
  481.      *   Bottom: Output
  482.      *
  483.      * Standard Modding:
  484.      *   Top: Ores
  485.      *   Sides: Output
  486.      *   Bottom: Fuel
  487.      *
  488.      * The Modding one is designed after the GUI, the vanilla one is designed because its
  489.      * intended use is for the hopper, which logically would take things in from the top.
  490.      *
  491.      * This will possibly be removed in future updates, and make vanilla the definitive
  492.      * standard.
  493.      */
  494.  
  495.     @Override
  496.     public int getStartInventorySide(ForgeDirection side)
  497.     {
  498.         if (ForgeDummyContainer.legacyFurnaceSides)
  499.         {
  500.             if (side == ForgeDirection.DOWN) return 1;
  501.             if (side == ForgeDirection.UP) return 0;
  502.             return 2;
  503.         }
  504.         else
  505.         {
  506.             if (side == ForgeDirection.DOWN) return 2;
  507.             if (side == ForgeDirection.UP) return 0;
  508.             return 1;
  509.         }
  510.     }
  511.  
  512.     @Override
  513.     public int getSizeInventorySide(ForgeDirection side)
  514.     {
  515.         return 1;
  516.     }
  517. }
Advertisement
Add Comment
Please, Sign In to add comment