Solved get hashmap<String, Boolean> from config

Discussion in 'Plugin Development' started by guitargun, Oct 20, 2014.

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

    guitargun

    I want to get the hashmap where the server admins set the true/false for the items inside the config
    only I don't know how to get this specific hashmap. I saw some threads about reading files with their hashmaps but from what I understand it reads the whole file not only 1 hashmap. I know I have 2 in the config but lets stick to 1 since it is just the same but with a different name.

    loading hashmap 1:
    Code:java
    1. public class MobCatch implements Listener{
    2. Main plugin;
    3.  
    4. private HashMap<String, Boolean> catchablemob = new HashMap<String, Boolean>();
    5.  
    6.  
    7.  
    8. @EventHandler
    9. public void onmobcatch(PlayerInteractEntityEvent event) {
    10.  
    11. }
    12.  
    13.  
    14. @SuppressWarnings("unchecked")
    15. private void load(){
    16. catchablemob = (HashMap<String, Boolean>) plugin.config.getMapList("MobEggs");
    17. System.out.println(catchablemob);
    18. }
    19.  
    20. public MobCatch(Main instance){
    21. this.plugin = instance;
    22. load();
    23. }


    and the config
    Code:
    MobEggs:
      Cow : false
      Pig : false
      Horse : false
      Sheep : false
      Mooshroom : false
      Villager : false
      Chicken : false
      Squid : false
      Wolf : false
      Ocalot : false
     
    MonsterEggs:
      Zombie : false
      Skeleton : false
      Spider : false
      CaveSpider : false
      Silverfish : false
      Blaze : false
      Ghast : false
      PigZombie : false
      Slime : false
      MagmaCube : false
      Creeper : false
      Witch : false
      Enderman : false
    currently I have a classcastexeption. I think the config itself has some errors but I couldn't find how to make a config hashmap by hand.
     
  2. Offline

    FerusGrim

    You don't make YAML a 'HashMap'. YAML is already a hashmap. It has a key, and a value.

    Say you have the below:
    Code:
    General:
        Manager: JSON
        Enable: true
    And you want to get the Manager string. The 'key', would be "General.Manager", while the Object value would be "JSON". With YAML, the values are natively Map<String, Object>.

    Try this:
    Code:java
    1. Map<String, Boolean> catchableMob = new HashMap<String, Boolean>();
    2. for (String key : getConfig().getConfigurationSection("Monster").getKeys()) {
    3. catchableMob.put(key, getConfig().getBoolean("Monster." key));
    4. }


    Note, that my configuration know-how is a bit rusty. But you should be able to do something like that, even if my syntax is a bit off.
     
    guitargun likes this.
  3. Offline

    Unica

    Why exactly do you need a hashmap ;)?

    Can't u just change values inside the config at once?
     
  4. Offline

    guitargun

    ok thx.
    I changed it to what you said and it works. Never knew that the YAML is a hashmap already
     
  5. Offline

    fireblast709

    Why do you use a Map<SomeType, Boolean>, why not a Set<SomeType>?
     
  6. Offline

    FerusGrim

    So that later, he can do something along the lines of:
    Code:java
    1. if (catchableMob.get("Creeper"))
     
  7. FerusGrim if(Set#contains())? I've yet to ever see an example that requires a Map<String, Boolean> map, as you only store one bit of information - whether the String is 'true' or 'false'. Being in a collection, or not being in a collection is essentially the same information.
     
  8. Offline

    fireblast709

    And get a NullPointerException if "Creeper" is not in the catchableMob Map?
     
  9. Offline

    guitargun

    ow on I grouped the passive and aggressive mobs and found that it would always return false because it didn't exist. I fixed it by adding the configuration part in the boolean getter.
    final fix:
    Code:java
    1. for(String key : plugin.getConfig().getConfigurationSection("Monsters").getKeys(true)){
    2. boolean v = plugin.getConfig().getConfigurationSection("Monsters").getBoolean(key);
    3. if(key.equals("MagmaCube")){
    4. key = "LavaSlime";
    5. }
    6. catchablemonster.put(key, v);
    7. }
     
Thread Status:
Not open for further replies.

Share This Page