Solved Get username when no display name specified

Discussion in 'Spigot Plugin Development' started by IndieGuts, May 31, 2017.

  1. Hey guys, I'm still decently new to Java coding, still learning overtime, and I've learned some of the basics, and I've tried a few methods, and haven't found any solutions online.

    What I'm trying to do is, when they join, send them a MOTD & if they haven't joined before, send them a "Welcome" message, but I'm trying to use player.getCustomName(), so if a player is using a nickname, or related, it'll use their custom name, but how could I add a 'check' (or another term)
    that if they DON'T have a "custom name" to simply use their username?


    Code (Text):
    package com.indieguts.IndieCore;

    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;

    public class MOTD implements Listener {

        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent playerJoins) {
            Player player = playerJoins.getPlayer();
            player.sendMessage(ChatColor.translateAlternateColorCodes('&', "Welcome back " + player.getCustomName() + "!"));
            if(!player.hasPlayedBefore()) {
                player.sendMessage(ChatColor.translateAlternateColorCodes('&', "Welcome to the server " + player.getCustomName() + "!"));
                player.sendMessage(ChatColor.translateAlternateColorCodes('&', "Your displayname is &a" + player.getDisplayName())); //DEBUG
                if(player.getCustomName() == "null" ) {
                    player.setCustomName( player.getDisplayName());
                    player.sendMessage("You had no custom name!"); //DEBUG
                }
            }
        }

    }
    at the moment, it'll simply say "null" instead of a username.
     
  2. Woopsie, I added the

    Code (Text):
                if(player.getCustomName() == "null" ) {
                    player.setCustomName( player.getDisplayName());
                    player.sendMessage("You had no custom name!");
    in the completely wrong spot!

    Sorry to anyone who face-palmed when they saw my mistake... my condolences to your forehead...