Advertisement
naturaGodhead

Untitled

Jul 21st, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.57 KB | None | 0 0
  1. package com.natura.minestuckarsenal.tileentity;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.UUID;
  6.  
  7. import javax.annotation.Nonnull;
  8.  
  9. import com.mraof.minestuck.entity.item.EntityGrist;
  10. import com.mraof.minestuck.util.GristHelper;
  11. import com.mraof.minestuck.util.GristSet;
  12. import com.mraof.minestuck.util.GristType;
  13. import com.mraof.minestuck.util.IdentifierHandler;
  14. import com.mraof.minestuck.util.IdentifierHandler.PlayerIdentifier;
  15.  
  16. import net.minecraft.entity.Entity;
  17. import net.minecraft.nbt.NBTTagCompound;
  18. import net.minecraft.tileentity.TileEntity;
  19. import net.minecraft.util.ClassInheritanceMultiMap;
  20. import net.minecraft.util.ITickable;
  21. import net.minecraft.util.math.AxisAlignedBB;
  22. import net.minecraft.util.math.MathHelper;
  23. import net.minecraft.world.World;
  24. import net.minecraft.world.chunk.Chunk;
  25. import net.minecraftforge.fml.common.FMLCommonHandler;
  26. import net.minecraftforge.fml.relauncher.Side;
  27. import net.minecraftforge.fml.relauncher.SideOnly;
  28. import net.minecraftforge.fml.server.FMLServerHandler;
  29.  
  30. /* Thanks to Ender IO team for the entity selection code. */
  31.  
  32. public class TileEntityGristGatherer extends TileEntity implements ITickable {
  33.  
  34.     int counter = 0;
  35.     private int range = 8;
  36.     private PlayerIdentifier player;
  37.     private int ownerId;
  38.    
  39.     public TileEntityGristGatherer() {
  40.        
  41.     }
  42.  
  43.     public @Nonnull AxisAlignedBB getBounds() {
  44.         return new AxisAlignedBB(getPos()).expand(getRange() + (range == 0 ? 1 / 32f : 0), 0, getRange() + (range == 0 ? 1 / 32f : 0)).expand(-(getRange() + (range == 0 ? 1 / 32f : 0)), 0, -(getRange() + (range == 0 ? 1 / 32f : 0)));
  45.      }
  46.  
  47.      public float getRange() {
  48.         return range;
  49.      }
  50.      
  51.      public PlayerIdentifier setPlayer(PlayerIdentifier player) {
  52.         this.player = player;
  53.         return this.player;
  54.      }
  55.      
  56.      public PlayerIdentifier getPlayer() {
  57.          return this.player;
  58.      }
  59.      
  60.      @Override
  61.      public NBTTagCompound writeToNBT(NBTTagCompound compound) {
  62.         super.writeToNBT(compound);
  63.         player.saveToNBT(compound, "ident");
  64.         return compound;
  65.      }
  66.        
  67.      @Override
  68.      public void readFromNBT(NBTTagCompound compound) {
  69.         super.readFromNBT(compound);
  70.         if(FMLCommonHandler.instance().getMinecraftServerInstance() != null) {
  71.             this.player = IdentifierHandler.load(compound, "ident");
  72.         }
  73.      }
  74.    
  75.     @Override
  76.     public void update() {
  77.        
  78.         if(counter == 20) {
  79.            
  80.             List<EntityGrist> list = selectEntitiesWithinAABB(this.getWorld(), getBounds());
  81.            
  82.             if(list.size() != 0) {
  83.                     for(int i = 0; i == list.size() - 1; i++)
  84.                     {
  85.                         if(player != null && list.get(i) != null) {
  86.                             NBTTagCompound nbtData = new NBTTagCompound();
  87.                             list.get(i).writeToNBT(nbtData);
  88.                             int value = nbtData.getShort("Value");
  89.                             GristType type = GristType.getTypeFromString(nbtData.getString("Type"));
  90.                                 if(value != 0 && type != null) {
  91.                                     GristHelper.increase(player, new GristSet(type, value));
  92.                                 }
  93.                             list.get(i).setDead();
  94.                         }
  95.                     }
  96.             }
  97.            
  98.             counter = 0;
  99.         }
  100.        
  101.         else {
  102.             counter++;
  103.         }
  104.        
  105.         this.markDirty();
  106.     }
  107.    
  108.     private List<EntityGrist> selectEntitiesWithinAABB(World worldIn, AxisAlignedBB bb) {
  109.         List<EntityGrist> result = new ArrayList<EntityGrist>();
  110.  
  111.         final int minChunkX = MathHelper.floor((bb.minX) / 16.0D);
  112.         final int maxChunkX = MathHelper.floor((bb.maxX) / 16.0D);
  113.         final int minChunkZ = MathHelper.floor((bb.minZ) / 16.0D);
  114.         final int maxChunkZ = MathHelper.floor((bb.maxZ) / 16.0D);
  115.         final int minChunkY = MathHelper.floor((bb.minY) / 16.0D);
  116.         final int maxChunkY = MathHelper.floor((bb.maxY) / 16.0D);
  117.  
  118.         for (int chunkX = minChunkX; chunkX <= maxChunkX; ++chunkX) {
  119.           for (int chunkZ = minChunkZ; chunkZ <= maxChunkZ; ++chunkZ) {
  120.             Chunk chunk = worldIn.getChunkFromChunkCoords(chunkX, chunkZ);
  121.             final ClassInheritanceMultiMap<Entity>[] entityLists = chunk.getEntityLists();
  122.             final int minChunkYClamped = MathHelper.clamp(minChunkY, 0, entityLists.length - 1);
  123.             final int maxChunkYClamped = MathHelper.clamp(maxChunkY, 0, entityLists.length - 1);
  124.             for (int chunkY = minChunkYClamped; chunkY <= maxChunkYClamped; ++chunkY) {
  125.               for (Entity entity : entityLists[chunkY]) {
  126.                 if (!entity.isDead && (entity instanceof EntityGrist) && entity.getEntityBoundingBox().intersects(bb)) {
  127.                   result.add((EntityGrist) entity);
  128.                  
  129.                     return result;
  130.                  
  131.                 }
  132.               }
  133.             }
  134.           }
  135.         }
  136.  
  137.         return result;
  138.       }
  139.    
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement