Solved Getting if player is moving

Discussion in 'Plugin Development' started by Leviticalpixel10, Dec 27, 2016.

Thread Status:
Not open for further replies.
  1. Hi, I've accidentally somehow wrecked a project I was working on and it was erased from my hard drive, so now I'm writing it over again, trying to use more efficient methods of doing things. I want to figure out when a player is moving so that the particle effects that are in the plugin don't screw up with the location. I've been using this method:
    //this is created outside the run portion of a runnable
    Location location = new Location(playerworld,playerx,y,z);
    //running every two ticks
    public void run(){
    Location loc = player.getlocation()
    if(location.equals(new location(,loc.x,loc.y,loc.z))){
    //do stuff
    } else {
    //do more stuff but not the other stuff
    So I'm wondering if there is a more efficient way to see if a player is moving or not, especially because the effects flicker slightly when your moving and shouldn't be the same effect. If anyone can help he that would be wonderful!
  2. Offline


    @Leviticalpixel10 Try using a PlayerMoveEvent and check if their location is the one that you defined.
  3. Offline


    Also make sure you're checking if they moved a full block to prevent lag / unneeded checks
  4. @RenditionsRule I don't have a set location, as I just want the particles to do something when the player is still, but it can be anywhere. In this case, I was setting the location to be the the player's location after the code was executed, so that it was always checking after he moved to see if the player was still again
    hope that makes sense.

    @kameronn how would I go about rounding the coordinates up or down?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
  5. Offline


    To check if a player is moving:
    1. on player move event.
    2. If the event.getTo is the same as event.getFrom
    3. the player is not moving.
  6. @Zombie_Striker is there a way to put that into the method I have? here's all of the code for my class right now:
    Show Spoiler

    package org.ldsessions.particle;
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.craftbukkit.v1_11_R1.entity.CraftPlayer;
    import org.bukkit.entity.Player;
    import org.bukkit.scheduler.BukkitRunnable;
    import org.bukkit.scheduler.BukkitTask;
    import org.bukkit.util.Vector;
    import org.ldsessions.ldsessions;
    import org.ldsessions.util.MethodsUtil;
    import org.ldsessions.util.ParticleTypes;
    import net.minecraft.server.v1_11_R1.EnumParticle;
    import net.minecraft.server.v1_11_R1.PacketPlayOutWorldParticles;
    public class ParticleEffect {
        private Player player;
        private ParticleTypes effects;
        private String particle = "";
        public ParticleEffect() {
            setEffects(new ParticleTypes(EnumParticle.FLAME));
        public ParticleEffect(Player player) {
            setEffects(new ParticleTypes(EnumParticle.FLAME));
        public ParticleEffect(Player player, ParticleTypes effects) {
        @SuppressWarnings({ "unused" })
        public void helix(double radius) {
  , "helix");
            BukkitTask loop = new BukkitRunnable() {
                double t = 0;
                double k = 0;
                double r = radius;
                Location location = new Location(player.getWorld(), player.getLocation().getX(),
                        player.getLocation().getY(), player.getLocation().getZ());
                int q = 0;
                public void run() {
                    Location loc = player.getLocation();
                    if (location.equals(new Location(loc.getWorld(), loc.getX(), loc.getY(), loc.getZ()))) {
                        double x = r * Math.sin(t);
                        double y = k;
                        double z = r * Math.cos(t);
                        double x2 = -r * Math.sin(t);
                        double y2 = k;
                        double z2 = -r * Math.cos(t);
                        if (getEffects().getLenth() == 1) {
                            loc.add(x, y, z);
                            MethodsUtil.spawnParticles(getEffects().getParticle1(), loc, 1, 0);
                            loc.subtract(x, y, z);
                            loc.add(x2, y2, z2);
                            MethodsUtil.spawnParticles(getEffects().getParticle1(), loc, 1, 0);
                            loc.subtract(x2, y2, z2);
                        } else {
                            loc.add(x, y, z);
                            MethodsUtil.spawnParticles(getEffects().getParticle1(), loc, 1, 0);
                            loc.subtract(x, y, z);
                            loc.add(x2, y2, z2);
                            MethodsUtil.spawnParticles(getEffects().getParticle2(), loc, 1, 0);
                            loc.subtract(x2, y2, z2);
                        switch (q) {
                        case 0:
                            k = k + Math.PI / 64;
                        case 1:
                            k = k - Math.PI / 64;
                        case 2:
                        t = t + Math.PI / 16;
                        if (k >= 2.20) {
                            k = 2.19999999;
                            q = 2;
                            Bukkit.getScheduler().scheduleAsyncDelayedTask(ldsessions.plugin, new Runnable() {
                                public void run() {
                                    q = 1;
                            }, 15);
                        if (k <= 0) {
                            k = .000001;
                            q = 2;
                            Bukkit.getScheduler().scheduleAsyncDelayedTask(ldsessions.plugin, new Runnable() {
                                public void run() {
                                    q = 0;
                            }, 10);
                        loc = player.getLocation();
                        location = new Location(loc.getWorld(), loc.getX(), loc.getY(), loc.getZ());
                    } else if (!location.equals(new Location(loc.getWorld(), loc.getX(), loc.getY(), loc.getZ()))) {
                        k = 0;
                        q = 0;
                        double x = 0.5 * Math.sin(t + loc.getYaw());
                        double y = 0.5 * Math.cos(t) + 1;
                        double z = 0;
                        MethodsUtil.spawnParticles(getEffects().getParticle1(), loc, 1, (int) 0.03);
                        loc.subtract(x, y, z);
                        t = t + Math.PI / 16;
                        loc = player.getLocation();
                        location = new Location(loc.getWorld(), loc.getX(), loc.getY(), loc.getZ());
                    if (!( == "helix")) {
            }.runTaskTimer(ldsessions.plugin, 0, 1);
        public void flameoftitans() {
  , "flameoftitans");
            setParticle("Flame of the Titans");
            player.sendMessage(MethodsUtil.prefix + " You have started Flame of the Titans!");
            BukkitTask loop = new BukkitRunnable() {
                double r = 2.75;
                double k = 0.2;
                double positivet = 0;
                double negativet = 0;
                Location location = new Location(player.getWorld(), player.getLocation().getX(),
                        player.getLocation().getY(), player.getLocation().getZ());
                int q = 1;
                public void run() {
                    Location loc = player.getLocation();
                    if (location.equals(new Location(loc.getWorld(), loc.getX(), loc.getY(), loc.getZ()))) {
                        Location playerlocplus2 = loc.add(0, 0.2, 0);
                        double x1 = r * Math.sin(positivet);
                        double y1 = k;
                        double z1 = r * Math.cos(positivet);
                        double x2 = -r * Math.sin(positivet);
                        double y2 = k;
                        double z2 = -r * Math.cos(positivet);
                        double x3 = r * Math.sin(positivet + 77);
                        double y3 = k;
                        double z3 = r * Math.cos(positivet + 77);
                        double x4 = -r * Math.sin(positivet + 77);
                        double y4 = k;
                        double z4 = -r * Math.cos(positivet + 77);
                        Location loc1 = new Location(loc.getWorld(), loc.getX() + x1, loc.getY() + y1, loc.getZ() + z1);
                        Location loc2 = new Location(loc.getWorld(), loc.getX() + x2, loc.getY() + y2, loc.getZ() + z2);
                        Location loc3 = new Location(loc.getWorld(), loc.getX() + x3, loc.getY() + y3, loc.getZ() + z3);
                        Location loc4 = new Location(loc.getWorld(), loc.getX() + x4, loc.getY() + y4, loc.getZ() + z4);
                        Vector vec1 = MethodsUtil.getVectorTo(loc1, playerlocplus2);
                        Vector vec2 = MethodsUtil.getVectorTo(loc2, playerlocplus2);
                        Vector vec3 = MethodsUtil.getVectorTo(loc3, playerlocplus2);
                        Vector vec4 = MethodsUtil.getVectorTo(loc4, playerlocplus2);
                        PacketPlayOutWorldParticles packet1 = new PacketPlayOutWorldParticles(EnumParticle.FLAME, false,
                                (float) loc1.getX(), (float) loc1.getY(), (float) loc1.getZ(), (float) vec1.getX(),
                                (float) vec1.getY(), (float) vec1.getZ(), (float) 0.055, 0, 0);
                        PacketPlayOutWorldParticles packet2 = new PacketPlayOutWorldParticles(EnumParticle.FLAME, false,
                                (float) loc2.getX(), (float) loc2.getY(), (float) loc2.getZ(), (float) vec2.getX(),
                                (float) vec2.getY(), (float) vec2.getZ(), (float) 0.055, 0, 0);
                        PacketPlayOutWorldParticles packet3 = new PacketPlayOutWorldParticles(EnumParticle.FLAME, false,
                                (float) loc3.getX(), (float) loc3.getY(), (float) loc3.getZ(), (float) vec3.getX(),
                                (float) vec3.getY(), (float) vec3.getZ(), (float) 0.055, 0, 0);
                        PacketPlayOutWorldParticles packet4 = new PacketPlayOutWorldParticles(EnumParticle.FLAME, false,
                                (float) loc4.getX(), (float) loc4.getY(), (float) loc4.getZ(), (float) vec4.getX(),
                                (float) vec4.getY(), (float) vec4.getZ(), (float) 0.055, 0, 0);
                        double x5 = r * Math.sin(negativet);
                        double y5 = k;
                        double z5 = r * Math.cos(negativet);
                        double x6 = -r * Math.sin(negativet);
                        double y6 = k;
                        double z6 = -r * Math.cos(negativet);
                        double x7 = r * Math.sin(negativet + 77);
                        double y7 = k;
                        double z7 = r * Math.cos(negativet + 77);
                        double x8 = -r * Math.sin(negativet + 77);
                        double y8 = k;
                        double z8 = -r * Math.cos(negativet + 77);
                        Location loc5 = new Location(loc.getWorld(), loc.getX() + x5, loc.getY() + y5, loc.getZ() + z5);
                        Location loc6 = new Location(loc.getWorld(), loc.getX() + x6, loc.getY() + y6, loc.getZ() + z6);
                        Location loc7 = new Location(loc.getWorld(), loc.getX() + x7, loc.getY() + y7, loc.getZ() + z7);
                        Location loc8 = new Location(loc.getWorld(), loc.getX() + x8, loc.getY() + y8, loc.getZ() + z8);
                        Vector vec5 = MethodsUtil.getVectorTo(loc5, playerlocplus2);
                        Vector vec6 = MethodsUtil.getVectorTo(loc6, playerlocplus2);
                        Vector vec7 = MethodsUtil.getVectorTo(loc7, playerlocplus2);
                        Vector vec8 = MethodsUtil.getVectorTo(loc8, playerlocplus2);
                        PacketPlayOutWorldParticles packet5 = new PacketPlayOutWorldParticles(EnumParticle.FLAME, false,
                                (float) loc5.getX(), (float) loc5.getY(), (float) loc5.getZ(), (float) vec5.getX(),
                                (float) vec5.getY(), (float) vec5.getZ(), (float) 0.055, 0, 0);
                        PacketPlayOutWorldParticles packet6 = new PacketPlayOutWorldParticles(EnumParticle.FLAME, false,
                                (float) loc6.getX(), (float) loc6.getY(), (float) loc6.getZ(), (float) vec6.getX(),
                                (float) vec6.getY(), (float) vec6.getZ(), (float) 0.055, 0, 0);
                        PacketPlayOutWorldParticles packet7 = new PacketPlayOutWorldParticles(EnumParticle.FLAME, false,
                                (float) loc7.getX(), (float) loc7.getY(), (float) loc7.getZ(), (float) vec7.getX(),
                                (float) vec7.getY(), (float) vec7.getZ(), (float) 0.055, 0, 0);
                        PacketPlayOutWorldParticles packet8 = new PacketPlayOutWorldParticles(EnumParticle.FLAME, false,
                                (float) loc8.getX(), (float) loc8.getY(), (float) loc8.getZ(), (float) vec8.getX(),
                                (float) vec8.getY(), (float) vec8.getZ(), (float) 0.055, 0, 0);
                        CraftPlayer p = (CraftPlayer) player;
                        positivet += Math.PI / 32;
                        negativet -= Math.PI / 32;
                        loc = player.getLocation();
                        location = new Location(loc.getWorld(), loc.getX(), loc.getY(), loc.getZ());
                    } else {
                        q = 0;
                        loc = player.getLocation();
                        location = new Location(loc.getWorld(), loc.getX(), loc.getY(), loc.getZ());
                    if (!( == "flameoftitans")) {
            }.runTaskTimer(ldsessions.plugin, 0, 2);
        public void stop() {
        public Player getPlayer() {
            return player;
        public void setPlayer(Player player) {
            this.player = player;
        public ParticleTypes getEffects() {
            return effects;
        public void setEffects(ParticleTypes effects) {
            this.effects = effects;
        public String getParticle() {
            return particle;
        private void setParticle(String particle) {
            this.particle = particle;
  7. Offline


    Here is what you will need to do.
    1. Create a new class that will implement listener.
    2. Inside that class, make the event as described above.
    3. Go into the main class and register this new listener.
  8. @Zombie_Striker , I know how to make an event, but is there a way to implement a listener into the class right there?

    By the way, your 'no spoonfeed' policy isn't very good, especially for visual learners. Not saying give me all the code I need to make a plugin, I'm saying rather than be like do this - give an example of the code in different context, or at least say more than oh go do this like that person may be new to the API and not no what the object or class thing is.
    Last edited: Dec 27, 2016
  9. Offline


    Although true, there are some problems with spoonfeeding:
    (Bold ones apply to me/ experienced coders. The non-bold ones apply to other members that are not me since they may not know how to code/ don't test their code. )

    For people who are new to the API, we do use psudocode or post only one line if needed. (for example, changing "¶a Message" to ChatColor.RED+"message", but we only do that when the member is not getting the idea/ we know that they know java, but made a mistake)

    For situations that are not API related, and code structure related (like this post), we don't do it because it shows you do not have the right mindset for making plugin/programs, generally meaning they are new to Java. Although we help members that are new to Bukkit, they need to know Java before we can help them. We don't help new Java devs because Bukkit is not meant for newcomers to jump into without any programming background and because helping them now with code will force them to come back here every time they have a problem, giving them no motivation to learn Java and understand the language they are writing in (meaning we get more posts about simple problems.)

    Now, back to your main problem:
    That class is built to be a util, so making it a listener would require bits of that class/ classes referencing it to be re-written. That is why I recommend you make a new class for the listener.
  10. Offline


    You should only have 1 listener per plugin for a certain event. Implementing the Listener interface is not good practice for the written class. Right now your class contains a scheduler to spawn some sort particle display on the players location, if you only want this to happen when the player is moving you should really convert to using a simple listener class. This way you would not have to keep creating scheduled tasks for each user. You can even trigger your own events when the PlayerMoveEvent has triggered to handle the different types of particle displays.

    Here is little example of how your code could be more efficient as requested:

    Show Spoiler

    2. public class ParticlesEventListener implements Listener {
    4. public final JavaPlugin plugin;
    6. /**
    7.   * Self invoking registry, constructor is not neccesary.
    8.   */
    9. public ParticlesEventListener(JavaPlugin plugin) {
    10. this.plugin = plugin;
    12. plugin.getServer().getPluginManager().registerEvents(this, plugin);
    13. }
    15. @EventHandler
    16. public void onPlayerMoveEvent(PlayerMoveEvent event) {
    17. // blabla check
    19. // if the condition is met to display a helix, invoke the HelixParticleEvent
    20. // you will need the plugin instance, I just do not like to use static references to an instance of my plugin
    21. plugin.getServer().getPluginManager().callEvent(new HelixParticleEvent(event.player /* don't know if this is a valid field, use your intellisense of your ide */);
    22. }
    24. @EventHandler
    25. public void onHelixParticleEvent(HelixParticleEvent event) {
    26. // display a helix
    28. // this way you can also invoke this display using something like a command or an other activity (on hit)
    29. }
    31. private static class HelixParticleEvent extends Event /* optionally: implements Cancellable */ {
    33. private final Player player;
    35. HelixParticleEvent(Player player) {
    36. this.player = player;
    37. }
    39. // blabla overrides
    40. }
    41. }
    43. // in your onEnable, run the following
    45. new ParticlesEventListener(this /* e.g. your plugin instance */);

  11. Offline


    Why are you creating another event that only gets called when the player moves? So, you're using an event to call another event? Why not just move all the code from HelixParticleEvent into the playermoveevent and save some lines/ removed the event class?
  12. @ShaneCraftDev thank you for this! Time to go re rewrite my plugin!

    @Zombie_Striker this is more of what you should do because some people do not know how to make a custom event.
  13. Offline


    The only reason I have is that I'd prefer a separate event to invoke it at any given time. I guess declaring a static method would work just as well. I've been using much typescript lately, it has become too much of a standard for me to call events for mostly everything through out my applications I suppose.
Thread Status:
Not open for further replies.

Share This Page