Solved Custom event problem?

Discussion in 'Plugin Development' started by chaseoes, Sep 29, 2012.

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

    chaseoes

    So here's what I have:
    Code:java
    1. @EventHandler(priority = EventPriority.LOWEST)
    2. public void firstJoinDetection(PlayerLoginEvent event) {
    3. // Define our variables.
    4. Player player = event.getPlayer();
    5.  
    6. // Call the first join event.
    7. if (!player.hasPlayedBefore()) {
    8. System.out.println(player);
    9. FirstJoinEvent fjp = new FirstJoinEvent(player);
    10. plugin.getServer().getPluginManager().callEvent(fjp);
    11. return;
    12. }

    As you see on line 8 there, I print the player, which works fine resulting in:
    Code:
    CraftPlayer{name=chaseoes}
    And then it calls my custom event, this is what I have for that:
    Code:java
    1. package me.chaseoes.firstjoinplus;
    2.  
    3. import org.bukkit.entity.Player;
    4. import org.bukkit.event.Event;
    5. import org.bukkit.event.HandlerList;
    6.  
    7. public class FirstJoinEvent extends Event {
    8. private static final HandlerList handlers = new HandlerList();
    9. public Player p;
    10.  
    11. public FirstJoinEvent(Player player) {
    12. player = p;
    13. }
    14.  
    15. public HandlerList getHandlers() {
    16. return handlers;
    17. }
    18.  
    19. public static HandlerList getHandlerList() {
    20. return handlers;
    21. }
    22.  
    23. public Player getPlayer() {
    24. return p;
    25. }
    26. }

    Then I listen to my custom event:
    Code:java
    1. @EventHandler
    2. public void onFirstJoin(FirstJoinEvent event) {
    3. System.out.println(event.getPlayer());
    4. }

    However, from that code, the player is returning null (proven by that debugging where I print the player). I'm passing a non-null player to my custom event, but I get it back as null? Am I missing something here?

    Thanks!
     
  2. Code:
    public Player p;
     
    public FirstJoinEvent(Player player) {
        player = p;
    }
    because you do it the wrong way around. Currently you assign 'player' (which is the one you pass in) to 'p' which is currently null, resulting in both variables being null. Hence, do it the other way around.
     
  3. Offline

    chaseoes

    Oh, wow.. thanks!
     
  4. to prevent this kind of bugs in the future, mark al the variables inside your class as final whits are not indented to chance during the objects life spam
     
  5. doesn't matter as he's assigning it in the constructor, only if he'd mark the parameter as final. using the 'this' operator would help a lot more.
     
Thread Status:
Not open for further replies.

Share This Page