Changing player nametag colors

Discussion in 'Plugin Development' started by TCO_007, Apr 11, 2014.

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

    TCO_007

    Hello! I am creating a mini game and I want to make it so that the colors nametag (the one above there head and in chat) is Red if they are on team red, and Blue if they are on team blue. Any help?
    This is what I have so far for the team red code but I have no idea where to start for the changing of colors. Thanks!
    Team Red Code:
    Code:java
    1. package me.TCOB055.AttackersVSDefenders;
    2.  
    3. import me.TCOB055.AttackersVSDefenders.Main;
    4.  
    5. import org.bukkit.ChatColor;
    6. import org.bukkit.command.Command;
    7. import org.bukkit.command.CommandExecutor;
    8. import org.bukkit.command.CommandSender;
    9. import org.bukkit.entity.Player;
    10.  
    11. public class Teams implements CommandExecutor {
    12.  
    13. private Main plugin;
    14. public Teams(Main instance){
    15. plugin = instance;
    16. }
    17.  
    18. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String [] args){
    19. if (sender instanceof Player){
    20. Player p = (Player) sender;
    21. if(commandLabel.equalsIgnoreCase("Team red")){
    22. if (p.hasPermission("TeamSelect.use")){
    23. if (!plugin.TeamRed.contains(p.getName())){
    24. if (!plugin.TeamBlue.contains(p.getName())){
    25. plugin.TeamRed.add(p.getName());
    26. plugin.TeamBlue.add(p.getName());
    27. p.sendMessage("You have been added to team " + ChatColor.RED + "Red!");
    28.  
    29. }else{
    30. p.sendMessage(ChatColor.GOLD + "You are already on blue team!");}
    31. }
    32. }
    33. }
    34.  
    35. }
    36.  
    37. return false;
    38. }
    39. }
     
  2. Offline

    amhokies

    TCO_007
    You can use TagAPI for the above the head names.
     
  3. Offline

    GeorgeeeHD

    Also line 21 is wrong, you cant check for that as a command. Check if the label equals Team, then check for the arguments.
     
  4. Offline

    JoeyDevs

    That works fine so far i know! Im using it to for my server the setup my RPG plugin!
     
  5. Offline

    TCO_007

    GeorgeeeHD Is there somewhere I can learn to do that because I have always used the method I have on 21 before but usually it was 1 word long. Maybe there is a difference then? Im sorry, Im somewhat new to coding XD

    How do I get this to work? It crosses it out which I assume means that it doesnt work anymore. What method would I use now to fix it JoeyDevs
    Code:java
    1. @EventHandler
    2. public void onPlayerRedChooseColor(PlayerReceiveNameTagEvent e){
    3. Player p = e.getNamedPlayer();
    4. if (plugin.TeamRed.contains(p)){
    5. e.setTag(ChatColor.RED + p.getName());
    6. }

    The (PlayerReceiveNameTagEvent e) becomes crossed out and underlined in yellow.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  6. Offline

    amhokies

    Take a look at the thread. The PlayerReceiveNameTagEvent is deprecated in order to be replaced by a different Event. http://forums.bukkit.org/threads/tagapi-change-color-the-name-over-peoples-heads.99485/
     
  7. Offline

    GeorgeeeHD

    @TCO_007

    this is to check for multiple arguments:

    Code:java
    1. if(commandLabel.equalsIgnoreCase("Team")) {
    2. if(args.length == 1) {
    3. if(args[0].equalsIgnoreCase("Red")) {
    4. //Do code here
    5. }
    6. }
    7. }


    args.length returns an int of how many arguments the player types so /team red would be 1 argument /team join red would be 2 arguments.

    Then to get a specific argument, u can do args[argument length -1] so in /team join, join is equal to args[0]

    Hope this helps :p
     
  8. Offline

    TCO_007

    Thanks GeorgeeeHD
    so does this look right?
    Code:java
    1. public class Teams implements CommandExecutor{
    2.  
    3. private Main plugin;
    4. public Teams(Main instance){
    5. plugin = instance;
    6. }
    7.  
    8. public boolean onRedTeamCommand(CommandSender sender, Command cmd, String commandLabel, String [] args){
    9. if (sender instanceof Player){
    10. Player p = (Player) sender;
    11. if(commandLabel.equalsIgnoreCase("Team")) {
    12. if(args.length == 1) {
    13. if(args[0].equalsIgnoreCase("Red")) {
    14. if (p.hasPermission("TeamSelect.use")){
    15. if (!plugin.TeamRed.contains(p.getName())){
    16. if (!plugin.TeamSelected.contains(p.getName())){
    17. plugin.TeamRed.add(p.getName());
    18. plugin.TeamSelected.add(p.getName());
    19. p.sendMessage("You have been added to team " + ChatColor.RED + "Red!");
    20. TagAPI.refreshPlayer(p);
    21.  
    22. }else{
    23. p.sendMessage(ChatColor.GOLD + "You are already on blue team!");}
    24. }
    25. }else{
    26. p.sendMessage(ChatColor.GRAY + "You cannot choose your own team! Donate online for that permission!");
    27. }
    28. }
    29.  
    30. }
    31.  
    32. }
    33. }
    34. return false;
    35. }
    36.  
    37. public boolean onBlueTeamCommand(CommandSender sender, Command cmd, String commandLabel, String [] args){
    38. if (sender instanceof Player){
    39. Player p = (Player) sender;
    40. if(commandLabel.equalsIgnoreCase("Team")) {
    41. if(args.length == 1) {
    42. if(args[0].equalsIgnoreCase("Blue")) {
    43. if (p.hasPermission("TeamSelect.use")){
    44. if (!plugin.TeamRed.contains(p.getName())){
    45. if (!plugin.TeamBlue.contains(p.getName())){
    46. plugin.TeamSelected.add(p.getName());
    47. plugin.TeamBlue.add(p.getName());
    48. p.sendMessage("You have been added to team " + ChatColor.BLUE + "Blue!");
    49. TagAPI.refreshPlayer(p);
    50.  
    51. }else{
    52. p.sendMessage(ChatColor.GOLD + "You are already on red team!");}
    53. }
    54. }else{
    55. p.sendMessage(ChatColor.GRAY + "You cannot choose your own team! Donate online for that permission!");
    56. }
    57. }
    58.  
    59. }
    60.  
    61. }
    62. }
    63. return false;
    64. }
    65. }
    66.  
     
  9. Offline

    coasterman10

    Using Scoreboards would be the best design choice here, both eliminating the overhead of a separate plugin API and allowing other scoreboard features.
     
  10. Offline

    TCO_007

    Where could I learn how to do that @coasterman101

    Btw, how would I register this command in Main class?
    Code:java
    1. public boolean onRedTeamCommand(CommandSender sender, Command cmd, String commandLabel, String [] args){
    2. if (sender instanceof Player){
    3. Player p = (Player) sender;
    4. if(commandLabel.equalsIgnoreCase("Team")) {
    5. if(args.length == 1) {
    6. if(args[0].equalsIgnoreCase("Red")) {
    7. if (p.hasPermission("TeamSelect.use")){
    8. if (!plugin.TeamRed.contains(p.getName())){
    9. if (!plugin.TeamSelected.contains(p.getName())){
    10. plugin.TeamRed.add(p.getName());
    11. plugin.TeamSelected.add(p.getName());
    12. p.sendMessage("You have been added to team " + ChatColor.RED + "Red!");
    13. TagAPI.refreshPlayer(p);
    14.  


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
Thread Status:
Not open for further replies.

Share This Page