Solved Iterating through a HashMap

Discussion in 'Plugin Development' started by Eats_Rainbows, Jul 15, 2013.

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

    Eats_Rainbows

    I have a HashMap<String, BlockState> ...
    Code:
    Map<String, BlockState> blockstates = new HashMap<>();
    and I need to iterate through it for this loop...
    Code:
    for (BlockState state : blockstates.get(player.getName())) {
        if (state.getBlock().getType() != Material.AIR) {
            state.getBlock().getWorld().playEffect(state.getLocation(), Effect.STEP_SOUND, 30);
            state.getBlock().setType(Material.AIR);
            state.update(true);
        }
    }
    but as I have learned you cannot directly do that. I have Google'd , but I have had no luck finding something that works and explains how it does work.
     
  2. Offline

    andf54

    Maps have a method called entrySet which gives you all its key and value combos (entries). You can foreach loop trough that.
     
  3. Offline

    Eats_Rainbows

    Okay I have this set up...
    Code:
    for (Map.Entry<String, BlockState> stateMap : blockstates.entrySet()) {
                               
    }
    but how would I get a certain player's value?
     
  4. Offline

    Henzz

    Eats_Rainbows
    PHP:
    for (Entry<StringBlockStateentry blockstates.entrySet()) {
        
    String name entry.getKey();
        
    BlockState state entry.getValue();
    }
     
  5. Offline

    andf54

    stateMap.getKey() stateMap.getValue()
     
  6. Offline

    Eats_Rainbows

    Yeah, I saw those methods, but won't that grab all the keys and values in the map? I need to get the values of a certain player so I change the BlockStates.
     
  7. Offline

    andf54

    No, an entry has a single key and a value.

    Map.Entry<String, BlockState> stateMap is not a map, it's an entry.
     
  8. Offline

    Eats_Rainbows

    So I would get a players BlockStates like this???
    Code:
    for (Entry<String, BlockState> entry : blockstates.entrySet()) {
        if (entry.getKey().equals(player.getName())) {
            BlockState state = entry.getValue();
            if (state.getBlock().getType() != Material.AIR) {
                state.getBlock().getWorld().playEffect(state.getLocation(), Effect.STEP_SOUND, 30);
                state.getBlock().setType(Material.AIR);
            State.update(true);
            }
        }
    }
    Sorry if I am failing like a noob, I've never done anything that requires this.
     
  9. Offline

    desht

    Are you trying to associate a player with a list of block states? If so, you need a structure like:
    PHP:
    Map<String, List<BlockState>> blockstates = new HashMap<String, List<BlockState>>();
    then you can do:
    PHP:
    if (blockstates.containsKey(playerName)) {
      for (
    BlockState bs blockstates.get(playerName)) {
        
    // ...
      
    }
    }
    and of course when you're adding BlockState objects to the structure, you'll need to create each player's list the first time round, e.g.:
    PHP:
    if (!blockstates.containsKey(playerName)) {
      
    blockstates.put(playerName, new ArrayList<BlockState>());
    }
    blockstates.get(playerName).add(bs);
     
  10. Offline

    Eats_Rainbows

    desht Thanks so much! That was exactly what I needed, the plugin works! :D
     
Thread Status:
Not open for further replies.

Share This Page