Advertisement
Guest User

Untitled

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