Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.95 KB | None | 0 0
  1. package io.github.bananapuncher714.operation.gunsmoke.api.entity.projectile;
  2.  
  3. import java.util.HashSet;
  4. import java.util.List;
  5. import java.util.Set;
  6. import java.util.TreeSet;
  7. import java.util.UUID;
  8.  
  9. import org.bukkit.Location;
  10. import org.bukkit.Material;
  11. import org.bukkit.block.Block;
  12. import org.bukkit.entity.Entity;
  13.  
  14. import io.github.bananapuncher714.operation.gunsmoke.api.EnumTickResult;
  15. import io.github.bananapuncher714.operation.gunsmoke.api.entity.GunsmokeEntity;
  16. import io.github.bananapuncher714.operation.gunsmoke.api.entity.bukkit.GunsmokeEntityWrapper;
  17. import io.github.bananapuncher714.operation.gunsmoke.api.util.ProjectileTarget;
  18. import io.github.bananapuncher714.operation.gunsmoke.api.util.ProjectileTargetBlock;
  19. import io.github.bananapuncher714.operation.gunsmoke.api.util.ProjectileTargetEntity;
  20. import io.github.bananapuncher714.operation.gunsmoke.core.util.GunsmokeUtil;
  21. import io.github.bananapuncher714.operation.gunsmoke.core.util.VectorUtil;
  22.  
  23. public abstract class GunsmokeProjectile extends GunsmokeEntity {
  24.     private Set< UUID > hitEntities;
  25.     private Set< Location > hitBlocks;
  26.    
  27.     public GunsmokeProjectile( Location location ) {
  28.         super( location );
  29.        
  30.         hitEntities = new HashSet< UUID >();
  31.         hitBlocks = new HashSet< Location >();
  32.     }
  33.  
  34.     @Override
  35.     public EnumTickResult tick() {
  36.         // First get a set of all things hit, then call events in order from closest hit to farthest hit
  37.         Set< ProjectileTarget > hitTargets = new TreeSet< ProjectileTarget >();
  38.        
  39.         // Detect if this hit any entities
  40.         // TODO Add way to hit Gunsmoke entities too...
  41.         List< Entity > nearbyEntities = GunsmokeUtil.getNearbyEntities( null, location, velocity );
  42.         for ( Entity entity : nearbyEntities ) {
  43.             if ( getHitEntities().contains( entity.getUniqueId() ) ) {
  44.                 continue;
  45.             }
  46.             Location intersection = VectorUtil.rayIntersect( entity, location, velocity );
  47.             if ( intersection != null ) {
  48.                 // We know we hit something
  49.                 GunsmokeEntityWrapper wrappedEntity = new GunsmokeEntityWrapper( entity );
  50.                
  51.                 hitTargets.add( new ProjectileTargetEntity( this, intersection, wrappedEntity ) );
  52.                 getHitEntities().add( entity.getUniqueId() );
  53.             }
  54.         }
  55.        
  56.         // Now start on block hit detection
  57.         // Get the point where our projectile should be after this tick
  58.         Location destination = location.clone().add( velocity );
  59.         // Get the distance squared because getting the root is a scam
  60.         double distance = location.distanceSquared( destination );
  61.  
  62.         // Don't want to detect the same block twice
  63.         Set< Block > hitBlocks = new HashSet< Block >();
  64.         // Don't hit the current block we're in
  65.         hitBlocks.add( location.getBlock() );
  66.         // Get the first iteration done
  67.         Location hitBlock = GunsmokeUtil.rayTrace( location, velocity );
  68.         // While we still haven't reached the full destination
  69.         while ( distance >= location.distanceSquared( hitBlock ) ) {
  70.             Block block = hitBlock.getBlock();
  71.            
  72.             // TODO Add GunsmokeStructure detection
  73.             if ( !getHitBlocks().contains( block.getLocation() ) ) {
  74.                 if ( block.getType() != Material.AIR ) {
  75.                     if ( !hitBlocks.contains( block ) ) {
  76.                         hitTargets.add( new ProjectileTargetBlock( this, hitBlock, block ) );
  77.                         hitBlocks.add( block );
  78.                         getHitBlocks().add( block.getLocation() );
  79.                     }
  80.                 }
  81.             }
  82.            
  83.             hitBlock = GunsmokeUtil.rayTrace( hitBlock, velocity );
  84.         }
  85.        
  86.         // Now that we have our targets we can start calling our other methods in order
  87.         // Event calling should be handled by the implementation
  88.         for ( ProjectileTarget target : hitTargets ) {
  89.             hit( target );
  90.         }
  91.         location.add( velocity );
  92.        
  93.         // Erase this projectile from existence if it falls beyond the void
  94.         return ( location.getY() > -64 ) ? EnumTickResult.CONTINUE : EnumTickResult.CANCEL;
  95.     }
  96.    
  97.     protected Set< UUID > getHitEntities() {
  98.         return hitEntities;
  99.     }
  100.    
  101.     protected Set< Location > getHitBlocks() {
  102.         return hitBlocks;
  103.     }
  104.  
  105.     abstract public void hit( ProjectileTarget target );
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement