[Tut] Set Motd with Player name

Discussion in 'Resources' started by FisheyLP, May 5, 2014.

Thread Status:
Not open for further replies.
  1. Do you ever wanted to set the motd like this: Welcome on my server, <name>? Then this is the right thread you are looking for. At first make a config.yml in your project folder and write in it: Motd: ''. In onEnable() write this:
    Code:Java
    1.  
    2. public void onEnable() {
    3. saveDefaultConfig();
    4. }
    5.  

    Then make two methods:
    Code:Java
    1.  
    2. public void setMotd(String motd) {
    3. getConfig().set("Motd", motd);
    4. saveConfig();
    5. }
    6.  

    and
    Code:Java
    1.  
    2. public String getMotd() {
    3. return ChatColor.translateAlternateColorCodes('&', getConfig().getString("Motd"));
    4. }
    5.  

    next lets make our command to set and display the motd:
    Code:Java
    1.  
    2. @Override
    3. public boolean onCommand(CommandSender sender, Command command, String cmd,
    4. String[] args) {
    5.  
    6. Player p = (Player) sender;
    7. if (commandLabel.equalsIgnoreCase("motd")) {
    8. if (args.length == 0) {
    9. //Display player the motd
    10. p.sendMessage("Motd: §r" + getMotd());
    11. return true;
    12. } else {
    13. //Player writes a new Motd:
    14. String msg = "";
    15. for (int i = 0; i < args.length; i++) {
    16. msg = msg + " " + args;
    17. }
    18. msg = msg.substring(1);
    19. setMotd(msg);
    20. p.sendMessage("Motd set: §r"
    21. + getMotd());
    22. return true;
    23. }
    24. }
    25. return true;
    26. }
    27.  

    Next we need two events, the first is the PlayerJoinEvent:
    Code:Java
    1.  
    2. @EventHandler
    3. public void onJoin(PlayerJoinEvent e) {
    4. Player p = e.getPlayer();
    5. String ip = "" + p.getAddress().getAddress().getHostAddress();
    6. ip = ip.replace(".", "_");
    7. if (!getConfig().contains("IPs." + ip)) {
    8. getConfig().set("IPs." + ip, p.getName());
    9. saveConfig();
    10. }
    11. }
    12.  

    It just puts in the config the players ip and his name if it doesnt contains it.
    The next event is the ServerListPingEvent, thats when you click in Minecraft on Multiplayer and it shows you all the servers:
    Code:Java
    1.  
    2. @EventHandler
    3. public void onPing(ServerListPingEvent e) {
    4. String ip = "" + e.getAddress();
    5. ip = ip.substring(1);
    6. try {
    7. e.setMotd(getMotd().replace("<player>",
    8. getConfig().getString("IPs." + ip.replace(".", "_"))));
    9. } catch (Exception e2) {
    10. e.setMotd(getMotd());
    11. }
    12.  
    13. }
    14.  

    it replaces the <player> in the motd with the players name (from the config), if it doesnt work then it doesnt replace the <player> with the players name :) I hope that helped you! Please give me feedback if it helped you
     
  2. Offline

    bigteddy98

    Nice for small servers, but do not forget to mention that if you're having thousands of players on your server your "config file", which is made to be little configuration, is not going to like thousands of lines in it. I highly advise using a database for this.
     
    Plo124 likes this.
  3. if someone have such a big server, he can use a database but this tutorial is made for a config file
     
  4. Offline

    Garris0n

    Don't use raw color codes, use the ChatColor enum.
     
    iiHeroo likes this.
  5. Offline

    macguy8

    As well, don't use .replace to convert & into chat colors - Use ChatColor.translateAlternateColorCodes('&', stringToTranslate) (or some method like that) as it will only filter certain things. For example...

    Welcome to AwesomeServer! We have Factions & Creative!

    would be messed up by your translator, whereas ChatColor's version will preserve the & sign.
     
  6. Okey then I use ChatColor.translateAlternateCoorCodes ...
     
  7. Offline

    ccrama

    I once created a system like this, but removed it because multiple players on the same IP will see only the latest player's name to join the server
     
Thread Status:
Not open for further replies.

Share This Page