Solved getDisplayName() not working with join events

Discussion in 'Spigot Plugin Development' started by Diamond_King099, May 23, 2015.

  1. I'm making a simple plugin to change the join/quit messages. Here is my code so far:

    Code (Text):
        @EventHandler
        void onPlayerJoin(PlayerJoinEvent event) {
            String joinMsg = plugin.getConfig().getString("join-msg");
            Player player = event.getPlayer();
           
            if(joinMsg.contains("%player")) {
                joinMsg = joinMsg.replace("%player", player.getDisplayName());
            }
           
            joinMsg = ChatColor.translateAlternateColorCodes('&', joinMsg);
           
            event.setJoinMessage(joinMsg);
        }
       
        //Quit
        @EventHandler
        void onPlayerQuit(PlayerQuitEvent event) {
            String leaveMsg = plugin.getConfig().getString("leave-msg");
            Player player = event.getPlayer();
           
            if(leaveMsg.contains("%player")) {
                leaveMsg = leaveMsg.replace("%player", player.getDisplayName());
            }
           
            leaveMsg = ChatColor.translateAlternateColorCodes('&', leaveMsg);
           
            event.setQuitMessage(leaveMsg);
        }

    My problem with this is that when the player joins, it shows their actual username, not their display name. However, the display name shows up just fine when a player leaves.

    I know that there are several other threads on this, but none of their "solutions" have worked for me.

    I've tried setting the join message to null and using Bukkit.broadcast(), as well as making the player send a blank chat message. I'd really rather not have to use a scheduler, and I doubt that would work anyway...

    Any ideas?
     
  2. Try using .getCustomName(); for the join, if that doesnt work, I honestly dont know
     
  3. Change the priorities.
     
  4. That causes an error. I believe that .getCustomName(); is for getting a mobs nametag or something like that.
     
  5. Nope, doesn't work. The rest of the message shows up fine, it just shows the actual username instead of the displayname from PEX or Essentials.
     
  6. Essentials has their own way, you're gonna have to hook into essentials to get that. Unless someone else has some way of getting it from essentials.
     
  7. If I do it that way, will it also use the group color from PEX? Or will it only get their nickname? (If they have one)
     
  8. probably just their nickname.
     
  9. Hmm, well I guess it just can't be done then.
     
  10. It can, just delay it by 1 tick.
     
  11. Tried this, didn't work. It now shows the Vanilla join message. (Might just be my code).
    Code (Text):
        @EventHandler
        void onPlayerJoin(final PlayerJoinEvent event) {
            Bukkit.getScheduler().scheduleSyncDelayedTask(KingdomTweaks.plugin, new Runnable() {
                public void run() {
                    String joinMsg = KingdomTweaks.plugin.getConfig().getString("join-msg");
                    Player player = event.getPlayer();
                   
                    if(joinMsg.contains("%player")) {
                        joinMsg = joinMsg.replace("%player", player.getDisplayName());
                    }
                   
                    joinMsg = ChatColor.translateAlternateColorCodes('&', joinMsg);
                   
                    event.setJoinMessage(joinMsg);
                }
            }, 20L * 1);

        }
     
  12. Try this:


    @EventHandler
    void onPlayerJoin(final PlayerJoinEvent event) {
    event.setJoinMessage("");
    Bukkit.getScheduler().scheduleSyncDelayedTask(KingdomTweaks.plugin, new Runnable() {
    public void run() {
    String joinMsg = KingdomTweaks.plugin.getConfig().getString("join-msg");
    Player player = event.getPlayer();

    if(joinMsg.contains("%player")) {
    joinMsg = joinMsg.replace("%player", player.getDisplayName());
    }

    joinMsg = ChatColor.translateAlternateColorCodes('&', joinMsg);

    Bukkit.broadcastMessage(joinMsg);
    }
    }, 20L * 1);

    }
     
  13. Hey! That works!
    Too bad that it shows up after the Essentials MOTD though.
     
  14. Then:

    instead of "Bukkit.broadcastMessage(joinMsg);" write:

    for(Player p : Bukkit.getOnlinePlayers())if(!p.equals(event.getPlayer()))p.sendMessage(joinMsg);

    Then it gets hidden from the player itself! :)
     
  15. When in doubt, tick out.

    EDIT: Saw someone already helped you solve it.
     
  16. Thanks so much!
     
  17. That's what I meant to do :)