6
6
import org .bukkit .configuration .file .YamlConfiguration ;
7
7
import org .bukkit .plugin .java .JavaPlugin ;
8
8
import org .librazy .nyaautils_lang_checker .LangKey ;
9
+ import org .yaml .snakeyaml .Yaml ;
9
10
10
11
import java .io .File ;
11
12
import java .io .IOException ;
@@ -69,13 +70,37 @@ public abstract class LanguageRepository {
69
70
*/
70
71
protected abstract String getLanguage ();
71
72
73
+ private static NyaaCoreLoader corePlugin = null ;
74
+
75
+ // helper function to load language map
76
+ private static void loadResourceMap (JavaPlugin plugin , String codeName ,
77
+ Map <String , String > targetMap , boolean ignoreInternal , boolean ignoreNormal ) {
78
+ if (plugin == null || codeName == null || targetMap == null ) throw new IllegalArgumentException ();
79
+ InputStream stream = plugin .getResource ("lang/" + codeName + ".yml" );
80
+ if (stream != null ) {
81
+ YamlConfiguration section = YamlConfiguration .loadConfiguration (new InputStreamReader (stream ));
82
+ loadLanguageSection (targetMap , section , "" , ignoreInternal , ignoreNormal );
83
+ }
84
+ }
85
+
86
+ // helper function to load language map
87
+ private static void loadLocalMap (JavaPlugin plugin , String codeName ,
88
+ Map <String , String > targetMap , boolean ignoreInternal , boolean ignoreNormal ) {
89
+ if (plugin == null || codeName == null || targetMap == null ) throw new IllegalArgumentException ();
90
+ if (Boolean .parseBoolean (System .getProperty ("nyaautils.i18n.refreshLangFiles" , "false" ))) return ;
91
+ File langFile = new File (plugin .getDataFolder (), codeName + ".yml" );
92
+ if (langFile .exists () && langFile .isFile ()) {
93
+ YamlConfiguration section = YamlConfiguration .loadConfiguration (langFile );
94
+ loadLanguageSection (targetMap , section , "" , ignoreInternal , ignoreNormal );
95
+ }
96
+ }
97
+
72
98
/**
73
99
* Load the internal map
74
100
* should only be called once from {@link NyaaCoreLoader#onLoad()}
75
101
*
76
102
* @param plugin the NyaaCore plugin
77
103
*/
78
- private static NyaaCoreLoader corePlugin = null ;
79
104
public static void initInternalMap (NyaaCoreLoader plugin ) {
80
105
if (internalMap .size () != 0 || corePlugin != null ) {
81
106
plugin .getLogger ().warning ("Multiple internalMap initiation" );
@@ -86,18 +111,10 @@ public static void initInternalMap(NyaaCoreLoader plugin) {
86
111
String codeName = lang .codeName ;
87
112
Map <String , String > map = new HashMap <>();
88
113
internalMap .put (codeName , map );
89
- InputStream stream ;
90
- YamlConfiguration section ;
91
- stream = plugin .getResource ("lang/" + DEFAULT_LANGUAGE + ".yml" );
92
- if (stream != null ) {
93
- section = YamlConfiguration .loadConfiguration (new InputStreamReader (stream ));
94
- loadLanguageSection (map , section , "" , false , true );
95
- }
96
- stream = plugin .getResource ("lang/" + codeName + ".yml" );
97
- if (stream != null ) {
98
- section = YamlConfiguration .loadConfiguration (new InputStreamReader (stream ));
99
- loadLanguageSection (map , section , "" , false , true );
100
- }
114
+ loadResourceMap (plugin , DEFAULT_LANGUAGE , map , false , true );
115
+ loadLocalMap (plugin , DEFAULT_LANGUAGE , map , false , true );
116
+ loadResourceMap (plugin , codeName , map , false , true );
117
+ loadLocalMap (plugin , codeName , map , false , true );
101
118
plugin .getLogger ().info (String .format ("NyaaCore internalMap loaded: %s" , codeName ));
102
119
}
103
120
}
@@ -112,36 +129,15 @@ public void load() {
112
129
if (codeName == null ) codeName = DEFAULT_LANGUAGE ;
113
130
map .clear ();
114
131
// load languages
115
- InputStream stream ;
116
- YamlConfiguration section ;
117
- stream = corePlugin .getResource ("lang/" + DEFAULT_LANGUAGE + ".yml" );
118
- if (stream != null ) {
119
- section = YamlConfiguration .loadConfiguration (new InputStreamReader (stream ));
120
- loadLanguageSection (map , section , "" , true , false );
121
- }
122
- stream = corePlugin .getResource ("lang/" + codeName + ".yml" );
123
- if (stream != null ) {
124
- section = YamlConfiguration .loadConfiguration (new InputStreamReader (stream ));
125
- loadLanguageSection (map , section , "" , true , false );
126
- }
132
+ loadResourceMap (corePlugin , DEFAULT_LANGUAGE , map , true , false );
133
+ loadLocalMap (corePlugin , DEFAULT_LANGUAGE , map , true , false );
134
+ loadResourceMap (corePlugin , codeName , map , true , false );
135
+ loadLocalMap (corePlugin , codeName , map , true , false );
127
136
128
- stream = plugin .getResource ("lang/" + DEFAULT_LANGUAGE + ".yml" );
129
- if (stream != null ) {
130
- section = YamlConfiguration .loadConfiguration (new InputStreamReader (stream ));
131
- loadLanguageSection (map , section , "" , false , false );
132
- }
133
- stream = plugin .getResource ("lang/" + codeName + ".yml" );
134
- if (stream != null ) {
135
- section = YamlConfiguration .loadConfiguration (new InputStreamReader (stream ));
136
- loadLanguageSection (map , section , "" , false , false );
137
- }
138
-
139
- if ("false" .equals (System .getProperty ("nyaautils.i18n.refreshLangFiles" , "false" ))) { // Do not refresh, so still loading from dataFolder
140
- File localLangFile = new File (plugin .getDataFolder (), codeName + ".yml" );
141
- if (localLangFile .exists () && localLangFile .isFile ()) {
142
- loadLanguageSection (map , YamlConfiguration .loadConfiguration (localLangFile ), "" , false , false );
143
- }
144
- }
137
+ loadResourceMap (getPlugin (), DEFAULT_LANGUAGE , map , false , false );
138
+ loadLocalMap (getPlugin (), DEFAULT_LANGUAGE , map , false , false );
139
+ loadResourceMap (getPlugin (), codeName , map , false , false );
140
+ loadLocalMap (getPlugin (), codeName , map , false , false );
145
141
146
142
// save (probably) modified language file back to disk
147
143
File localLangFile = new File (plugin .getDataFolder (), codeName + ".yml" );
0 commit comments