Solved How to change player nametag color?

Discussion in 'Spigot Plugin Development' started by Shkiperrr, Mar 26, 2020.

  1. I would like to change the color of a player's name based on the team they are on in my minigame. Does anyone know how to do this?

    I want to change nametags color not in the chat but in the tab menu and above player heads.

    My code (so far):
    Code (Text):
    package me.Shkiperrr.ChangeTeam;

    import java.util.ArrayList;
    import java.util.List;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;

    public class Main extends JavaPlugin implements Listener {
     
        public Inventory inv;
     
        @Override
        public void onEnable() {
         
            createInv();
            this.getServer().getPluginManager().registerEvents(this, this);
         
        }

        @Override
        public void onDisable() {

        }

        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
         
            if (label.equalsIgnoreCase("changeteam")) {
                if (!(sender instanceof Player)) {
                    sender.sendMessage("You cannot do this!");
                }
                Player player = (Player) sender;
             
            }
         
            return false;
        }
     
        @EventHandler
        public void onClick(InventoryClickEvent event) {
         
            if (!event.getInventory().equals(inv)) {
                return;
            }
            if (event.getCurrentItem() == null) return;
            if (event.getCurrentItem().getItemMeta() == null) return;
            if (event.getCurrentItem().getItemMeta().getDisplayName() == null) return;
         
            event.setCancelled(true);
         
            Player player = (Player) event.getWhoClicked();
         
            if (event.getSlot() == 3) {
                player.addPotionEffect(new PotionEffect(PotionEffectType.NIGHT_VISION, 99999999, 2));
                player.addPotionEffect(new PotionEffect(PotionEffectType.SLOW_DIGGING, 99999999, 0));
             
            }
         
        }
     
        public void createInv() {
         
            inv = Bukkit.createInventory(null, 9, ChatColor.GOLD + "" + ChatColor.BOLD + "Select Team");
         
            ItemStack item = new ItemStack(Material.GREEN_WOOL);
            ItemMeta meta = item.getItemMeta();
         
            meta.setDisplayName(ChatColor.GREEN + "ZOMBIE TEAM");
            List<String> lore = new ArrayList<String>();
            lore.add(ChatColor.GRAY + "Click to join team!");
            meta.setLore(lore);
            item.setItemMeta(meta);
            inv.setItem(3, item);
         
            item.setType(Material.RED_WOOL);
            meta.setDisplayName(ChatColor.RED + "SURVIVORS TEAM");
            item.setItemMeta(meta);
            inv.setItem(5, item);
         
            item.setType(Material.BARRIER);
            meta.setDisplayName(ChatColor.DARK_RED + "" + ChatColor.BOLD + "Close Menu");
            lore.clear();
            meta.setLore(lore);
            item.setItemMeta(meta);
            inv.setItem(8, item);
         
        }
     
    }
     
    #1 Shkiperrr, Mar 26, 2020
    Last edited: Mar 27, 2020
  2. p.setDisplayName()?
     
  3. Nope, that does not work, or i am doing something wrong.
    Here's the code:
    Code (Text):
    @EventHandler
        public void onClick(InventoryClickEvent event) {
         
            if (!event.getInventory().equals(inv)) {
                return;
            }
            if (event.getCurrentItem() == null) return;
            if (event.getCurrentItem().getItemMeta() == null) return;
            if (event.getCurrentItem().getItemMeta().getDisplayName() == null) return;
         
            event.setCancelled(true);
         
            Player player = (Player) event.getWhoClicked();
         
            if (event.getSlot() == 3) {
                player.addPotionEffect(new PotionEffect(PotionEffectType.NIGHT_VISION, 99999999, 2));
                player.addPotionEffect(new PotionEffect(PotionEffectType.SLOW_DIGGING, 99999999, 0));
                player.setDisplayName(ChatColor.DARK_GREEN + player.getDisplayName());
                player.sendMessage(ChatColor.GOLD + "You changed your team to" + ChatColor.DARK_GREEN + " ZOMBIES");
            }
            if (event.getSlot() == 5) {
                player.setDisplayName(ChatColor.RED + player.getDisplayName());
                player.sendMessage(ChatColor.GOLD + "You changed your team to" + ChatColor.RED + " SURVIVORS");
            }
            if (event.getSlot() == 8) {
                player.closeInventory();
            }
            return;
        }
     
    #3 Shkiperrr, Mar 27, 2020
    Last edited: Mar 27, 2020
  4. Your three if () return; could be in one line.
    All the if should be if else if instead.
    When setting their displayName, use player.getName(). For the name in the tab list, there’s a specific method, if I remember correctly it is player.setPlayerListName()
     
  5. Thanks, a lot!
    For those who want to see what it looks like:
    Code (Text):
    player.setDisplayName(ChatColor.DARK_GREEN + player.getName());
                player.setPlayerListName(ChatColor.DARK_GREEN + player.getName());
     
  6. You could've used Scoreboard for the teams selection because it's far more efficient enough to be done with and is already in the API. You can .setColor(); to select a color of it so it would show both color in list name, above player head name, and chat name.