Confused by my listener

Discussion in 'Plugin Development' started by unrealdesign, Apr 18, 2013.

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

    unrealdesign

    So basically I don't know what is happening in this Listener and it's doing a couple things I don't think it should lol.

    Errors:
    Show Spoiler


    18:35:53 [SEVERE] Could not pass event PlayerMoveEvent to FZHideNSeek v1.0
    org.bukkit.event.EventException
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:427)
    at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    a:62)
    at org.bukkit.plugin.TimedRegisteredListener.callEvent(TimedRegisteredLi
    stener.java:26)
    at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
    ava:479)
    at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:464)
    at net.minecraft.server.v1_5_R2.PlayerConnection.a(PlayerConnection.java
    :217)
    at net.minecraft.server.v1_5_R2.Packet10Flying.handle(SourceFile:136)
    at org.spigotmc.netty.NettyNetworkManager.b(NettyNetworkManager.java:176
    )
    at net.minecraft.server.v1_5_R2.PlayerConnection.d(PlayerConnection.java
    :110)
    at net.minecraft.server.v1_5_R2.ServerConnection.b(SourceFile:35)
    at org.spigotmc.netty.NettyServerConnection.b(NettyServerConnection.java
    :66)
    at net.minecraft.server.v1_5_R2.MinecraftServer.r(MinecraftServer.java:5
    80)
    at net.minecraft.server.v1_5_R2.DedicatedServer.r(DedicatedServer.java:2
    29)
    at net.minecraft.server.v1_5_R2.MinecraftServer.q(MinecraftServer.java:4
    69)
    at net.minecraft.server.v1_5_R2.MinecraftServer.run(MinecraftServer.java
    :401)
    at net.minecraft.server.v1_5_R2.ThreadServerApplication.run(SourceFile:5
    73)
    Caused by: java.lang.NullPointerException
    at org.fragzone.listeners.Listeners.handleMove(Listeners.java:61)
    at sun.reflect.GeneratedMethodAccessor208.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.ja
    va:425)

    ... 15 more

    Note: This error is from the line: "p.sendMessage(HideNSeek.instanceOf().inThisArena.get(p));"

    Code:
    Code:java
    1.  
    2.  
    3. @EventHandler
    4. public void handleMove(PlayerMoveEvent e) {
    5. final Player p = e.getPlayer();
    6. if(e instanceof Player){
    7. if(HideNSeek.instanceOf().inThisArena.containsKey(p)){
    8. String arena = HideNSeek.instanceOf().inThisArena.get(p);
    9. if(!(HideNSeek.instanceOf().arenaStatus.get(arena).equals("hiding")) || !(HideNSeek.instanceOf().arenaStatus.get(arena).equals("seeking"))){
    10. if(HideNSeek.instanceOf().arenaHider.containsKey(p)){
    11. if(HideNSeek.instanceOf().hiderHidden.get(p).equals(arena)){
    12. if(HideNSeek.instanceOf().playerBlockLoc.containsKey(p)){
    13. p.teleport(p.getLocation());
    14. }
    15. }
    16. } else if(HideNSeek.instanceOf().arenaSeeker.containsKey(p)){
    17. if(HideNSeek.instanceOf().arenaStatus.get(arena).equals("hiding")){
    18. e.setCancelled(true);
    19. }
    20. }
    21. }
    22. }
    23. } else {
    24. p.sendMessage("fml");
    25. }
    26. }
    27.  



    First off: If I don't do "if(e instanceof Player)" then it gives errors. But, it does both sides of the if statement? I don't understand this, help would be appreciated! Ex: It sends the player the message "p.sendMessage("fml");" and also doesn't let the player move "p.teleport(p.getLocation());"

    Second off: if(HideNSeek.instanceOf().inThisArena.containsKey(p)) gives me errors. Why?


    Other Information:

    instanceOf() method:
    Code:java
    1.  
    2. private static HideNSeek instance;
    3.  
    4.  
    5. public static HideNSeek instanceOf() {
    6. return instance;
    7. }
    8.  
    9.  


    I tried:I tried to put in the main Listener instead of the Handler class and use this.plugin but that didn't work either. It won't return anything to do with the main class, even normal strings.

    Yes, all of the HashMaps return strings.

    Lastly
    The only way I got it to run both commands at the same time
    Code:java
    1.  
    2. @EventHandler
    3. public void handleMove(PlayerMoveEvent e) {
    4. final Player p = e.getPlayer();
    5. if(e instanceof Player){
    6. p.teleport(p.getLocation());
    7. } else {
    8. p.sendMessage("fml");
    9. }
    10. }
     
  2. Offline

    Morthis

    Are you setting instance = this in your onEnable?
     
  3. Offline

    Technius

    unrealdesign
    1. instance may be null
    2. HideNSeek.instanceOf().inThisArena may be null.
    3. You should just pass your main class's reference to your listener with a constructor. It will be easier and faster to use.
    4. "e" is not an instance of Player. "e" is a PlayerMoveEvent, which is a PlayerEvent, which is an Event, which is an Object.
     
  4. Offline

    unrealdesign

    Thank you both for your responses! I did not do anything in the onEnable.

    Technius
    Special thank you to you for explaining step-by-step.
    1. I forgot to define it in onEnable()
    2. It isn't, and if it is, it should just return and ignore anything, correct?
    3. I can't do this, because what I really do, is have a Listener class, then have a Handler class and the Handler methods are all protected.
    4. I was just trouble shooting, and I was just being dumb. I'm still very novice to Java and can't completely warp my head around this, but I'll look more into that. Thanks! :)

    Technius Morthis
    All I changed was add "instance = this;" into the onEnable() and it didn't seem to fix the problem. Do you know what I might be missing??
     
  5. Incorrect because you're not returning that, you're using a method on it.

    HideNSeek.instanceOf().inThisArena.get(p)

    So it doesn't just return and ignore it if inThisArena is null since it would translate to 'null.get()' which won't be possible.

    So check that as well... you should just make sure by printing a message with the values.. like:
    Code:
    System.out.print("instanceof = " + HideNSeek.instanceOf());
    System.out.print("inThisArena = " + HideNSeek.instanceOf().inThisArena);
    Before the NPE line and see which one says = null.

    That is assuming that 'HideNSeek' is a class, not a field...
     
  6. Offline

    unrealdesign

    So should I check something like: "HideNSeek.instanceOf().inThisArena != null" ? This is where my novice abilities come to an end :/ Thanks again for all this help.​
    HideNSeek is the main class​


    inThisArena is defined by this in the main class HideNSeek:
    Code:
    public Map<Player, String> inThisArena = new HashMap<Player, String>();[/cpde]
     
  7. unrealdesign
    No, you should not have that null in the first place.

    And I've also given you a way to find which variable is actually null...

    Also, you should NOT store Player objects long-term, it will cause you problems, use String and use player's name instead.
     
  8. Offline

    unrealdesign

    Sorry about the double post but I'm going to post what I know and maybe you can link me in the way of learning more about this problem I'm having.

    Basically in my main class I have defined:
    Code:java
    1.  
    2. private static HideNSeek instance;
    3.  
    4. public Map<Player, String> inThisArena = new HashMap<Player, String>();
    5.  
    6. public void onEnable() {
    7. instance = this;
    8.  
    9. public static HideNSeek instanceOf() {
    10. return instance;
    11. }
    12.  


    What I don't understand is how I can't then reach it through this? Is there a better way I can get into my main class from my Handlers.class which has protected methods and no connection to HideNSeek, and only connections to Listeners.class.

    Any links that could help me understand how instance is coming out to be null? I debuged like you said and I figured out: "instanceof = null" but I defined it as this in the onEnable() so it should do something there? Not sure :/


    Edit: Also, I'll do what you said and add all of the Player Hashmaps to Strings, that'll be a lot of work, but I bet it would cause problems eventually :p, thanks
     
  9. That looks fine, do what I said about printing the values so you know exacly which one is null and which one to follow and figure out where it breaks.

    I also suggest setting instance = null; in your onDisable() to not hold the main class in memory if plugin is disabled.

    EDIT:
    A note of warning, while changing Player to String key your code won't give compile errors on get() and containsKey() methods because they accept any object!
    Therefore you might miss replacing their arguments from player to player.getName()... I suggest just renaming the map's name in your main class so you can easily follow the compile errors in your editor and see where it's used, then rename it back.
     
  10. Offline

    unrealdesign

    @ edit: haha yeah, that's what I did. It took a little bit but I got all of it. Hardest part was the for statements sending messages, got kind of confusing for a second.

    And yes the value that printed null was "instanceof" which I don't understand how that works, was hinting at that in the other post.
     
  11. unrealdesign
    It shouldn't be null, there's something wrong there... well, I'm out of ideas, if you're willing to post your compiled jar I can find out what the problem is.

    EDIT: I hope you used Bukkit.getPlayerExact() to get the Player object back from the name, because getPlayer() is partial matching and you have a slight chance of glitching when people with similar names play.
     
  12. Offline

    unrealdesign

    Ehh sure, I really can't do anything past this point without more help :p I'll just PM you since you're the only one really helping anyways
     
Thread Status:
Not open for further replies.

Share This Page