OnPlayerJoin

Discussion in 'Plugin Development' started by Unshakablefaith, Apr 20, 2014.

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

    Unshakablefaith

    Please, someone tell me, why doesn't this send a message to the Player when they join?

    Code:
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerLoginEvent;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Main extends JavaPlugin implements Listener {
     
        public void onEnable() {
            getLogger().info("Plugin Enabled!");
        }
     
        public void onDisable() {
            getLogger().info("Plugin Disabled!");
        }
     
        @EventHandler
        public void onPlayerJoin(PlayerLoginEvent event) {
            event.getPlayer().sendMessage("Welcome to the Server!");
        }
     
    }
    
     
  2. Offline

    zDubsCrazy

    in onEnable(), insert: "getServer().getPluginManager().registerEvents(this, this);"
     
  3. Offline

    agent6262

    in onEnable() you need to add
    Code:java
    1. this.getServer().getPluginManager().registerEvents(this, this);
     
  4. Offline

    Unshakablefaith

    Can you please explain what this does and why I need it? :)

    Or you? Can you please explain it?

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

    zDubsCrazy

    Serves as the event registrator. Without it, your events will not work.
     
  6. Offline

    Unshakablefaith

    Don't mean to be vexing. Where did you acquire this knowledge at?

    The purpose of this plugin was to send a basic message to the player when he joins the server. "Welcome to the server!". The plugin still doesn't work properly, that is, nothing happens.
    Code:
    public class Main extends JavaPlugin implements Listener {
     
        public void onEnable() {
            getServer().getPluginManager().registerEvents(this, this);
            getLogger().info("PLUGIN ENABLED!");
        }
     
        public void onDisable() {
            getLogger().info("PLUGIN DISABLED!");
        }
     
        @EventHandler
        public void onPlayerJoin(PlayerLoginEvent event) {
            Player player = event.getPlayer();
            if (player.hasPlayedBefore()) {
                player.sendMessage("Welcome to the server " + ChatColor.GREEN
                        + player + "!");
            }
        }
     
    }
    And, where did you get to understand that getPluginManager() does that?

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

    zDubsCrazy

    I learned researching! And asking for help on Bukkit! And because it still does not work?
     
  8. Offline

    Unshakablefaith

  9. Offline

    beeselmane

    Code:java
    1. @EventHandler
    2. public void onPlayerJoin(PlayerLoginEvent event) {
    3. Player player = event.getPlayer();
    4. player.sendMessage(ChatColor.GREEN.toString() + "Welcome to the server " + player.getName() + "!");
    5. }
    6. }


    I made a few small changes ;) if it still doesn't work, then what are the errors?
     
  10. Offline

    Unshakablefaith


    This still doesn't work!:(
    This is the current code.
    Code:
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerLoginEvent;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Main extends JavaPlugin implements Listener {
     
        public void onEnable() {
            getServer().getPluginManager().registerEvents(this, this);
            getLogger().info("PLUGIN ENABLED!");
        }
     
        public void onDisable() {
            getLogger().info("PLUGIN DISABLED!");
        }
     
        @EventHandler
        public void onPlayerJoin(PlayerLoginEvent event) {
            Player player = event.getPlayer();
            player.sendMessage(ChatColor.GREEN.toString()
                    + "Welcome to the server " + player.getName() + "!");
        }
    }
     
  11. Offline

    beeselmane

    do you have a package? (can you post your plugin.yml :p)
     
  12. Offline

    Unshakablefaith

    Code:
    name: Test12
    main: me.Anthony.Test12.Main
    version: 1.4
     
  13. Offline

    beeselmane

    you have to put the main class into the package me.Anthony.Test12
    so just put "package me.Anthony.Test12" at the top of the main :p (and put it in those folders)
    (I see no other errors so that seems like it would be it) :p
     
  14. Offline

    zDubsCrazy

    USE EXACTLY THIS:
    CODE:
    Code:
    @EventHandler
    public void onJoin(PlayerJoinEvent event) {
     
    Player p = (Player) event.getPlayer();
     
    p.sendMessage(ChatColor.GREEN + "Welcome!");
     
    }
    In onEnable(): getServer().getPluginManager().registerEvents(this, this);
     
  15. Offline

    Unshakablefaith

    It's works, it was something on my end. My run.bat file did not contain the method (-o true). Thank you everyone! Very helpful!
     
  16. Offline

    itzrobotix

    Correct me if I'm wrong, but I thought PlayerLoginEvent was fired before the player was on the server? So how could it send them a message, use the PlayerJoinEvent as zDubsCrazy said. Also you did forget to register the events :p
     
  17. Offline

    agent6262

    itzrobotix true and that's why people use synchronized delayed tasks in that event
     
    itzrobotix likes this.
  18. Offline

    Maurdekye

    itzrobotix Yes, PlayerLoginEvent is called when the player clicks to join the server from their server list, and PlayerJoinEvent is called when the player spawns in the world. That's why his event didn't work originally; the player wasn't in the server when the event was being called.
     
  19. Offline

    itzrobotix

    So if that is when the PlayerLoginEvent is called when is the PlayerPreLoginEvent called?
     
  20. Offline

    Maurdekye

    itzrobotix There's a PlayerPreLoginEvent?
    Edit: Just checked, it's actually deprecated. Likely PlayerPreLoginEvent and PlayerLoginEvent served a similar purpose to eachother as do PlayerLoginEvent and PlayerJoinEvent.
     
    itzrobotix likes this.
Thread Status:
Not open for further replies.

Share This Page