mapsbukkitdimensional

3 Dimensional map


I have this code:

File folder = new File(getDataFolder() + "/messages");
        String[] fileNames = folder.list();
        for (int i = 0; i < fileNames.length; i++) {
            configManager.loadConfigFile("messages/" + fileNames[i]);
            String lang = fileNames[i].substring(8, fileNames[i].length() - 4);
            YamlConfiguration messagesConfig = configManager.getConfig("messages/" + fileNames[i]);
            messagesConfigs.put(lang, messagesConfig);
        }
private static Map<String, Map<String, String>> messages = new HashMap<String, Map<String, String>>();

    public void loadMessages(String lang) {
        for (String mes : PluginBase.plugin.messagesConfigs.get(lang).getKeys(false)) {
            messages.put(lang, new HashMap<String, String>().put(mes, PluginBase.plugin.messagesConfigs.get(lang).getString(mes)));
        }
    }

    public static String getMessage(String lang, String id) {
        return messages.get(lang).get(id);
    }

I have languages, callers and messagers ("us" -> "nopermission" -> "You do not have a permission!"). When I call getMessage(String lang, String caller) I want to get message for the language lang and caller caller. Current code gets error at line messages.put(lang, new HashMap<String, String>().put(mes, PluginBase.plugin.messagesConfigs.get(lang).getString(mes)));

Thank so much.


Solution

  • You are getting that error because HashMap.put() does not return anything in an empty map.

    If you want to fix this, instead of using:

    messages
      .put(
        lang,
        new HashMap<String, String>()
          .put(
            mes, 
            PluginBase.plugin.messagesConfigs.get(lang).getString(mes)
          )
      );
    

    You should use:

    Map<String, String> message = new HashMap<String, String>();
    message.put(mes, PluginBase.plugin.messagesConfigs.get(lang).getString(mes));
    
    messages.put(lang, message);