PlayerName becoming Null?

Discussion in 'Plugin Development' started by Raydond123, Jan 23, 2015.

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

    Raydond123

    Hello, I'm making a plugin that lists all the staff members online.
    Currently, here's the code that adds them to the string list:
    Code:
    public static ArrayList<String> staffList = new ArrayList<String>();
        private static HashMap<String, String> names = new HashMap<String, String>();
        private static HashMap<String, String> List = new HashMap<String, String>();
    
    Player player = e.getPlayer();
            String Player = player.getName();
    
    staffList.add(Player);
               
                StringBuilder sb = new StringBuilder();
                for(String name: staffList) {
                   
                    Player playerP = Bukkit.getServer().getPlayer(name);
                   
                    String playerUuid = playerP.getUniqueId().toString();
                    FileConfiguration playerConfig;
                    File file = new File("plugins/Essentials/userdata/" + playerUuid + ".yml");
                    if(file.exists()) {
                    playerConfig = YamlConfiguration.loadConfiguration(file);
                    } else {
                        return;
                    }
                   
                    if(playerConfig.getString("nickname") != null) {
                        String playerName = playerConfig.getString("nickname");
                        names.put(name, playerName);
                    } else {
                        String playerName = player.getDisplayName();
                        names.put(name, playerName);
                    }
                   
                    String prefix = PermissionsEx.getUser(playerP).getPrefix();
                    String suffix = PermissionsEx.getUser(playerP).getSuffix();
                    String playerName = names.get(Player);
                   
                    sb.append(prefix + playerName + suffix + ChatColor.GRAY + ", " + ChatColor.RESET);
    
                        List.remove("staffMembers");
                        List.put("staffMembers", sb.toString());
                       
                    names.remove(name);
                }
    If one player with the permission, it adds them to the stringlist with the prefix and everything correctly. But, if another player with the permission joins, then it makes the first players' displayName null.

    Can someone please tell me why this is?
     
  2. Offline

    Skionz

    @Raydond123 That shouldn't be working at all. You can't use modifiers besides final inside a method. Also, please follow naming conventions. Your code is hard to read.
     
  3. Offline

    Raydond123

    @Skionz So if this shouldn't be working at all, then what would?
     
  4. Offline

    Skionz

    @Raydond123 Declare your arrays and maps outside of any method instead of what you have now and initialize them when 'onEnable' is invoked.
     
  5. Offline

    Raydond123

    @Skionz I'm not sure what you mean by that. ;/
    Also, why wouldn't it work? I just got it too work because if you didn't know, this is all inside a PlayerJoinEvent...
    It's working perfectly... Is that not supposed to happen... :confused:
     
  6. Offline

    1Rogue

    Explain why the fields are static.
     
  7. Offline

    Raydond123

    Wow, I didn't notice that... Removed them from being "static"!
    Just to clarify, static means that only one instance of the field or method can exist, right?
     
  8. Offline

    mythbusterma

    @Raydond123

    Yes. It means that the field will be associated with the class, and not any particular instance.
     
  9. Offline

    xTrollxDudex

    Additionally static collections will cause memory leaks when the server reloads
     
Thread Status:
Not open for further replies.

Share This Page