Solved Find closest beacon?

Discussion in 'Plugin Development' started by trg601, Aug 7, 2013.

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

    trg601

    How to find the location of the closest beacon?
    And also check if there is no beacon in world?

    If it is possible to also check where the closest active beacon is that would be the best :D
    My idea is too make an item or command that sends you to the nearest beacon.
    And maybe later I would implement saving favorite beacons :D

    Thanks - trg601 :D
     
  2. Offline

    xxMOxMOxx

    Theres a chance beacons are considered entities and would show up with getNearbyEntities(), but I'm pretty sure they're blocks. You could try storing beacon locations in a file maybe.
     
  3. Offline

    trg601

    If it is a block then how do I find the nearest block of it?
    Thanks for responding :D

    I tried a bunch of different methods on here and nothing is working :(
    If anybody knows how to scan the world for a beacon block then please
    Tell me :(

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
  4. Offline

    xTrollxDudex

    Beacons are Tile Entities. They can be found like this:
    PHP:
    World world /*your world*/
    for(Chunk c world.getLoadedChunks()){
    for(
    BlockState b c.getTileEntities()){
    if(
    instanceof Beacon){
    Beacon beacon = (Beaconb;
     
    //do stuff
    }}}
    Now you can use "beacon" and check the distance between the player
     
  5. Offline

    trg601

    Really? well how do I do that? (sorry i'm a noob)

    I got it to take you to the first created beacon...
    But i'm not sure how to tell which one is closest to the player :/

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

    trg601

  7. Offline

    Assist

    trg601
    Code:java
    1. public Location getClosest(Player player) {
    2. Map<Double, Location> map = new HashMap<Double, Location>();
    3.  
    4. for(Chunk chunk : player.getWorld().getLoadedChunks()) {
    5. for(BlockState state : chunk.getTileEntities()) {
    6. if(state instanceof Beacon) {
    7. Beacon beacon = (Beacon) state;
    8.  
    9. map.put(player.distance(beacon.getLocation(), beacon.getLocation());
    10. }
    11. }
    12. }
    13.  
    14. Object object = java.util.Collections.min(map.values());
    15. // object returns closest beacon location.
    16. }
     
  8. Offline

    xTrollxDudex

    Assist trg601
    Use distanceSquared, it's higher performance because you don't need to do a square root.
     
    Assist likes this.
  9. Offline

    trg601

    Assist xTrollxDudex
    Thank you both for your replies :D
    I still can't figure out how to fix this problem,
    When I put the code in there, import the things, and then I still get errors that I don't know how to fix :(
    Code:java
    1. map.put(player.distance(beacon.getLocation(), beacon.getLocation());

    On there I get the error on "player.distance" it is telling me to " add cast to 'player' "
    I tried switching that to distanceSquared, but I still get the same error :(
    And, I get an error on
    Code:java
    1. Object object = java.util.Collections.min(map.values());
    The error is on min :(

    Thanks :D
     
  10. Offline

    Assist

    trg601
    Sorry, it was 6:46am when I wrote that, I was a bit tired :D

    This one should work:
    Code:java
    1. public Location getClosest(Player player) {
    2. Map <Double, Location> map = new HashMap <Double, Location>();
    3.  
    4. for (Chunk chunk: player.getWorld().getLoadedChunks()) {
    5. for (BlockState state: chunk.getTileEntities()) {
    6. if (state instanceof Beacon) {
    7. Beacon beacon = (Beacon) state;
    8.  
    9. map.put(player.getLocation().distanceSquared(beacon.getLocation()), beacon.getLocation());
    10. }
    11. }
    12. }
    13.  
    14. double key = Collections.min(map.keySet());
    15. return map.get(key);
    16. }
     
    trg601 likes this.
  11. Offline

    trg601

    I am very impressed that you wrote that at 6:46 in general :D
    But I have one last question, how do I teleport the player to that location?
     
  12. Offline

    Assist

    trg601
    Code:java
    1. somePlayer.teleport(getClosest(somePlayer));
     
    trg601 likes this.
  13. Offline

    trg601

    YAY!!!!!!!!!!
    Thank you so much assist!!!!
    This works awesomely!
    Now I only have one more question, do you know how to check if there is no beacons?
    Because if there isn't any then it throws an error in game...
    Anyways, if you don't know how that doesn't matter that much anyways because this is still really epic :D
    Thanks :D :D :D :D :D :D
     
  14. Offline

    Assist

    trg601
    Yeah, add this to the getClosest() method:
    Code:java
    1. if (map.size() > 0) {
    2. double key = Collections.min(map.keySet());
    3. return map.get(key);
    4. }
    If the map size is 0, return null or some random location.

    But what I'd do is check if the location is not null
    Code:java
    1. Location closest = getClosest(somePlayer);
    2.  
    3. if (closest != null) {
    4. somePlayer.teleport(closest);
    5. }
     
  15. Offline

    trg601

    Thanks again, I got it working perfectly now thanks to you :D
    And I got it to take you to the block above the beacon and in the center for added affect :D
    Thanks :D

    EDIT: set as solved, thanks :D
     
Thread Status:
Not open for further replies.

Share This Page