Skip to content

Commit 57bafed

Browse files
committed
cleanup
1 parent 6eadca3 commit 57bafed

File tree

2 files changed

+68
-16
lines changed

2 files changed

+68
-16
lines changed

src/main/java/org/maxgamer/quickshop/util/language/text/TextManager.java

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ public class TextManager implements Reloadable {
3232
private final QuickShop plugin;
3333
private final Distribution distribution;
3434
// <File <Locale, Section>>
35-
private final Map<String, Map<String, JsonConfiguration>> locale2ContentMapping = new HashMap<>();
36-
private final Map<String, JsonConfiguration> bundledFile2ContentMapping = new HashMap<>();
35+
private final TextMapper mapper = new TextMapper();
3736
private final static String CROWDIN_LANGUAGE_FILE = "/master/src/main/resources/lang/%locale%/messages.json";
3837
public final List<PostProcessor> postProcessors = new ArrayList<>();
3938
private List<String> disabledLanguages = new ArrayList<>();
@@ -65,9 +64,8 @@ private File getOverrideFilesFolder(@NotNull String crowdinPath) {
6564
* Reset everything
6665
*/
6766
private void reset() {
68-
locale2ContentMapping.clear();
67+
mapper.reset();
6968
postProcessors.clear();
70-
bundledFile2ContentMapping.clear();
7169
disabledLanguages.clear();
7270
}
7371

@@ -97,11 +95,11 @@ public void load() {
9795
this.reset();
9896
disabledLanguages = plugin.getConfig().getStringList("disabled-languages");
9997
// Initial file mapping
100-
locale2ContentMapping.computeIfAbsent(CROWDIN_LANGUAGE_FILE, e -> new HashMap<>()); // Prevent nullportinter exception
101-
distribution.getAvailableFiles().forEach(file -> locale2ContentMapping.computeIfAbsent(file, e -> new HashMap<>()));
98+
//locale2ContentMapping.computeIfAbsent(CROWDIN_LANGUAGE_FILE, e -> new HashMap<>()); // Prevent nullportinter exception
99+
//distribution.getAvailableFiles().forEach(file -> locale2ContentMapping.computeIfAbsent(file, e -> new HashMap<>()));
102100

103101
// Read bundled language files
104-
distribution.getAvailableFiles().forEach(crowdinFile -> this.bundledFile2ContentMapping.computeIfAbsent(crowdinFile, e -> loadBundled(crowdinFile)));
102+
//distribution.getAvailableFiles().forEach(crowdinFile -> this.bundledFile2ContentMapping.computeIfAbsent(crowdinFile, e -> loadBundled(crowdinFile)));
105103

106104
// Multi File and Multi-Language loader
107105
distribution.getAvailableLanguages().parallelStream().forEach(crowdinCode -> distribution.getAvailableFiles().parallelStream().forEach(crowdinFile -> {
@@ -116,8 +114,8 @@ public void load() {
116114
JsonConfiguration configuration = getDistributionConfiguration(crowdinFile, crowdinCode);
117115
// Loading override text (allow user modification the translation)
118116
JsonConfiguration override = getOverrideConfiguration(crowdinFile, minecraftCode);
119-
applyOverrideConfiguration(configuration,override);
120-
locale2ContentMapping.get(crowdinFile).computeIfAbsent(minecraftCode, e -> configuration);
117+
applyOverrideConfiguration(configuration, override);
118+
mapper.deploy(crowdinFile, minecraftCode, configuration, loadBundled(crowdinFile));
121119
Util.debugLog("Locale " + crowdinFile.replace("%locale%", crowdinCode) + " has been successfully loaded");
122120
} catch (CrowdinOTA.OTAException e) {
123121
// Key founds in available locales but not in custom mapping on crowdin platform
@@ -137,7 +135,7 @@ public void load() {
137135
postProcessors.add(new ColorProcessor());
138136
}
139137

140-
private void applyOverrideConfiguration(JsonConfiguration distributionConfiguration, JsonConfiguration overrideConfiguration){
138+
private void applyOverrideConfiguration(JsonConfiguration distributionConfiguration, JsonConfiguration overrideConfiguration) {
141139
for (String key : overrideConfiguration.getKeys(true)) {
142140
if ("language-version".equals(key) || "config-version".equals(key) || "version".equals(key)) {
143141
continue;
@@ -178,7 +176,7 @@ private JsonConfiguration getOverrideConfiguration(String overrideFile, String l
178176
* @return The text object
179177
*/
180178
public Text of(@NotNull String path, String... args) {
181-
return new Text(this, (CommandSender) null, locale2ContentMapping.get(CROWDIN_LANGUAGE_FILE), bundledFile2ContentMapping.get(CROWDIN_LANGUAGE_FILE), path, args);
179+
return new Text(this, (CommandSender) null, mapper.getDistribution(CROWDIN_LANGUAGE_FILE), mapper.getBundled(CROWDIN_LANGUAGE_FILE), path, args);
182180
}
183181

184182
/**
@@ -190,7 +188,7 @@ public Text of(@NotNull String path, String... args) {
190188
* @return The text object
191189
*/
192190
public Text of(@Nullable CommandSender sender, @NotNull String path, String... args) {
193-
return new Text(this, sender, locale2ContentMapping.get(CROWDIN_LANGUAGE_FILE), bundledFile2ContentMapping.get(CROWDIN_LANGUAGE_FILE), path, args);
191+
return new Text(this, sender, mapper.getDistribution(CROWDIN_LANGUAGE_FILE), mapper.getBundled(CROWDIN_LANGUAGE_FILE), path, args);
194192
}
195193

196194
/**
@@ -202,7 +200,7 @@ public Text of(@Nullable CommandSender sender, @NotNull String path, String... a
202200
* @return The text object
203201
*/
204202
public Text of(@Nullable UUID sender, @NotNull String path, String... args) {
205-
return new Text(this, sender, locale2ContentMapping.get(CROWDIN_LANGUAGE_FILE), bundledFile2ContentMapping.get(CROWDIN_LANGUAGE_FILE), path, args);
203+
return new Text(this, sender, mapper.getDistribution(CROWDIN_LANGUAGE_FILE), mapper.getBundled(CROWDIN_LANGUAGE_FILE), path, args);
206204
}
207205

208206
/**
@@ -213,7 +211,7 @@ public Text of(@Nullable UUID sender, @NotNull String path, String... args) {
213211
* @return The text object
214212
*/
215213
public TextList ofList(@NotNull String path, String... args) {
216-
return new TextList(this, (CommandSender) null, locale2ContentMapping.get(CROWDIN_LANGUAGE_FILE), bundledFile2ContentMapping.get(CROWDIN_LANGUAGE_FILE), path, args);
214+
return new TextList(this, (CommandSender) null, mapper.getDistribution(CROWDIN_LANGUAGE_FILE), mapper.getBundled(CROWDIN_LANGUAGE_FILE), path, args);
217215
}
218216

219217
/**
@@ -225,7 +223,7 @@ public TextList ofList(@NotNull String path, String... args) {
225223
* @return The text object
226224
*/
227225
public TextList ofList(@Nullable UUID sender, @NotNull String path, String... args) {
228-
return new TextList(this, sender, locale2ContentMapping.get(CROWDIN_LANGUAGE_FILE), bundledFile2ContentMapping.get(CROWDIN_LANGUAGE_FILE), path, args);
226+
return new TextList(this, sender, mapper.getDistribution(CROWDIN_LANGUAGE_FILE), mapper.getBundled(CROWDIN_LANGUAGE_FILE), path, args);
229227
}
230228

231229
/**
@@ -237,7 +235,7 @@ public TextList ofList(@Nullable UUID sender, @NotNull String path, String... ar
237235
* @return The text object
238236
*/
239237
public TextList ofList(@Nullable CommandSender sender, @NotNull String path, String... args) {
240-
return new TextList(this, sender, locale2ContentMapping.get(CROWDIN_LANGUAGE_FILE), bundledFile2ContentMapping.get(CROWDIN_LANGUAGE_FILE), path, args);
238+
return new TextList(this, sender, mapper.getDistribution(CROWDIN_LANGUAGE_FILE), mapper.getBundled(CROWDIN_LANGUAGE_FILE), path, args);
241239
}
242240

243241
public static class TextList {
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package org.maxgamer.quickshop.util.language.text;
2+
3+
import com.dumptruckman.bukkit.configuration.json.JsonConfiguration;
4+
import org.jetbrains.annotations.NotNull;
5+
import org.jetbrains.annotations.Nullable;
6+
7+
import java.util.Collections;
8+
import java.util.HashMap;
9+
import java.util.Map;
10+
11+
public class TextMapper {
12+
private final Map<String, Map<String, JsonConfiguration>> locale2ContentMapping = new HashMap<>();
13+
private final Map<String, JsonConfiguration> bundledFile2ContentMapping = new HashMap<>();
14+
15+
public void reset(){
16+
this.locale2ContentMapping.clear();
17+
this.bundledFile2ContentMapping.clear();
18+
}
19+
20+
public void deploy(@NotNull String distributionPath, @NotNull String locale, @NotNull JsonConfiguration distribution, @NotNull JsonConfiguration bundled){
21+
this.bundledFile2ContentMapping.put(distributionPath,bundled);
22+
this.locale2ContentMapping.computeIfAbsent(distributionPath,e->new HashMap<>());
23+
this.locale2ContentMapping.get(distributionPath).put(locale,distribution);
24+
}
25+
26+
public void remove(@NotNull String distributionPath){
27+
this.bundledFile2ContentMapping.remove(distributionPath);
28+
this.locale2ContentMapping.remove(distributionPath);
29+
}
30+
31+
public void remove(@NotNull String distributionPath, @NotNull String locale){
32+
if(this.locale2ContentMapping.containsKey(distributionPath)){
33+
this.locale2ContentMapping.get(distributionPath).remove(locale);
34+
}
35+
}
36+
37+
public void removeBundled(@NotNull String distributionPath){
38+
this.bundledFile2ContentMapping.remove(distributionPath);
39+
}
40+
41+
42+
public @Nullable JsonConfiguration getBundled(@NotNull String distributionPath){
43+
return this.bundledFile2ContentMapping.get(distributionPath);
44+
}
45+
46+
public @NotNull Map<String,JsonConfiguration> getDistribution(@NotNull String distributionPath){
47+
return this.locale2ContentMapping.getOrDefault(distributionPath, Collections.emptyMap());
48+
}
49+
50+
public @Nullable JsonConfiguration getDistribution(@NotNull String distributionPath, @NotNull String locale){
51+
return this.getDistribution(distributionPath).get(locale);
52+
}
53+
54+
}

0 commit comments

Comments
 (0)