Advertisement
Guest User

TileEntityGrinder

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