Solved Particle Vector Not Working

Discussion in 'Spigot Plugin Development' started by User5546, Jun 15, 2018.

  1. Hey, I've recently began learning bukkit and java (3 weeks ago), then embarked on the math for particle shapes, ex. rotation matricies, vectors, polar and spherical coordinates, etc.

    Anyway, to get to the point, I have stumbled upon a yt channel that taught how to harmonize all of the characteristics above and utilize them into MC. After following step-by step, at the end of the tutorial, it seemed like my code did not work like his did (meaning the code did not spawn anything when I left clicked). Usually I figure the issue out after debugging for a considerable amount of time, however that did not seem to be the case this now.

    Here is the code:

    Code (Java):
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.Particle;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.scheduler.BukkitRunnable;
    import org.bukkit.util.Vector;

        public class VectBeam {
           
            Plugin VectBeam = Bukkit.getPluginManager().getPlugin("VectBeam");
           

            @EventHandler
        public void toggle(PlayerInteractEvent event){
            final 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.getWorld().spawnParticle(Particle.DRIP_LAVA, player.getLocation().getX(), player.getLocation().getY(), player.getLocation().getZ(), 50, 0, 0, 0);
                        loc.subtract(x,y,z);
                       
                    if (t > 30){
                        this.cancel();
                    }
                    }
                }.runTaskTimer(VectBeam, 0, 1);
            }
        }
     
  2. Did it spawn anything? If not, make sure your event listener was registered.

    if it is spawning something, but it's just a single particle at the player's location- that's because you're not using "loc" when spawning the particle. So you're calculating a location but never using it- you should put it in the spawnParticle() call instead of player.getLocation().
     
  3. Nah, nothing spawned. I've tried implementing an event listener and extend it to a JavaPlugin, however nothing changed. I don't get any console errors so can't really check that out.

    Here is the code now that I tweaked it a bit:



    Code (Text):


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

        public class VectBeam extends JavaPlugin implements Listener {
           
            Plugin VectBeam = Bukkit.getPluginManager().getPlugin("VectBeam");
           
               
        @EventHandler
        public void toggle (PlayerInteractEvent event){
            final 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.getWorld().spawnParticle(Particle.DRIP_LAVA, player.getLocation().getX(), player.getLocation().getY(), player.getLocation().getZ(), 50, 0, 0, 0);
                        loc.subtract(x,y,z);
                       
                    if (t > 30){
                        this.cancel();
                    }
                    }
                }.runTaskTimer(VectBeam, 0L, 1L);
            }
        }



    }
               
           
       
       
     
     
  4. I don't think this is having the effect you're intending. this won't really create any shape, just create a straight line coming from the player's eye's going outward. In this case, it would be far, far more effective to do this in a for loop and store a location and vector outside of it, then within the for loop calling location.add(vector) and location.getWorld.spawnParticle(), something to the tune of this:
    Code (Java):
    Location startLocation;
    Vector direction.normalize().multiply(0.5);
    for (int i = 0; i > 60; i++) {
        startLocation.getWorld().spawnParticle();
        startLocation.add(direction);
    }
    the runnable should not be necessary unless you're trying to make some sort of animation, in which case the for loop would just need to be converted to a runnable in your code.
    More importantly, your variable VectBeam (which should be in lowerCamelCase, as using UpperCamelCase will make people trying to help you think its a class or object instead of a variable), will return null as you are trying to get the plugin before it has loaded. IF this is your main class, you can simply call "this."
    However, if this is indeed your main class, you need to register this event! You have no onEnable method, so you will have to add that and call
    Code (Java):
    this.getServer().getPluginManager().registerEvents(this, this);
    If this is not your main class, VectBeam will still return null as the plugin has not been loaded at the time of you calling that. Instead of using that variable in your BukkitRunnable, call
    Code (Java):
    MainClass.getProvidingPlugin(MainClass.class)
    and make sure to register this event still by doing
    Code (Java):
    this.getServer().getPluginManager().registerEvents(new VectBeam(), this);
    As based on your code you seem to just be beginning with java development, here are a few links to catch you up on these little things that will even stop the best of developers every once and a while:
    https://www.spigotmc.org/wiki/crash-course-to-java/
    https://www.spigotmc.org/wiki/creating-a-blank-spigot-plugin-in-intellijidea/
    https://www.spigotmc.org/wiki/create-a-simple-command/
    https://www.spigotmc.org/wiki/using-the-event-api/
    https://www.spigotmc.org/wiki/plugin-yml/

    browse through these, make some corrections, and see if that fixes your problem!
    (As a side note, here's a very helpful link that will help you throughout your entire development career here on the spigot forums: https://hub.spigotmc.org/javadocs/spigot/index.html?overview-summary.html)
     
    • Winner Winner x 1