How to define a region?

Discussion in 'Plugin Development' started by mine-care, Jun 4, 2014.

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

    mine-care

    Hello fellow coders!
    Im wondering how to specify a LINE region like it folows:

    ▀|▀|▀|▀|▀|▀|▀|▀|▀ <these are blocks. and i want a region that starts from the first left to the last right, the reason why i need a location is to be able to compare it with how close a player is to it.
    Thanks to all.

    Currently added each location to Location [] but how do i get the one closest to a player and get the distance between it and another loc?

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

    Axe2760

    Code:
    public class Region{
      public Location[] locations;
     
      public Region(Location[] locations){
        this.locations = locations;
      }
     
      public Location closestToPlayer(Location location){
        Location topOfStack = locations[0];
     
        for (Location loc: locations){
          if (loc.distance(location) < loc.distance(topOfStack)){
            topOfStack = loc;
          }
        }
     
        return topOfStack;
      }
     
     
    }
    
    Untested, and I've never done that before so I can't say for certain it will work...but it's an idea, you can probably work off of something like that. to get the distance between locs, the javadocs say to use Location.distance(Location otherLoc)
     
    unforgiven5232 likes this.
  3. Offline

    mine-care

    Axe2760
    Oh my!
    Thanks a ton! Ill try it tomorrow when I will be at pc and ill reply, thanks again!

    oh wait, isnt that going to return the topOfStack wich is location [0]? shouldnt we somehow get the "keySet" as its known on hmaps?

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

    Axe2760

    mine-care I suppose you could do that. It would return the topOfStack which is the location out of the array that's closest to the location you specified
     
  5. Offline

    mine-care

    oooohhh... thanks, ill try it in 10 mins :)
     
  6. Offline

    mine-care

    Axe2760 i think it will work but for some reason i cant understand where i put the locations of the region and where the players location to be compared and then processed... Any example? im trying to use it by player move event.
    Thanks.

    and as i said it returns the loc [0] always returns the 1st location arg.
    :/ Btw i had to modify it mabe i messed it... Plz tell me, thanks
    Code:java
    1. package Utill;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.Location;
    5. import org.bukkit.World;
    6.  
    7. public class Region{
    8. static World world = Bukkit.getWorld("world");
    9. static Location loc = new Location(world, -1584, 5, 101);
    10. static Location loc1 = new Location(world, -1585, 5, 101);
    11. static Location loc2 = new Location(world, -1586, 5, 101);
    12. static Location loc3 = new Location(world, -1587, 5, 101);
    13. static Location loc4 = new Location(world, -1588, 5, 101);
    14. static Location loc5 = new Location(world, -1589, 5, 101);
    15. static Location loc6 = new Location(world, -1590, 5, 101);
    16. static Location loc7= new Location(world, -1591, 5, 101);
    17. static Location loc8 = new Location(world, -1592, 5, 101);
    18. public static Location[] locations = new Location[] {loc3,loc1,loc2,loc,loc4,loc5,loc6,loc7,loc8 };
    19.  
    20. public Region(Location[] locations){
    21. Region.locations = locations;
    22. }
    23. public static Location closestToPlayer(Location location){
    24. Location topOfStack = locations[0];
    25. for (Location loc: locations){
    26. if (loc.distance(location) < loc.distance(topOfStack)){
    27. topOfStack = loc;
    28. }
    29. }
    30.  
    31. return topOfStack;
    32. }
    33.  
    34.  
    35. }
    36.  


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

    Axe2760

  8. Offline

    mine-care

    Axe2760 You actualy did a lot! :D not solved yet though
    thanks for your time
     
Thread Status:
Not open for further replies.

Share This Page