NameTag change for a player

Discussion in 'Spigot Plugin Development' started by Dori_mon, Jun 27, 2016.

  1. Code (Text):
    package me.Dori_mon.NameTags;

    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.plugin.java.JavaPlugin;

    import net.md_5.bungee.api.ChatColor;

    public class Main extends JavaPlugin implements Listener{
       
        public void onEnable() {
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
            System.out.println(">> NameTags has been Enabled! <<");
            System.out.println(">> Plugin by Dori_mon <<");
            getConfig().options().copyDefaults(true);
            saveConfig();
        }
       
        public void onDisable() {
            System.out.println("<< NameTags has been Disabled! >>");
            System.out.println("<< Plugin by Dori_mon >>");
        }
       
        @EventHandler
        public void Admin(PlayerJoinEvent event) {
            Player Admin = event.getPlayer();
            if (Admin.getPlayer().hasPermission("rank.Admin")) {
                String a = getConfig().getString("admin-Name");
                a = ChatColor.translateAlternateColorCodes('&', a);
                Admin.setDisplayName(a + Admin.getName());
            }
        }
       
        @EventHandler
        public void Helper() {
           
        }
       
        @EventHandler
        public void iLusion() {
           
        }
       
        @EventHandler
        public void Mod() {
           
        }
       
        @EventHandler
        public void Developer() {
           
        }
       
        @EventHandler
        public void Member() {
           
        }
       
        @EventHandler
        public void Spartan() {
           
        }
       
        @EventHandler
        public void King() {
           
        }
       
        @EventHandler
        public void ZEUS() {
           
        }
       
        @EventHandler
        public void TITAN() {
           
        }
       
       

    }
     

    How can I make a NameTag change for a player?
    Im trying to make a plugin that will make all the player's name shown places to &c[A] + p.getName()
     
  2. Please don't tell me you have a separate event for every rank... *facepalm*

    The beauty of Java is that you can have one method to create the nametag for every rank.
     
  3. Can you explain?
     
  4. No explanation is necessary, you're literally already doing what you need to do..
    Code (Text):
    if (Admin.getPlayer().hasPermission("rank.Admin")) {
    you just have it split up for absolutely no reason.

    By the way, naming conventions have been murdered here today.
     

  5. Lol, I read the code again and saw how dumb I am...
    But how can I change the nameTag of a player?
     
  6. Code (Text):

    @EventHandler
        public void modifyNametag(PlayerJoinEvent event) {
            Player p = event.getPlayer();
            String playerRank = "none";
            if(p.hasPermission("rank.moderator"))
            {
                playerRank = "moderator";
            }
            else if(p.hasPermission("rank.admin"))
            {
                playerRank = "admin";
             }
             else
             {
                 playerRank = "default";
              }
              String prefix = (getConfig().getString("prefix." + playerRank));
              prefix = ChatColor.translateAlternateColorCodes('&', prefix);
              p.setDisplayName(prefix + p.getName());
            }
        }
     
    Config:
    Code (Text):

    prefix:
      default: "&1Default"
      moderator: "&2Mod"
      admin: "&3Admin"
     
    I wrote it all out in the forum's text editor, so if something is wrong derp. Notice how we can place all the ranks into one method? Rather than creating a new join event for each rank.

    EDIT: As for your changing nametags issue, I would simply use TAGAPI
     
    • Like Like x 1

  7. Sorry, I don't really know how to use API's yet. Is there any other way without using API's?
     
  8. You want to change player's display name or name tag?
     
    • Agree Agree x 1
  9. Both...
     
  10. Changing display name is rather simple. You just have to use player.setDisplayName(name). For NameTag you have to create Scoreboard Team with colored prefix and then add player to this team. This automaticylly changes player NameTag and TabList color.
     
    • Agree Agree x 1
    • Winner Winner x 1

  11. Can you write the method for this?
     
  12. Why u want to change both? Cuz display name bypasses the name tag ..
     
  13. Why don't you try it yourself. Google is amazing, you don't even need to go to college nowadays, you can learn anything just by Googling it. These forums are just a faster way of finding a personalized solution.
     
  14. I need to make a plugin that will change both
     
  15. I googled it and got no answer. Can you guys please help me?