Solved Player location changes help!

Discussion in 'Plugin Development' started by fierchecer, Oct 28, 2014.

Thread Status:
Not open for further replies.
  1. Hi, I have been pondering over this for a few hours now, I am looking for a way to get the players location, then (Given time later) check if it is the same or not. Essentially this will be used to check if the player has moved.

    It also cannot use PlayerMoveEvent because i am allowing people to move their heads etc. But not themselves.

    Any help is greatly appreciated.
     
  2. Offline

    FerusGrim

    Player#getLocation, at some point. Then, later, check if Player#getLocation is the same?

    Code:java
    1. public boolean isInSameLocation(Location oldLoc, Location curLoc) {
    2. return oldLoc.getX() == curLoc.getX()
    3. && oldLoc.getY() == curLoc.getY()
    4. && oldLoc.getZ() == curLoc.getZ();
    5. }
     
  3. Offline

    Watto

    fierchecer

    As FerusGrim said.. To compare you can use getLocation().distance(otherlocation) which returns a double. If they haven't moved it will be 0.0
     
    FerusGrim likes this.
  4. Offline

    FerusGrim

    Ha. I forgot that Location#distance existed.
     
  5. Offline

    Watto

    FerusGrim

    I've seen people comparing X to X, Y to Y and Z to Z too often :p
     
    FerusGrim likes this.
  6. Yes, Umm, Okay I think my explanation to this was slightly lacking, I have a particle plugin that for various different effects that will spawn the particles in a circle around the player, I am attempting to (while the player is standing still (no co ordinate change)) spawn one type of particle and then when a player is moving spawn and play a different particle. FerusGrim Watto

    If need be I can re-direct you to where I have seen this.
     
  7. Offline

    FerusGrim

    If you need to check if the player is moving, then you should really use PlayerMoveEvent...
     
  8. But is it not possible to use a runnable to every tick: get the players location and then for the next tick get the location and compare it to the location from tick 1?
     
  9. Offline

    FerusGrim

    It's... possible, ye.

    So is stabbing yourself in the leg.

    Just use PlayerMoveEvent. Trust me.
     
  10. Offline

    Watto

    fierchecer

    Yeah it is.. If you only need the previous block location you could store the last location in a HashMap called something like 'lastBlockLocation' and then compare it to the current block.

    Although i'm not sure if that's the most efficient way.
     
  11. I guess something like this could be used?

    Code:java
    1. Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
    2. public void run() {
    3. for(Player player : Bukkit.getOnlinePlayers()){
    4. Location Current = player.getLocation();
    5. if(player.getLocation() != Current){
    6. //Player is moving / has moved
    7. }else{
    8. //Player is in the same spot
    9.  
    10. //Could use a Hashmap / Array to store players who are moving for example.
    11. }
    12. }
    13.  
    14. }
    15. }, 1, 1);


    I can see that it is possible because in this video I am able to move my head (etc.) and the particles still stay the same, but when I actually move the particle will change to a different one.
     
  12. Offline

    fireblast709

    FerusGrim Watto use distanceSquared, saves you a sqrt (which is expensive to compute).
     
  13. Offline

    Watto

    fierchecer

    Hm.. noo. It looks to me like you're comparing two of the same Locations to eachother which means that if statement will always be true, tell me if i'm wrong on that one..

    I'm assuming you don't want to use PlayerMoveEvent because you also need to run code when they aren't moving too.

    Code:java
    1. HashMap<UUID, Location> lastBlockLocation = new HashMap<UUID, Location>();
    2. Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
    3. public void run() {
    4. for(Player player : Bukkit.getOnlinePlayers()){
    5.  
    6. Location Current = player.getLocation();
    7. Location Last = lastBlockLocation.get(player.getUniqueID());
    8.  
    9. if(Last != Current){
    10. //Player is moving / has moved
    11. }else{
    12. //Player is in the same spot
    13.  
    14. //Could use a Hashmap / Array to store players who are moving for example.
    15. }
    16. }
    17.  
    18. }
    19. }, 1, 1);


    Something like that should work (Wrote through the browser so might be some errors, using your previous example code) however you'll have to find the appropriate place to use 'lastBlockLocation.put' and actually store the last block location which is the easiest part.

    fireblast709
    Oh and thank you :3

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 14, 2016
  14. @Watto
    So the 'lastBlockLocation.put' needs to go where the location is updated, I'm guessing, I have never really used hashmaps so i am slightly confused.

    By no means do I want you to do it for me, I will never learn that way :)
     
  15. Offline

    Watto

    fierchecer

    The HashMap needs to be updated every tick, and just make sure to call it after the 'Last' variable or you'll assign your current position to the HashMap which would result in problems obviously.

    It will be easier to understand what to do if you know how a HashMap works.
     
  16. Watto
    Got it working :D
    I just wanna say thanks, you really figured it out all on your own, and just told me to add 1 extra line of code, It now works perfectly.

    So once again, thank you soo much :)
     
  17. Offline

    Watto


    Glad i could help, remember to mark this as solved too :3
     
Thread Status:
Not open for further replies.

Share This Page