Advertisement
Guest User

Untitled

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