How do i get the distance of a player from another player

Discussion in 'Plugin Development' started by IcyRelic, Jan 27, 2012.

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

    IcyRelic

    im trying to make a local chat and i dont know how to get the distance away from another player and how would i only let them see the message
     
  2. player1.getLocation().distance(player2.getLocation());
     
  3. Offline

    IcyRelic

    how do i only let them see the message
     
  4. Offline

    ItsHarry

    player.sendMessage(location.toString());
     
  5. Offline

    IcyRelic

    but? isnt that for a message i want lets say every player with in 300 blocks to see it and no1 else

    also player1.getLocation().distance(player2.getLocation()); it doesnt know what player1 or 2 is

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 23, 2016
  6. Offline

    darkmage0252

    You need to define what player1 or player2 is by

    Player Player1 = event.getPlayer();

    for an example.
     
  7. Offline

    IcyRelic

    then what is player2
     
  8. Offline

    Jaker232

  9. Offline

    darkmage0252

    I think you should read some tutorials to learn the basics before you try this.
     
  10. Offline

    IcyRelic

    i know the basics i made Paid2Level AutoOperator and InfoX im pretty sure i know the basics
     
  11. Offline

    tkausl

    But you dont know how to iterate over the Playerlist?
     
  12. Offline

    IcyRelic

    ok so this code
    Code:
    double x = 0;
     
    double y = 0;
     
    Player player;
     
    double z = 0;
     
    for(Player p : Bukkit.getOnlinePlayers()){
     
    x = p.getLocation().getX() +- 40;
     
    z = p.getLocation().getZ() +- 40;
     
    y = p.getLocation().getY() +- 40;
     
    if(player.getLocation().getZ() == z){
     
     
     
    }
     
    }
    how do i send the message to the people with in 40 blocks and in the x and y cord with out sending it 3 times to same person
     
  13. Offline

    mb01

    Do you really need to know the exact distance? If you only need to know if a point is inside a circle centered on another point, you don't need to call .distance(). It does a square root and that an expensive operation. Look there: http://freespace.virgin.net/hugo.elias/routines/r_dist.htm
    Of course the fastest way is to use an axis aligned bounding box instead of a circle/sphere, but the result isn't the same.

    It's a collision detection by the way, there is a lot of tutorial on the net explaining this. Do you need a code example on how to do both? It's not Bukkit related but I guess that's fine. Anyway my point is, don't use .distance() if you don't need to know the exact distance :p
     
  14. Offline

    IcyRelic

    i dont understand
     
  15. Offline

    DrBowe

    I assure you, and I do not mean too much offense by this, that you do not know the basics. Judging from the confusion in this thread, you have very little experience with Java and/or OOP (Object Oriented Programming). I highly suggest you read up further on Java before attempting to progress further here, as it is rather difficult to get far in plugin development without a fundamental knowledge of the language you're programming in.
     
  16. Offline

    IcyRelic

    i do know most java but this is one of the things i do not know
     
  17. Offline

    nala3

    (3 dimensional) Distance = √(x2 - x1)²+(y2 - y1)²+(z2 - z1)²

    So since that would be a rather resource consuming calculation, it would be better to do as mb01 suggested, or instead of using floats, use ints to at least make the calculation a little less resource intensive.
     
  18. Offline

    TheFieldZy

    Java isn't just syntax. I could learn syntax in a day, just like I could learn the spanish alphabet in a day. But that doesn't mean I can speak Spanish. This is more of a math part of programming, which you either learn in high level math classes (or low level) or you understand over time.
     
  19. Offline

    ItsHarry

    This is bs. You didn't even know player1 and player2 were variables..
     
  20. Offline

    IcyRelic

    no its not bs because if i do
    Player player1 = (player) sender
    Player player2 = (player) sender
    then they are the same person
    and im useing events and i dont know much about onPlayerChat but how to set the format so i dont know whatt to set the players1 and 2 to
     
  21. *faceOnKeyboard*

    srsly, what do you think player1 and player2 stand for? They stand for two DIFFERENT players. One you get from the event and the other from somewhere else, like Bukkit.getPlayer(name).
     
    ItsHarry likes this.
  22. Offline

    ItsHarry

    Use common sense...

    player1.getLocation().distance(player2.getLocation());

    Player1 and player2 are obviously different people, if you knew JAVA (Which you don't), you would know methods need arguments.. What would be the point of passing an object to itself? ..
     
  23. Offline

    IcyRelic

    sooo like this
    Player player1 = event.getPlayer();
    and
    Player player2 = Bukkit.getOnlinePlayers();

    i do know java so yea i dont know it all but i know alot just not how to get a distance i learn more every day

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 23, 2016
  24. Offline

    Technius

    *Facepalm*

    Bukkit.getOnlinePlayers returns an array.

    Find out the distance of players:
    Code:java
    1.  
    2. Player player = //your player
    3. Entity[] entities = player.getNearbyEntities(10,10,10); //gets entities within 10 blocks
    4. List<Player> players = new ArrayList<Player>();
    5. for(Entity e:entities)
    6. {
    7. if(e instanceof Player)
    8. {
    9. players.add((Player)e); //if this entity is a player, add it to the list
    10. }
    11. }
    12. List<Double> distances = new ArrayList<Double>();
    13. for(Player p:players)
    14. {
    15. distances.add(player.getLocation().distance(p.getLocation())); //add the distances to a list
    16. }
    17.  


    If you "know a lot" then you should be able to know this. If you "know a lot" you should know what psuedo-code is. Obviously copy/paste won't teach you anything, and shows you that you don't know that much. Just making two or three plugins that only do basic things doesn't mean that you're "good". Please read some books about Java and please learn what a "variable" is. I mean no offense to you, but if you want to be offended, feel offended. Have a good day.
     
  25. Offline

    IcyRelic

    ok then i know SOME but still learning and your gave me an error in eclipse "Type mismatch: cannot convert from List<Entity> to Entity[]"
    and i only changed Player player = //your player to
    Player player = Event.getPlayer();
     
  26. Offline

    Technius

    Then fix it. Replace the Entity[] with List<Entity>. "Don't just sit there gawking at us like we know everything".
     
Thread Status:
Not open for further replies.

Share This Page