Solved Help with my Method of Teams With enums

Discussion in 'Spigot Plugin Development' started by 225wre, Apr 17, 2019 at 12:54 AM.

  1. I have this method to add players through an enumeration system, but when a player chooses a team he can choose the other one, I do not know how to solve it

    Code (Text):
        private List<UUID> blues = new ArrayList<>();
        private List<UUID> reds = new ArrayList<>();
        private static GameTeam team;
     
        public enum GameTeam{
            BLUE,RED
        }
     
        public static GameTeam getGameTeam(){
            return team;
        }
     
        public  GameTeam addToGameTeam(GameTeam teams){
           return teams = team;
        }
     
        public boolean IsInTeam(Player p){
            if(getGameTeam() != null || blues.contains(p.getUniqueId()) || reds.contains(p.getUniqueId())){
                return false;
            }
            return true;
        }
     
        public void RemoveForTeam(Player p){
            if(reds.contains(p.getUniqueId())){
                reds.remove(p.getUniqueId());
            }
            if(blues.contains(p.getUniqueId())){
                blues.remove(p.getUniqueId());
            }
        }
     
        public void AddToTeam(GameTeam team,Player p){
            FileConfiguration mess = ConfigManager.getInstance().getMessages();
            String tblue = mess.getString("Teams.Blue.Title");
            String sblue = mess.getString("Teams.Blue.Subtitle");
            int stblue = mess.getInt("Teams.Blue.Stayin");
            String tred = mess.getString("Teams.Red.Title");
            String sred = mess.getString("Teams.Red.Subtitle");
            int stred = mess.getInt("Teams.Red.Stayin");
            if(IsInTeam(p) == true){
                switch(team){
                case BLUE:
                    blues.add(p.getUniqueId());
                    Util.SendInstantTitle(p, tblue, sblue, stblue);
                    Util.PlaySound(p, Sound.valueOf(mess.getString("Teams.Red.Sound").toUpperCase()));
                    p.sendMessage(Util.Chat(mess.getString("Teams.Blue.Message")));
                  break;
                case RED:
                    reds.add(p.getUniqueId());
                    Util.SendInstantTitle(p, tred, sred, stred);
                    Util.PlaySound(p, Sound.valueOf(mess.getString("Teams.Red.Sound").toUpperCase()));
                    p.sendMessage(Util.Chat(mess.getString("Teams.Red.Message")));
                  break;
                }
            }else{
                p.sendMessage(Util.Chat(mess.getString("YouHave-Already-in-Team")));
            }
        }
     
     
        public void ClearTeams(){
            blues.clear();
            reds.clear();
        }
     
        public List<UUID> getRedMembers(){
            return reds;
        }
     
        public List<UUID> getBluesMembers(){
            return blues;
        }
     
        public boolean CompareToTeam(Player p, GameTeam team){
            if(team.equals(team)){
                return true;
            }
            return false;
        }
     
    #1 225wre, Apr 17, 2019 at 12:54 AM
    Last edited: Apr 17, 2019 at 5:53 PM
  2. You can do a HashMap. Like this: HashMap<UUID, GameTeam> teams;
     
  3. Or in your addToTeam(), you check if the player is in the list of the other one, for it, you can use: if(blues.contains(player.getUniqueID) reds.add(player.getUniqueID);

    And Remember, always name the method with the first letter in lower case.
     
  4. i Try with this, but don't works
     
  5. my code don't works
    Code (Text):
    package gamemanager;

    import java.util.HashMap;
    import java.util.UUID;

    import org.bukkit.Sound;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.entity.Player;

    import ultimate.main.ConfigManager;
    import util.Util;

    public class TeamManager {
        private HashMap<UUID,Teams> reds = new HashMap<>();
        private HashMap<UUID, Teams> blues = new HashMap<>();
        private static Teams team;
     
        public enum Teams{
            BLUE,RED
        }
     
     
        public  Teams addToGameTeam(Teams teams){
           return teams = team;
        }
     
        public boolean isInTeam(Player p){
            if(blues.containsKey(p.getUniqueId()) || reds.containsKey(p.getUniqueId())){
                return false;
            }
            return true;
        }
     
        public void removeForTeam(Player p){
            if(reds.containsKey(p.getUniqueId())){
                reds.remove(p.getUniqueId());
            }
            if(blues.containsKey(p.getUniqueId())){
                blues.remove(p.getUniqueId());
            }
        }
     
        public void addToTeam(Teams team,Player p){
            FileConfiguration mess = ConfigManager.getInstance().getMessages();
            String tblue = mess.getString("Teams.Blue.Title");
            String sblue = mess.getString("Teams.Blue.Subtitle");
            int stblue = mess.getInt("Teams.Blue.Stayin");
            String tred = mess.getString("Teams.Red.Title");
            String sred = mess.getString("Teams.Red.Subtitle");
            int stred = mess.getInt("Teams.Red.Stayin");
            if(isInTeam(p)){
                switch(team){
                case BLUE:
                    blues.put(p.getUniqueId(), Teams.BLUE);
                    Util.SendInstantTitle(p, tblue, sblue, stblue);
                    Util.PlaySound(p, Sound.valueOf(mess.getString("Teams.Red.Sound").toUpperCase()));
                    p.sendMessage(Util.Chat(mess.getString("Teams.Blue.Message")));
                  break;
                case RED:
                    reds.put(p.getUniqueId(), Teams.RED);
                    Util.SendInstantTitle(p, tred, sred, stred);
                    Util.PlaySound(p, Sound.valueOf(mess.getString("Teams.Red.Sound").toUpperCase()));
                    p.sendMessage(Util.Chat(mess.getString("Teams.Red.Message")));
                  break;
                }
            }else{
                p.sendMessage(Util.Chat(mess.getString("YouHave-Already-in-Team")));
            }
        }
     
     
        public void clearTeams(){
            blues.clear();
            reds.clear();
        }
     
        public HashMap<UUID, Teams> getRedMembers(){
            return reds;
        }
     
        public HashMap<UUID, Teams> getBluesMembers(){
            return blues;
        }
     
        public boolean compareToBlueTeam(Player p){
            if(blues.containsKey(p.getUniqueId()) && blues.containsKey(Teams.BLUE)){
                return true;
            }
            return false;
        }
     
        public boolean compareToRedTeam(Player p){
            if(reds.containsKey(p.getUniqueId()) && reds.containsKey(Teams.RED)){
                return true;
            }
            return false;
        }
       
     
  6. If I'm being honest, your code is a mess. First, you have a static variable, team, that can contain a value from your Teams enum. But it is never given a value, so it's always null. And this method:
    Code (Text):
       public Teams addToGameTeam(Teams teams) {
           return teams = team;
        }
    Will assign this null value to the teams parameters you pass it, and then return the assigned result, which is null. Why??

    Also, you have two HashMaps, blues and reds, that contain the UUIDs of the players on that team, and a value from your BLUE or RED enum. Why do you need this enum value? The lists are separate and named for the color of the team. You won't ever have red players on the blue list, so this value is useless and should be eliminated. This should be:
    Code (Text):
    List<UUID> blues = new ArrayList<>();
    List<UUID> reds = new ArrayList<>();
    Your method isInTeam(player) is incorrectly named. With that name it should return true if the player is in a team, instead it return false. Either switch the return value, or rename it to hasNoTeam(player)
    Finally, these methods make no sense
    Code (Text):
        public boolean compareToBlueTeam(Player p){
            if(blues.containsKey(p.getUniqueId()) && blues.containsKey(Teams.BLUE)){
                return true;
            }
            return false;
        }
     
        public boolean compareToRedTeam(Player p){
            if(reds.containsKey(p.getUniqueId()) && reds.containsKey(Teams.RED)){
                return true;
            }
            return false;
        }
    You are making the check blues.containsKey(Teams.BLUE). This check will always return false, because the KEY of your hashmap is the Player's UUID, NOT the Teams enum. That's stored as the value, not the key. But blues.containsValue(Teams.BLUE) is equally pointless, you are only checking to see that the entire hashmap value set contains this value once. That won't show you if that player's team is set to the value you're checking, which seems to be what you want to do. Maybe you're looking for this:
    Code (Text):
    if(blues.containsKey(p.getUniqueId()) && blues.get(p.getUniqueId) == Teams.BLUE)
    But again, I'm not sure why you're checking this. Do you plan on adding red players to the blues list? As I said before, you don't even need this to be a HashMap, make it a list of UUIDs.

    I'm not trying to be mean, I just don't think you understand HashMaps and enums enough to make them do what you want. Perhaps if you could explain exactly what you're trying to accomplish.
     

  7. I fix it

    Code (Text):
        private static HashMap<UUID,Teams> reds;
        private static HashMap<UUID, Teams> blues;
        private static Teams team;
       
        public enum Teams{
            BLUE,RED
        }
       
       
        public  Teams addToGameTeam(Teams teams){
           return teams = team;
        }
       
        public boolean isInTeam(Player p){
            if(blues.containsKey(p.getUniqueId()) || reds.containsKey(p.getUniqueId())){
                return false;
            }
            return true;
        }
       
        public void removeForTeam(Player p){
            if(reds.containsKey(p.getUniqueId())){
                reds.remove(p.getUniqueId());
            }
            if(blues.containsKey(p.getUniqueId())){
                blues.remove(p.getUniqueId());
            }
        }
       
        public void addToTeam(Teams team,Player p){
            FileConfiguration mess = ConfigManager.getInstance().getMessages();
            String tblue = mess.getString("Teams.Blue.Title");
            String sblue = mess.getString("Teams.Blue.Subtitle");
            int stblue = mess.getInt("Teams.Blue.Stayin");
            String tred = mess.getString("Teams.Red.Title");
            String sred = mess.getString("Teams.Red.Subtitle");
            int stred = mess.getInt("Teams.Red.Stayin");
            if(isInTeam(p)){
                switch(team){
                case BLUE:
                    blues.put(p.getUniqueId(), Teams.BLUE);
                    Util.SendInstantTitle(p, tblue, sblue, stblue);
                    Util.PlaySound(p, Sound.valueOf(mess.getString("Teams.Red.Sound").toUpperCase()));
                    p.sendMessage(Util.Chat(mess.getString("Teams.Blue.Message")));
                  break;
                case RED:
                    reds.put(p.getUniqueId(), Teams.RED);
                    Util.SendInstantTitle(p, tred, sred, stred);
                    Util.PlaySound(p, Sound.valueOf(mess.getString("Teams.Red.Sound").toUpperCase()));
                    p.sendMessage(Util.Chat(mess.getString("Teams.Red.Message")));
                  break;
                }
            }else{
                p.sendMessage(Util.Chat(mess.getString("YouHave-Already-in-Team")));
            }
        }
       
       
        public void clearTeams(){
            blues.clear();
            reds.clear();
        }
       
        public HashMap<UUID, Teams> getRedMembers(){
            return reds;
        }
       
        public HashMap<UUID, Teams> getBluesMembers(){
            return blues;
        }
       
        public boolean compareToBlueTeam(Player p){
            if(blues.containsKey(p.getUniqueId()) && blues.containsKey(Teams.BLUE)){
                return true;
            }
            return false;
        }
       
        public boolean compareToRedTeam(Player p){
            if(reds.containsKey(p.getUniqueId()) && reds.containsKey(Teams.RED)){
                return true;
            }
            return false;
        }
       
       
        public static void setUpBlue(){
            blues = new HashMap<>();
        }
       
        public static void setUpRed(){
            reds = new HashMap<>();
        }
     
     

Share This Page