Guest User

TileEntityICSB

a guest
Aug 9th, 2017
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.73 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 TileEntityICSB extends TileEntity implements ITickable{
  26.    
  27.     public static final int SIZE = 27;
  28.     int currentTickRate = 20;
  29.     int counter = 0;
  30.     // This item handler will hold the inventory slots
  31.     public 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.             TileEntityICSB.this.markDirty();
  37.         }
  38.     };
  39.     // This item handler will hold the filter slot
  40.     public ItemStackHandler filterStackHandler = new ItemStackHandler(1) {
  41.         @Override
  42.         protected void onContentsChanged(int slot) {
  43.             // We need to tell the tile entity that something has changed so
  44.             // that the chest
  45.             //contents is persisted
  46.             TileEntityICSB.this.markDirty();
  47.         }
  48.     };
  49.    
  50.     private boolean hasBeenCleared;
  51.     @Override
  52.     public void readFromNBT(NBTTagCompound compound) {
  53.         super.readFromNBT(compound);
  54.         if (compound.hasKey("items")) {
  55.             itemStackHandler.deserializeNBT((NBTTagCompound) compound.getTag("items"));
  56.            
  57.         }
  58.         if(compound.hasKey("filterItem"))
  59.         {
  60.              filterStackHandler.deserializeNBT((NBTTagCompound) compound.getTag("filterItem"));
  61.         }
  62.     }
  63.    
  64.     @Override
  65.     public NBTTagCompound writeToNBT(NBTTagCompound compound) {
  66.         super.writeToNBT(compound);
  67.         compound.setTag("items", itemStackHandler.serializeNBT());
  68.         compound.setTag("filterItem", filterStackHandler.serializeNBT());
  69.         return compound;
  70.     }
  71.    
  72.    
  73.  
  74.     @Override
  75.     public boolean hasCapability(Capability<?> capability, EnumFacing facing) {
  76.         if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) {
  77.             return true;
  78.         }
  79.         return super.hasCapability(capability, facing);
  80.     }
  81.  
  82.     @Override
  83.     public <T> T getCapability(Capability<T> capability, EnumFacing facing) {
  84.         if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) {
  85.              return (T) itemStackHandler;
  86.         }
  87.        
  88.         return super.getCapability(capability, facing);
  89.        
  90.     }
  91.     public ItemStackHandler filterstack()
  92.     {
  93.         return filterStackHandler;
  94.     }
  95.     public ItemStackHandler temstack()
  96.     {
  97.         return itemStackHandler;
  98.     }
  99.     @Override
  100.     public void update() {
  101.         TileEntityICSB tee = (TileEntityICSB) world.getTileEntity(pos);
  102.         if(!this.world.isRemote)
  103.         {
  104.             counter++;
  105.             if (counter >= currentTickRate)
  106.             {
  107.                 counter = 0;
  108.                
  109.                 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);
  110.                 boolean didSomething = false;
  111.                 if(!entityItemList.isEmpty())
  112.                 {
  113.                    
  114.                     TileEntityICSB te = (TileEntityICSB)world.getTileEntity(pos);
  115.                    
  116.                     if(te != null && te.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null))
  117.                     {
  118.                         IItemHandler itemHandler = te.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null);
  119.                         IItemHandler filterHandler = te.filterstack();
  120.                         for(EntityItem ei : entityItemList)
  121.                         {
  122.                             ItemStack eitem = ei.getItem();
  123.                            
  124.                             if(!ei.isDead && filterHandler.getStackInSlot(0).getItem() == eitem.getItem() && !filterHandler.getStackInSlot(0).isEmpty())
  125.                             {
  126.                                
  127.                                
  128.                                
  129.                                    
  130.                                    
  131.                                 ItemStack original = ei.getItem().copy();
  132.                                 ItemStack left = ItemHandlerHelperIndex.insertItemStacked(itemHandler, original, false, 27);
  133.                                
  134.                                 if(left.getCount() < original.getCount())
  135.                                 {
  136.                                     didSomething=true;
  137.                                 }
  138.                            
  139.                                 if(left.isEmpty() || left.getCount() == 0)
  140.                                 {
  141.                                     ei.setDead();
  142.                                 }else
  143.                                 {
  144.                                     ei.setItem(left);
  145.                                 }
  146.                                
  147.                              
  148.                                    
  149.                            
  150.                                
  151.                           }
  152.                            
  153.                                 if(!ei.isDead && filterHandler.getStackInSlot(0).isEmpty())
  154.                                 {
  155.                                    
  156.                                    
  157.                                    
  158.                                        
  159.                                        
  160.                                     ItemStack original = ei.getItem().copy();
  161.                                     ItemStack left = ItemHandlerHelperIndex.insertItemStacked(itemHandler, original, false);
  162.                                    
  163.                                     if(left.getCount() < original.getCount())
  164.                                     {
  165.                                         didSomething=true;
  166.                                     }
  167.                                
  168.                                     if(left.isEmpty() || left.getCount() == 0)
  169.                                     {
  170.                                         ei.setDead();
  171.                                     }else
  172.                                     {
  173.                                     ei.setItem(left);
  174.                                    
  175.                                 }
  176.                             }
  177.                         }
  178.                      
  179.                      }
  180.                    
  181.                 }
  182.                 if(!didSomething)
  183.                 {
  184.                     if(currentTickRate < 20)
  185.                     {
  186.                         currentTickRate++;
  187.                     }
  188.                 }
  189.                 else
  190.                 {
  191.                     if(currentTickRate > 1)
  192.                     {
  193.                         currentTickRate--;
  194.                     }
  195.                 }
  196.             }
  197.         }
  198.        
  199.     }
  200.  
  201.    
  202.  
  203.    
  204. }
Add Comment
Please, Sign In to add comment