Region Handler

Discussion in 'Plugin Development' started by FlareLine, Apr 18, 2014.

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

    FlareLine

    I'm making a little Minigame at the moment and I'm wondering how to go about checking a player's region.
    The Minigame could have 100 to 200 people on it at one time, so I'm not sure if using an Event Handler for PlayerMoveEvent would be the best option, and I see no method for getting the event of when a Player enters a region in the WorldGuard source.
    I have also tried "WGRegionEvents" and tried to use the methods out of it, however the server fails to register the event, as blah.blah.blah.RegionEnterEvent does not exist.
    Would anyone have any advice on how I would go about this?
     
  2. Offline

    xXDJONESXx

    If your going to create a Mini-game I suggest not using WorldGuard, just create your own one.
    Also, if your going to have a lot of people on the server don't just use raw PlayerMoveEvent because it fires of when a player moves there head or hasn't even moved to a different block and this causes stress for your server, use something like:

    Code:java
    1. public boolean compareLocations(Location location1, Location location2){
    2. if (location1.getBlockX() == location2.getBlockX() && location1.getBlockY() == location2.getBlockY() && location1.getBlockZ() == location2.getBlockZ())
    3. return true;
    4. else
    5. return false;
    6. }
    7.  
    8. //On move event
    9. if (!compareLocations(event.getFrom(), event.getTo()))
    10. //Do something.
     
  3. Offline

    TigerHix

    I have created a Region class in my minigame framework, which can tell if a player is in a cuboid region, hope this helps.

    Code:java
    1. import org.bukkit.Location;
    2.  
    3. public class Region {
    4.  
    5. private Location first;
    6. private Location second;
    7.  
    8. public Region(Location first, Location second) {
    9. this.first = first;
    10. this.second = second;
    11. }
    12.  
    13. public boolean includes(Location location) {
    14. if (location.getWorld().getUID() != first.getWorld().getUID() || location.getWorld().getUID() != second.getWorld().getUID()) return false;
    15. int x1 = first.getBlockX();
    16. int x2 = second.getBlockX();
    17. int y1 = first.getBlockY();
    18. int y2 = second.getBlockY();
    19. int z1 = first.getBlockZ();
    20. int z2 = second.getBlockZ();
    21. return between(location.getBlockX(), x1, x2) && between(location.getBlockY(), y1, y2) && between(location.getBlockZ(), z1, z2);
    22. }
    23.  
    24. private boolean between(int target, int i, int j) {
    25. return (target < i && target > j) || (target > i && target < j);
    26. }
    27.  
    28. public Location getFirst() {
    29. return first;
    30. }
    31.  
    32. public void setFirst(Location first) {
    33. this.first = first;
    34. }
    35.  
    36. public Location getSecond() {
    37. return second;
    38. }
    39.  
    40. public void setSecond(Location second) {
    41. this.second = second;
    42. }
    43.  
    44. }


    To create a region, use

    Code:java
    1. Region region = new Region(location1, location2);


    To test if a player is in this region, use

    Code:java
    1. boolean boo = region.includes(player.getLocation());
     
  4. Offline

    FlareLine

    TigerHix Cheers, This could be very helpful.
     
  5. Offline

    TigerHix


    Hell, just found that my class won't check the are the worlds same. Fixed it.
     
Thread Status:
Not open for further replies.

Share This Page