Guest User

TileEntityICC

a guest
Jun 29th, 2017
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.19 KB | None | 0 0
  1. package com.jam.icc.tileentity;
  2.  
  3. import java.util.List;
  4.  
  5. import com.jam.icc.block.BlockItemCollectorChest;
  6.  
  7. import net.minecraft.entity.item.EntityItem;
  8. import net.minecraft.entity.player.EntityPlayer;
  9. import net.minecraft.inventory.IInventory;
  10. import net.minecraft.inventory.Slot;
  11. import net.minecraft.item.Item;
  12. import net.minecraft.item.ItemStack;
  13. import net.minecraft.nbt.NBTTagCompound;
  14. import net.minecraft.tileentity.TileEntity;
  15. import net.minecraft.util.EntitySelectors;
  16. import net.minecraft.util.EnumFacing;
  17. import net.minecraft.util.ITickable;
  18. import net.minecraft.util.math.AxisAlignedBB;
  19. import net.minecraftforge.common.capabilities.Capability;
  20. import net.minecraftforge.items.CapabilityItemHandler;
  21. import net.minecraftforge.items.IItemHandler;
  22. import net.minecraftforge.items.ItemHandlerHelper;
  23. import net.minecraftforge.items.ItemStackHandler;
  24.  
  25. public class TileEntityICC extends TileEntity implements ITickable{
  26.  
  27. public static final int SIZE = 10;
  28. int currentTickRate = 20;
  29. int counter = 0;
  30. // This item handler will hold the inventory slots
  31. private ItemStackHandler itemStackHandler = new ItemStackHandler(SIZE) {
  32. @Override
  33. protected void onContentsChanged(int slot) {
  34. // We need to tell the tile entity that something has changed so
  35. // that the chest contents is persisted
  36. TileEntityICC.this.markDirty();
  37. }
  38. };
  39.  
  40. private boolean hasBeenCleared;
  41. @Override
  42. public void readFromNBT(NBTTagCompound compound) {
  43. super.readFromNBT(compound);
  44. if (compound.hasKey("items")) {
  45. itemStackHandler.deserializeNBT((NBTTagCompound) compound.getTag("items"));
  46. }
  47. }
  48.  
  49. @Override
  50. public NBTTagCompound writeToNBT(NBTTagCompound compound) {
  51. super.writeToNBT(compound);
  52. compound.setTag("items", itemStackHandler.serializeNBT());
  53. return compound;
  54. }
  55.  
  56. public boolean canInteractWith(EntityPlayer playerIn) {
  57. // If we are too far away from this tile entity you cannot use it
  58. return !isInvalid() && playerIn.getDistanceSq(pos.add(0.5D, 0.5D, 0.5D)) <= 64D;
  59. }
  60.  
  61. @Override
  62. public boolean hasCapability(Capability<?> capability, EnumFacing facing) {
  63. if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) {
  64. return true;
  65. }
  66. return super.hasCapability(capability, facing);
  67. }
  68.  
  69. @Override
  70. public <T> T getCapability(Capability<T> capability, EnumFacing facing) {
  71. if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) {
  72. return (T) itemStackHandler;
  73. }
  74. return super.getCapability(capability, facing);
  75.  
  76. }
  77. //code for picking up items
  78. @Override
  79. public void update() {
  80. TileEntityICC tee = (TileEntityICC) world.getTileEntity(pos);
  81. if(!this.world.isRemote)
  82. {
  83. counter++;
  84. if (counter >= currentTickRate)
  85. {
  86. counter = 0;
  87.  
  88. List<EntityItem> entityItemList = this.world.getEntitiesWithinAABB(EntityItem.class, new AxisAlignedBB(this.pos.add(-5,-5,-5),this.pos.add(6,6,6)),EntitySelectors.IS_ALIVE);
  89. boolean didSomething = false;
  90. if(!entityItemList.isEmpty())
  91. {
  92. EnumFacing facing = this.world.getBlockState(pos).getValue(BlockItemCollectorChest.FACING);
  93. TileEntity te = this.world.getTileEntity(pos);
  94.  
  95. if(te != null && te.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, facing.getOpposite()))
  96. {
  97. IItemHandler itemHandler = te.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, facing);
  98. for(EntityItem ei : entityItemList)
  99. {
  100. ItemStack eitem = ei.getItem();
  101.  
  102. if(!ei.isDead && itemHandler.getStackInSlot(9).getItem() == eitem.getItem() && !itemHandler.getStackInSlot(9).isEmpty())
  103. {
  104.  
  105.  
  106.  
  107.  
  108.  
  109. ItemStack original = ei.getItem().copy();
  110. ItemStack left = ItemHandlerHelperIndex.insertItemStacked(itemHandler, original, false);
  111.  
  112. if(left.getCount() < original.getCount())
  113. {
  114. didSomething=true;
  115. }
  116.  
  117. if(left.isEmpty() || left.getCount() == 0)
  118. {
  119. ei.setDead();
  120. }else
  121. {
  122. ei.setItem(left);
  123. }
  124.  
  125.  
  126.  
  127.  
  128.  
  129. }
  130.  
  131. if(!ei.isDead && itemHandler.getStackInSlot(9).isEmpty())
  132. {
  133.  
  134.  
  135.  
  136.  
  137.  
  138. ItemStack original = ei.getItem().copy();
  139. ItemStack left = ItemHandlerHelperIndex.insertItemStacked(itemHandler, original, false);
  140.  
  141. if(left.getCount() < original.getCount())
  142. {
  143. didSomething=true;
  144. }
  145.  
  146. if(left.isEmpty() || left.getCount() == 0)
  147. {
  148. ei.setDead();
  149. }else
  150. {
  151. ei.setItem(left);
  152.  
  153. }
  154. }
  155. }
  156.  
  157. }
  158.  
  159. }
  160. if(!didSomething)
  161. {
  162. if(currentTickRate < 20)
  163. {
  164. currentTickRate++;
  165. }
  166. }
  167. else
  168. {
  169. if(currentTickRate > 1)
  170. {
  171. currentTickRate--;
  172. }
  173. }
  174. }
  175. }
  176.  
  177. }
  178.  
  179.  
  180.  
  181.  
  182. }
Advertisement
Add Comment
Please, Sign In to add comment