Advertisement
Guest User

Untitled

a guest
Feb 16th, 2015
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 14.82 KB | None | 0 0
  1. package com.expansion.tileentity;
  2.  
  3. import com.expansion.MachinesRecpie.GrinderRecpie;
  4.  
  5. import net.minecraft.block.Block;
  6. import net.minecraft.block.BlockFurnace;
  7. import net.minecraft.block.material.Material;
  8. import net.minecraft.entity.player.EntityPlayer;
  9. import net.minecraft.entity.player.InventoryPlayer;
  10. import net.minecraft.init.Blocks;
  11. import net.minecraft.init.Items;
  12. import net.minecraft.inventory.Container;
  13. import net.minecraft.inventory.ContainerFurnace;
  14. import net.minecraft.inventory.IInventory;
  15. import net.minecraft.inventory.SlotFurnaceFuel;
  16. import net.minecraft.item.Item;
  17. import net.minecraft.item.ItemBlock;
  18. import net.minecraft.item.ItemHoe;
  19. import net.minecraft.item.ItemStack;
  20. import net.minecraft.item.ItemSword;
  21. import net.minecraft.item.ItemTool;
  22. import net.minecraft.nbt.NBTTagCompound;
  23. import net.minecraft.nbt.NBTTagList;
  24. import net.minecraft.tileentity.TileEntity;
  25. import net.minecraft.util.ChatComponentText;
  26. import net.minecraft.util.EnumFacing;
  27. import net.minecraft.util.IChatComponent;
  28. import net.minecraft.util.MathHelper;
  29. import net.minecraftforge.fml.relauncher.Side;
  30. import net.minecraftforge.fml.relauncher.SideOnly;
  31.  
  32. public class TileEntityGrinder extends TileEntity implements IInventory {
  33.  
  34.     private static final int[] slotsTop = new int[] {0};
  35.     private static final int[] slotsBottom = new int[] {2, 1};
  36.     private static final int[] slotsSides = new int[] {1};
  37.     private ItemStack[] furnaceItemStacks = new ItemStack[3];
  38.     private int furnaceBurnTime;
  39.     private int currentItemBurnTime;
  40.     private int cookTime;
  41.     private int totalCookTime;
  42.     private String furnaceCustomName;
  43.     private static final String __OBFID = "CL_00000357";
  44.  
  45.     public int getSizeInventory()
  46.     {
  47.         return this.furnaceItemStacks.length;
  48.     }
  49.  
  50.     public ItemStack getStackInSlot(int index)
  51.     {
  52.         return this.furnaceItemStacks[index];
  53.     }
  54.  
  55.     public ItemStack decrStackSize(int index, int count)
  56.     {
  57.         if (this.furnaceItemStacks[index] != null)
  58.         {
  59.             ItemStack itemstack;
  60.  
  61.             if (this.furnaceItemStacks[index].stackSize <= count)
  62.             {
  63.                 itemstack = this.furnaceItemStacks[index];
  64.                 this.furnaceItemStacks[index] = null;
  65.                 return itemstack;
  66.             }
  67.             else
  68.             {
  69.                 itemstack = this.furnaceItemStacks[index].splitStack(count);
  70.  
  71.                 if (this.furnaceItemStacks[index].stackSize == 0)
  72.                 {
  73.                     this.furnaceItemStacks[index] = null;
  74.                 }
  75.  
  76.                 return itemstack;
  77.             }
  78.         }
  79.         else
  80.         {
  81.             return null;
  82.         }
  83.     }
  84.  
  85.     public ItemStack getStackInSlotOnClosing(int index)
  86.     {
  87.         if (this.furnaceItemStacks[index] != null)
  88.         {
  89.             ItemStack itemstack = this.furnaceItemStacks[index];
  90.             this.furnaceItemStacks[index] = null;
  91.             return itemstack;
  92.         }
  93.         else
  94.         {
  95.             return null;
  96.         }
  97.     }
  98.  
  99.     public void setInventorySlotContents(int index, ItemStack stack)
  100.     {
  101.         boolean flag = stack != null && stack.isItemEqual(this.furnaceItemStacks[index]) && ItemStack.areItemStackTagsEqual(stack, this.furnaceItemStacks[index]);
  102.         this.furnaceItemStacks[index] = stack;
  103.  
  104.         if (stack != null && stack.stackSize > this.getInventoryStackLimit())
  105.         {
  106.             stack.stackSize = this.getInventoryStackLimit();
  107.         }
  108.  
  109.         if (index == 0 && !flag)
  110.         {
  111.             this.totalCookTime = this.func_174904_a(stack);
  112.             this.cookTime = 0;
  113.             this.markDirty();
  114.         }
  115.     }
  116.  
  117.     public String getName()
  118.     {
  119.         return this.hasCustomName() ? this.furnaceCustomName : "container.furnace";
  120.     }
  121.  
  122.     public boolean hasCustomName()
  123.     {
  124.         return this.furnaceCustomName != null && this.furnaceCustomName.length() > 0;
  125.     }
  126.  
  127.     public void setCustomInventoryName(String p_145951_1_)
  128.     {
  129.         this.furnaceCustomName = p_145951_1_;
  130.     }
  131.  
  132.     public void readFromNBT(NBTTagCompound compound)
  133.     {
  134.         super.readFromNBT(compound);
  135.         NBTTagList nbttaglist = compound.getTagList("Items", 10);
  136.         this.furnaceItemStacks = new ItemStack[this.getSizeInventory()];
  137.  
  138.         for (int i = 0; i < nbttaglist.tagCount(); ++i)
  139.         {
  140.             NBTTagCompound nbttagcompound1 = nbttaglist.getCompoundTagAt(i);
  141.             byte b0 = nbttagcompound1.getByte("Slot");
  142.  
  143.             if (b0 >= 0 && b0 < this.furnaceItemStacks.length)
  144.             {
  145.                 this.furnaceItemStacks[b0] = ItemStack.loadItemStackFromNBT(nbttagcompound1);
  146.             }
  147.         }
  148.  
  149.         this.furnaceBurnTime = compound.getShort("BurnTime");
  150.         this.cookTime = compound.getShort("CookTime");
  151.         this.totalCookTime = compound.getShort("CookTimeTotal");
  152.         this.currentItemBurnTime = getItemBurnTime(this.furnaceItemStacks[1]);
  153.  
  154.         if (compound.hasKey("CustomName", 8))
  155.         {
  156.             this.furnaceCustomName = compound.getString("CustomName");
  157.         }
  158.     }
  159.  
  160.     public void writeToNBT(NBTTagCompound compound)
  161.     {
  162.         super.writeToNBT(compound);
  163.         compound.setShort("BurnTime", (short)this.furnaceBurnTime);
  164.         compound.setShort("CookTime", (short)this.cookTime);
  165.         compound.setShort("CookTimeTotal", (short)this.totalCookTime);
  166.         NBTTagList nbttaglist = new NBTTagList();
  167.  
  168.         for (int i = 0; i < this.furnaceItemStacks.length; ++i)
  169.         {
  170.             if (this.furnaceItemStacks[i] != null)
  171.             {
  172.                 NBTTagCompound nbttagcompound1 = new NBTTagCompound();
  173.                 nbttagcompound1.setByte("Slot", (byte)i);
  174.                 this.furnaceItemStacks[i].writeToNBT(nbttagcompound1);
  175.                 nbttaglist.appendTag(nbttagcompound1);
  176.             }
  177.         }
  178.  
  179.         compound.setTag("Items", nbttaglist);
  180.  
  181.         if (this.hasCustomName())
  182.         {
  183.             compound.setString("CustomName", this.furnaceCustomName);
  184.         }
  185.     }
  186.  
  187.     public int getInventoryStackLimit()
  188.     {
  189.         return 64;
  190.     }
  191.  
  192.     public boolean isBurning()
  193.     {
  194.         return this.furnaceBurnTime > 0;
  195.     }
  196.  
  197.     @SideOnly(Side.CLIENT)
  198.     public static boolean isBurning(IInventory p_174903_0_)
  199.     {
  200.         return p_174903_0_.getField(0) > 0;
  201.     }
  202.  
  203.     public void update()
  204.     {
  205.         boolean flag = this.isBurning();
  206.         boolean flag1 = false;
  207.  
  208.         if (this.isBurning())
  209.         {
  210.             --this.furnaceBurnTime;
  211.         }
  212.  
  213.         if (!this.worldObj.isRemote)
  214.         {
  215.             if (!this.isBurning() && (this.furnaceItemStacks[1] == null || this.furnaceItemStacks[0] == null))
  216.             {
  217.                 if (!this.isBurning() && this.cookTime > 0)
  218.                 {
  219.                     this.cookTime = MathHelper.clamp_int(this.cookTime - 2, 0, this.totalCookTime);
  220.                 }
  221.             }
  222.             else
  223.             {
  224.                 if (!this.isBurning() && this.canSmelt())
  225.                 {
  226.                     this.currentItemBurnTime = this.furnaceBurnTime = getItemBurnTime(this.furnaceItemStacks[1]);
  227.  
  228.                     if (this.isBurning())
  229.                     {
  230.                         flag1 = true;
  231.  
  232.                         if (this.furnaceItemStacks[1] != null)
  233.                         {
  234.                             --this.furnaceItemStacks[1].stackSize;
  235.  
  236.                             if (this.furnaceItemStacks[1].stackSize == 0)
  237.                             {
  238.                                 this.furnaceItemStacks[1] = furnaceItemStacks[1].getItem().getContainerItem(furnaceItemStacks[1]);
  239.                             }
  240.                         }
  241.                     }
  242.                 }
  243.  
  244.                 if (this.isBurning() && this.canSmelt())
  245.                 {
  246.                     ++this.cookTime;
  247.  
  248.                     if (this.cookTime == this.totalCookTime)
  249.                     {
  250.                         this.cookTime = 0;
  251.                         this.totalCookTime = this.func_174904_a(this.furnaceItemStacks[0]);
  252.                         this.smeltItem();
  253.                         flag1 = true;
  254.                     }
  255.                 }
  256.                 else
  257.                 {
  258.                     this.cookTime = 0;
  259.                 }
  260.             }
  261.  
  262.             if (flag != this.isBurning())
  263.             {
  264.                 flag1 = true;
  265.                 BlockFurnace.setState(this.isBurning(), this.worldObj, this.pos);
  266.             }
  267.         }
  268.  
  269.         if (flag1)
  270.         {
  271.             this.markDirty();
  272.         }
  273.     }
  274.  
  275.     public int func_174904_a(ItemStack p_174904_1_)
  276.     {
  277.         return 200;
  278.     }
  279.  
  280.     private boolean canSmelt()
  281.     {
  282.         if (this.furnaceItemStacks[0] == null)
  283.         {
  284.             return false;
  285.         }
  286.         else
  287.         {
  288.             ItemStack itemstack = GrinderRecpie.instance().getSmeltingResult(this.furnaceItemStacks[0]);
  289.             if (itemstack == null) return false;
  290.             if (this.furnaceItemStacks[2] == null) return true;
  291.             if (!this.furnaceItemStacks[2].isItemEqual(itemstack)) return false;
  292.             int result = furnaceItemStacks[2].stackSize + itemstack.stackSize;
  293.             return result <= getInventoryStackLimit() && result <= this.furnaceItemStacks[2].getMaxStackSize(); //Forge BugFix: Make it respect stack sizes properly.
  294.         }
  295.     }
  296.  
  297.     public void smeltItem()
  298.     {
  299.         if (this.canSmelt())
  300.         {
  301.             ItemStack itemstack = GrinderRecpie.instance().getSmeltingResult(this.furnaceItemStacks[0]);
  302.  
  303.             if (this.furnaceItemStacks[2] == null)
  304.             {
  305.                 this.furnaceItemStacks[2] = itemstack.copy();
  306.             }
  307.             else if (this.furnaceItemStacks[2].getItem() == itemstack.getItem())
  308.             {
  309.                 this.furnaceItemStacks[2].stackSize += itemstack.stackSize; // Forge BugFix: Results may have multiple items
  310.             }
  311.  
  312.             if (this.furnaceItemStacks[0].getItem() == Item.getItemFromBlock(Blocks.sponge) && this.furnaceItemStacks[0].getMetadata() == 1 && this.furnaceItemStacks[1] != null && this.furnaceItemStacks[1].getItem() == Items.bucket)
  313.             {
  314.                 this.furnaceItemStacks[1] = new ItemStack(Items.water_bucket);
  315.             }
  316.  
  317.             --this.furnaceItemStacks[0].stackSize;
  318.  
  319.             if (this.furnaceItemStacks[0].stackSize <= 0)
  320.             {
  321.                 this.furnaceItemStacks[0] = null;
  322.             }
  323.         }
  324.     }
  325.  
  326.     public static int getItemBurnTime(ItemStack p_145952_0_)
  327.     {
  328.         if (p_145952_0_ == null)
  329.         {
  330.             return 0;
  331.         }
  332.         else
  333.         {
  334.             Item item = p_145952_0_.getItem();
  335.  
  336.             if (item instanceof ItemBlock && Block.getBlockFromItem(item) != Blocks.air)
  337.             {
  338.                 Block block = Block.getBlockFromItem(item);
  339.  
  340.                 if (block == Blocks.wooden_slab)
  341.                 {
  342.                     return 150;
  343.                 }
  344.  
  345.                 if (block.getMaterial() == Material.wood)
  346.                 {
  347.                     return 300;
  348.                 }
  349.  
  350.                 if (block == Blocks.coal_block)
  351.                 {
  352.                     return 16000;
  353.                 }
  354.             }
  355.  
  356.             if (item instanceof ItemTool && ((ItemTool)item).getToolMaterialName().equals("WOOD")) return 200;
  357.             if (item instanceof ItemSword && ((ItemSword)item).getToolMaterialName().equals("WOOD")) return 200;
  358.             if (item instanceof ItemHoe && ((ItemHoe)item).getMaterialName().equals("WOOD")) return 200;
  359.             if (item == Items.stick) return 100;
  360.             if (item == Items.coal) return 1600;
  361.             if (item == Items.lava_bucket) return 20000;
  362.             if (item == Item.getItemFromBlock(Blocks.sapling)) return 100;
  363.             if (item == Items.blaze_rod) return 2400;
  364.             return net.minecraftforge.fml.common.registry.GameRegistry.getFuelValue(p_145952_0_);
  365.         }
  366.     }
  367.  
  368.     public static boolean isItemFuel(ItemStack p_145954_0_)
  369.     {
  370.         return getItemBurnTime(p_145954_0_) > 0;
  371.     }
  372.  
  373.     public boolean isUseableByPlayer(EntityPlayer player)
  374.     {
  375.         return this.worldObj.getTileEntity(this.pos) != this ? false : player.getDistanceSq((double)this.pos.getX() + 0.5D, (double)this.pos.getY() + 0.5D, (double)this.pos.getZ() + 0.5D) <= 64.0D;
  376.     }
  377.  
  378.     public void openInventory(EntityPlayer player) {}
  379.  
  380.     public void closeInventory(EntityPlayer player) {}
  381.  
  382.     public boolean isItemValidForSlot(int index, ItemStack stack)
  383.     {
  384.         return index == 2 ? false : (index != 1 ? true : isItemFuel(stack) || SlotFurnaceFuel.isBucket(stack));
  385.     }
  386.  
  387.     public int[] getSlotsForFace(EnumFacing side)
  388.     {
  389.         return side == EnumFacing.DOWN ? slotsBottom : (side == EnumFacing.UP ? slotsTop : slotsSides);
  390.     }
  391.  
  392.     public boolean canInsertItem(int index, ItemStack itemStackIn, EnumFacing direction)
  393.     {
  394.         return this.isItemValidForSlot(index, itemStackIn);
  395.     }
  396.  
  397.     public boolean canExtractItem(int index, ItemStack stack, EnumFacing direction)
  398.     {
  399.         if (direction == EnumFacing.DOWN && index == 1)
  400.         {
  401.             Item item = stack.getItem();
  402.  
  403.             if (item != Items.water_bucket && item != Items.bucket)
  404.             {
  405.                 return false;
  406.             }
  407.         }
  408.  
  409.         return true;
  410.     }
  411.  
  412.     public String getGuiID()
  413.     {
  414.         return "minecraft:furnace";
  415.     }
  416.  
  417.     public Container createContainer(InventoryPlayer playerInventory, EntityPlayer playerIn)
  418.     {
  419.         return new ContainerFurnace(playerInventory, (IInventory) this);
  420.     }
  421.  
  422.     public int getField(int id)
  423.     {
  424.         switch (id)
  425.         {
  426.             case 0:
  427.                 return this.furnaceBurnTime;
  428.             case 1:
  429.                 return this.currentItemBurnTime;
  430.             case 2:
  431.                 return this.cookTime;
  432.             case 3:
  433.                 return this.totalCookTime;
  434.             default:
  435.                 return 0;
  436.         }
  437.     }
  438.  
  439.     public void setField(int id, int value)
  440.     {
  441.         switch (id)
  442.         {
  443.             case 0:
  444.                 this.furnaceBurnTime = value;
  445.                 break;
  446.             case 1:
  447.                 this.currentItemBurnTime = value;
  448.                 break;
  449.             case 2:
  450.                 this.cookTime = value;
  451.                 break;
  452.             case 3:
  453.                 this.totalCookTime = value;
  454.         }
  455.     }
  456.  
  457.     public int getFieldCount()
  458.     {
  459.         return 4;
  460.     }
  461.  
  462.     public void clear()
  463.     {
  464.         for (int i = 0; i < this.furnaceItemStacks.length; ++i)
  465.         {
  466.             this.furnaceItemStacks[i] = null;
  467.         }
  468.     }
  469.  
  470.     @Override
  471.     public IChatComponent getDisplayName() {
  472.        
  473.         return new ChatComponentText(getName());
  474.     }
  475.    
  476. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement