Worldguard - Check if player enter / leave

Discussion in 'Plugin Development' started by splint, Jun 14, 2014.

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

    splint

    Hi all, I want to add to my music plugin a way to launch music when a player walk into a specific region. For that, I think to do like that.

    - Check the region name.
    - If the region name start with "music", search in regionmusic.yml the region name.
    - Get the associated URL.
    - Launch the music.

    That not too hard, but I have a problem: Worldguard cause some "null" errors on the checking region name.

    Code:
    [GRAVE] Could not pass event PlayerMoveEvent to Spouties v0.2.1
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:427)
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62)
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:477)
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:462)
            at net.minecraft.server.v1_6_R3.PlayerConnection.a(PlayerConnection.java:227)
            at net.minecraft.server.v1_6_R3.Packet10Flying.handle(SourceFile:136)
            at net.minecraft.server.v1_6_R3.NetworkManager.b(NetworkManager.java:296)
            at net.minecraft.server.v1_6_R3.PlayerConnection.e(PlayerConnection.java:116)
            at org.getspout.spout.SpoutPlayerConnection.e(SpoutPlayerConnection.java:195)
            at net.minecraft.server.v1_6_R3.ServerConnection.b(SourceFile:37)
            at net.minecraft.server.v1_6_R3.DedicatedServerConnection.b(SourceFile:30)
            at net.minecraft.server.v1_6_R3.MinecraftServer.t(MinecraftServer.java:592)
            at net.minecraft.server.v1_6_R3.DedicatedServer.t(DedicatedServer.java:227)
            at net.minecraft.server.v1_6_R3.MinecraftServer.s(MinecraftServer.java:488)
            at net.minecraft.server.v1_6_R3.MinecraftServer.run(MinecraftServer.java:421)
            at net.minecraft.server.v1_6_R3.ThreadServerApplication.run(SourceFile:583)
    Caused by: java.lang.NullPointerException
            at com.minecamp.spouties.EnteringRegion.onPlayerMove(EnteringRegion.java:35)
            at sun.reflect.GeneratedMethodAccessor62.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)
            ... 15 more
    >
    This my code:
    EnteringRegion.java
    Code:java
    1. package com.minecamp.spouties;
    2.  
    3. import java.util.Iterator;
    4.  
    5. import org.bukkit.Server;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.event.EventHandler;
    8. import org.bukkit.event.EventPriority;
    9. import org.bukkit.event.Listener;
    10. import org.bukkit.event.player.PlayerMoveEvent;
    11. import org.bukkit.plugin.Plugin;
    12. import org.bukkit.plugin.java.JavaPlugin;
    13.  
    14. import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
    15. import com.sk89q.worldguard.protection.ApplicableRegionSet;
    16. import com.sk89q.worldguard.protection.regions.ProtectedRegion;
    17.  
    18. public class EnteringRegion extends JavaPlugin implements Listener {
    19. private Server server;
    20.  
    21. private WorldGuardPlugin getwg() {
    22. this.server = getServer();
    23. Plugin pl = server.getPluginManager().getPlugin("WorldGuard");
    24. if(pl != null) {
    25. return ((WorldGuardPlugin)pl);
    26. } else {
    27. return null;
    28. }
    29. }
    30.  
    31. @EventHandler(priority = EventPriority.NORMAL)
    32. public void onPlayerMove(PlayerMoveEvent event) {
    33. Player p = event.getPlayer();
    34. if (p.getWorld().getName().equalsIgnoreCase("world") == true) {
    35. ApplicableRegionSet ar = this.getwg().getRegionManager(p.getWorld()).getApplicableRegions(p.getLocation());
    36. Iterator<ProtectedRegion> prs = ar.iterator();
    37. while (prs.hasNext()) {
    38. ProtectedRegion pr = prs.next();
    39. if (pr.getId().startsWith("music") == true) {
    40. event.getPlayer().sendMessage("That work !");
    41. }
    42. }
    43. }
    44. }
    45.  
    46. }


    On my main class, I put that on my onEnable() fonction:
    Code:java
    1. PluginManager pm = getServer().getPluginManager();
    2. pm.registerEvents((Listener) this.listener, this);


    Thank for the future help.
     
  2. Offline

    Garris0n

    Something is null on line 35. Figure out what it is. I'd assume it's the WorldGuard plugin, but maybe something else can return null there.

    Also, doing that every time a player moves is going to be extremely taxing on the server.
     
  3. Offline

    nateracecar5

    I would suggest doing "Ctrl + Shift + F" in your code. If I'm correct, and you're using Eclipse, it will clean up your code for you and make it much easier to read. It comes in handy when trying to read error logs. Now, to help you read the log next time you have an issue:


    Code:java
    1. Caused by: java.lang.NullPointerException
    2. at com.minecamp.spouties.EnteringRegion.onPlayerMove(EnteringRegion.java:35)
    3. at sun.reflect.GeneratedMethodAccessor62.invoke(Unknown Source)
    4. at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    5. at java.lang.reflect.Method.invoke(Unknown Source)
    6. at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:425)
    7. ... 15 more


    This is the part of the error you want to look at. You can see it says "Cause by: java.lang.NullPointerException". As you already said in the post, it is a null pointer exception. On the next line, it's giving you the package name, the class name, and the function name, then the line it's on. Here is where I'm getting that from:

    "com.minecamp.spoties" = package
    "EnteringRegion" = Class
    "onPlayerMove" is the function

    Now it's much easier to read right? Inside the () next to "onPlayerMove", you see it says "EnteringRegion.java:35". That 35 is the line number. Much easier to read, and it isn't as spooky as what it would normally look like.

    I'm not saying you didn't know this, because you probably did. But if you didn't, here is some help next time. :)
     
  4. Offline

    splint

    No one know how to use Worldguard api ?
    I need help :(
    Garris0n
     
  5. Offline

    Garris0n

    Did you read any of the posts on your thread?
     
  6. Offline

    splint

    I just fix myself this errors, and I know the error was on line 35, because he returned null, BUT I just don't understand why, and that why I'm here ;)
     
Thread Status:
Not open for further replies.

Share This Page