Can't add a player to a team

Discussion in 'Plugin Development' started by Creeoer, Aug 25, 2014.

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

    Creeoer

    Bascially, I am adding the player to two teams, runner and hunter, the message displays saying you are a runner, but it doesn't actually add you to the team, here is my runner method and class, it probally has something to do with my uuid list:

    Where the method is excuted:
    Code:
     else if (rand.nextInt() % 3 == 0){
                    Runners.addRunner(p);
                  p.sendMessage(ChatColor.GREEN + "You are now a runner!");
    The method in the class:
    Code:
    private static List<UUID> runners = new ArrayList<UUID>();
       
       
        public static void addRunner(Player player){
            if(runners.contains(player)){
                return;
            }
            runners.add(player.getUniqueId());
            player.setDisplayName(ChatColor.GREEN + player.getName());
       
           
            }
        public static List<UUID> getRunners(){
            return runners;
     
  2. Offline

    Tenzor

    Not sure if this would fix your problem, but I think you might want:
    Code:
            if(runners.contains(player.getUniqueId())){
                return;
            }
     
  3. Offline

    mine-care

    Where is the where the method is executed: code? Is player null or something? Can I see full code? And yes it might be what Tenzor suggested
     
  4. Offline

    Creeoer

    mine-care well I think I solved my problem, and the first bit of code is where the method was excuted, Runners.addRunner(p); I dont think you need my full code?
     
  5. Offline

    Luke_Lax

    Any reason you've made it all static?
     
  6. Offline

    Creeoer

    Luke_Lax because if my methods weren't static, It wouldn't let me access them
     
  7. Offline

    Garris0n

    Create an instance of the class.
     
    mine-care likes this.
  8. Offline

    mine-care

    Creeoer
    Read oracle documentation about classes. Static is not for easy access it's for a specific object of a class being the same for all instances
     
  9. Offline

    BetaNyan

    In some cases static is needed and in some cases it's not. It's not meant so it's just so you can easily access methods.
     
  10. Offline

    Goblom

    Why are we talking about static stuff here, he has a problem with adding things to a list, not with static stuff.

    Anyways.

    Code:
    public static void addRunner(Player player){
            if(runners.contains(player)){
                return;
            }
            runners.add(player.getUniqueId());
            player.setDisplayName(ChatColor.GREEN + player.getName());
    }
    Ever consider that runners is a uuid list, and that you are checking if a player object exists in it? That will not work how you like it.

    Other than that everything looks fine. To get the players from the getRunners method do something like this...

    Code:java
    1. public static List<Player> getPlayers() {
    2. List<Player> list = new ArrayList();
    3. for (UUID id : Runner.getRunners()) {
    4. Player p = Bukkit.getPlayer(id);
    5. if (p != null) {
    6. list.add(p);
    7. }
    8. }
    9. return list;
    10. }

    [/quote]
     
  11. Offline

    Creeoer

    Goblom Thanks, and how exactly would I use this method to give me a player object for both runners and hunters?
    Like
    Code:
    for (UUID id : Hunters.getInstance().getHunters()) {
                          Player p = Bukkit.getPlayer(id);
    How would I add my runner class to this? Which btw has all the same methods like getRunners()
     
  12. Offline

    Goblom

    Creeoer Just do the same thing i have above but before you return the list add another for loop for the hunter ids.
     
  13. Offline

    Creeoer

    Goblom nevermind, I realize your method returns a list of players.
     
Thread Status:
Not open for further replies.

Share This Page