Skip to content

Commit 201fae8

Browse files
committed
Tweaks TextManager storage structure
1 parent 82e9a94 commit 201fae8

File tree

4 files changed

+81
-21
lines changed

4 files changed

+81
-21
lines changed

pom.xml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,6 @@
309309
<excludes>
310310
<exclude>src/main/resources/bin</exclude>
311311
<exclude>src/main/resources/lang</exclude>
312-
<exclude>src/main/resources/lang-original</exclude>
313312
</excludes>
314313
</resource>
315314
<resource>
@@ -322,11 +321,6 @@
322321
<filtering>false</filtering>
323322
<directory>src/main/resources/lang</directory>
324323
</resource>
325-
<resource>
326-
<targetPath>lang-original</targetPath>
327-
<filtering>false</filtering>
328-
<directory>src/main/resources/lang-original</directory>
329-
</resource>
330324
</resources>
331325
</build>
332326

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

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.maxgamer.quickshop.util.language.text;
22

33
import com.dumptruckman.bukkit.configuration.json.JsonConfiguration;
4+
import lombok.SneakyThrows;
45
import org.apache.commons.io.IOUtils;
56
import org.bukkit.Bukkit;
67
import org.bukkit.command.CommandSender;
@@ -49,15 +50,19 @@ public TextManager(QuickShop plugin) {
4950
/**
5051
* Generate the override files storage path
5152
*
52-
* @param crowdinPath The crowdin file path
53+
* @param path The distribution file path
5354
* @return Override files storage path
5455
*/
56+
@SneakyThrows
5557
@NotNull
56-
private File getOverrideFilesFolder(@NotNull String crowdinPath) {
57-
File file = new File(crowdinPath);
58-
File folder = new File(new File(plugin.getDataFolder(), "overrides"), file.getName() + ".overrides");
59-
folder.mkdirs();
60-
return folder;
58+
private File getOverrideFilesFolder(@NotNull String path) {
59+
File file = new File(path);
60+
String module = file.getParentFile().getName();
61+
File moduleFolder = new File(new File(plugin.getDataFolder(), "overrides"), module);
62+
moduleFolder.mkdirs();
63+
File fileFolder = new File(moduleFolder,file.getName());
64+
fileFolder.mkdirs();
65+
return file;
6166
}
6267

6368
/**
@@ -78,7 +83,7 @@ private JsonConfiguration loadBundled(String file) {
7883
JsonConfiguration bundledLang = new JsonConfiguration();
7984
try {
8085
File fileObject = new File(file);
81-
bundledLang.loadFromString(new String(IOUtils.toByteArray(new InputStreamReader(plugin.getResource("lang-original/" + fileObject.getName())), StandardCharsets.UTF_8)));
86+
bundledLang.loadFromString(new String(IOUtils.toByteArray(new InputStreamReader(plugin.getResource("lang/" + fileObject.getName())), StandardCharsets.UTF_8)));
8287
} catch (IOException | InvalidConfigurationException ex) {
8388
bundledLang = new JsonConfiguration();
8489
plugin.getLogger().log(Level.SEVERE, "Cannot load bundled language file from Jar, some strings may missing!", ex);
@@ -127,7 +132,13 @@ public void load() {
127132
postProcessors.add(new ColorProcessor());
128133
}
129134

130-
private boolean localeEnabled(String locale, List<String> regex){
135+
/**
136+
* Gets specific locale status
137+
* @param locale The locale
138+
* @param regex The regexes
139+
* @return The locale enabled status
140+
*/
141+
private boolean localeEnabled(@NotNull String locale, @NotNull List<String> regex){
131142
for (String languagesRegex : regex) {
132143
try {
133144
if (locale.matches(languagesRegex)) {
@@ -140,7 +151,12 @@ private boolean localeEnabled(String locale, List<String> regex){
140151
return false;
141152
}
142153

143-
private void applyOverrideConfiguration(JsonConfiguration distributionConfiguration, JsonConfiguration overrideConfiguration) {
154+
/**
155+
* Merge override data into distribution configuration to override texts
156+
* @param distributionConfiguration The configuration that from distribution (will override it)
157+
* @param overrideConfiguration The configuration that from local
158+
*/
159+
private void applyOverrideConfiguration(@NotNull JsonConfiguration distributionConfiguration, @NotNull JsonConfiguration overrideConfiguration) {
144160
for (String key : overrideConfiguration.getKeys(true)) {
145161
if ("language-version".equals(key) || "config-version".equals(key) || "version".equals(key)) {
146162
continue;
@@ -149,7 +165,14 @@ private void applyOverrideConfiguration(JsonConfiguration distributionConfigurat
149165
}
150166
}
151167

152-
private JsonConfiguration getDistributionConfiguration(String distributionFile, String distributionCode) throws Exception {
168+
/**
169+
* Getting configuration from distribution platform
170+
* @param distributionFile Distribution path
171+
* @param distributionCode Locale code on distribution platform
172+
* @return The configuration
173+
* @throws Exception Any errors when getting it
174+
*/
175+
private JsonConfiguration getDistributionConfiguration(@NotNull String distributionFile, @NotNull String distributionCode) throws Exception {
153176
JsonConfiguration configuration = new JsonConfiguration();
154177
try {
155178
// Load the locale file from local cache if available
@@ -162,7 +185,15 @@ private JsonConfiguration getDistributionConfiguration(String distributionFile,
162185
return configuration;
163186
}
164187

165-
private JsonConfiguration getOverrideConfiguration(String overrideFile, String locale) throws IOException, InvalidConfigurationException {
188+
/**
189+
* Getting user's override configuration for specific distribution path
190+
* @param overrideFile The distribution
191+
* @param locale the locale
192+
* @return The override configuration
193+
* @throws IOException IOException
194+
* @throws InvalidConfigurationException File invalid
195+
*/
196+
private JsonConfiguration getOverrideConfiguration(@NotNull String overrideFile, @NotNull String locale) throws IOException, InvalidConfigurationException {
166197
File localOverrideFile = new File(getOverrideFilesFolder(overrideFile), locale + ".json");
167198
if (!localOverrideFile.exists()) {
168199
localOverrideFile.getParentFile().mkdirs();

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

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,41 +12,76 @@ public class TextMapper {
1212
private final Map<String, Map<String, JsonConfiguration>> locale2ContentMapping = new HashMap<>();
1313
private final Map<String, JsonConfiguration> bundledFile2ContentMapping = new HashMap<>();
1414

15+
/**
16+
* Reset TextMapper
17+
*/
1518
public void reset(){
1619
this.locale2ContentMapping.clear();
1720
this.bundledFile2ContentMapping.clear();
1821
}
1922

23+
/**
24+
* Deploy new locale to TextMapper with cloud values and bundle values
25+
* @param distributionPath Distribution Path
26+
* @param locale The locale code
27+
* @param distribution The values from Distribution platform
28+
* @param bundled The values from bundled file
29+
*/
2030
public void deploy(@NotNull String distributionPath, @NotNull String locale, @NotNull JsonConfiguration distribution, @NotNull JsonConfiguration bundled){
2131
this.bundledFile2ContentMapping.put(distributionPath,bundled);
2232
this.locale2ContentMapping.computeIfAbsent(distributionPath,e->new HashMap<>());
2333
this.locale2ContentMapping.get(distributionPath).put(locale,distribution);
2434
}
2535

36+
/**
37+
* Remove all locales data under specific distribution path
38+
* @param distributionPath The distribution path
39+
*/
2640
public void remove(@NotNull String distributionPath){
2741
this.bundledFile2ContentMapping.remove(distributionPath);
2842
this.locale2ContentMapping.remove(distributionPath);
2943
}
30-
44+
/**
45+
* Remove specific locales data under specific distribution path
46+
* @param distributionPath The distribution path
47+
* @param locale The locale
48+
*/
3149
public void remove(@NotNull String distributionPath, @NotNull String locale){
3250
if(this.locale2ContentMapping.containsKey(distributionPath)){
3351
this.locale2ContentMapping.get(distributionPath).remove(locale);
3452
}
3553
}
3654

55+
/**
56+
* Remove specific bundled data
57+
* @param distributionPath The distribution path
58+
*/
3759
public void removeBundled(@NotNull String distributionPath){
3860
this.bundledFile2ContentMapping.remove(distributionPath);
3961
}
4062

41-
63+
/**
64+
* Getting specific bundled data
65+
* @param distributionPath The distribution path
66+
* @return The bundled data, null if never deployed
67+
*/
4268
public @Nullable JsonConfiguration getBundled(@NotNull String distributionPath){
4369
return this.bundledFile2ContentMapping.get(distributionPath);
4470
}
45-
71+
/**
72+
* Getting locales data under specific distribution data
73+
* @param distributionPath The distribution path
74+
* @return The locales data, empty if never deployed
75+
*/
4676
public @NotNull Map<String,JsonConfiguration> getDistribution(@NotNull String distributionPath){
4777
return this.locale2ContentMapping.getOrDefault(distributionPath, Collections.emptyMap());
4878
}
49-
79+
/**
80+
* Getting specific locale data under specific distribution data
81+
* @param distributionPath The distribution path
82+
* @param locale The specific locale
83+
* @return The locale data, null if never deployed
84+
*/
5085
public @Nullable JsonConfiguration getDistribution(@NotNull String distributionPath, @NotNull String locale){
5186
return this.getDistribution(distributionPath).get(locale);
5287
}
File renamed without changes.

0 commit comments

Comments
 (0)