WorldGuard get player's region?

Discussion in 'Plugin Development' started by Agent000037, Jun 25, 2017.

Thread Status:
Not open for further replies.
  1. This is super simple.

    Which method would I use to get the region(s) a player is in?

    For example, if I throw an egg in "spawn" region, e.setCancelled();

    some pseudo code:
    Code:
    public void onEggThrow (EggThrowEvent e) {
      if (player.getRegionPlayerIsIn().equals("spawn") {
      player.sendMessage("You can't throw eggs at spawn!");
      e.setCancelled();
      }
    }
    I want to do something like ^^ that ^^ except i don't know what to put for getRegionPlayerIsIn()

    I've looked everywhere, but I can't find it... any help?

    Seriously??? Nobody knows how to do this?? it's so simple...

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

    timtower Administrator Administrator Moderator

  3. Offline

    timtower Administrator Administrator Moderator

    @Agent000037
    Code:
    WorldGuardPlugin worldGuard = getWorldGuard();
    Vector pt = toVector(block); // This also takes a location
    
    RegionManager regionManager = worldGuard.getRegionManager(world);
    ApplicableRegionSet set = regionManager.getApplicableRegions(pt);
     
  4. Yeah, i saw that somewhere and tried to implement it... but it didn't quite make any sense... what does the regionmanager and applicableregion set part do? and where does that like, compare the player's location with the region, etc
     
  5. Offline

    timtower Administrator Administrator Moderator

    @Agent000037 The regionManager manages the regions.
    getApplicableRegions gets all regions on that location.
    Comparing is done inside that method.
     
  6. I don't know why i'm so confused...
    what's the output? how can I like, take that then based on whether the player is or is not in the region, perform an action..?
     
  7. Offline

    timtower Administrator Administrator Moderator

  8. ok I think I understand...

    pt is the player's location.
    getApplicableRegions(pt) will get the ID's of the regions YOU are in (because of pt)?
    so it'll be like this...

    set:
    spawn
    enchanting
    protected

    now you say if set.contains("spawn"); or something like that, and that's how you can tell if someone is in that region??
     
  9. Offline

    timtower Administrator Administrator Moderator

    @Agent000037 You need to loop over the regions, as the contains will check anything but a ProtectedRegion will never be equal to a string.
     
  10. Ok. I'm going to experiment a bit with that, and hopefully get it to work... but if a ProtectedRegion isn't a string, then what do i want it to be equal to?? Like, what am I looking for (instead of string)
     
  11. Offline

    timtower Administrator Administrator Moderator

    You compare the value of getId()
     
  12. Ohhh I knew that xD alright thanks :)

    So I'm a bit of a noob with java (kinda, but not THAT noob-ish)

    WorldGuardPlugin worldGuard = getWorldGuard();
    Vector pt = toVector(block); // This also takes a location

    RegionManager regionManager = worldGuard.getRegionManager(world);
    ApplicableRegionSet set = regionManager.getApplicableRegions(pt);

    this was your code, so I'm assuming block and world I have to make... (World world = new world, etc) although how do i go about getWorldGuard()? I'm not sure what that is xD sorry
     
    Last edited by a moderator: Jun 26, 2017
  13. Offline

    timtower Administrator Administrator Moderator

  14. Oh yeah... haha I knew that too

    Alrighty, so... I believe I've gotten everything understood now, the only thing I'm still questioning is that set. Can I just do set.get(1) to get the first of that list... or how's that work?
     
  15. Offline

    timtower Administrator Administrator Moderator

  16. Yeah... I had a feeling you were going to say that.

    I don't know what an iterator is ._.
    Do you have a link to a vid that explains it? Cause I keep seeing it so it's prob kinda important for plugin making, etc.
     
  17. Offline

    timtower Administrator Administrator Moderator

  18. Alright I tried using some of that to make this:

    Code:
    Player p = e.getPlayer();
          
            p.sendMessage(ChatColor.RED + "STOP THROWING EGGS");
          
            World world = p.getWorld();
            Location loc = p.getLocation();
          
            WorldGuardPlugin worldGuard = getWorldGuard();
    
            RegionManager regionManager = worldGuard.getRegionManager(world);
            ApplicableRegionSet set = regionManager.getApplicableRegions(loc);
            int size = set.size();
          
            p.sendMessage( "Size: " + size );
          
            ArrayList<ProtectedRegion> list = (ArrayList<ProtectedRegion>) set.iterator();
                  
            p.sendMessage( "get(1): " + list.get(1));
    Although, whenever I throw an egg, DO NOT THROW EGGS and Size: 2 are outputted just fine... but the last line (get(1): X) doesn't appear... am I doing something wrong?

    Sorry for also bothering you like a LOT btw. hehe..
     
  19. Offline

    InstanceofDeath

    https://docs.oracle.com/javase/7/docs/api/java/util/Iterator.html .

    Well to make it easier you could loop (I hope ApplicableRegionSet implements Collection) trough all Elements of this Set with a foreach loop.

    Code:java
    1.  
    2. for(ProtectedRegion setElement : set) {
    3.  
    4. //This part will run as many times as they are applicable regions
    5. //And now you can use if-statements for specific things. setElement is in each loop the next Region
    6. // use break; for jumping out of the loop
    7.  
    8. }
    9.  
     
Thread Status:
Not open for further replies.

Share This Page