Advertisement
redfoxhint

Tile

Oct 26th, 2014
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.74 KB | None | 0 0
  1. package com.arucraft.tileentity;
  2.  
  3. import net.minecraft.entity.player.EntityPlayer;
  4. import net.minecraft.init.Items;
  5. import net.minecraft.inventory.IInventory;
  6. import net.minecraft.item.Item;
  7. import net.minecraft.item.ItemStack;
  8. import net.minecraft.nbt.NBTTagCompound;
  9. import net.minecraft.nbt.NBTTagList;
  10. import net.minecraft.tileentity.TileEntity;
  11.  
  12. public class TileEntityAlloyOven extends TileEntity implements IInventory {
  13.  
  14.     private ItemStack slots[];
  15.  
  16.     public int triplePower;
  17.     public int tripleCookTime;
  18.  
  19.     private String customName;
  20.  
  21.     public TileEntityAlloyOven() {
  22.         slots = new ItemStack[5];
  23.     }
  24.  
  25.     @Override
  26.     public int getSizeInventory() {
  27.         return slots.length;
  28.     }
  29.  
  30.     @Override
  31.     public ItemStack getStackInSlot(int i) {
  32.         return slots[i];
  33.     }
  34.  
  35.     @Override
  36.     public ItemStack decrStackSize(int i, int j) {
  37.         if (slots[i] != null) {
  38.             if (slots[i].stackSize <= j) {
  39.                 ItemStack itemstack = slots[i];
  40.                 slots[i] = null;
  41.                 return itemstack;
  42.             }
  43.             ItemStack itemstack1 = slots[i].splitStack(j);
  44.  
  45.             if (slots[i].stackSize == 0) {
  46.                 slots[i] = null;
  47.             }
  48.            
  49.             return itemstack1; 
  50.         }else{
  51.             return null;
  52.         }
  53.     }
  54.    
  55.     public void readFromNBT (NBTTagCompound nbt) {
  56.         super.readFromNBT(nbt);
  57.         NBTTagList list = nbt.getTagList("Items", 10);
  58.         slots = new ItemStack[getSizeInventory()];
  59.        
  60.         for (int i = 0; i < list.tagCount(); i++) {
  61.             NBTTagCompound nbt1 = (NBTTagCompound)list.getCompoundTagAt(i);
  62.             byte b0 = nbt1.getByte("Slot");
  63.            
  64.             if (b0 >= 0 && b0 < slots.length) {
  65.                 slots[b0] = ItemStack.loadItemStackFromNBT(nbt1);
  66.             }
  67.         }
  68.        
  69.         triplePower = nbt.getShort("PowerTime");
  70.         tripleCookTime = nbt.getShort("CookTime");
  71.     }
  72.    
  73.     public void writeToNBT(NBTTagCompound nbt) {
  74.         super.writeToNBT(nbt);
  75.         nbt.setShort("PowerTime", (short)triplePower);
  76.         nbt.setShort("CookTime", (short)tripleCookTime);
  77.         NBTTagList list = new NBTTagList();
  78.        
  79.         for (int i = 0; i < slots.length; i++) {
  80.             if (slots[i] != null) {
  81.                 NBTTagCompound nbt1 = new NBTTagCompound();
  82.                 nbt1.setByte("Slot", (byte)i);
  83.                 slots[i].writeToNBT(nbt1);
  84.                 list.appendTag(nbt1);
  85.             }
  86.         }
  87.        
  88.         nbt.setTag("Items", list);
  89.        
  90.     }
  91.    
  92.    
  93.  
  94.     @Override
  95.     public ItemStack getStackInSlotOnClosing(int i) {
  96.         if (slots[i] != null) {
  97.             ItemStack itemstack = slots[i];
  98.             slots[i] = null;
  99.             return itemstack;
  100.         }else{
  101.             return null;
  102.         }
  103.     }
  104.  
  105.     @Override
  106.     public void setInventorySlotContents(int i, ItemStack itemstack) {
  107.         slots[i] = itemstack;
  108.         if (itemstack != null && itemstack.stackSize > getInventoryStackLimit()) {
  109.             itemstack.stackSize = getInventoryStackLimit();
  110.         }
  111.     }
  112.  
  113.     @Override
  114.     public String getInventoryName() {
  115.         return this.hasCustomInventoryName() ? this.customName : "container.alloyoven";
  116.     }
  117.  
  118.     @Override
  119.     public boolean hasCustomInventoryName() {
  120.         return this.customName != null && this.customName.length() > 0;
  121.     }
  122.  
  123.     @Override
  124.     public int getInventoryStackLimit() {
  125.         return 64;
  126.     }
  127.  
  128.     @Override
  129.     public boolean isUseableByPlayer(EntityPlayer player) {
  130.         if (worldObj.getTileEntity(xCoord, yCoord, zCoord) != this) {
  131.             return false;
  132.         }else{
  133.             return player.getDistanceSq((double)xCoord + 0.5D, (double)yCoord + 0.5D, (double)zCoord + 0.5D) <= 64;
  134.         }
  135.     }
  136.  
  137.     public void openInventory() {}
  138.     public void closeInventory() {}
  139.  
  140.     @Override
  141.     public boolean isItemValidForSlot(int i, ItemStack itemstack) {
  142.         return i == 3 ? false : (i == 1 ? hasItemPower(itemstack) : true);
  143.     }
  144.  
  145.     public boolean hasItemPower(ItemStack itemstack) {
  146.         return getItemPower(itemstack) > 0;
  147.     }
  148.  
  149.     private static int getItemPower (ItemStack itemstack) {
  150.         if (itemstack == null) {
  151.             return 0;
  152.         }else{
  153.             Item item = itemstack.getItem();
  154.  
  155.             if (item == Items.coal) return 50;
  156.  
  157.             return 0;
  158.         }
  159.     }
  160.  
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement