Way to speed up bukkit runnables?

Discussion in 'Plugin Development' started by man_in_matrix, Jan 13, 2022.

  1. Offline

    man_in_matrix

    So, I'm using a runnable to create a particle beam starting at the player and going forwards 1 block at the angle of the player's eye at the time of the runnable being activated. This code works perfectly however, the code is a bit slow to fully travel all of the blocks and I cannot speed up the particle because I do not want to sacrifice accuracy.

    Runnable :
    Code:
    package me.neo.customitems;
    
    import org.bukkit.Location;
    import org.bukkit.Particle;
    import org.bukkit.World;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.scheduler.BukkitRunnable;
    import org.bukkit.util.Vector;
    
    public class NinjaBladeAbility implements Listener {
      public static void teleportAura(Player player) {
     
          new BukkitRunnable() {
            
              double t = 0;
            @SuppressWarnings("unused")
            World world = player.getWorld();
              Location loc = player.getLocation();
              Vector direction = loc.getDirection().normalize();
              public void run() {
                  t = t + 1;
                  double x = direction.getX() * t;
                  double y = direction.getY() * t + 1;
                  double z = direction.getZ() * t;
                  loc.add(x,y,z);
                  player.spawnParticle(Particle.DRIP_LAVA, loc, 0, 0, 0, 0, 1);
                
              
                    for (Entity e : loc.getChunk().getEntities()) {
                          if (e.getLocation().distance(loc) < 2) {
                              if (!e.equals(player)) {
                                  player.teleport(e.getLocation());
                                  e.setGlowing(true);
                                
                               
                            
                          }
                      }
                    
                }
                
                  loc.subtract(x,y,z);
                
                  if (t > 30) {
                      this.cancel();
                    
                    
                    
                    
                  }             
              }        
          }.runTaskTimer(Main.getPlugin(Main.class), 0, 0);    
      }
    }
    [​IMG]

    Any way to make that beam travel instantly without sacrificing accuracy?

    Sorry for messing up grammar I'm tired.

    EDIT: proper image link https://imgur.com/gallery/1DB5CTI
     
  2. Offline

    The_Spaceman

    Another way to do this is by calculating all the points where a particle will spawn and at the end teleport the player to the end location.
    Or skip every other location for the player teleportation.
     

Share This Page