simple-tech

Minecraft: Better than Adventure! mod that adds simple blocks to automate tasks
git clone git://memoryshards.xyz/simple-tech.git
Log | Files | Refs | README | LICENSE

TileEntityAllocator.java (3144B)


      1 package ambos.simpletech.block.entity;
      2 
      3 import com.mojang.nbt.CompoundTag;
      4 import com.mojang.nbt.ListTag;
      5 import net.minecraft.core.block.entity.TileEntity;
      6 import net.minecraft.core.entity.player.EntityPlayer;
      7 import net.minecraft.core.item.ItemStack;
      8 import net.minecraft.core.player.inventory.IInventory;
      9 
     10 public class TileEntityAllocator extends TileEntity implements IInventory {
     11     private ItemStack allocatorFilterItem;
     12 
     13     @Override
     14     public int getSizeInventory() {
     15         return 1;
     16     }
     17 
     18     @Override
     19     public ItemStack getStackInSlot(int i) {
     20         return i == 0 ? this.allocatorFilterItem : null;
     21     }
     22 
     23     @Override
     24     public ItemStack decrStackSize(int i, int j) {
     25         if (i != 0) {
     26             return null;
     27         } else if (this.allocatorFilterItem != null) {
     28             ItemStack itemstack;
     29             if (this.allocatorFilterItem.stackSize <= j) {
     30                 itemstack = this.allocatorFilterItem;
     31                 this.allocatorFilterItem = null;
     32                 return itemstack;
     33             } else {
     34                 itemstack = this.allocatorFilterItem.splitStack(j);
     35                 if (this.allocatorFilterItem.stackSize == 0) {
     36                     this.allocatorFilterItem = null;
     37                 }
     38 
     39                 return itemstack;
     40             }
     41         } else {
     42             return null;
     43         }
     44     }
     45 
     46     @Override
     47     public void setInventorySlotContents(int i, ItemStack itemStack) {
     48         if (i == 0) {
     49             this.allocatorFilterItem = itemStack;
     50             if (itemStack != null && itemStack.stackSize > this.getInventoryStackLimit()) {
     51                 itemStack.stackSize = this.getInventoryStackLimit();
     52             }
     53         }
     54     }
     55 
     56     @Override
     57     public String getInvName() {
     58         return "Allocator";
     59     }
     60 
     61     @Override
     62     public int getInventoryStackLimit() {
     63         return 1;
     64     }
     65 
     66     @Override
     67     public boolean canInteractWith(EntityPlayer entityPlayer) {
     68         return this.worldObj.getBlockTileEntity(this.x, this.y, this.z) == this &&
     69                 entityPlayer.distanceToSqr((double) this.x + 0.5D, (double) this.y + 0.5D,
     70                         (double) this.z + 0.5D) <= 64.0D;
     71     }
     72 
     73     @Override
     74     public void sortInventory() {
     75     }
     76 
     77     @Override
     78     public void readFromNBT(CompoundTag nbttagcompound) {
     79         super.readFromNBT(nbttagcompound);
     80         ListTag items = nbttagcompound.getList("Items");
     81         if (items.tagCount() != 0) {
     82             CompoundTag item = (CompoundTag) items.tagAt(0);
     83             int slot = item.getByte("Slot") & 255;
     84             if (slot == 0) {
     85                 this.allocatorFilterItem = ItemStack.readItemStackFromNbt(item);
     86             }
     87         }
     88     }
     89 
     90     @Override
     91     public void writeToNBT(CompoundTag nbttagcompound) {
     92         super.writeToNBT(nbttagcompound);
     93         ListTag items = new ListTag();
     94         if (this.allocatorFilterItem != null) {
     95             CompoundTag item = new CompoundTag();
     96             item.putByte("Slot", (byte) 0);
     97             this.allocatorFilterItem.writeToNBT(item);
     98             items.addTag(item);
     99         }
    100 
    101         nbttagcompound.put("Items", items);
    102     }
    103 }