Skip to content

Commit 7f3434e

Browse files
committed
Support multiple languages and use enum translation keys
1 parent de2ae46 commit 7f3434e

32 files changed

Lines changed: 560 additions & 303 deletions

src/main/java/de/kcodeyt/plotplugin/PlotPlugin.java

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
import de.kcodeyt.plotplugin.manager.PlayerManager;
3030
import de.kcodeyt.plotplugin.manager.PlayerNameFunction;
3131
import de.kcodeyt.plotplugin.manager.PlotManager;
32-
import de.kcodeyt.plotplugin.util.Language;
32+
import de.kcodeyt.plotplugin.lang.Language;
3333
import de.kcodeyt.plotplugin.util.PlotLevelRegistration;
3434
import de.kcodeyt.plotplugin.util.PlotLevelSettings;
3535
import de.kcodeyt.plotplugin.util.Utils;
@@ -39,6 +39,7 @@
3939

4040
import java.io.BufferedReader;
4141
import java.io.File;
42+
import java.io.IOException;
4243
import java.io.InputStreamReader;
4344
import java.util.*;
4445
import java.util.concurrent.ThreadLocalRandom;
@@ -85,17 +86,20 @@ public void onEnable() {
8586
this.playersConfig = new Config(new File(this.getDataFolder(), "players.yml"), Config.YAML);
8687

8788
final File langDir = new File(this.getDataFolder(), "lang");
88-
if(!langDir.exists()) {
89-
if(!langDir.mkdirs()) {
89+
final File[] files = langDir.listFiles();
90+
if(!langDir.exists() || files == null || files.length == 0) {
91+
if(!langDir.exists() && !langDir.mkdirs()) {
9092
this.getLogger().error("Could not create the language directory for this plugin!");
9193
return;
9294
}
9395

9496
try(final InputStreamReader inputReader = new InputStreamReader(this.getResource("lang"));
9597
final BufferedReader bufferedReader = new BufferedReader(inputReader)) {
9698
String line;
97-
while((line = bufferedReader.readLine()) != null)
99+
while((line = bufferedReader.readLine()) != null) {
100+
System.out.println("LINE: " + line);
98101
this.saveResource("lang/" + line);
102+
}
99103
} catch(Exception e) {
100104
this.getLogger().error("Could not find the language resources of this plugin!", e);
101105
return;
@@ -105,32 +109,21 @@ public void onEnable() {
105109
this.saveResource("config.yml");
106110
final Config config = this.getConfig();
107111

108-
if(!config.exists("lang")) {
109-
config.set("lang", DEFAULT_LANGUAGE);
112+
if(!config.exists("default_lang")) {
113+
config.set("default_lang", DEFAULT_LANGUAGE);
110114
config.save();
111115
}
112116

113-
final String language = config.getString("lang");
114-
File languageFile = new File(langDir, language + ".txt");
115-
116-
if(!languageFile.exists()) {
117-
if(!language.equals(DEFAULT_LANGUAGE)) {
118-
languageFile = new File(langDir, DEFAULT_LANGUAGE + ".txt");
119-
120-
if(!languageFile.exists()) {
121-
this.getLogger().error("Could not find the default language file!");
122-
return;
123-
}
124-
} else {
125-
this.getLogger().error("Could not find the default language file!");
126-
}
117+
try {
118+
final String defaultLang = config.getString("default_lang");
127119

120+
this.language = new Language(langDir, defaultLang);
121+
this.getLogger().info("This plugin is using the " + this.language.getDefaultLang() + " as default language file!");
122+
} catch(IOException e) {
123+
this.getLogger().error(e.getMessage(), e);
128124
return;
129125
}
130126

131-
this.language = new Language(languageFile);
132-
this.getLogger().info("This plugin is using the " + languageFile.getName() + " language file!");
133-
134127
this.plotManagerMap = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
135128
this.levelRegistrationMap = new HashMap<>();
136129

src/main/java/de/kcodeyt/plotplugin/command/PlotCommand.java

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import cn.nukkit.command.CommandSender;
2222
import de.kcodeyt.plotplugin.PlotPlugin;
2323
import de.kcodeyt.plotplugin.command.defaults.*;
24+
import de.kcodeyt.plotplugin.lang.TranslationKey;
2425

2526
import java.util.Arrays;
2627
import java.util.LinkedHashSet;
@@ -36,27 +37,27 @@ public PlotCommand(PlotPlugin plugin) {
3637
this.plugin = plugin;
3738
this.subCommands = new LinkedHashSet<>();
3839

39-
this.subCommands.add(new AddHelperCommand(this.plugin));
40-
this.subCommands.add(new AutoCommand(this.plugin));
41-
this.subCommands.add(new ClaimCommand(this.plugin));
42-
this.subCommands.add(new ClearCommand(this.plugin));
43-
this.subCommands.add(new DenyCommand(this.plugin));
44-
this.subCommands.add(new DisposeCommand(this.plugin));
45-
this.subCommands.add(new GenerateCommand(this.plugin));
46-
this.subCommands.add(new HomeCommand(this.plugin));
47-
this.subCommands.add(new InfoCommand(this.plugin));
48-
this.subCommands.add(new MergeCommand(this.plugin));
49-
this.subCommands.add(new RegenAllRoadsCommand(this.plugin));
50-
this.subCommands.add(new RegenRoadCommand(this.plugin));
51-
this.subCommands.add(new ReloadCommand(this.plugin));
52-
this.subCommands.add(new RemoveHelperCommand(this.plugin));
53-
this.subCommands.add(new SetOwnerCommand(this.plugin));
54-
this.subCommands.add(new SetRoadsCommand(this.plugin));
55-
this.subCommands.add(new SettingCommand(this.plugin));
56-
this.subCommands.add(new TeleportCommand(this.plugin));
57-
this.subCommands.add(new UndenyCommand(this.plugin));
58-
this.subCommands.add(new UnlinkCommand(this.plugin));
59-
this.subCommands.add(new WarpCommand(this.plugin));
40+
this.subCommands.add(new AddHelperCommand(this.plugin, this));
41+
this.subCommands.add(new AutoCommand(this.plugin, this));
42+
this.subCommands.add(new ClaimCommand(this.plugin, this));
43+
this.subCommands.add(new ClearCommand(this.plugin, this));
44+
this.subCommands.add(new DenyCommand(this.plugin, this));
45+
this.subCommands.add(new DisposeCommand(this.plugin, this));
46+
this.subCommands.add(new GenerateCommand(this.plugin, this));
47+
this.subCommands.add(new HomeCommand(this.plugin, this));
48+
this.subCommands.add(new InfoCommand(this.plugin, this));
49+
this.subCommands.add(new MergeCommand(this.plugin, this));
50+
this.subCommands.add(new RegenAllRoadsCommand(this.plugin, this));
51+
this.subCommands.add(new RegenRoadCommand(this.plugin, this));
52+
this.subCommands.add(new ReloadCommand(this.plugin, this));
53+
this.subCommands.add(new RemoveHelperCommand(this.plugin, this));
54+
this.subCommands.add(new SetOwnerCommand(this.plugin, this));
55+
this.subCommands.add(new SetRoadsCommand(this.plugin, this));
56+
this.subCommands.add(new SettingCommand(this.plugin, this));
57+
this.subCommands.add(new TeleportCommand(this.plugin, this));
58+
this.subCommands.add(new UndenyCommand(this.plugin, this));
59+
this.subCommands.add(new UnlinkCommand(this.plugin, this));
60+
this.subCommands.add(new WarpCommand(this.plugin, this));
6061
}
6162

6263
@Override
@@ -75,15 +76,20 @@ public boolean execute(CommandSender sender, String label, String[] args) {
7576
return subCommand.execute(player, args);
7677
}
7778

78-
player.sendMessage(this.translate("help-title"));
79+
player.sendMessage(this.translate(player, TranslationKey.HELP_TITLE));
7980
for(SubCommand subCommand : this.subCommands)
80-
if(subCommand.hasPermission(player)) player.sendMessage(this.translate("help-" + subCommand.getName()));
81-
player.sendMessage(this.translate("help-end"));
81+
if(subCommand.hasPermission(player))
82+
player.sendMessage(this.translate(player, subCommand.getHelpTranslationKey()));
83+
player.sendMessage(this.translate(player, TranslationKey.HELP_END));
8284
return true;
8385
}
8486

85-
private String translate(String message) {
86-
return this.plugin.getLanguage().translate(message);
87+
protected String translate(Player player, TranslationKey key) {
88+
return this.plugin.getLanguage().translate(player, key);
89+
}
90+
91+
protected String translate(Player player, TranslationKey key, Object... params) {
92+
return this.plugin.getLanguage().translate(player, key, params);
8793
}
8894

8995
}

src/main/java/de/kcodeyt/plotplugin/command/SubCommand.java

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,35 +19,37 @@
1919
import cn.nukkit.Player;
2020
import cn.nukkit.command.data.CommandParameter;
2121
import de.kcodeyt.plotplugin.PlotPlugin;
22+
import de.kcodeyt.plotplugin.lang.TranslationKey;
2223
import lombok.AccessLevel;
2324
import lombok.Getter;
2425
import lombok.Setter;
2526

26-
import java.util.Arrays;
27-
import java.util.LinkedHashSet;
28-
import java.util.Set;
29-
import java.util.TreeSet;
27+
import java.util.*;
3028

3129
@Getter
3230
public abstract class SubCommand {
3331

3432
protected final PlotPlugin plugin;
33+
protected final PlotCommand parent;
3534

3635
private final String name;
36+
private final TranslationKey helpTranslationKey;
3737
private final Set<String> aliases;
3838

3939
private final Set<CommandParameter> parameters;
4040

4141
@Setter(AccessLevel.PROTECTED)
4242
private String permission;
4343

44-
protected SubCommand(PlotPlugin plugin, String name, String alias) {
45-
this(plugin, name, new String[]{alias});
44+
protected SubCommand(PlotPlugin plugin, PlotCommand parent, String name, String alias) {
45+
this(plugin, parent, name, new String[]{alias});
4646
}
4747

48-
protected SubCommand(PlotPlugin plugin, String name, String... aliases) {
48+
protected SubCommand(PlotPlugin plugin, PlotCommand parent, String name, String... aliases) {
4949
this.plugin = plugin;
50+
this.parent = parent;
5051
this.name = name;
52+
this.helpTranslationKey = TranslationKey.valueOf("HELP_" + this.name.toUpperCase(Locale.ROOT));
5153
this.aliases = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);
5254
this.aliases.add(name);
5355
this.aliases.addAll(Arrays.asList(aliases));
@@ -64,12 +66,12 @@ protected void addParameter(CommandParameter parameter) {
6466
this.parameters.add(parameter);
6567
}
6668

67-
protected String translate(String message) {
68-
return this.plugin.getLanguage().translate(message);
69+
protected String translate(Player player, TranslationKey key) {
70+
return this.parent.translate(player, key);
6971
}
7072

71-
protected String translate(String message, Object... params) {
72-
return this.plugin.getLanguage().translate(message, params);
73+
protected String translate(Player player, TranslationKey key, Object... params) {
74+
return this.parent.translate(player, key, params);
7375
}
7476

7577
}

src/main/java/de/kcodeyt/plotplugin/command/defaults/AddHelperCommand.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,18 @@
2020
import cn.nukkit.command.data.CommandParamType;
2121
import cn.nukkit.command.data.CommandParameter;
2222
import de.kcodeyt.plotplugin.PlotPlugin;
23+
import de.kcodeyt.plotplugin.command.PlotCommand;
2324
import de.kcodeyt.plotplugin.command.SubCommand;
25+
import de.kcodeyt.plotplugin.lang.TranslationKey;
2426
import de.kcodeyt.plotplugin.manager.PlotManager;
2527
import de.kcodeyt.plotplugin.util.Plot;
2628

2729
import java.util.UUID;
2830

2931
public class AddHelperCommand extends SubCommand {
3032

31-
public AddHelperCommand(PlotPlugin plugin) {
32-
super(plugin, "addhelper", "add", "trust");
33+
public AddHelperCommand(PlotPlugin plugin, PlotCommand parent) {
34+
super(plugin, parent, "addhelper", "add", "trust");
3335
this.addParameter(CommandParameter.newType("player", CommandParamType.TARGET));
3436
}
3537

@@ -38,34 +40,34 @@ public boolean execute(Player player, String[] args) {
3840
final PlotManager plotManager = this.plugin.getPlotManager(player.getLevel());
3941
final Plot plot;
4042
if(plotManager == null || (plot = plotManager.getMergedPlot(player.getFloorX(), player.getFloorZ())) == null) {
41-
player.sendMessage(this.translate("no-plot"));
43+
player.sendMessage(this.translate(player, TranslationKey.NO_PLOT));
4244
return false;
4345
}
4446

4547
final String targetName = (args.length > 0 ? args[0] : "").trim();
4648
final UUID targetId = this.plugin.getUniqueIdByName(targetName);
4749

4850
if(targetName.equalsIgnoreCase(player.getName()) && !player.hasPermission("plot.command.admin.addhelper")) {
49-
player.sendMessage(this.translate("player-self"));
51+
player.sendMessage(this.translate(player, TranslationKey.PLAYER_SELF));
5052
return false;
5153
}
5254

5355
if(targetName.isEmpty() || targetId == null) {
54-
player.sendMessage(this.translate("no-player"));
56+
player.sendMessage(this.translate(player, TranslationKey.NO_PLAYER));
5557
return false;
5658
}
5759

5860
if(!player.hasPermission("plot.command.admin.addhelper") && !plot.isOwner(player.getUniqueId())) {
59-
player.sendMessage(this.translate("no-plot-owner"));
61+
player.sendMessage(this.translate(player, TranslationKey.NO_PLOT_OWNER));
6062
return false;
6163
}
6264

6365
if(plot.addHelper(targetId)) {
6466
plotManager.savePlots();
65-
player.sendMessage(this.translate("added-helper", this.plugin.getCorrectName(targetId)));
67+
player.sendMessage(this.translate(player, TranslationKey.ADDED_HELPER, this.plugin.getCorrectName(targetId)));
6668
return true;
6769
} else {
68-
player.sendMessage(this.translate("already-helper", this.plugin.getCorrectName(targetId)));
70+
player.sendMessage(this.translate(player, TranslationKey.ALREADY_HELPER, this.plugin.getCorrectName(targetId)));
6971
return false;
7072
}
7173
}

src/main/java/de/kcodeyt/plotplugin/command/defaults/AutoCommand.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,25 @@
1818

1919
import cn.nukkit.Player;
2020
import de.kcodeyt.plotplugin.PlotPlugin;
21+
import de.kcodeyt.plotplugin.command.PlotCommand;
2122
import de.kcodeyt.plotplugin.command.SubCommand;
2223
import de.kcodeyt.plotplugin.event.PlotClaimEvent;
2324
import de.kcodeyt.plotplugin.event.PlotPreClaimEvent;
25+
import de.kcodeyt.plotplugin.lang.TranslationKey;
2426
import de.kcodeyt.plotplugin.manager.PlotManager;
2527
import de.kcodeyt.plotplugin.util.Plot;
2628

2729
public class AutoCommand extends SubCommand {
2830

29-
public AutoCommand(PlotPlugin plugin) {
30-
super(plugin, "auto", "a");
31+
public AutoCommand(PlotPlugin plugin, PlotCommand parent) {
32+
super(plugin, parent, "auto", "a");
3133
}
3234

3335
@Override
3436
public boolean execute(Player player, String[] args) {
3537
PlotManager plotManager = this.plugin.getPlotManager(player.getLevel());
3638
if(plotManager == null && this.plugin.getDefaultPlotLevel() == null || plotManager == null && (plotManager = this.plugin.getPlotManager(this.plugin.getDefaultPlotLevel())) == null) {
37-
player.sendMessage(this.translate("no-plot-world"));
39+
player.sendMessage(this.translate(player, TranslationKey.NO_PLOT_WORLD));
3840
return false;
3941
}
4042

@@ -57,11 +59,11 @@ public boolean execute(Player player, String[] args) {
5759
this.plugin.getServer().getPluginManager().callEvent(plotClaimEvent);
5860

5961
finalPlotManager.teleportPlayerToPlot(player, plot);
60-
player.sendMessage(this.translate("auto-success"));
62+
player.sendMessage(this.translate(player, TranslationKey.AUTO_SUCCESS));
6163
});
6264
return true;
6365
} else {
64-
player.sendMessage(this.translate("auto-failure"));
66+
player.sendMessage(this.translate(player, TranslationKey.AUTO_FAILURE));
6567
return false;
6668
}
6769
}

src/main/java/de/kcodeyt/plotplugin/command/defaults/ClaimCommand.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,26 @@
1818

1919
import cn.nukkit.Player;
2020
import de.kcodeyt.plotplugin.PlotPlugin;
21+
import de.kcodeyt.plotplugin.command.PlotCommand;
2122
import de.kcodeyt.plotplugin.command.SubCommand;
2223
import de.kcodeyt.plotplugin.event.PlotClaimEvent;
2324
import de.kcodeyt.plotplugin.event.PlotPreClaimEvent;
25+
import de.kcodeyt.plotplugin.lang.TranslationKey;
2426
import de.kcodeyt.plotplugin.manager.PlotManager;
2527
import de.kcodeyt.plotplugin.util.Plot;
2628

2729
public class ClaimCommand extends SubCommand {
2830

29-
public ClaimCommand(PlotPlugin plugin) {
30-
super(plugin, "claim", "c");
31+
public ClaimCommand(PlotPlugin plugin, PlotCommand parent) {
32+
super(plugin, parent, "claim", "c");
3133
}
3234

3335
@Override
3436
public boolean execute(Player player, String[] args) {
3537
final PlotManager plotManager = this.plugin.getPlotManager(player.getLevel());
3638
final Plot plot;
3739
if(plotManager == null || (plot = plotManager.getMergedPlot(player.getFloorX(), player.getFloorZ())) == null) {
38-
player.sendMessage(this.translate("no-plot"));
40+
player.sendMessage(this.translate(player, TranslationKey.NO_PLOT));
3941
return false;
4042
}
4143

@@ -53,11 +55,11 @@ public boolean execute(Player player, String[] args) {
5355
final PlotClaimEvent plotClaimEvent = new PlotClaimEvent(player, plot, false);
5456
this.plugin.getServer().getPluginManager().callEvent(plotClaimEvent);
5557

56-
player.sendMessage(this.translate("claim-success"));
58+
player.sendMessage(this.translate(player, TranslationKey.CLAIM_SUCCESS));
5759
});
5860
return true;
5961
} else {
60-
player.sendMessage(this.translate("claim-failure"));
62+
player.sendMessage(this.translate(player, TranslationKey.CLAIM_FAILURE));
6163
return false;
6264
}
6365
}

0 commit comments

Comments
 (0)