Parsing player.getLocation() into x,y,z coords?

Discussion in 'Plugin Development' started by tommykins20, Dec 23, 2011.

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

    tommykins20

    Hia again!
    I'm trying to make a /setspawn command and so far I got a /spawn command, havn't tested yet though, and now I'm making the /setspawn. So I check if the sender is a player and I get the location of the player using
    Code:
    Location spawnloc = player.getLocation();
    then I try to do
    Code:
    World world = player.getWorld();
    world.setSpawnLocation(x,y,z);
    You may notice it requires three paremeters, which is the x,y and z. I have gotten the location of the sender who performed the command but I don't know how to make three integeters which is the location x,y and z of the players location. Can someone help with this? I tried for about 5 mins but gave up :mad:
    Thanks!
    Happy crafting,
    -tommykins20
     
  2. Offline

    unicode21B9

    location.getX(), getY() and getZ()
     
  3. Offline

    Delocaz

    Something like this:
    Code:
    Location spawnloc = player.getLocation();
    player.getWorld().setSpawnLocation(spawnloc.getX(), spawnloc.getY(), spawnloc.getZ());
     
  4. Offline

    tommykins20

    Thank you guys for replying.. I tried that but if you do getX() it returns a double variable, so I have to change the type from int to double and the setSpawnLocation() method requires ints, so I did getBlockX() and that works, just not as well.. any comments or solutions?
    thx
     
  5. Offline

    Chiller

    player.getWorld().setSpawnLocation((int) spawnloc.getX(), (int) spawnloc.getY(), (int) spawnloc.getZ());
     
  6. Offline

    tommykins20

    Thank you very much :)
     
  7. Offline

    unicode21B9

    (int) spawnloc.getX() is equivalent to spawnloc.getBlockX(). You might want to use (int) Math.round(spawnloc.getX()).
     
  8. Offline

    Chiller

    When it casts a double to an int it already has to round...
     
  9. Offline

    ItsHarry

    This.
     
  10. Offline

    unicode21B9

    Casting does not round, but floor the value.
    That.
     
  11. Offline

    Chiller

    There.
     
  12. Offline

    tommykins20

    Here
     
  13. .getBlockX/Y/Z() gets Integer :)
     
  14. Offline

    Chiller

    Where.
     
  15. Offline

    trotski94

    When?
     
  16. Offline

    skore87

    Who?
     
  17. Offline

    bergerkiller

    Why?
     
  18. Offline

    Chiller

    How?
     
  19. Offline

    DDoS

    What?
     
  20. Offline

    Delocaz

    C-c-combo breaker!
     
  21. Offline

    unicode21B9

    Stop spamming and delete all your posts please. Go post in the Offtopic forum if you feel like spamming.
     
  22. Offline

    Chiller

    Just because your not part of this doesn't mean you also can't have fun!
     
  23. Offline

    halley

    These are not equivalent. You don't want to simply cast with (int)spawnloc.getX(), you should do the spawnloc.getBlockX() preferably or Math.floor(spawnloc.getX()). For negative mid-block values, the typecast will be off by one block, closer to zero.

    I posted this some time ago in another thread:

    When you find problems with an off-by-one result in negative integers, the usual cause is inconsistent ways of rounding floating point numbers to make integers.

    Math.floor(-176.4) gives -177.0
    Math.round(-176.4) gives -176.0
    (int)(-176.4) gives -176

    Math.floor(-176.6) gives -177.0
    Math.round(-176.6) gives -177.0
    (int)(-176.6) gives -176

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 22, 2016
Thread Status:
Not open for further replies.

Share This Page