Particle Trails - what's wrong with the code?

Discussion in 'Spigot Plugin Help' started by Aberforth, Mar 12, 2020.

  1. Code (Text):
    package me.a.trails.Events;

    import org.bukkit.Location;
    import org.bukkit.Particle;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.scheduler.BukkitRunnable;
    import org.bukkit.util.Vector;


    public class Events implements Listener {

        @EventHandler
        public void toggle(PlayerInteractEvent event){
                Player player = event.getPlayer();
                if (event.getAction() == Action.LEFT_CLICK_AIR){
                    new BukkitRunnable() {
                        double t = 0;
                        public void run() {
                            t = t + 0.5;
                            Location loc = player.getLocation();
                            Vector direction = loc.getDirection().normalize();
                            double x = direction.getX() * t;
                            double y = direction.getY() * t + 1.5;
                            double z = direction.getZ() * t;
                            loc.add(x,y,z);
                            player.spawnParticle(Particle.FIREWORKS_SPARK, loc, 1);
                         
                        if (t > 40) {
                            this.cancel();
                        }
                        }
                    };
                }
        }
    }
    I tried to make particle trails when a player left clicks air, I can't get it to work though. Help?
     
  2. You never start your bukkit runnable...
     
  3. You need to start running your task, but be careful not to use asynchronously as you are accessing the Bukkit api and it is not very safe to do it async.
     
  4. How do I do that? I'm new to java.
     
  5. You should always look up the docs of whatever you are working with to understand what it does. https://hub.spigotmc.org/javadocs/spigot/org/bukkit/scheduler/BukkitRunnable.html
    You could watch a tutorial on how BukkitRunnables work aswell, though I do not encourage you to copy any code at all, it is way better to understand and be able to reproduce.
     
  6. Code (Java):
    new BukkitRunnable()
                        {
                            @Override
                            public void run()
                            {
                                Bukkit.getPlayerExact(name).getOpenInventory().getTopInventory().setContents(event.getInventory().getContents());
                            }
                        }.runTaskLaterAsynchronously(PLUGIN, 1);
    There are many runTask difference based on what you're trying to achieve (runTaskLater, runTaskTimer, etc.). PLUGIN is your plugin instance and 1 is the delay in tick
     
    • Like Like x 1
  7. It gives me the "cannot be resolved to a variable" error. What exactly do I put in there?
     
  8. An instance of the main class.. the one that extends JavaPlugin
     
    • Like Like x 1
  9. Ok, I managed to do it, thanks for the help.