Help on Getting If a Player is in a WorldGuard region

Discussion in 'Plugin Development' started by Pacothegint, Dec 24, 2012.

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

    Pacothegint

    Ok so I'm creating a plugin that will use worldguard regions as a "channel" what i want it to do is whenever a player gets within a certain distance say 10 blocks from the region they join the "channel" and considering I am a complete noob I have no idea on how to determine how far they are away from the region or let alone let them chat in that region.
     
  2. Offline

    mythcaptor

    In a PlayerChatEvent, get all players online, iterate through the all the online players, checking if each one is in the same worldguard region as chatting player (I'm assuming WorldGuard has a method to check if a player is in a region), and send the message to only players who are in the same region as the chatting player.

    edit: noticed you want a given distance from region, find the midpoint of the region, get half the x and z from the region, add those x and z values to the midpoint to find one corner, and subtract them to find the other corner, add givenDistance to the x and z off the corner you added to, and subtract them from the corner you subtracted them from, iterate through online players, if their x is between the two x values of those points, and their z is between the two z alues, send the message to them.
     
  3. Offline

    drampelt

    As for distance from the region, you can do firstlocation.distance(secondlocation) to get the distance between the 2 points. Hope this gets you started in the right direction
     
  4. Offline

    Pacothegint

    Thanks for the quick replies I'll see what i can do

    OK i got it to check and see if they are in a region but im not sure how to take that info and then send the chat to every body in the region

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

    mythcaptor

    This might gum up other chat plugins, but you could cancel the chat event and loop through all the players in the region, sending the player's message to each other player individually
    Code:java
    1. Player.sendMessage("<" + event.getPlayer().getName() + "> " + event.getMessage);

    (put that in a PlayerChatEvent)
     
  6. Offline

    fireblast709

    • You would need to use AsyncPlayerChatEvent (PlayerChatEvent is depreciated)
    • Mind thread-safety. player.sendMessage("something") is not thread-safe (afaik)
     
  7. Offline

    -_Husky_-

    Or just e.getFormat() then use that format then send the players a message, that always works too! :)
     
  8. Offline

    Pacothegint

    Guys Im trying to test and see if it can find people in the region and i get this error
    Code:
    Caused by: java.lang.NullPointerException
        at com.github.pacothegint.pacochatlistener.isPlayerInRegion(pacochatlistener.java:32)
        at com.github.pacothegint.pacochatlistener.onPlayerInteractEntity(pacochatlistener.java:22)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:425)
        ... 14 more
    
    I narrowed it down to this code in my Listener
    Code:
    if (plugin.worldGuard != null)
    //For the first
    if (isPlayerInRegion((Player) event.getRightClicked()))
    //for the second
    I have worldguard/edit in my plugins folder so im not sure whats happening
     
  9. Offline

    fireblast709

    pacochatlistener.java, line 32. Method: isPlayerInRegion(). Can you get that snippet for me please :3
     
  10. Offline

    Pacothegint

    I think this it
    Code:
    private boolean isPlayerInRegion(Player player) {
                // Make sure that a WorldGuard instance was found
                if (plugin.worldGuard != null) {
                    // Get the players position (as a WorldEdit Vector)
                    Vector playerVector = new Vector(player.getLocation().getX(), player.getLocation().getY(), player.getLocation().getZ());     
                 
                    // Get WorldGuards Region Manager
                    RegionManager rm = plugin.worldGuard.getRegionManager(player.getWorld());
                 
                    // Make sure a RegionManager was found
                    if (rm != null)    {
                        if (rm.getApplicableRegions(playerVector).size() > 0) return true;
                    }
                }
     
  11. Offline

    fireblast709

    Now, I still don't know what line 32 is, but these are some possible sources of the error:
    • plugin is null
    • player is null
     
  12. Offline

    Pacothegint

    In pacochatplayerlistener line 32 is if (plugin.worldGuard != null) {
    but let me check real quick
     
  13. Offline

    fireblast709

    if so then plugin is null
     
  14. Offline

    Pacothegint

    OK ill see if i can find out why

    I havent found it yet if you want to try i can give you the github url

    EDIT: Like i know what line is causing the error but i cant figure out why

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

    fireblast709

    you need to define plugin somewhere in your class. Usually, you do that with the constructor
    Code:java
    1. public <main class> plugin;
    2. public <classname>(<main class> plugin)
    3. {
    4. this.plugin = plugin;
    5. }
    And you create an object like
    Code:java
    1. new <classname>(<main class instance>);
    2. // Use the following line to create it in the main class
    3. new <classname>(this);

    Just replace the <classname> with your class and <main class> with your main class
     
  16. Offline

    Pacothegint

    OK now it doesn't give me a error but it doesn't find if they are in a region
     
  17. Offline

    fireblast709

    recheck your logic ;3
     
  18. Offline

    Pacothegint

    GAHH All it does is tells me that they arent in a region even if im in a region

    So apparently this code
    Code:
    private boolean isPlayerInRegion(Player player) {
            // Make sure that a WorldGuard instance was found
            if (plugin.worldGuard != null) {
                // Get the players position (as a WorldEdit Vector)
                Vector playerVector = new Vector(player.getLocation().getX(), player.getLocation().getY(), player.getLocation().getZ());     
     
                // Get WorldGuards Region Manager
                RegionManager rm = plugin.worldGuard.getRegionManager(player.getWorld());
     
                // Make sure a RegionManager was found
                if (rm != null)    {
                    if (rm.getApplicableRegions(playerVector).size() > 0) return true;
                }
            }
            // No region found at players position
            return false;
        }
    cant tell whether they are in a region or not
    OR its this code
    Code:
    f (event.getPlayer() instanceof Player) {
                // Tell the player if the RightClicked Player is in a region or not
                if (isPlayerInRegion((Player) event.getPlayer())) {
                    event.getPlayer().sendMessage("Player is in a WorldGuard Region");
     
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 30, 2016
  19. Offline

    fireblast709

    add debug to see what if-statements are passed
     
  20. Offline

    Pacothegint

    Ummm ? Sorry im a complete noob
     
  21. Offline

    Gildan27


    Try putting some logging statements, or send a message to the player, like:
    Code:
    player.sendMessage("Entering isPlayerInRegion");
    and:

    Code:
    player.sendMessage("plugin.worldGuard is not null");
    ...stuff like that, so you can see what path the code is following.

    You can also BukkitUtil.toVector() to get the vector (I don't think this is your problem, but just FYI:
    Code:
    rm.getApplicableRegions(BukkitUtil.toVector(player.getLocation()));
    My guess is that plugin.worldGuard is null. How is it being set?
     
  22. Offline

    Pacothegint

    OK so this is the new code with the messages
    Code:
     private boolean isPlayerInRegion(Player player) {
                // Make sure that a WorldGuard instance was found
           
                if (plugin.worldGuard != null) {
                    player.sendMessage("Entering isPlayerInRegion");
                    // Get the players position (as a WorldEdit Vector)
                    Vector playerVector = new Vector(player.getLocation().getX(), player.getLocation().getY(), player.getLocation().getZ()); 
             
                    // Get WorldGuards Region Manager
                    RegionManager rm = plugin.worldGuard.getRegionManager(player.getWorld());
                    player.sendMessage("WorldGuard");
                    // Make sure a RegionManager was found
                    if (rm != null)    {
                        if (rm.getApplicableRegions(playerVector).size() > 0) return true;
                        player.sendMessage("REgion ");
                    }
                }
                player.sendMessage("False");
                // No region found at players position
                return false;
            }
    [​IMG]
    i have a github for it if you want to see it
     
  23. Offline

    fireblast709

    which means rm.getApplicableRegions(playerVector) returns an empty list. If you are sure that you are in the region, this means playerVector would be incorrect
     
  24. Offline

    Pacothegint

    Ok i got the worldedit mod so i can be 100% sure im in the region
    [​IMG]
    So you think it could be this line
    Code:
    Vector playerVector = new Vector(player.getLocation().getX(), player.getLocation().getY(), player.getLocation().getZ());      
     
  25. Offline

    fireblast709

    Then your vector is ill-defined. Or it would not show some messages
     
  26. Offline

    Pacothegint

    Hmm I'm starting to think this would be easier just to be able to define my own regions
     
  27. Offline

    fireblast709

    Pacothegint just try to use player.getLocation() instead of a vector
     
  28. Offline

    Pacothegint

    OK ill try it thanks
     
  29. Offline

    kingBS11

    Yeah I think you should get their location first, and check if they are withing a whatever block radius of the location then put whatever you want it to do. Also, you might want to make your title a little more specific
     
Thread Status:
Not open for further replies.

Share This Page