1.7.10 Saving and Loading HashMaps

Discussion in 'Spigot Plugin Development' started by Kreeuw, Jan 24, 2020.

  1. Hi guys my name is Antonio I'm new to development and I would like to know how I can save and load a hashMap. My hashMap:
    Code (Text):
    public HashMap<String, Long> cooldowns = new HashMap<String, Long>();
    This HashMap is in another RmCheck file, to access the HashMap it is necessary to type:
    Code (Text):
    rmca.cooldowns
    Follow my main code:
    Code (Text):


    public class RecompensaMensal extends JavaPlugin {

        RmCheck rmca = new RmCheck();
        String jogador = rmca.playerNome;

        @Override
        public void onEnable() {
            registerCommands();
           
           //Here you have to load the hash
            getLogger().info("Plugin RM ativo.");

        }

        @Override
        public void onDisable() {
        //Here you have to save the hash


            getLogger().info("Plugin RM desativo.");
        }
     
     
  2. Google.
    is.
    your.
    friend.

    There's tons of threads on this already, unless you're looking for something else?
     
    • Funny Funny x 1
    • Winner Winner x 1
  3. I have tried to see several tutorials and articles but I can't do it at all.
     
  4. Well... since you seem to have chosen not to befriend google, I shall present a solution to this problem:
    Saving the map:
    Code (Java):
    final FileConfiguration config = getConfig();
    config.save("cooldowns", map);
    saveConfig();
    Getting the map:
    Code (Java):
    final FileConfiguration config = getConfig();
    final ConfigurationSection section = config.getConfigurationSection("cooldowns");
    Map<String,Long> cooldowns = new HashMap(section.getValues(false));
    Not really rocket science
     
    #4 Schottky, Jan 24, 2020
    Last edited: Jan 25, 2020
  5. Does not work, several errors appear; C
     
  6. What are the errors though..? You cannot expect us to guess it!
     
    • Agree Agree x 1
  7. Code (Text):

       @Override
        public void onEnable() {
            final FileConfiguration config = getConfig();
            final ConfigurationSection section = config.get("cooldowns");
            Map<String,Long> cooldowns = new HashMap(section.getValues(false));
            getLogger().info("Plugin RM ativo.");

        }

        @Override
        public void onDisable() {
            final FileConfiguration config = getConfig();
            config.save("cooldowns", map);
            saveConfig();

    }
     
    Code (Text):

    final ConfigurationSection section = config.get("cooldowns");
     
    Here it requires ConfigurationSection, but it is receiving Object

    Code (Text):

    config.save("cooldowns", map);
     
    Cannot Resolve Symbol 'map'
     
  8. welp, the first is my bad; it should be
    Code (Java):
    config.getConfigurationSection();
    The second one... well... where do you expect your map (like... the map with the cooldowns in it) has to go?
     
  9. Ok the 1st error has been solved, but the second error is that there is no 'map' variable
    Code (Text):

    @Override
    public void onDisable() {
        final FileConfiguration config = getConfig();
        config.save("cooldowns", map);
        saveConfig();

    }
     
     
  10. Exactly. What kind of map could you possibly use instead of that value? What kind of map could you possibly want to save?
     
  11. Code (Text):
    rmca.cooldown
    But, it does not work
     
  12. Please send details and son‘t do that ‚does not work‘ stuff. What exactly is not working?
    From what I have seen, you are blindly copying my code without even thinking about it. The map that you are retrieving is your rmca.cooldown map. You‘ll need to assign (this: =) it to your map. Also, you need to think about the first load; what happens if the map is not in the config (hint: check if section == null). Please don‘t just blindly copy code, but try to understand what the code does.