Solved Replacing player's name in chat.

Discussion in 'Plugin Development' started by q8minecraft, Nov 6, 2015.

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

    q8minecraft

    I've been trying to replace the player's name in chat when the player gets called. For example

    Player1: Player2 How are you?

    This is what I got so far..
    Code:
        @EventHandler
        public void onchat(PlayerChatEvent e) {
            String msg = e.getMessage();
            String name = e.getPlayer().getName();
            for(Player player : Bukkit.getOnlinePlayers()){
                if(msg.contains(player.getName())){
                    msg.replace(player.getName(), ChatColor.YELLOW + player.getName() + ChatColor.WHITE);
                    player.playSound(player.getLocation(), Sound.ORB_PICKUP, 1F, 1F);
                }
            }
        }
    Thanks in advance!
     
  2. Offline

    Zombie_Striker

    @q8minecraft
    So, what's your problem? Does your code not work? Did you debug?

    If this is a post that is basically asking "hows it look", then you posted in the wrong section.
     
  3. Offline

    q8minecraft

    @Zombie_Striker I can hear the sound, but the text itself isn't changing color.
     
  4. Offline

    Hawktasard

    @q8minecraft
    You have to actually set it. String#replace will return a new string.
    edit: e.setMessage(e.getMessage().replace("123", "456"));
     
    q8minecraft likes this.
  5. Offline

    q8minecraft

    @Hawktasard oh, I thought I only needed to replace it, silly me :p
    Thanks!
     
  6. Offline

    Scimiguy

    If your problem is resolved, edit the title of your thread and mark it as solved, thanks
     
    q8minecraft likes this.
  7. Offline

    q8minecraft

    @Scimiguy I sincerely apologize, forgot to do that. Thanks!
     
Thread Status:
Not open for further replies.

Share This Page