What have I done wrong? (Hashmaps)

Discussion in 'Plugin Development' started by Darkedge, Sep 11, 2011.

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

    Darkedge

    Here's my code that puts the player in the hashmaps, It's in a method that is triggered when a player executes a certain command:
    Code:
            if(MusicEvents.pluginEnabled.containsKey(player)){
                if(MusicEvents.pluginEnabled.get(player)){
                    MusicEvents.pluginEnabled.put(player, false);
                    player.sendMessage("MusicEvents disabled!");
                } else {
                    MusicEvents.pluginEnabled.put(player, true);
                    player.sendMessage("MusicEvents enabled!");
                }
            } else {
                MusicEvents.pluginEnabled.put(player, false);
                player.sendMessage("MusicEvents enabled!");
            }
    Here's the code that compares the online players with the players on the hashmap (Is there a better way to compare a hashmap with online players?)
    Code:
            plugin.sm.playGlobalCustomMusic(plugin, period[x], false);
            Player[] OnlinePlayers = plugin.getServer().getOnlinePlayers();
            for (int i=0; i<OnlinePlayers.length; i++) {
                if(MusicEvents.pluginEnabled.containsKey(OnlinePlayers[i])) {
                    if(MusicEvents.pluginEnabled.get(OnlinePlayers[i])) {
                        plugin.sm.stopMusic((SpoutPlayer) OnlinePlayers[i]);
    I've probably made some noob mistake -_-
    Ideas guys?
    Thanks in advance.
     
  2. Offline

    Timberjaw

    You forgot to mention what the actual problem is. :)

    Also, it's probably better to use the player name as the key (i.e. a hashmap of <String, Boolean>) than the player object itself.
     
  3. Offline

    Darkedge

    Sorry. I feel like an idiot -_- basically what happens is even if the player is in the hashmap, it does not execute the block below it, I have no idea why, it looks like it should work to me, any ideas?
    Thanks for the reply.
    Darkedge.
     
  4. Offline

    ItsHarry

    2 things:
    1. change your for loop to the enhanced version:
    Code:
    for (Player player : Bukkit.getServer().getOnlinePlayers()) {
        stuff();
    }
    2. Maybe it's the stopMusic that doesn't work, is the command even executed at all?
    Add a System.out.println() to check if it even works
     
Thread Status:
Not open for further replies.

Share This Page