Cannot measure distance between worlds

Discussion in 'Plugin Development' started by NoChanceSD, Aug 30, 2015.

Thread Status:
Not open for further replies.
  1. Offline

    NoChanceSD

    Somehow, the following piece of code sometimes throws a exception:
    Code:
    for (final Player p : pl.getWorld().getPlayers()) {
                if (p.getLocation().distanceSquared(pl.getLocation()) <= RANGE) {
    
                }
      }
    The exception is: "java.lang.IllegalArgumentException: Cannot measure distance between worldA and worldB"
    So how is this even possible when i'm explicitly getting players only from the world "Player pl" is in?
    I have found some old threads about it but can't find a reason for this exception to happen.
     
  2. Offline

    mythbusterma

    @NoChanceSD

    That's rather odd. Try printing out the world of both locations to see if you can figure out what's happening.
     
  3. Offline

    Zombie_Striker

    @NoChanceSD
    Just add an if statement that checks if p.getworld is the same as pl.get world.
     
  4. Offline

    adam753

    Actually, never mind what I just said.

    Sidenote: why are you getting the distance squared? Surely just the regular distance is what you want for checking if it's in range?
     
  5. Offline

    NoChanceSD

    @Zombie_Striker Obviously i know that would solve it, that's not the question here.
     
  6. Offline

    mythbusterma

    @NoChanceSD

    Again, print out some more information to see if you can figure out what's going on.
     
  7. Offline

    NoChanceSD

    Missed your post somehow, the problem is i can't replicate this behavior, it has been reported to me by some users and i have seen it happen randomly on my own server too.
     
  8. Offline

    Oxyorum

    @NoChanceSD

    Is it possible that maybe this occurs on servers with many players, such that players further in the list have time to switch dimension before they are checked, causing this error?

    I'm just throwing an idea out there.
     
    Last edited: Aug 31, 2015
  9. Offline

    teej107

    No, it's not possible.
     
  10. Offline

    Konato_K

    @adam753 Because square roots are very slow, using distanceSquared is always the way unless you actually need to display the distance.
     
  11. Offline

    adam753

    Well, I've got no idea, but if it helps I'd probably do my debugging like this:
    Code:java
    1.  
    2. List<Player> playersOnWorld = pl.getWorld().getPlayers();
    3. //Output this list of players to a log file, along with the name of pl's world and a timestamp
    4.  
    5. for (final Player p : playersOnWorld) {
    6. try {
    7. //Output p's world name to your log file (+timestamp)
    8. if (p.getLocation().distanceSquared(pl.getLocation()) <= RANGE) {
    9.  
    10. }
    11. //Write in your log file that the problem has happened (+timestamp)
    12. //Send a message to p and/or pl so they can (hopefully) tell you what they did to cause the problem
    13. }
    14. }
     
  12. Offline

    NoChanceSD

    @adam753 I will add some debug and run it on my server, hopefully it happens again and gives some more information.
     
  13. Offline

    mythbusterma

    Performing a square root using the default hotspot VM takes a less than negligible amount of time, unless by "very slow" you mean less than 20 ns on modern hardware, in which case it is.

    There is literally no reason to use it unless that is more convenient.
     
  14. Offline

    adam753

    @mythbusterma
    This code looks like it's being run inside PlayerMoveEvent, so given that the code could feasibly be run tens of thousands of times per second on a large server, I can understand why he would want to optimize.
    (Then again, I could be wrong.)
     
  15. Offline

    mythbusterma

    @adam753

    Tens of thousands of times per second is not how often that event is called, even with 200+ players on a server.

    Also, at the point you're worrying about that, there's far bigger performance draws than square root, such as everyone using the wrong data structure, unnecessary iteration, etc.
     
  16. Offline

    Konato_K

    @mythbusterma I don't mean is horribly slow, probably the "very" shouldn't be there, but it is slower and I have seen plugins that use it on every PlayerMoveEvent, which of course, kills performance. In any case, if you don't need to display the actual distance then there is not really a reason to get the actual distance, for sorting or making comparissions the squared distance will always work fine.
     
Thread Status:
Not open for further replies.

Share This Page