BukkitScheduler doesn't repeat the task correctly

19 views Asked by At

I am trying to do a grenade launcher, and I want to be able to code an animation of the grenade when the player launches it with a right click (= I want the player to be able to see the grenade before she hits the ground) To do this, I tried to do things with BukkitScheduler but I don't know why it doesn't work.

double timer = 5;
    double t = 0.0;
    @EventHandler
    public void onRightClickWeapon(PlayerInteractEvent e) {
        Player p = e.getPlayer();
        Action a = e.getAction();
        
        if (a==Action.RIGHT_CLICK_AIR || a==Action.RIGHT_CLICK_BLOCK) {
            if (p.getItemInHand().getType() == this.gMat.getType() && p.getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase(this.gMat.getItemMeta().getDisplayName())) {
                
                
                BukkitScheduler scheduler = Bukkit.getServer().getScheduler();
                scheduler.runTaskLater(Bukkit.getPluginManager().getPlugin("ShooterPlugin"), () -> {
                    timer = 0;
                }, 100L);
                
                EnumParticle trail = EnumParticle.CLOUD;
                
                Location x1 = p.getLocation();
                Vector director = p.getEyeLocation().getDirection();
                
                double g = 4.72;
                
                scheduler.runTaskTimer(Bukkit.getPluginManager().getPlugin("ShooterPlugin"), () -> {
                    Vector pos = new Vector(director.getX() * t, 
                            -(g*Math.pow(t*0.25, 2)) / 2 + director.getY()*t*0.25 + p.getEyeHeight(), director.getZ() * t);
                    Location grenadeLoc = p.getEyeLocation();

                    Location particleLoc = x1.add(pos);
                    grenadeLoc = grenadeLoc.add(pos);
                    
                    PacketPlayOutWorldParticles packetParticle = new PacketPlayOutWorldParticles(trail, false, (float) particleLoc.getX(), (float) particleLoc.getY(), 
                            (float) particleLoc.getZ(),0,0,0,0,0, null);
                    CraftPlayer cp = (CraftPlayer) p;
                    cp.getHandle().playerConnection.sendPacket(packetParticle);

                    t += 1; 
                    
                },0 , 8L);
                
                
                
            }
        }
    }

When I run this code, it lets me do the thing 1 time, and I can't do it anymore after

0

There are 0 answers