Guest User

TileEntityGristGatherer

a guest
Jul 20th, 2018
118
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.natura.minestuckarsenal.tileentity;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. import javax.annotation.Nonnull;
  7.  
  8. import com.mraof.minestuck.entity.item.EntityGrist;
  9.  
  10. import net.minecraft.block.state.IBlockState;
  11. import net.minecraft.entity.Entity;
  12. import net.minecraft.entity.player.EntityPlayer;
  13. import net.minecraft.nbt.NBTTagCompound;
  14. import net.minecraft.tileentity.TileEntity;
  15. import net.minecraft.util.ClassInheritanceMultiMap;
  16. import net.minecraft.util.ITickable;
  17. import net.minecraft.util.math.AxisAlignedBB;
  18. import net.minecraft.util.math.BlockPos;
  19. import net.minecraft.util.math.MathHelper;
  20. import net.minecraft.world.World;
  21. import net.minecraft.world.chunk.Chunk;
  22.  
  23. /* Thanks to Ender IO team for the entity selection code. */
  24.  
  25. public class TileEntityGristGatherer extends TileEntity implements ITickable {
  26.  
  27.     int counter = 0;
  28.     private int range = 8;
  29.     EntityPlayer player;
  30.    
  31.     public TileEntityGristGatherer(EntityPlayer player) {
  32.         this.player = player;
  33.     }
  34.    
  35.     public @Nonnull AxisAlignedBB getBounds() {
  36.         return new AxisAlignedBB(getPos()).expand(getRange() + (range == 0 ? 1 / 32f : 0), 0, getRange() + (range == 0 ? 1 / 32f : 0));
  37.      }
  38.  
  39.      public float getRange() {
  40.         return range;
  41.      }
  42.      
  43.      @Override
  44.      public NBTTagCompound writeToNBT(NBTTagCompound compound) {
  45.         super.writeToNBT(compound);
  46.         compound.setString("player", player.getName());
  47.         return compound;
  48.      }
  49.        
  50.      @Override
  51.      public void readFromNBT(NBTTagCompound compound) {
  52.         super.readFromNBT(compound);   
  53.         this.player = this.world.getPlayerEntityByName(compound.getString("player"));
  54.      }
  55.        
  56.      @Override
  57.      public boolean shouldRefresh(World world, BlockPos pos, IBlockState oldState, IBlockState newSate)
  58.      {
  59.            
  60.          return oldState.getBlock() != newSate.getBlock();
  61.            
  62.      }
  63.    
  64.     @Override
  65.     public void update() {
  66.         NBTTagCompound compound = new NBTTagCompound();
  67.         this.writeToNBT(compound);
  68.         this.markDirty();
  69.        
  70.         if(counter == 20) {
  71.            
  72.             List<EntityGrist> list = selectEntitiesWithinAABB(this.getWorld(), getBounds());
  73.            
  74.             if(list.size() != 0) {
  75.                     for(int i = 0; i == list.size() - 1; i++)
  76.                     {
  77.                         if(player != null && list.get(i) != null) {
  78.                             list.get(i).addGrist(player);
  79.                             list.get(i).setDead();
  80.                         }
  81.                     }
  82.             }
  83.            
  84.             counter = 0;
  85.         }
  86.        
  87.         else {
  88.             counter++;
  89.         }
  90.     }
  91.    
  92.     private List<EntityGrist> selectEntitiesWithinAABB(World worldIn, AxisAlignedBB bb) {
  93.         List<EntityGrist> result = new ArrayList<EntityGrist>();
  94.  
  95.         final int minChunkX = MathHelper.floor((bb.minX) / 16.0D);
  96.         final int maxChunkX = MathHelper.floor((bb.maxX) / 16.0D);
  97.         final int minChunkZ = MathHelper.floor((bb.minZ) / 16.0D);
  98.         final int maxChunkZ = MathHelper.floor((bb.maxZ) / 16.0D);
  99.         final int minChunkY = MathHelper.floor((bb.minY) / 16.0D);
  100.         final int maxChunkY = MathHelper.floor((bb.maxY) / 16.0D);
  101.  
  102.         for (int chunkX = minChunkX; chunkX <= maxChunkX; ++chunkX) {
  103.           for (int chunkZ = minChunkZ; chunkZ <= maxChunkZ; ++chunkZ) {
  104.             Chunk chunk = worldIn.getChunkFromChunkCoords(chunkX, chunkZ);
  105.             final ClassInheritanceMultiMap<Entity>[] entityLists = chunk.getEntityLists();
  106.             final int minChunkYClamped = MathHelper.clamp(minChunkY, 0, entityLists.length - 1);
  107.             final int maxChunkYClamped = MathHelper.clamp(maxChunkY, 0, entityLists.length - 1);
  108.             for (int chunkY = minChunkYClamped; chunkY <= maxChunkYClamped; ++chunkY) {
  109.               for (Entity entity : entityLists[chunkY]) {
  110.                 if (!entity.isDead && (entity instanceof EntityGrist) && entity.getEntityBoundingBox().intersects(bb)) {
  111.                   result.add((EntityGrist) entity);
  112.                  
  113.                     return result;
  114.                  
  115.                 }
  116.               }
  117.             }
  118.           }
  119.         }
  120.  
  121.         return result;
  122.       }
  123.    
  124. }
RAW Paste Data