1.16.5 Two keys in hashmap?

Discussion in 'Spigot Plugin Development' started by CROACH, Jul 5, 2021.

  1. Hi. I am currently working on a project for my minecraft server where you can get gadgets for playing and then console will execute command /gadd <player> <gadget> <timediff>, but I can't figure out how to make a hashmap (or something else to store the values) as I would like to store it like this (in config.yml, on plugin disable):
    data:
    player:
    trampoline:
    (time in miliseconds)
     
  2. SteelPhoenix

    Moderator

    Maps cannot contain duplicate keys, that's one of a map's key principles.
    You may want to use a multimap (multiple values per key)
     
    • Agree Agree x 1
  3. If you’re looking to store the data in YAML, don’t store in the config. Look at some guides on custom data files.
     
  4. then how to do that?
     
  5. It's probably best to create your own object to store whatever you want for each user which will keep the system expandable
     
    • Agree Agree x 1
  6. Can you give me a small example?
     
  7. You make getters in your class and when you want to save that class' data you do FileConfiguration#set(path, data) and then FileConfiguration#save(File)
     
  8. You can always create a HashMap where the key is the player and the value is a Pair (class from Bukkit I believe) of the gadget and timediff.
    If you prefer not to them you can just make 2 HashMaps - one for each.
     
  9. To keep data persistent you'll have to allocate the data to the class each time the plugin is enabled, this can be done by grabbing those values you've stored wherever
     
  10. And now how to put data inside this hashmap?
     
  11. Code (Java):
    import com.mojang.datafixers.util.Pair;
    ...
    Player player;
    [gadget_type] gadget;
    [timediff_type] timediff;
    HashMap<Player,List<Pair<[gadget_type],[timediff_type]>>> map = new HashMap<Player,List<Pair<[gadget_type],[timediff_type]>>>();
    ...
    Pair<[gadget_type],[timediff_type]> pair = Pair.of(gadget,timediff);
    if (!map.containsKey(player)) map.put(player, new ArrayList<Pair<[gadget_type],[timediff_type]>>());
    map.get(player).add(pair);
    Obviously, [gadget_type] and [timediff_type] are the type of gadget and timediff (respectively), I just don't know what types they are.
    If you want this to be even easier and much more accessible, I suggest you make it a HashMap with the key being the player and the value being another HashMap - the inner HashMap's key will be the gadget and the value timediff:
    Code (Java):
    Player player;
    [gadget_type] gadget;
    [timediff_type] timediff;
    HashMap<Player,HashMap<[gadget_type],[timediff_type]>> map = new HashMap<Player,HashMap<[gadget_type],[timediff_type]>>();
    ...
    if (!map.containsKey(player)) map.put(player, new HashMap<[gadget_type],[timediff_type]>());
    map.get(player).put(gadget,timediff);
     
  12. Oh my god...
    That solution totally blew me up!
    But now, how to get value of timediff from specific gadget?

    By the way, the type of gadget is String, and type of timediff is Long
     
  13. I suggest you use the 2nd solution (HashMap instead of Pair).
    This function will return the timediff for a given player and a given gadget (will return null if the player isn't in the map or the gadget isn't in the player's map):
    Code (Java):
    public Long getGadgetValue(Player player, String gadget) {
        if (map.containsKey(player)) return map.get(player).get(gadget);
        return null;
    }