Send a message to a player on first time join

Discussion in 'Plugin Development' started by edragy, Jul 8, 2012.

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

    edragy

    I need to send a message to the player when they join the server for the first time
     
  2. Offline

    Cowboys1919

    If you are looking for a plugin, this may be of interest.
    If you want to make one, store a list of players that have visited the server and test it with the onPlayerJoin event
     
  3. Offline

    one4me

    Code:
    @EventHandler
    public void onPlayerJoinEvent(PlayerJoinEvent event) {
      if(!event.getPlayer().hasPlayedBefore()) {
        event.getPlayer().sendMessage("Message Here");
      }
    }
    
     
    ferrybig likes this.
  4. Offline

    Cowboys1919

    Ha, too easy, forget what i said.
     
  5. Offline

    edragy

    I have tried that code, it does nothing, no errors
     
  6. Offline

    one4me

    Have you played on the server before?
     
  7. Offline

    JayzaSapphire

    Have you registered your listener?
     
  8. Offline

    edragy

    No, I created a new server to test it and i also deleted the line if .... and it also didn't work
     
  9. Offline

    one4me

  10. Offline

    Cowboys1919

    If you deleted the line with the if and it didn't work then you must not have registered the listener right
     
  11. Offline

    edragy

    how do I register the listener?

    also how do I make it so it sends this message after all other plugins send there join event messages?

    one4me
     
  12. Offline

    Cowboys1919

    Well, to do it all in the same class, implement Listener, and in onEnable() register with getServer().getPluginManager().registerEvents(this, this);
     
  13. Offline

    one4me

    Do what Cowboys1919 said to register the listener. To make it send the message after everything else you could try changing the the event handler to the higgest priority by replacing @EventHandler with @EventHandler(priority = EventPriority.HIGHEST).
     
  14. Offline

    edragy

    tell me what code to type and where to type it plz
     
  15. Offline

    Cowboys1919

    ...
    I pretty much just said what to type, i guess you're looking for copy/paste stuff.
    but really what else do you need i gave you all the code.
    COPY: getServer().getPluginManager().registerEvents(this, this); Into onEnable()
    IMPLEMENT: Listener
     
  16. Offline

    edragy

    sorry I am noob at java, how do you implement listener?

    is this line it goes in? public class CreeperCraftPlugin extends JavaPlugin {
     
  17. Offline

    Cowboys1919

    Jamboozlez likes this.
  18. Offline

    edragy

    and listener imports org.bukkit.event.Listener, right?
     
  19. Offline

    Cowboys1919

    Doesn't work without it does it?
    Yes that is the thing you have to import.
     
  20. Offline

    edragy

    Does deleting a player's .dat file in world/players make it so the server thinks it is a first time join?

    I can't get it to do it after all other messages!

    Wait, I have a idea, can I make it so people who are joining first time get message A and people who have joined b4 get message B? Also, can I make it so I get those messages from a config file or is that too hard?

    one4me can you help?

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

    one4me

    For me to do that, it'd help if you went ahead and posted your entire class.
     
  22. Offline

    edragy

    Code:
    package com.gmail.edragy.CreeperCraft;
       
    import java.util.logging.Logger;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.plugin.java.JavaPlugin;
       
        public class CreeperCraftPlugin extends JavaPlugin implements Listener {
       
            Logger log;
       
            public void onEnable(){
                getServer().getPluginManager().registerEvents(this, this);
                log = this.getLogger();
                log.info("CreeperCraft 1.00 has been enabled.");
            }
       
            public void onDisable(){
                log.info("CreeperCraft 1.00 has been disbaled.");
            }
        @EventHandler(priority = EventPriority.HIGHEST)
            public void onPlayerJoinEvent(PlayerJoinEvent event) {
              if(!event.getPlayer().hasPlayedBefore()) {
                event.getPlayer().sendMessage("Message Here");
              }
            }
                public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
                    if(cmd.getName().equalsIgnoreCase("fakejoin")){
                        if (args.length > 1) {
                            sender.sendMessage(ChatColor.RED + "Usage: /fakejoin (Player)");
                            return false;
                        }
                        if (args.length < 1) {
                            sender.sendMessage(ChatColor.RED + "Usage: /fakejoin (Player)");
                            return false;
                        }
                        this.getServer().broadcastMessage(ChatColor.YELLOW + args[0] + " joined the game.");
                        sender.sendMessage(ChatColor.RED + "You forced " + args[0] + " to join the game.");
                        return true;
                    } else if (cmd.getName().equalsIgnoreCase("spawn")) {
                        sender.sendMessage(ChatColor.RED + "Don't use that! Instead, use /mvs");
                        return true;
                    }
                    else if (cmd.getName().equalsIgnoreCase("fakequit")) {
                        if(sender instanceof Player){
                        if (args.length != 1) {
                        sender.sendMessage(ChatColor.RED + "Usage: /fakequit (Player)");
                        return true;
                        //you can just return false and not send the sender a message as they will get the message that is in the plugin.yml but it seems you prefer this method.
                        }else{
                        this.getServer().broadcastMessage(ChatColor.YELLOW + args[0] + " left the game.");
                        sender.sendMessage(ChatColor.RED + "You forced " + args[0] + " to leave the game.");
                        sender.sendMessage("Your name is "+((Player)sender).getDisplayName());
                        return true;
                        }
                        }else{
                        sender.sendMessage("You must be a player to use this command.");
                        return true;
                        }
                    }
                        else if (cmd.getName().equalsIgnoreCase("new")) {
                            if(sender instanceof Player){
                            if (args.length != 0) {
                            sender.sendMessage(ChatColor.RED + "Usage: /fakequit (Player)");
                            return true;
                            //you can just return false and not send the sender a message as they will get the message that is in the plugin.yml but it seems you prefer this method.
                            }else{
                                sender.sendMessage(ChatColor.YELLOW + "---------------------");
                            sender.sendMessage(ChatColor.YELLOW + "Welcome to CreeperCraft, "+((Player)sender).getDisplayName() + "!");
                            sender.sendMessage(ChatColor.GREEN + "Creepercraft has four main worlds: " + ChatColor.RED + "Survival, Hardcore, ");
                            sender.sendMessage(ChatColor.RED + "Creative and Creative Flatland." + ChatColor.GREEN + "To get to these worlds, type /warp portals.");
                            return true;
                            }
                            }else{
                            sender.sendMessage("You must be a player to use this command.");
                            return true;
                        }
                                    }
                    return false;
                            }
                {
                }
    }
    one4me
     
  23. Offline

    Cowboys1919

    Code:
    if (event.getPlayer().hasPlayedBefore())
      event.getPlayer().sendMessage("Normal Join Message");
    else
      event.getPlayer().sendMessage("Welcome First Time Message");")
    This is untested, but i think should work.
     
  24. Offline

    edragy

    It works, but how would I get it so that .getDisplayName() gets the name of the person who joins for the first time?
     
  25. Look at this:

    Code:
    if (event.getPlayer().hasPlayedBefore())
      event.getPlayer().sendMessage("Normal Join Message");
    else {
      event.getPlayer().sendMessage("Welcome First Time Message");
      event.getPlayer().getDisplayName(); // this is how you get the display name of the player
    }
    It is very easy, if you wan't to do something with it you can store it in a String, like this:
    Code:
    String playerName = event.getPlayer().getDisplayName();
    greetz blackwolf12333
     
  26. Offline

    edragy

    ok thanks but how do I send a message to all players but the one that joined for the first time?

    I have a problem, when I put one line of code after the "if" line, it works, two lines, like below, doesn't.

    This works:
    Code:
        @EventHandler(priority = EventPriority.HIGHEST)
            public void onPlayerJoinEvent(PlayerJoinEvent event) {
              String playerName = event.getPlayer().getDisplayName();
            if (event.getPlayer().hasPlayedBefore())
                event.getPlayer().sendMessage("---------------------------------------------------");
                else {
                  event.getPlayer().sendMessage("Hi there, " + ChatColor.YELLOW + playerName + " !");
                  event.getPlayer().sendMessage("Welcome to CreeperCraft!");
                  this.getServer().broadcastMessage(ChatColor.DARK_GREEN + "Welcome new player: " + ChatColor.YELLOW + playerName);
    This doesn't:
    Code:
        @EventHandler(priority = EventPriority.HIGHEST)
            public void onPlayerJoinEvent(PlayerJoinEvent event) {
              String playerName = event.getPlayer().getDisplayName();
            if (event.getPlayer().hasPlayedBefore())
                event.getPlayer().sendMessage("---------------------------------------------------");
                event.getPlayer().sendMessage("---------------------------------------------------");            else {
                  event.getPlayer().sendMessage("Hi there, " + ChatColor.YELLOW + playerName + " !");
                  event.getPlayer().sendMessage("Welcome to CreeperCraft!");
                  this.getServer().broadcastMessage(ChatColor.DARK_GREEN + "Welcome new player: " + ChatColor.YELLOW + playerName);
    Cowboys1919

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 26, 2016
  27. Code:
    for(Player p : Bukkit.getServer().getOnlinePlayers()) {
        if(!(p == event.getPlayer())) {
            p.sendMessage(Whatever you want to send them here;
        }
    }
    Put that in your onPlayerJoin function or whatever you called it:p
     
  28. Offline

    one4me

    Almost forgot, edragy
    Here you are, I think this is what you're trying to do -
    Code:
    package com.gmail.edragy.CreeperCraft;
       
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    public class main extends JavaPlugin implements Listener {
      public void onDisable(){
      }
      public void onEnable(){
        getServer().getPluginManager().registerEvents(this, this);
      }
      @EventHandler(priority = EventPriority.HIGHEST)
      public void onPlayerJoinEvent(PlayerJoinEvent event) {
        this.getServer().broadcastMessage(event.getJoinMessage());
        event.setJoinMessage(null);
        if(!event.getPlayer().hasPlayedBefore()) {
          for(Player p : Bukkit.getServer().getOnlinePlayers()) {
            if(!(p == event.getPlayer())) {
              p.sendMessage(ChatColor.LIGHT_PURPLE + "Please welcome " + event.getPlayer().getDisplayName() + " to the server.");
            }
          }
          event.getPlayer().performCommand("new");
        }
        else {
          event.getPlayer().sendMessage(ChatColor.LIGHT_PURPLE + "Welcome back, " + event.getPlayer().getDisplayName() + "!");
        }
      }
      public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
        if(cmd.getName().equalsIgnoreCase("spawn")) {
          sender.sendMessage(ChatColor.RED + "Don't use that! Instead, use /mvs");
          return true;
        }
        if(cmd.getName().equalsIgnoreCase("fakejoin")){
          if(sender instanceof Player && args.length == 0) {
            this.getServer().broadcastMessage(ChatColor.YELLOW + sender.getName() + " joined the game.");
            sender.sendMessage(ChatColor.RED + "You have fakejoined the game.");
            return true;
          }
          if (args.length == 1) {
            this.getServer().broadcastMessage(ChatColor.YELLOW + Bukkit.getOfflinePlayer(args[0]).getName() + " joined the game.");
            sender.sendMessage(ChatColor.RED + "You forced " + args[0] + " to join the game.");
            return true;
          }
          if(args.length > 1) {
            sender.sendMessage(ChatColor.RED + "Usage: /fakejoin (Player)");
            return true;
          }
        }
        if(cmd.getName().equalsIgnoreCase("fakequit")) {
          if(sender instanceof Player && args.length == 0) {
            this.getServer().broadcastMessage(ChatColor.YELLOW + sender.getName() + " left the game.");
            sender.sendMessage(ChatColor.RED + "You have fakequit the game.");
            return true;
          }
          if(args.length == 1) {
            this.getServer().broadcastMessage(ChatColor.YELLOW + Bukkit.getOfflinePlayer(args[0]).getName() + " left the game.");
            sender.sendMessage(ChatColor.RED + "You forced " + args[0] + " to leave the game.");
            return true;
          }
          if(args.length > 1) {
            sender.sendMessage(ChatColor.RED + "Usage: /fakequit (Player)");
            return true;
          }
        }
        if(cmd.getName().equalsIgnoreCase("new")) {
          if(sender instanceof Player){
            if(args.length != 0) {
              sender.sendMessage(ChatColor.RED + "Usage: /new");
              return true;
            }
            else {
              sender.sendMessage(ChatColor.YELLOW + "Welcome to CreeperCraft, "+((Player)sender).getDisplayName() + "!");
              sender.sendMessage(ChatColor.GREEN + "Creepercraft has four main worlds:");
              sender.sendMessage(ChatColor.RED + " - Survival, Hardcore, Creative and Creative Flatland");
              sender.sendMessage(ChatColor.GREEN + " To get to these worlds, type /warp portals.");
              return true;
            }
          }
          else {
            sender.sendMessage("You must be a player to use this command.");
            return true;
          }
        }
        return false;
      }
    }
    
    My only question is why not use use Vanish, MobDisguise, or any of the other already made and test plugins that can accomplish what you're trying to do?
     
  29. Ever heard of { and }...if you really don't know anything about programming you should first have a look at this:
    http://docs.oracle.com/javase/tutorial/java/index.html

    i hope that will help you understand java a bit, it did learn me some things too:p
     
  30. Offline

    edragy

    told you I was a noob :D

    how do I make it so it gets the messages from config?
    here is my code:
    Code:
         @EventHandler(priority = EventPriority.HIGHEST)
            public void onPlayerJoinEvent(PlayerJoinEvent event) {
              String playerName = event.getPlayer().getDisplayName();
             if (event.getPlayer().hasPlayedBefore())
                 event.getPlayer().sendMessage("-----------------------------------------------------\n" + ChatColor.YELLOW + "Welcome to " + ChatColor.GREEN + "CreeperCraft, " + ChatColor.YELLOW + playerName + "!\n" + ChatColor.RESET + "-----------------------------------------------------\n" + ChatColor.DARK_AQUA + "Check out our website for all the latest news and information\nabout CreeperCraft. Website: www.creeperminecraft.tk" + ChatColor.GOLD + "\nSupport CreeperCraft! Get Donator rank today!" + ChatColor.RED + " /donate\n" + ChatColor.RESET + "-----------------------------------------------------");
    
                else {
                  event.getPlayer().sendMessage("Hi there, " + ChatColor.YELLOW + playerName + ChatColor.RESET + "!");
                  event.getPlayer().sendMessage("Welcome to CreeperCraft!");
                  this.getServer().broadcastMessage(ChatColor.DARK_GREEN + "Welcome new player: " + ChatColor.YELLOW + playerName);
                }
         }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 26, 2016
Thread Status:
Not open for further replies.

Share This Page