OnJoin event Not Working

Discussion in 'Plugin Development' started by SteppingHat, Jul 26, 2012.

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

    SteppingHat

    So, I have this chunck of of code
    Code:
    @EventHandler
        public void onJoin(PlayerJoinEvent event) {
            Player player = event.getPlayer();
            Bukkit.getServer().broadcastMessage(player.getDisplayName() + " has logged in and ");
     
            if (toTeleport.contains(player.getName())) {
                Location originalPlayerLocation = toTeleportLoc.get(event.getPlayer().getName());
                player.teleport(originalPlayerLocation);
                toTeleport.remove(player.getName());
                toTeleportLoc.remove(event.getPlayer().getName());
                Bukkit.getServer().broadcastMessage("is in the hashmap");
            } else {
                Bukkit.getServer().broadcastMessage("is not in the hashmap");
            }
        }
    And it doesn't seem to work. The hashmap works but the onJoin event itself doesn't work as in it doesn't display any message at all.

    So, what am I doing wrong :p
     
  2. Offline

    r0306

  3. Offline

    Taien

    It's because you're using Bukkit.getServer(). I'm assuming this code is in a separate class from the main code? If it is, you need to construct it with a reference to the plugin, or if it isn't (if it's part of the main class) you can just use this.getServer().etc.etc
     
  4. Offline

    r0306

    Taien
    Nah. You can also call up Bukkit.broadcastMessage() and it will still work. No constructors are needed for this.
     
  5. Registered the event?
     
  6. Offline

    NerdsWBNerds

    Could you post your main java class code as well. (onEnable and onDisable)
     
  7. Offline

    SteppingHat

    Yes. Always :)
    It's just one class. That is just after the onEnable event. The code for the first section of the plugin is
    Code:
    @Override
        public void onDisable() {
            Bukkit.getServer().getScheduler().cancelTasks(this);
            PluginDescriptionFile pdfFile = this.getDescription();
            this.log.info(pdfFile.getName() + " has been disabled!");
        }
     
        @Override
        public void onEnable() {
            PluginDescriptionFile pdfFile = this.getDescription();
            log.info("--------TEMPTELEPORT by SteppingHat--------");
            this.log.info(pdfFile.getName() + " v" + pdfFile.getVersion() + " has been enabled!");
            log.info("-------------------------------------------");
     
            this.getConfig().options().copyDefaults(true);
            this.saveConfig();
        }
     
        @EventHandler
        public void onJoin(PlayerJoinEvent event) {
            Player player = event.getPlayer();
            Bukkit.getServer().broadcastMessage(player.getDisplayName() + " has logged in and ");
       
            if (toTeleport.contains(player.getName())) {
                Location originalPlayerLocation = toTeleportLoc.get(event.getPlayer().getName());
                player.teleport(originalPlayerLocation);
                toTeleport.remove(player.getName());
                toTeleportLoc.remove(event.getPlayer().getName());
                Bukkit.getServer().broadcastMessage("is in the hashmap");
            } else {
                Bukkit.getServer().broadcastMessage("is not in the hashmap");
            }
        }
    What does this have to do with the Bukkit.broadcastMessahe()? It doesn't work with or without it.
    Please elaborate on what that means :p
    Posted above :)

    Thanks for all your help so far. It's all much appreciated.
     
  8. Offline

    azazad

    Hmm... I don't seem to see any call to registerEvents() in there...
     
  9. Offline

    SteppingHat

    Well, considering that I don't know what that means, can you please explain?
    This is my first time using event's in my plugins so yeah, I don't know how any of it works :p
     
  10. Offline

    NerdsWBNerds

    Insert the following code in your onEnable method.

    Code:
    getServer().getPluginManager().registerEvents(this, this);
    
    If you are using a seperate class as your listener, replace the first this with that listener.
     
  11. Offline

    SteppingHat

    Not working :(
    It's all still in the same class. Not a single one of the defined messages appear.
     
  12. Offline

    SteppingHat

  13. Offline

    grandwazir

    You have implemented the Listener class yes? You have also forgotten to set an event priority although I am not sure if that is your issue.
     
  14. Offline

    SteppingHat

    Im just using the one class.
    Do I need another class?
    If so, what would be in it.

    Thanks!
     
  15. Offline

    grandwazir

    Take a look at this example listener, as far as I am aware you need your class to implement the listener class to receive events.
     
  16. Offline

    russjr08

    After where you put "extends JavaPlugin" you need to add "implements Listener" so it would be something like

    Code:java
    1.  
    2. //Package here
    3.  
    4.  
    5. // Imports here
    6.  
    7. public class YourClass extends JavaPlugin implements Listener{
    8.  
    9. //Methods go here
    10.  
    11. }
    12.  


    Don't forget to import listener!
     
  17. Offline

    SteppingHat

    What do I put in the main class?
     
  18. Offline

    russjr08

    You look like you already had everything in the main class, you just weren't implementing listener :)
     
  19. Offline

    grandwazir

    You don't need to put anything extra as long as you implement listener.
     
  20. Offline

    np98765

    I'm having this same issue, too... One class, I registered the event and everything. I'm using the PlayerLoginEvent as well...

    Code:
    public class MyClass extends JavaPlugin implements Listener {
     
        @Override
        public void onEnable() {
            getServer().getPluginManager().registerEvents(this, this);
            getLogger().info("Enabled.");
        }
       
        @Override
        public void onDisable() {
            getLogger().info("Disabled.");
        }
       
        @EventHandler
        public void onJoin(PlayerLoginEvent event) {
            Player p = event.getPlayer();
            if (p.hasPermission("some.useless.node") || p.isOp()) {
                ItemStack bluewool = new ItemStack(Material.WOOL, 1, (short)11);
                p.getInventory().setHelmet(bluewool);   
                p.sendMessage("Success!");
            }
        }
    }
    
    I'm not getting the Success! message that I should be. Also, an unrelated question -- Is that the correct way to use blue wool? I tried bluewool.setData(11) but got an error that ints weren't allowed, only MaterialData was. -_-
     
  21. Offline

    grandwazir

    I am guessing you also need to define an event priority to receive messages. Like this:

    Code:
    @EventHandler(priority=EventPriority.NORMAL)
    public void onJoin(PlayerLoginEvent event) {
    ....
    
     
  22. Offline

    np98765

    Just tried that... It didn't work. -_-

    Code:
    @EventHandler(priority=EventPriority.NORMAL)
    public void onJoin(PlayerLoginEvent event) {
            Player p = event.getPlayer();
            if (p.hasPermission("some.useless.node") || p.isOp()) {
                ItemStack bluewool = new ItemStack(Material.WOOL, 1, (short)11);
                p.getInventory().setHelmet(bluewool);
                p.sendMessage("Success!");
            }
    
    Any other ideas? As far as I can see, I did everything right... (This is my first time using events, so I'm probably making a simple mistake).
     
  23. Offline

    grandwazir

    Put the success message outside the if statement to check if the event is being called or not. You could also use System.out.print to ensure you see the message in the log rather than in chat, it is easier to debug.
     
  24. Offline

    np98765

    Ok...

    Code:
    @EventHandler(priority=EventPriority.NORMAL)
    public void onJoin(PlayerLoginEvent event) {
    Player p = event.getPlayer();
            if (p.hasPermission("some.useless.node") || p.isOp()) {
                ItemStack bluewool = new ItemStack(Material.WOOL, 1, (short)11);
                p.getInventory().setHelmet(bluewool);
            }
    p.sendMessage("Success!");
    getLogger().info("Yay!");
    }
    
    The console got Yay! while I got nothing.

    Figured it out thanks to Bavestry . :)

    Turns out we need to use PlayerJoinEvent, not PlayerLoginEvent -- Anyone know why? :eek:

    I was just using PlayerLoginEvent because I saw it in the tutorial and happened to need it...

    Hat (not sure if you're watching this :p)

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

    zack6849

    from what i understand PlayerLoginEvent is when the player clicks the join button, but before they actually log in.
    however im not entirely sure
     
  26. Offline

    Bavestry

    zack6849
    Wouldn't that be the PlayerPreLoginEvent?
     
  27. Offline

    Firefly

    PlayerLoginEvent is fired before the Player object is actually "initialized" PlayerJoinEvent occurs when the Player object is no longer null. Therefore you should use PlayerJoinEvent for most cases where you need to access the Player itself involved with the event.
     
Thread Status:
Not open for further replies.

Share This Page