Hashmap issue

Discussion in 'Plugin Development' started by KaiPol, Dec 31, 2013.

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

    KaiPol

    Okay so I've been working with HashMaps for a while and I think I understand them. What I'm trying to do is to check for all of the players to see if any of them are in the "ingame" hashmap, and if they are then the player joins the game but if they aren't then the player joins the lobby. Here's some of the code but it doesn't work:
    Code:java
    1. if(player.getWorld().getName().equalsIgnoreCase("flatlands")){
    2. for(Player all : Bukkit.getServer().getOnlinePlayers()){
    3. if(ingame.containsKey(all.getName())){
    4. }
    5. Bukkit.broadcastMessage(ChatColor.GREEN + "" + ChatColor.BOLD + player.getName() + " has joined the game!");
    6. }
    7. }else{
    8. Bukkit.broadcastMessage(ChatColor.GREEN + "" + ChatColor.BOLD + player.getName() + " has joined the lobby!");
    9. }
    10. }


    Here's the code for the HashMap.
    Code:java
    1. HashMap<String, Block> ingame = new HashMap<String,Block>();


    And right now I have a code that puts the player into the hashmap for testing
    Code:
                    Player player = (Player) sender;
                    if(args.length == 0){
                    ingame.put(player.getName(), null);
                    }
                    if(args.length == 1){
                        Player targetPlayer = player.getServer().getPlayer(args[0]);
                        ingame.put(targetPlayer.getName(), null);
                    }
                }
    Can anybody help?
     
  2. Offline

    WhippyCleric

    Sorry I am slightly confused by the context. What are you trying to achieve exactly? What is the ingame hashmap supposed to contain? As I assume it's not just players in the world as you already have that information
     
  3. Offline

    AoH_Ruthless

    KaiPol
    Why would it work??? You are opening a bracket then immediately closing it:
    Code:java
    1. if(ingame.containsKey(all.getName())){}

    Also, you can't have an else statement against a for loop.
     
  4. Offline

    KaiPol

    AoH_Ruthless ah, didn't notice that.
    WhippyCleric
    Basically I am trying to check all the players to see if any of them are in the hashmap.

    I'll try to mess around with it a bit and see if it works

    Ugh, I can't figure it out. I've tried HashMaps and ArrayLists now ;-;

    Ah nevermind I fixed it! If anyone else runs into this problem just create a boolean and set it to true or false when you want to :)

    I was trying to over complicate things.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 6, 2016
Thread Status:
Not open for further replies.

Share This Page