Send a message to a user on login?

Discussion in 'Plugin Development' started by TobyG123, Aug 25, 2012.

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

    TobyG123

    I have this:

    Code:
    @EventHandler
        public void onPlayerLogin(PlayerLoginEvent event) {
            Player player = event.getPlayer();
            player.sendMessage("Unimportant");
               
           
        }
    however it does not want to work. I just started with Bukkit developing today, and wouldn't be surprised if it was a simple, easy-to-fix mistake.

    Thank you!
     
  2. Offline

    Giant

    You are looking for PlayerJoinEvent as opposed to PlayerLoginEvent. Reason for that is during Login you are staring at a nice "Logging in...".
     
  3. Offline

    travja

    use this:
    Code:java
    1. @EventHandler
    2. public void onPlayerLogin(PlayerLoginEvent event){
    3. final Player player = event.getPlayer();
    4. Bukkit.getServer().getScheduler().scheduleSyncTask(plugin, new Runnable(){
    5. public void run(){
    6. player.sendMessage("Unimportant");
    7. }
    8. }
    9. }


    EDIT: And half ninja'd by Giant
     
  4. Offline

    Giant

    travja that scheduler is not really required though. It should work just fine without. Edit, yours would also still listen to the Login event instead of the Join event... ;)
     
  5. Offline

    travja

    Giant True, but I find even with PlayerJoinEvent that they aren't quite in game yet and therefore won't do anything with them...
     
  6. Offline

    Giant

    travja During PlayerJoinEvent the player is in the game even though the player himself might still see a black screen. Anything you do to the player at that point, will be happening in the game. It might however happen that a spam happy plugin like Essentials is beating you to being the last plugin to send the message, causing yours to fall out of view...

    The reason why teleporting during the event is not too clever is because you would break off a chunkload mid loading. And I suppose it may cause the client to crash... :p
     
  7. Offline

    TobyG123

    Hmm. Two things.

    1. An error occurs that says that plugin cant be resolved to a variable.
    2. Another one on one of the brackets that says a ) and a ; are missing.

    Thank you for your help!
     
  8. Offline

    travja

    replace plugin with this and replace scheduleSyncDelayedTask with scheduleSyncTask
     
  9. Offline

    Giant

    TobyG123 simply changing Login to Join in your own code will work just as well... :)
     
  10. Offline

    travja

  11. Offline

    TobyG123

    Giant Still nothing.

    I dont care, here is the ENTIRE plugin:

    Code:
    package net.arcadecraft;
     
     
    import java.util.logging.Logger;
     
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.event.player.PlayerLoginEvent;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class AlphaAccess extends JavaPlugin {
     
        public void onEnable(){
            getLogger().info("AlphaAccess has been enabled!");
            getLogger().info("Custom ArcadeCraft plugin by ");
           
        }
     
        public void onDisable() {
            getLogger().info("AlphaAccess has been disabled!");
        }
        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent event) {
            Player player = event.getPlayer();
            player.sendMessage(ChatColor.GREEN + "Welcome to ArcadeCraft Alpha," + ChatColor.BLUE + player + ChatColor.GREEN + "!" + ChatColor.GOLD + "If you are a developer or have Alpha Access, please use /access to authenticate.");
               
       
        }
       
       
       
     
     
       
       
       
    }
    
    The scheduler gave me a "Plugin could not be assigned to a variable or something like that.

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

    Giant

    You forgot to register the event in your onEnable...
    Add getServer().getPluginManager().registerEvents(this, this) to there.
    And also make sure you extend JavaPlugin AND implement Listener!
     
  13. Offline

    travja

    Add the scheduler as it is in my post.

    EDIT: Ninja'd by Giant with correct answer!
     
  14. Offline

    TobyG123

  15. Offline

    Giant

    Your very welcome! :)
    oooooh! I've reached the msg spam limit! :D
     
    TobyG123 likes this.
  16. Offline

    TobyG123

    Giant
    When I log in, it shows me as something like CraftPlayer{name=TobyG123}
     
  17. Offline

    Giant

    TobyG123 Oh, that is because you output the full Player object. Which then goes into it's toString() method... :p

    What you want to do to fix that is change the player in the message to player.getName()! Like so:
    Code:
    player.sendMessage(ChatColor.GREEN + "Welcome to ArcadeCraft Alpha," + ChatColor.BLUE + player.getName() + ChatColor.GREEN + "!" + ChatColor.GOLD + "If you are a developer or have Alpha Access, please use /access to authenticate.");
    
     
  18. Offline

    TobyG123

    Oh! I went to sleep without thanking you!

    Thank you!
     
  19. Offline

    Giant

    No worries, I K.O.ed at 7 or 8 am anyhow... :p
     
Thread Status:
Not open for further replies.

Share This Page