Teams

Discussion in 'Plugin Development' started by ProMCKingz, Dec 20, 2014.

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

    ProMCKingz

    I have made teams, however when I view them, they are not in "name" form, they are given like @1ec421.
    I would like them to be in name form, plus they are not static (they keep changing).
    I recon I have done the code wrong, however I am creating the teams on my main class (onEnable):
    Code:
        public void onEnable() {
            new Teams("Red").getName();
            new Teams("Blue");
            new Teams("Yellow");
            new Teams("Green");
    And my "Teams" class:
    Code:
    package me.promckingz.pigquest;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    
    import org.bukkit.entity.Player;
    
    public class Teams {
    
        private static List<Teams> AllTeams = new ArrayList<Teams>();
       
        private static HashMap<String, Teams> playerTeams = new HashMap<String, Teams>();
       
        private String teamName;
       
        public Teams(String teamName){
            this.teamName = teamName.trim();
            AllTeams.add(this);
    }
       
        public String getName(){
            return teamName;
        }
       
        public void add(Player player){
            playerTeams.put(player.getName(), this);
        }
       
        public boolean remove(Player player){
            if(hasTeam(player))
                return false;
                playerTeams.remove(player.getName());
                return true;
        }
       
        public static boolean hasTeam(Player player){
            return playerTeams.containsKey(player.getName());
        }
        public static Teams getTeam(Player player){
            if(!hasTeam(player))
                return null;
            return playerTeams.get(player.getName());
        }
       
        public static Teams getTeam(String name){
            for(Teams t : AllTeams)
                if(t.teamName.equalsIgnoreCase(name))
                    return t;
                return null;
        }
       
        public static List<Teams>getAllTeams(){
            return AllTeams;
        }
    }


    @AdamQpzm


    @AdamQpzm
    Updated code

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 29, 2016
  2. @ProMCKingz What does it return when you print the value of getName()? Also, you don't want to have static fields in this - I'd make a TeamManager class to manage teams.
     
  3. Offline

    PreFiXAUT

    @ProMCKingz What do you mean not in name form?? How to you get the Name or what do you want to achieve?

    Maybe you mean the Object#toString() Method?
     
  4. Offline

    ProMCKingz

    @PreFiXAUT
    I have a command that does:
    p.sendMessage("" + Teams.getAllTeams());
    And once executed the teams appear like this: "1ec421". Instead of "Red"
    @AdamQpzm
     
  5. @ProMCKingz That's because getAllTeams() returns a List, and not a String of all their names. Java doesn't know how to convert it to the format you want, so you have to create a method for it yourself. For example, loop over all the teams in getAllTeams() and call the getName() method on them.
     
  6. Offline

    ProMCKingz

    @AdamQpzm
    Loop over?
    What do you mean?
    @PreFiXAUT
    I have also tried .toString(); and .getName(); And it gives the same results
     
  7. Offline

    ProMCKingz

    @AdamQpzm
    Loop what?
    Like this:
    for (Teams player : Teams.getAllTeams()){
     
  8. Offline

    Skionz

    @ProMCKingz Yes, although there are plenty of ways to do it.
     
  9. Offline

    ProMCKingz

    @Skionz
    Now it won't allow player.sendMessage()
     
  10. Offline

    Skionz

    @ProMCKingz Because player is an instance of the Teams class and their is no sendMessage method in it. Did you write this class?
     
  11. Offline

    ProMCKingz

    @Skionz
    There* :D
    And yes, as showed on the title description
    So how can I send them a message?
     
    Last edited: Dec 22, 2014
  12. Offline

    Monkey_Swag

    @Skionz he did not, I'm pretty sure it's one of BcBroz's minigame tutorial videos.
     
  13. Offline

    Skionz

    Write your own class if you have no idea what TheBCBroz's does.
     
Thread Status:
Not open for further replies.

Share This Page