Advertisement
JackOUT

Untitled

Aug 24th, 2022
946
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.89 KB | Source Code | 0 0
  1. package games.coob.laserturrets.task;
  2.  
  3. import games.coob.laserturrets.model.TurretData;
  4. import games.coob.laserturrets.model.TurretRegistry;
  5. import games.coob.laserturrets.util.EntityUtil;
  6. import org.bukkit.Location;
  7. import org.bukkit.Material;
  8. import org.bukkit.block.Block;
  9. import org.bukkit.block.BlockFace;
  10. import org.bukkit.entity.LivingEntity;
  11. import org.bukkit.scheduler.BukkitRunnable;
  12. import org.bukkit.util.Vector;
  13. import org.mineacademy.fo.remain.CompMaterial;
  14. import org.mineacademy.fo.remain.CompParticle;
  15.  
  16. public class FlameTask extends BukkitRunnable {
  17.  
  18.     @Override
  19.     public void run() {
  20.         final TurretRegistry turretRegistry = TurretRegistry.getInstance();
  21.  
  22.         for (final TurretData turretData : turretRegistry.getFlameTurrets()) {
  23.             final Location location = turretData.getLocation();
  24.             final Block block = location.getBlock();
  25.             final int level = turretData.getCurrentLevel();
  26.             final int range = turretRegistry.getTurretRange(block, level);
  27.             final LivingEntity nearestEntity = EntityUtil.findNearestEntityNonBlacklisted(location, range, LivingEntity.class, block);
  28.  
  29.             if (nearestEntity == null)
  30.                 continue;
  31.  
  32.             final Location flameLocation = location.clone().add(0.5, 1.2, 0.5);
  33.             final Location targetLocation = nearestEntity.getLocation().add(0, 1.6, 0);
  34.             final Vector vector = targetLocation.subtract(flameLocation).toVector().normalize();
  35.             final double distance = location.distance(nearestEntity.getLocation());
  36.  
  37.             System.out.println("Distance: " + distance);
  38.             vector.add(new Vector(0, 0.1, 0));
  39.  
  40.             final double accuracy = 0.08;
  41.             vector.add(new Vector(Math.random() * accuracy - (accuracy / 2), Math.random() * accuracy - (accuracy / 2), Math.random() * accuracy - (accuracy / 2)));
  42.             for (double waypoint = 1; waypoint < distance; waypoint += 0.5) {
  43.                 vector.add(new Vector(0, -0.01, 0));
  44.                 flameLocation.add(vector);
  45.                 CompParticle.FLAME.spawn(flameLocation);
  46.             }
  47.         }
  48.     }
  49.  
  50.     private void shootFlames(final LivingEntity target, final Block turretBlock, final int length) {
  51.         final Location turretLocation = turretBlock.getLocation();
  52.         final Location targetLocation = target.getLocation().add(-0.5, 1, -0.5);
  53.         // vector.add(new Vector(Math.random() * accuracy - (accuracy / 2), Math.random() * accuracy - (accuracy / 2), Math.random() * accuracy - (accuracy / 2)));
  54.  
  55.         final Location flameLocation = turretLocation.clone();
  56.  
  57.         flameLocation.setY(turretLocation.getY() + 1.2);
  58.         flameLocation.setX(turretLocation.getX() + 0.5);
  59.         flameLocation.setZ(turretLocation.getZ() + 0.5);
  60.  
  61.         final double dX = flameLocation.getX() - targetLocation.getX();
  62.         final double dY = flameLocation.getY() - targetLocation.getY();
  63.         final double dZ = flameLocation.getZ() - targetLocation.getZ();
  64.  
  65.         final double yaw = Math.atan2(dZ, dX);
  66.         final double pitch = Math.atan2(Math.sqrt(dZ * dZ + dX * dX), dY) + Math.PI;
  67.  
  68.         final double X = Math.sin(pitch) * Math.cos(yaw);
  69.         final double Y = Math.sin(pitch) * Math.sin(yaw);
  70.         final double Z = Math.cos(pitch);
  71.  
  72.         final Vector vector = new Vector(X, Y, Z);
  73.  
  74.         for (double waypoint = 1; waypoint < length; waypoint += 0.5) {
  75.             flameLocation.add(vector);
  76.             CompParticle.FLAME.spawn(flameLocation);
  77.             /*final int randomInt = RandomUtil.nextInt(100);
  78.  
  79.             if (randomInt < 6)
  80.                 setFireToBlock(flameLocation.getBlock());*/
  81.         }
  82.     }
  83.  
  84.     private Vector createVectorBetweenLocations(final Location fromLocation, final Location toLocation) {
  85.         final double dX = fromLocation.getX() - toLocation.getX();
  86.         final double dY = fromLocation.getY() - toLocation.getY();
  87.         final double dZ = fromLocation.getZ() - toLocation.getZ();
  88.  
  89.         final double yaw = Math.atan2(dZ, dX);
  90.         final double pitch = Math.atan2(Math.sqrt(dZ * dZ + dX * dX), dY) + Math.PI;
  91.  
  92.         final double X = Math.sin(pitch) * Math.cos(yaw);
  93.         final double Y = Math.sin(pitch) * Math.sin(yaw);
  94.         final double Z = Math.cos(pitch);
  95.  
  96.         return new Vector(X, Z, Y);
  97.     }
  98.  
  99.  
  100.     private Vector vectorOffset(double yaw, double pitch) {
  101.         final int variation = 10; //Variation in degrees
  102.         final int var2 = variation * 2; //Used later for randomization
  103.         final double variationYaw = ((Math.random() * variation) - var2); //Vary by between 10 and -10 degrees
  104.         final double variationPitch = ((Math.random() * variation) - var2);
  105.  
  106.         yaw += variationYaw;
  107.         pitch += variationPitch;
  108.  
  109.         final double X = Math.sin(Math.toRadians(pitch));
  110.         final double Y = Math.sin(Math.toRadians(yaw));
  111.         final double Z = Math.cos(Math.toRadians(yaw));
  112.  
  113.         /*final double X = Math.sin(pitch) * Math.cos(yaw);
  114.         final double Y = Math.sin(pitch) * Math.sin(yaw);
  115.         final double Z = Math.cos(pitch);*/
  116.  
  117.         return new Vector(X, Y, Z);
  118.     }
  119.  
  120.     private void test() {
  121.         /*final int length = 50; // show twenty blocks ahead
  122.         final Location laserLocation = location.clone();
  123.  
  124.         laserLocation.setY(location.getY() + 1.2);
  125.         laserLocation.setX(location.getX() + 0.5);
  126.         laserLocation.setZ(location.getZ() + 0.5);
  127.  
  128.         final double dX = laserLocation.getX() - nearestEntity.getLocation().getX();
  129.         final double dY = laserLocation.getY() - nearestEntity.getLocation().getY();
  130.         final double dZ = laserLocation.getZ() - nearestEntity.getLocation().getZ();
  131.  
  132.         final double yaw = Math.atan2(dZ, dX);
  133.         final double pitch = Math.atan2(Math.sqrt(dZ * dZ + dX * dX), dY) + Math.PI;
  134.  
  135.         final double X = Math.sin(pitch) * Math.cos(yaw);
  136.         final double Y = Math.sin(pitch) * Math.sin(yaw);
  137.         final double Z = Math.cos(pitch);
  138.  
  139.         final Vector vector = new Vector(X, Z, Y);
  140.  
  141.         for (double waypoint = 1; waypoint < length; waypoint += 0.5) {
  142.             laserLocation.add(vector);
  143.             CompParticle.REDSTONE.spawn(laserLocation);
  144.         }*/
  145.     }
  146.  
  147.     private void setFireToBlock(final Block block) {
  148.         if (!CompMaterial.isAir(block)) {
  149.             final Block blockUp = block.getRelative(BlockFace.UP);
  150.  
  151.             if (CompMaterial.isAir(blockUp) && blockUp.getRelative(BlockFace.DOWN).getType().isSolid())
  152.                 blockUp.setType(Material.FIRE);
  153.         }
  154.     }
  155. }
  156.  
  157.  
  158.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement