99import org .bukkit .plugin .Plugin ;
1010
1111import java .io .*;
12- import java .util .Arrays ;
13- import java .util .List ;
14- import java .util .Optional ;
12+ import java .util .*;
1513import java .util .function .Consumer ;
1614import java .util .stream .Collectors ;
1715
1816public final class MultiLanguageAPI {
1917
18+ private static Map <Lang , FileConfiguration > cache = new HashMap <>();
19+
2020 private static File globalLangFolder = new File ("./plugins/MultiLanguageAPI/" );
2121 private static File globalLangFile = new File (globalLangFolder , "config.yml" );
2222
@@ -101,7 +101,25 @@ public String getString(Player player, String path) throws LangFileNotFoundExcep
101101 return getString (lang , path );
102102 }
103103
104+ /**
105+ * Clear configurations in cache.
106+ */
107+ public void clearCache () {
108+ cache .clear ();
109+ }
110+
111+ /**
112+ * Get config of {@link fr.mrcoq.lib.multilanguageapi.Lang}.
113+ * @param lang
114+ * @return
115+ * @throws LangFileNotFoundException
116+ */
104117 public FileConfiguration getConfig (Lang lang ) throws LangFileNotFoundException {
118+
119+ if (cache .containsKey (lang )) {
120+ return cache .get (lang );
121+ }
122+
105123 Optional <File > optionalFile = Arrays .stream (langFolder .listFiles ()).filter ((file ) -> lang .getLocals ().stream ().anyMatch (local -> file .getName ().startsWith (local ))).findFirst ();
106124
107125 FileConfiguration config = null ;
@@ -123,6 +141,8 @@ public FileConfiguration getConfig(Lang lang) throws LangFileNotFoundException {
123141 throw new LangFileNotFoundException (lang );
124142 }
125143
144+ cache .put (lang , config );
145+
126146 return config ;
127147 }
128148
@@ -199,6 +219,7 @@ public static Lang getDefaultLang() {
199219 }
200220
201221 FileConfiguration config = YamlConfiguration .loadConfiguration (globalLangFile );
222+
202223 String defaultLang = config .getString ("lang.default" );
203224
204225 return Lang .getFromLocal (defaultLang );
0 commit comments