[Tutorial] Teams (with names & armor colors!)

Discussion in 'Resources' started by FabeGabeMC, Jul 17, 2014.

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

    FabeGabeMC

    Hello Bukkit Community!
    Today I would like to share with you a way of creating teams in a really simple way!

    First of all...

    HOW IS IT SIMPLE?

    You may ask yourself.
    Well, by using certain methods implemented within Bukkit, it shouldn't be that hard!

    Okay! Let's begin with the steps.

    Teams Class
    First, we need to make a teams class, so it can store all of our team objects.
    Go ahead and create your class and its constructor this way:
    Code:java
    1. package me.fabegabe.teams.example;
    2.  
    3. /*
    4. * lots of imports...
    5. */
    6.  
    7. public class Team {
    8.  
    9. //Store the team's name.
    10. private String name;
    11. //Store the team's name color.
    12. private ChatColor displayColor;
    13. //Store the team's armor color.
    14. private Color armorColor;
    15. //Store the team's wool display color.
    16. private DyeColor woolColor;
    17. //Store their UUID's instead of names. (Players)
    18. private List<UUID> users = new ArrayList<UUID>();
    19. //Store all the teams created so far.
    20. private static List<Team> teams = new ArrayList<Team>();
    21.  
    22. //Teams Constructor (used to save variables)
    23.  
    24. public Team(String name, ChatColor displayColor,
    25. Color armorColor, DyeColor woolColor){
    26. this.name = name;
    27. this.displayColor = displayColor;
    28. this.armorColor = armorColor;
    29. this.woolColor = woolColor;
    30. }
    31.  
    32. //Retrieving all the info about the team is necessary!
    33.  
    34. //Getting the team's name.
    35. public String getName() {
    36. return name;
    37. }
    38.  
    39. //Getting the team's display color.
    40. public ChatColor getColor() {
    41. return color;
    42. }
    43.  
    44. //Getting the team's wool color.
    45. public DyeColor getWoolColor() {
    46. return woolColor;
    47. }
    48.  
    49. //Getting the team's armor color.
    50. public Color getArmorColor() {
    51. return armorColor;
    52. }
    53.  
    54. //Getting all the players in the team.
    55. public List<UUID> getPlayers() {
    56. return users;
    57. }
    58.  
    59. //Getting all teams.
    60. public static List<Team> getTeams() {
    61. return teams;
    62. }
    63.  
    64. //Let's create some join and leave methods
    65.  
    66. //Is in team check
    67. public static boolean isInTeam(Player p) {
    68. for(Team t : getTeams())
    69. return t.getPlayers().contains(p.getUniqueID());
    70. // if it doesn't contain the player, return false.
    71. return false;
    72. }
    73.  
    74. //Adding a player to a team.
    75. public static void join(Player p, Team t) {
    76. if(t == null || isInTeam(p))
    77. return;
    78. t.getPlayers().add(p.getUniqueID());
    79. giveArmor(p);
    80. }
    81.  
    82. //we don't need a second variable, since it will leave the player's current team.
    83. public static void leave(Player p) {
    84. if(!isInTeam(p))
    85. return;
    86. getTeam(p).getPlayers().remove(p.getUniqueID());
    87. }
    88.  
    89. //Let's now get the team of a certain property
    90.  
    91. public static Team getTeam(String name) {
    92. for(Team t : getTeams()){
    93. if(t.getName().equalsIgnoreCase(name))
    94. //If the team's name is 'name', we'll return that team.
    95. return t;
    96. }
    97. //Otherwise, we return nothing.
    98. return null;
    99. }
    100.  
    101.  
    102. public static Team getTeam(Player p) {
    103. //Looping through all the teams.
    104. for(Team t : getTeams()){
    105. if(t.getPlayers().contains(p.getUniqueID()))
    106. //If the player list contains their UUID, we'll return the team.
    107. return t;
    108. }
    109. //Otherwise, we will return nothing.
    110. return null;
    111. }
    112.  
    113.  
    114. public static void giveArmor(Player p) {
    115. if(!isInTeam(p))
    116. return;
    117. ItemStack helmet = new ItemStack(Material.LEATHER_HELMET, 1);
    118. //We are using leather armor meta since we want to add the team's armor color.
    119. LeatherArmorMeta hm = (LeatherArmorMeta) helmet.getItemMeta();
    120. hm.setColor(t.getArmorColor());
    121. //Setting the display name... and lore...
    122. hm.setDisplayName(t.getColor() + t.getName() + " Cap");
    123. hm.setLore(Arrays.asList(ChatColor.AQUA + "Fancy " + t.getColor()
    124. + t.getName() + ChatColor.AQUA + " cap."));
    125. helmet.setItemMeta(hm);
    126. ItemStack tunic = new ItemStack(Material.LEATHER_CHESTPLATE, 1);
    127. LeatherArmorMeta tm = (LeatherArmorMeta) tunic.getItemMeta();
    128. tm.setColor(t.getArmorColor());
    129. tm.setDisplayName(t.getColor() + t.getName() + " Tunic");
    130. tm.setLore(Arrays.asList(ChatColor.AQUA + "Fancy " + t.getColor()
    131. + t.getName() + ChatColor.AQUA + " tunic."));
    132. tunic.setItemMeta(tm);
    133. ItemStack pants = new ItemStack(Material.LEATHER_LEGGINGS, 1);
    134. LeatherArmorMeta pm = (LeatherArmorMeta) pants.getItemMeta();
    135. pm.setColor(t.getArmorColor());
    136. pm.setDisplayName(t.getColor() + t.getName() + " Pants");
    137. pm.setLore(Arrays.asList(ChatColor.AQUA + "Fancy " + t.getColor()
    138. + t.getName() + ChatColor.AQUA + " pants."));
    139. pants.setItemMeta(pm);
    140. ItemStack boots = new ItemStack(Material.LEATHER_BOOTS, 1);
    141. LeatherArmorMeta bm = (LeatherArmorMeta) boots.getItemMeta();
    142. bm.setColor(t.getArmorColor());
    143. bm.setDisplayName(t.getColor() + t.getName() + " Boots");
    144. bm.setLore(Arrays.asList(ChatColor.AQUA + "Fancy " + t.getColor()
    145. + t.getName() + ChatColor.AQUA + " boots."));
    146. boots.setItemMeta(bm);
    147. //We are adding the armor into the player's armor values/slots.
    148. p.getInventory().setHelmet(helmet);
    149. p.getInventory().setChestplate(tunic);
    150. p.getInventory().setLeggings(pants);
    151. p.getInventory().setBoots(boots);
    152. //Finally, let's update the player's inventory, although it is deprecated.
    153. p.updateInventory();
    154. }
    155.  
    156. }


    Using the variables

    We've gotten so far... so... what do we do with our team colors?
    We will use them in our armor, and our menu options.

    Menu class:

    Code:java
    1. package me.fabegabe.teams.example;
    2.  
    3. /*
    4. * lots of imports...
    5. */
    6.  
    7. public class Menu {
    8.  
    9. @SuppressWarnings("deprecation")
    10. //Set the return type to an inventory.
    11. public Inventory getTeamSelector() {
    12. //Creating our inventory.
    13. Inventory menu = Bukkit
    14. .createInventory(null, 9, ChatColor.BLUE + "Team Selector");
    15. //We're now iterating through all the teams' size so we can add those to our menu.
    16. for (int i = 0; i < Team.getTeams().size(); i++) {
    17. Team team = Team.getTeams().get(i);
    18. //Creating a new item stack for the wool in the menu.
    19. ItemStack wool = new ItemStack(Material.WOOL, 1, team
    20. .getWoolColor().getData());
    21. ItemMeta woolMeta = wool.getItemMeta();
    22. woolMeta.setDisplayName(team.getColor() + team.getName() + " Team");
    23. wool.setItemMeta(woolMeta);
    24. menu.setItem(i, wool);
    25. }
    26. return menu;
    27. }
    28.  
    29. }


    Finally.. how do you register a team?!

    You can call this method whenever you want!

    Code:java
    1. //Here we are telling the code to create a new team object for us and to add it to our existing teams.
    2. public static void registerTeam(String name, ChatColor color,
    3. Color armorColor, DyeColor woolColor) {
    4. Team t = new Team(name, color, armorColor, woolColor);
    5. Team.getTeams().add(t);
    6. }


    Of course, it would be easier if you would register them in your onEnable.. like me, for example ( :p ):


    Code:java
    1. public void onEnable() {
    2. registerTeams();
    3. }
    4.  
    5. public void registerTeams() {
    6. TeamManager.registerTeam("Green", ChatColor.GREEN, Color.LIME,
    7. DyeColor.LIME);
    8. TeamManager.registerTeam("Red", ChatColor.RED, Color.RED, DyeColor.RED);
    9. TeamManager.registerTeam("Blue", ChatColor.BLUE, Color.BLUE,
    10. DyeColor.LIGHT_BLUE);
    11. TeamManager.registerTeam("Yellow", ChatColor.YELLOW, Color.YELLOW,
    12. DyeColor.YELLOW);
    13. TeamManager.registerTeam("Dark Green", ChatColor.DARK_GREEN,
    14. Color.GREEN, DyeColor.GREEN);
    15. TeamManager.registerTeam("Gold", ChatColor.GOLD, Color.ORANGE,
    16. DyeColor.ORANGE);
    17. TeamManager.registerTeam("Purple", ChatColor.LIGHT_PURPLE,
    18. Color.PURPLE, DyeColor.PURPLE);
    19. TeamManager.registerTeam("Navy", ChatColor.DARK_BLUE, Color.NAVY,
    20. DyeColor.BLUE);
    21. TeamManager.registerTeam("Special", ChatColor.AQUA, Color.AQUA,
    22. DyeColor.CYAN);
    23. getLogger().log(Level.INFO, "Loaded teams.");
    24. }


    Screenshots


    Team Selection Menu
    [​IMG]

    Armor Dyes
    [​IMG]

    Congratulations! You have created your own teams plugin! Feel free to use this on any plugin you want (minigame or not).

    Please leave feedback and I hope this helped most of you. :D

    Take care everyone and until next time! (I'm not leaving the forums :p)

    ~FabeGabe :D
     
    PlayFriik and mickedplay like this.
  2. Offline

    Niknea

    Screenshots please?
     
  3. Offline

    xTigerRebornx

    FabeGabeMC I wouldn't call this a tutorial, as you barely explain anything that is done in your code. This is more of a snippet.
     
  4. Offline

    FabeGabeMC

  5. Offline

    Chrusty45

    FabeGabeMC Is there a way to add multiple players from a list?
     
  6. Offline

    LCastr0

    Instead of using a for loop, why don't you store the teams in a Map<Player, Team> so you can get the team by using
    map.get(player)?
     
    Skyost likes this.
  7. Offline

    FabeGabeMC

    LCastr0
    For me it is easier.
    Chrusty45
    Loop through all the players in a team and add the players (strings) to the other team.
    Make sure to remove them first though
     
  8. Offline

    LCastr0

    It's easier, but I think saving it in a map is safer...
     
    TheWolfBadger likes this.
  9. Offline

    Chrusty45

    FabeGabeMC Another question, I can't seem to get addPlayer to work. I can't use a string for the team name and I have no idea how to do this. My current code:
    Code:java
    1. for(String players : teamConfig.getStringList(key + ".Players")) {
    2. Player player = Bukkit.getPlayerExact(players);
    3. Team.addPlayer(player, key);
    4. }
     
  10. Offline

    FabeGabeMC

    Chrusty45
    don't use getPlayerExact. use getPlayer.
     
  11. Offline

    ChipDev

    It soothes me to read this, Its so good.
    :)
    meow good tutorial.
     
  12. Offline

    Chrusty45

    FabeGabeMC No... no.. That's not the problem. the method addPlayer(Player player, Team team) is unusable. If I want to add a player to the team, I can't use addPlayer("PlayerName", "TeamName"); because "TeamName" is not a team, it is a string.
     
  13. Offline

    FabeGabeMC

  14. Offline

    ZodiacTheories

  15. Offline

    Skyost

    Show Spoiler

    [​IMG]
     
Thread Status:
Not open for further replies.

Share This Page