Solved Getting position at differents intervals ?

Discussion in 'Spigot Plugin Development' started by RedRikudo, Jun 6, 2017.

  1. Hi !
    Here is my problem : I want to teleport a player to his arrow when it hit the ground. Here is what I thinked of :
    First of all, I verify at "EntityShootBowEvent" that the shooter is a player. If it is, so I "convert" the entity to a player. I made a method, to which I send the player and the projectile (the arrow) as arguments. Now here is my problem : I would like to pick the arrow position at differents interval, so when the subtraction of the latter equals to 0, I know the arrow stopped moving so it has reached to ground. Here is my code :

    Code (Text):
    public class BowEvent implements Listener {
     
        @EventHandler
        public void onPlayerKicked(EntityShootBowEvent e)
        {
            if (e.getEntityType() == EntityType.PLAYER)
            {
                Player p = (Player) e.getEntity();
                Projectile arrow = (Projectile) e.getProjectile();
             
                TeleportMethod.TpMethod(p, arrow);
            }
        }
    }
     
    and the "test method"
    Code (Text):
    public class TeleportMethod {

        public static void TpMethod(Player p, Projectile a)
        {
            int i = 0;
            while (i == 0)
            {
                double x = a.getLocation().getX();
                double y = a.getLocation().getY();
                double z = a.getLocation().getZ();
             
                    //Here I would like to set an intervall

                if ((x - a.getLocation().getX() == 0 && y - a.getLocation().getY() == 0) && z - a.getLocation().getZ() == 0)
                    i = 1;
            }
            p.teleport(a.getLocation());
        }
    }
    It would be very great if you could help me !! Thanks :)

    EDIT : I tried the sleep function from "Thread" but doesn't seem to work ...
     
  2. You should learn Java conventions.
     
  3. About methods, class, packages etc.. titles ? Yes I'm sorry I haven't thinked of that here ...
     
  4. Are you wanting to teleport the player incrementally? If not, couldn't you just use the ProjectileHitEvent?

    I'm honestly not sure if that event is called if the projectile hits a block, but it'd be easy enough to test.

    Otherwise you need to look into BukkitScheduler programming (just google, there are good tutorials). Never call Thread.sleep.
     
  5. Isn't this event called when it hit another entity ? I will try it out and I say you
     
  6. You can use Thread.sleep, just not on the main thread. If you're using it asychronously it's fine.
     
  7. I tried to set a thread's sleep (at the place I wrote I needed an intervall), but it doesn't seem to work ... Because I'm directly teleported at the moment I shoot the arrow, then the arrow keep on its trajectory. So, I don't know why but the two taken locations before and after the thread's sleep are the sames

    EDIT : tested your event, but seems to have no method to get the player object ...
     
  8. Use a Bukkit Runnable.
     
  9. How ? Sorry I'm a beginner xD
    Isn't there a simple way to pause the code (without freezing the server) ?
     
  10. I havnt looked at the code, but based on the nature of this thread you're more than likely not running it asynchronously. I suggest not touching this until you know what you're doing, in this case you should do as @TheRedstoneGeek suggested and use a BukkitRunnable, or a custom class that extends it preferably.
     
  11. Ok thanks :)
     
  12. Code (Text):
    public class MyRunnable extends BukkitRunnable {
       
        public void run() {
           
        }

    }
    Code (Text):
    MyRunnable.runTaskLater(plugin, delay);
     
  13. It's still generally not a good idea. If you're using the Bukkit scheduler for async tasks, it uses a thread pool and you're tying up a thread doing nothing.

    There is very rarely a case for sleep- if you want something to happen later, don't hold up a thread while you wait, just schedule it to happen later.
     
  14. You dont have to use Bukkits scheduler for running async tasks though. Async threads are fine to sleep if you need them to, but its better practice to use a Runnable