Skip to content

Commit e8f95dd

Browse files
committed
Added LanguageManager
1 parent 4bb0e6d commit e8f95dd

8 files changed

Lines changed: 415 additions & 71 deletions

File tree

src/main/java/command/CommandCentral.java

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ public boolean execute(Profile player, Command bukkitCmd, String commandInput, S
185185
: (bukkitCmd != null ? bukkitCmd.getName() : null);
186186

187187
if (usedLabel == null || usedLabel.isEmpty()) {
188-
String msg = language.getFormatted(
188+
String msg = language.getString(
189189
KEY_UNKNOWN_COMMAND,
190190
FMT_UNKNOWN_COMMAND,
191191
""
@@ -196,7 +196,7 @@ public boolean execute(Profile player, Command bukkitCmd, String commandInput, S
196196

197197
CommandRegistry cmd = getCommand(usedLabel).orElse(null);
198198
if (cmd == null) {
199-
String msg = language.getFormatted(
199+
String msg = language.getString(
200200
KEY_UNKNOWN_COMMAND,
201201
FMT_UNKNOWN_COMMAND,
202202
usedLabel
@@ -207,7 +207,7 @@ public boolean execute(Profile player, Command bukkitCmd, String commandInput, S
207207

208208
if (!isCommandVisible(player, cmd)) {
209209
// Intentionally pretend the command does not exist for this group
210-
String msg = language.getFormatted(
210+
String msg = language.getString(
211211
KEY_UNKNOWN_COMMAND,
212212
FMT_UNKNOWN_COMMAND,
213213
usedLabel
@@ -220,7 +220,7 @@ public boolean execute(Profile player, Command bukkitCmd, String commandInput, S
220220
if (!isModuleEnabled(cmd.getModuleName())) {
221221
player.msg(
222222
NamedTextColor.RED,
223-
language.getFormatted(
223+
language.getString(
224224
KEY_MODULE_DISABLED,
225225
FMT_MODULE_DISABLED,
226226
usedLabel.toLowerCase(Locale.ROOT)
@@ -233,7 +233,7 @@ public boolean execute(Profile player, Command bukkitCmd, String commandInput, S
233233
if (!cmd.isEnabled()) {
234234
player.msg(
235235
NamedTextColor.RED,
236-
language.getFormatted(
236+
language.getString(
237237
KEY_COMMAND_DISABLED,
238238
FMT_COMMAND_DISABLED,
239239
usedLabel.toLowerCase(Locale.ROOT)
@@ -244,7 +244,7 @@ public boolean execute(Profile player, Command bukkitCmd, String commandInput, S
244244

245245
// Permission check
246246
if (cmd.hasPermissionNode() && !player.hasPermission(cmd.getPermissionNode())) {
247-
String noPerm = language.get(KEY_NO_PERMISSION, MSG_NO_PERMISSION);
247+
String noPerm = language.getString(KEY_NO_PERMISSION, MSG_NO_PERMISSION);
248248
player.msg(NamedTextColor.RED, noPerm);
249249
return true;
250250
}
@@ -257,7 +257,7 @@ public boolean execute(Profile player, Command bukkitCmd, String commandInput, S
257257

258258
player.msg(
259259
NamedTextColor.RED,
260-
language.getFormatted(
260+
language.getString(
261261
KEY_INSUFFICIENT_FUNDS,
262262
FMT_INSUFFICIENT_FUNDS,
263263
formatted,
@@ -272,7 +272,7 @@ public boolean execute(Profile player, Command bukkitCmd, String commandInput, S
272272
double remaining = cooldownManager.getRemaining(player.getUuid(), cmd);
273273
player.msg(
274274
NamedTextColor.RED,
275-
language.getFormatted(
275+
language.getString(
276276
KEY_COOLDOWN_ACTIVE,
277277
FMT_COOLDOWN_ACTIVE,
278278
remaining
@@ -310,7 +310,7 @@ public boolean execute(Profile player, Command bukkitCmd, String commandInput, S
310310
e
311311
);
312312

313-
String msg = language.get(KEY_UNAVAILABLE, MSG_UNAVAILABLE);
313+
String msg = language.getString(KEY_UNAVAILABLE, MSG_UNAVAILABLE);
314314
player.msg(NamedTextColor.RED, msg);
315315
return true;
316316
}
@@ -372,14 +372,6 @@ private double getEffectiveCost(CommandRegistry cmd) {
372372
return cost;
373373
}
374374

375-
/**
376-
* Checks if a player has sufficient funds for a command.
377-
*
378-
* @param player the profile to check
379-
* @param cmd the command with associated cost
380-
* @return {@code true} if the player can afford the command (or no cost is defined),
381-
* {@code false} otherwise
382-
*/
383375
/**
384376
* Checks if a player has sufficient funds for a command.
385377
*
@@ -492,7 +484,7 @@ private void handleCommandException(Profile player, CommandRegistry cmd, Command
492484
switch (type) {
493485
case SYNTAX_ERROR: {
494486
// "Error: <message>"
495-
String errorPrefix = language.getFormatted(
487+
String errorPrefix = language.getString(
496488
KEY_COMMAND_ERROR,
497489
FMT_COMMAND_ERROR,
498490
e.getMessage()
@@ -501,7 +493,7 @@ private void handleCommandException(Profile player, CommandRegistry cmd, Command
501493

502494
// Optional usage line
503495
if (cmd.getSyntax() != null && !cmd.getSyntax().isEmpty()) {
504-
String usage = language.getFormatted(
496+
String usage = language.getString(
505497
KEY_SYNTAX_USAGE,
506498
FMT_SYNTAX_USAGE,
507499
cmd.getSyntax()
@@ -512,26 +504,26 @@ private void handleCommandException(Profile player, CommandRegistry cmd, Command
512504
}
513505

514506
case PERMISSION_ERROR: {
515-
String noPerm = language.get(KEY_NO_PERMISSION, MSG_NO_PERMISSION);
507+
String noPerm = language.getString(KEY_NO_PERMISSION, MSG_NO_PERMISSION);
516508
player.msg(NamedTextColor.RED, noPerm);
517509
break;
518510
}
519511

520512
case SUBCOMMAND_PERMISSION: {
521-
String noSubPerm = language.get(KEY_NO_SUB_PERMISSION, MSG_NO_SUB_PERMISSION);
513+
String noSubPerm = language.getString(KEY_NO_SUB_PERMISSION, MSG_NO_SUB_PERMISSION);
522514
player.msg(NamedTextColor.RED, noSubPerm);
523515
break;
524516
}
525517

526518
case UNAVAILABLE_ERROR: {
527-
String unavailable = language.get(KEY_UNAVAILABLE, MSG_UNAVAILABLE);
519+
String unavailable = language.getString(KEY_UNAVAILABLE, MSG_UNAVAILABLE);
528520
player.msg(NamedTextColor.RED, unavailable);
529521
break;
530522
}
531523

532524
case GENERAL_ERROR:
533525
default: {
534-
String msg = language.getFormatted(
526+
String msg = language.getString(
535527
KEY_COMMAND_ERROR,
536528
FMT_COMMAND_ERROR,
537529
e.getMessage()

src/main/java/main/Main.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import managers.ConfigManager;
1313
import managers.LanguageManager;
1414
import managers.ModuleManager;
15-
import managers.SimpleLanguageManager;
15+
import managers.YamlLanguageManager;
1616
import playerdata.Profile;
1717
import playerdata.ProfileManager;
1818
import playerdata.ProfileStorage;
@@ -42,7 +42,7 @@ public class Main extends JavaPlugin
4242
/** Handles enabling, disabling and monitoring of plugin modules. */
4343
private ModuleManager moduleManager;
4444

45-
private SimpleLanguageManager languageManager;
45+
private LanguageManager languageManager;
4646

4747
private ConfigManager mainConfig;
4848

@@ -81,7 +81,7 @@ public void onEnable() {
8181
ProfileStorage storage = new ProfileStorage(databaseAccess, this.getLogger());
8282
profileManager = new ProfileManager(storage);
8383

84-
languageManager = new SimpleLanguageManager();
84+
languageManager = new YamlLanguageManager(getLogger());
8585
moduleManager = new ModuleManager(this, database);
8686
commandCentral = new CommandCentral(this, database, languageManager);
8787

Lines changed: 52 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,70 @@
11
package managers;
22

3+
import net.kyori.adventure.text.Component;
34
import org.jetbrains.annotations.NotNull;
45

56
/**
6-
* Provides access to localized messages from config or resource files.
7-
* Implementations should be safe to call frequently from the main thread.
7+
* Per-scope language accessor (e.g. per module).
8+
*
9+
* Implementations are expected to:
10+
* - load messages from configuration (YAML, etc.)
11+
* - cache them in memory
12+
* - return Components with colors already parsed
13+
*
14+
* Keys are treated as case-insensitive.
815
*/
916
public interface LanguageManager {
1017

1118
/**
12-
* Gets a message by key, or the fallback if not found.
19+
* Maximum allowed length of a single message.
20+
* Longer values are treated as invalid and replaced by a placeholder.
21+
*/
22+
int MAX_MESSAGE_LENGTH = 512;
23+
24+
/**
25+
* Gets a localized message as a Component by key.
26+
*
27+
* @param key language key (e.g. "error_warp_not_found")
28+
* @return localized Component; never {@code null}.
29+
* Missing/invalid keys return a red %KEY% placeholder.
30+
*/
31+
@NotNull
32+
Component get(@NotNull String key);
33+
34+
/**
35+
* Gets a localized message as a Component and applies positional placeholders.
1336
*
14-
* @param key Language key (e.g. "command.no_permission")
15-
* @param fallback Fallback value if the key is missing
16-
* @return Localized text or fallback
37+
* Placeholders:
38+
* %1, %2, ..., %N
39+
*
40+
* Example:
41+
* lang.yml: error_warp_not_found: "&7Error: &cWe cant find warp &f%1&7."
42+
* lang.get("error_warp_not_found", warpName);
43+
*
44+
* @param key language key
45+
* @param arguments positional arguments (%1..%N)
46+
* @return localized Component; never {@code null}
47+
*/
48+
@NotNull
49+
Component get(@NotNull String key, Object... arguments);
50+
51+
/**
52+
* Plain-text version of the message with colors stripped.
53+
*
54+
* @param key language key
55+
* @return plain text; never {@code null}
1756
*/
1857
@NotNull
19-
String get(@NotNull String key, @NotNull String fallback);
58+
String getString(@NotNull String key);
2059

2160
/**
22-
* Gets and formats a message by key, or uses the fallback format string.
23-
* Arguments are passed to String.format (or similar) internally.
61+
* Plain-text version with positional arguments applied.
2462
*
25-
* @param key Language key
26-
* @param fallback Fallback format string
27-
* @param arguments Arguments to interpolate
28-
* @return Localized and formatted message
63+
* @param key language key
64+
* @param arguments positional arguments
65+
* @return plain text; never {@code null}
2966
*/
3067
@NotNull
31-
String getFormatted(@NotNull String key, @NotNull String fallback, Object... arguments);
32-
68+
String getString(@NotNull String key, Object... arguments);
69+
3370
}

src/main/java/managers/SimpleLanguageManager.java

Lines changed: 0 additions & 30 deletions
This file was deleted.

0 commit comments

Comments
 (0)