Guest User

TileEntityGrinder.java

a guest
Aug 19th, 2015
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.36 KB | None | 0 0
  1. package com.xenocorpse.wulfenite.tileentities;
  2.  
  3. import net.minecraft.block.Block;
  4. import net.minecraft.block.material.Material;
  5. import net.minecraft.entity.player.EntityPlayer;
  6. import net.minecraft.init.Blocks;
  7. import net.minecraft.init.Items;
  8. import net.minecraft.inventory.ISidedInventory;
  9. import net.minecraft.item.Item;
  10. import net.minecraft.item.ItemBlock;
  11. import net.minecraft.item.ItemStack;
  12. import net.minecraft.item.ItemTool;
  13. import net.minecraft.nbt.NBTTagCompound;
  14. import net.minecraft.nbt.NBTTagList;
  15. import net.minecraft.tileentity.TileEntity;
  16. import net.minecraft.util.EnumFacing;
  17. import net.minecraft.util.IChatComponent;
  18. import net.minecraftforge.fml.common.registry.GameRegistry;
  19. import net.minecraftforge.fml.relauncher.Side;
  20. import net.minecraftforge.fml.relauncher.SideOnly;
  21.  
  22. import com.xenocorpse.wulfenite.blocks.BlockGrinder;
  23. import com.xenocorpse.wulfenite.init.GrinderRecipes;
  24. import com.xenocorpse.wulfenite.init.wulfeniteBlocks;
  25. import com.xenocorpse.wulfenite.init.wulfeniteItems;
  26.  
  27. public class TileEntityGrinder extends TileEntity implements ISidedInventory{
  28.    
  29.     private static final int[] slotsTop = new int[] { 0 };
  30.     private static final int[] slotsBottom = new int[] { 2, 1 };
  31.     private static final int[] slotsSides = new int[] { 1 };
  32.    
  33.     private ItemStack[] grinderItemStacks = new ItemStack[3];
  34.    
  35.     public int grinderBurnTime;
  36.     public int currentBurnTime;
  37.     public int grinderGrindTime;
  38.    
  39.     private String grinderName;
  40.    
  41.     public void grinderName(String string){
  42.         this.grinderName = string;
  43.     }
  44.  
  45.     @Override
  46.     public int getSizeInventory() {
  47.  
  48.         return this.grinderItemStacks.length;
  49.     }
  50.  
  51.     @Override
  52.     public ItemStack getStackInSlot(int slot) {
  53.  
  54.         return this.grinderItemStacks[slot];
  55.     }
  56.  
  57.     @Override
  58.     public ItemStack decrStackSize(int par1, int par2) {
  59.  
  60.         if(this.grinderItemStacks[par1] != null){
  61.             ItemStack itemstack;
  62.             if(this.grinderItemStacks[par1].stackSize <= par2){
  63.                 itemstack = this.grinderItemStacks[par1];
  64.                 this.grinderItemStacks[par1] = null;
  65.                 return itemstack;
  66.             }else{
  67.                 itemstack = this.grinderItemStacks[par1].splitStack(par2);
  68.                
  69.                 if(this.grinderItemStacks[par1].stackSize == 0){
  70.                     this.grinderItemStacks[par1] = null;
  71.                 }
  72.                 return itemstack;
  73.             }
  74.            
  75.         }else{
  76.             return null;
  77.         }
  78.     }
  79.  
  80.     @Override
  81.     public ItemStack getStackInSlotOnClosing(int par1) {
  82.         if(this.grinderItemStacks[par1] != null){
  83.             ItemStack itemstack = this.grinderItemStacks[par1];
  84.             this.grinderItemStacks[par1] = null;
  85.             return itemstack;
  86.         }else{
  87.             return null;
  88.         }
  89.     }
  90.  
  91.     @Override
  92.     public void setInventorySlotContents(int par1, ItemStack itemstack) {
  93.         this.grinderItemStacks[par1] = itemstack;
  94.        
  95.         if(itemstack != null && itemstack.stackSize > this.getInventoryStackLimit()){
  96.             itemstack.stackSize = this.getInventoryStackLimit();
  97.         }
  98.     }
  99.    
  100.     @Override
  101.     public String getName() {
  102.  
  103.         return this.hasCustomName() ? this.grinderName : "Grinder";
  104.     }
  105.    
  106.     @Override
  107.     public boolean hasCustomName() {
  108.  
  109.         return this.grinderName != null && this.grinderName.length() > 0;
  110.     }
  111.    
  112.     @Override
  113.     public int getInventoryStackLimit() {
  114.  
  115.         return 64;
  116.     }
  117.    
  118.     public void readFromNBT(NBTTagCompound tagCompound) {
  119.         super.readFromNBT(tagCompound);
  120.         NBTTagList tagList = tagCompound.getTagList("Items", 10);
  121.         this.grinderItemStacks = new ItemStack[this.getSizeInventory()];
  122.  
  123.         for (int i = 0; i < tagList.tagCount(); ++i) {
  124.             NBTTagCompound tabCompound1 = tagList.getCompoundTagAt(i);
  125.             byte byte0 = tabCompound1.getByte("Slot");
  126.  
  127.             if (byte0 >= 0 && byte0 < this.grinderItemStacks.length) {
  128.                 this.grinderItemStacks[byte0] = ItemStack.loadItemStackFromNBT(tabCompound1);
  129.             }
  130.         }
  131.  
  132.         this.grinderBurnTime = tagCompound.getShort("BurnTime");
  133.         this.grinderGrindTime = tagCompound.getShort("GrindTime");
  134.         this.currentBurnTime = getItemBurnTime(this.grinderItemStacks[1]);
  135.  
  136.         if (tagCompound.hasKey("CustomName", 8)) {
  137.             this.grinderName = tagCompound.getString("CustomName");
  138.         }
  139.     }
  140.  
  141.     public void writeToNBT(NBTTagCompound tagCompound) {
  142.         super.writeToNBT(tagCompound);
  143.         tagCompound.setShort("BurnTime", (short) this.grinderBurnTime);
  144.         tagCompound.setShort("GrindTime", (short) this.grinderBurnTime);
  145.         NBTTagList tagList = new NBTTagList();
  146.  
  147.         for (int i = 0; i < this.grinderItemStacks.length; ++i) {
  148.             if (this.grinderItemStacks[i] != null) {
  149.                 NBTTagCompound tagCompound1 = new NBTTagCompound();
  150.                 tagCompound1.setByte("Slot", (byte) i);
  151.                 this.grinderItemStacks[i].writeToNBT(tagCompound1);
  152.                 tagList.appendTag(tagCompound1);
  153.             }
  154.         }
  155.  
  156.         tagCompound.setTag("Items", tagList);
  157.  
  158.         if (this.hasCustomName()) {
  159.             tagCompound.setString("CustomName", this.grinderName);
  160.         }
  161.     }
  162.    
  163.     @SideOnly(Side.CLIENT)
  164.     public int getCookProgressScaled(int par1) {
  165.         return this.grinderGrindTime * par1 / 200;
  166.     }
  167.  
  168.     @SideOnly(Side.CLIENT)
  169.     public int getBurnTimeRemainingScaled(int par1) {
  170.         if (this.currentBurnTime == 0) {
  171.             this.currentBurnTime = 200;
  172.         }
  173.  
  174.         return this.grinderBurnTime * par1 / this.currentBurnTime;
  175.     }
  176.    
  177.     public boolean isBurning() {
  178.         return this.grinderBurnTime > 0;
  179.     }
  180.    
  181.     public void updateEntity() {
  182.         boolean flag = this.grinderBurnTime > 0;
  183.         boolean flag1 = false;
  184.  
  185.         if (this.grinderBurnTime > 0) {
  186.             --this.grinderBurnTime;
  187.         }
  188.  
  189.         if (!this.worldObj.isRemote) {
  190.             if (this.grinderBurnTime == 0 && this.canSmelt()) {
  191.                 this.currentBurnTime = this.grinderBurnTime = getItemBurnTime(this.grinderItemStacks[1]);
  192.  
  193.                 if (this.grinderBurnTime > 0) {
  194.                     flag1 = true;
  195.                     if (this.grinderItemStacks[1] != null) {
  196.                         --this.grinderItemStacks[1].stackSize;
  197.  
  198.                         if (this.grinderItemStacks[1].stackSize == 0) {
  199.                             this.grinderItemStacks[1] = grinderItemStacks[1].getItem().getContainerItem(this.grinderItemStacks[1]);
  200.                         }
  201.                     }
  202.                 }
  203.             }
  204.  
  205.             if (this.isBurning() && this.canSmelt()) {
  206.                 ++this.grinderGrindTime;
  207.                 if (this.grinderGrindTime == 200) {
  208.                     this.grinderGrindTime = 0;
  209.                     this.smeltItem();
  210.                     flag1 = true;
  211.                 }
  212.             } else {
  213.                 this.grinderGrindTime = 0;
  214.             }
  215.         }
  216.  
  217.         if (flag != this.grinderBurnTime > 0) {
  218.             flag1 = true;
  219.             BlockGrinder.updateBlockState(this.grinderBurnTime > 0, this.pos, this.worldObj);
  220.         }
  221.  
  222.         if (flag1) {
  223.             this.markDirty();
  224.         }
  225.     }
  226.    
  227.     private boolean canSmelt() {
  228.         if (this.grinderItemStacks[0] == null) {
  229.             return false;
  230.         } else {
  231.             ItemStack itemstack = GrinderRecipes.smelting().getSmeltingResult(this.grinderItemStacks[0]);
  232.             if (itemstack == null) return false;
  233.             if (this.grinderItemStacks[2] == null) return true;
  234.             if (!this.grinderItemStacks[2].isItemEqual(itemstack)) return false;
  235.             int result = grinderItemStacks[2].stackSize + itemstack.stackSize;
  236.             return result <= getInventoryStackLimit() && result <= this.grinderItemStacks[2].getMaxStackSize();
  237.         }
  238.     }
  239.  
  240.     public void smeltItem() {
  241.         if (this.canSmelt()) {
  242.             ItemStack itemstack = GrinderRecipes.smelting().getSmeltingResult(this.grinderItemStacks[0]);
  243.  
  244.             if (this.grinderItemStacks[2] == null) {
  245.                 this.grinderItemStacks[2] = itemstack.copy();
  246.             } else if (this.grinderItemStacks[2].getItem() == itemstack.getItem()) {
  247.                 this.grinderItemStacks[2].stackSize += itemstack.stackSize;
  248.             }
  249.            
  250.             --this.grinderItemStacks[0].stackSize;
  251.            
  252.             if(this.grinderItemStacks[0].stackSize >= 0){
  253.                 this.grinderItemStacks[0] = null;
  254.             }
  255.         }
  256.     }
  257.    
  258.     public static int getItemBurnTime(ItemStack itemstack){
  259.         if(itemstack == null){
  260.             return 0;
  261.         }else{
  262.             Item item = itemstack.getItem();
  263.            
  264.             if(item instanceof ItemBlock && Block.getBlockFromItem(item) != Blocks.air){
  265.                 Block block = Block.getBlockFromItem(item);
  266.                
  267.                 if(block == wulfeniteBlocks.wulfenite_block){
  268.                     return 200;
  269.                 }
  270.                
  271.                 if(block.getMaterial() == Material.rock){
  272.                     return 300;
  273.                 }
  274.             }
  275.            
  276.             if(item == wulfeniteItems.item_wulfenite) return 1600;
  277.             if(item instanceof ItemTool && ((ItemTool) item).getToolMaterialName().equals("EMERALD")) return 300;
  278.             return GameRegistry.getFuelValue(itemstack);
  279.         }
  280.     }
  281.    
  282.     public static boolean isItemFuel(ItemStack itemstack){
  283.         return getItemBurnTime(itemstack) > 0;
  284.     }
  285.  
  286.     @Override
  287.     public boolean isUseableByPlayer(EntityPlayer player) {
  288.         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;
  289.     }
  290.  
  291.     @Override
  292.     public void openInventory(EntityPlayer player) {
  293.        
  294.     }
  295.  
  296.     @Override
  297.     public void closeInventory(EntityPlayer player) {
  298.        
  299.     }
  300.  
  301.     @Override
  302.     public boolean isItemValidForSlot(int par1, ItemStack itemstack) {
  303.         return par1 == 2 ? false : (par1 == 1 ? isItemFuel(itemstack) : true);
  304.     }
  305.    
  306.     @Override
  307.     public int getField(int id) {
  308.  
  309.         return 0;
  310.     }
  311.  
  312.     @Override
  313.     public void setField(int id, int value) {
  314.  
  315.        
  316.     }
  317.  
  318.     @Override
  319.     public int getFieldCount() {
  320.  
  321.         return 0;
  322.     }
  323.  
  324.     @Override
  325.     public void clear() {
  326.  
  327.        
  328.     }
  329.  
  330.     @Override
  331.     public IChatComponent getDisplayName() {
  332.  
  333.         return null;
  334.     }
  335.  
  336.     @Override
  337.     public int[] getSlotsForFace(EnumFacing side) {
  338.        
  339.         return side == EnumFacing.UP ? slotsTop : slotsSides;
  340.     }
  341.  
  342.     @Override
  343.     public boolean canInsertItem(int par1, ItemStack itemstack, EnumFacing par3) {
  344.         return this.isItemValidForSlot(par1, itemstack);
  345.        
  346.     }
  347.  
  348.     @Override
  349.     public boolean canExtractItem(int par1, ItemStack itemstack, EnumFacing par3) {
  350.        
  351.         return true;
  352.        
  353.     }
  354.  
  355. }
Advertisement
Add Comment
Please, Sign In to add comment