feat: revamp i18n key feature

This commit is contained in:
aroooo 2022-11-04 16:52:15 +01:00
parent 54671b62e5
commit 522da7369a
9 changed files with 56 additions and 58 deletions

View file

@ -30,24 +30,22 @@ public class NickoCommand implements CommandExecutor {
@Override @Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (args.length >= 1) { if (sender instanceof Player player) {
switch (args[0]) { if (args.length >= 1) {
case "disguise" -> new NickoDisguiseSubCmd(this).execute(sender); switch (args[0]) {
case "debug" -> new NickoDebugSubCmd(this).execute(sender, args); case "disguise" -> new NickoDisguiseSubCmd(this).execute(sender);
case "check" -> new NickoCheckSubCmd(this).execute(sender, args); case "debug" -> new NickoDebugSubCmd(this).execute(sender, args);
case "gui" -> new NickoGUISubCmd(this).execute(sender, args); case "check" -> new NickoCheckSubCmd(this).execute(player, args);
default -> sendHelpMessages(sender); case "gui" -> new NickoGUISubCmd(this).execute(sender, args);
} default -> sendHelpMessages(sender);
} else { }
if (sender instanceof Player player) {
new MainGUI(player).open();
return false;
} }
sender.sendMessage("The GUI can only be opened in-game."); new MainGUI(player).open();
return false; return false;
} }
sender.sendMessage("The GUI can only be opened in-game.");
return false; return false;
} }

View file

@ -3,9 +3,11 @@ package net.artelnatif.nicko.command.sub;
import net.artelnatif.nicko.NickoBukkit; import net.artelnatif.nicko.NickoBukkit;
import net.artelnatif.nicko.command.NickoCommand; import net.artelnatif.nicko.command.NickoCommand;
import net.artelnatif.nicko.disguise.AppearanceManager; import net.artelnatif.nicko.disguise.AppearanceManager;
import net.artelnatif.nicko.i18n.I18N;
import net.artelnatif.nicko.i18n.I18NDict;
import net.artelnatif.nicko.mojang.MojangUtils;
import net.artelnatif.nicko.utils.PlayerUtils; import net.artelnatif.nicko.utils.PlayerUtils;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import java.util.StringJoiner; import java.util.StringJoiner;
@ -15,11 +17,16 @@ public class NickoCheckSubCmd extends NickoSubCmd {
super(nickoCommand); super(nickoCommand);
} }
public void execute(CommandSender sender, String[] args) { public void execute(Player player, String[] args) {
final String targetName = args[1]; final String targetName = args[1];
final Player target = Bukkit.getPlayerExact(targetName); final Player target = Bukkit.getPlayerExact(targetName);
AppearanceManager appearanceManager; AppearanceManager appearanceManager;
if (MojangUtils.isUsernameInvalid(targetName)) {
player.sendMessage(I18N.translate(player, I18NDict.Error.INVALID_USERNAME));
return;
}
if (PlayerUtils.isPlayerOffline(target)) { if (PlayerUtils.isPlayerOffline(target)) {
appearanceManager = AppearanceManager.get(targetName); appearanceManager = AppearanceManager.get(targetName);
} else { } else {
@ -36,6 +43,6 @@ public class NickoCheckSubCmd extends NickoSubCmd {
builder.add("§7- §fSkin: §6" + appearanceManager.getSkin()); builder.add("§7- §fSkin: §6" + appearanceManager.getSkin());
} }
sender.sendMessage(builder.toString()); player.sendMessage(builder.toString());
} }
} }

View file

@ -3,10 +3,10 @@ package net.artelnatif.nicko.disguise;
import net.artelnatif.nicko.i18n.I18NDict; import net.artelnatif.nicko.i18n.I18NDict;
public class UpdateResult { public class UpdateResult {
private final I18NDict.Error errorMessage; private final I18NDict errorMessage;
private boolean error = false; private boolean error = false;
public UpdateResult(I18NDict.Error errorMessage) { public UpdateResult(I18NDict errorMessage) {
this.error = true; this.error = true;
this.errorMessage = errorMessage; this.errorMessage = errorMessage;
} }
@ -19,7 +19,7 @@ public class UpdateResult {
return error; return error;
} }
public I18NDict.Error getErrorMessage() { public I18NDict getErrorMessage() {
return errorMessage; return errorMessage;
} }
} }

View file

@ -23,9 +23,9 @@ public class PlayerJoinListener implements Listener {
final boolean skinChange = !player.getName().equals(appearanceManager.getSkin()); final boolean skinChange = !player.getName().equals(appearanceManager.getSkin());
final UpdateResult updateResult = appearanceManager.updatePlayer(skinChange); final UpdateResult updateResult = appearanceManager.updatePlayer(skinChange);
if (updateResult.isError()) { if (updateResult.isError()) {
player.sendMessage(I18N.translate(player, I18NDict.Event.PREVIOUS_SKIN_APPLY_FAIL.getKey(), I18N.translate(player, updateResult.getErrorMessage().getKey()))); player.sendMessage(I18N.translate(player, I18NDict.Event.PREVIOUS_SKIN_APPLY_FAIL, I18N.translate(player, updateResult.getErrorMessage())));
} else { } else {
player.sendMessage(I18N.translate(player, I18NDict.Event.PREVIOUS_SKIN_APPLIED.getKey())); player.sendMessage(I18N.translate(player, I18NDict.Event.PREVIOUS_SKIN_APPLIED));
} }
} }
}, 20L); }, 20L);

View file

@ -27,15 +27,15 @@ public class ResetItem extends BaseItem {
final AppearanceManager appearanceManager = AppearanceManager.get(player); final AppearanceManager appearanceManager = AppearanceManager.get(player);
if (!appearanceManager.hasData()) { if (!appearanceManager.hasData()) {
player.sendMessage(I18N.translate(player, I18NDict.Event.UNDISGUISE_NOTACTIVE.getKey())); player.sendMessage(I18N.translate(player, I18NDict.Event.UNDISGUISE_NOTACTIVE));
event.getView().close(); event.getView().close();
return; return;
} }
if (!appearanceManager.reset().isError()) { if (!appearanceManager.reset().isError()) {
player.sendMessage(I18N.translate(player, I18NDict.Event.UNDISGUISE_SUCCESS.getKey())); player.sendMessage(I18N.translate(player, I18NDict.Event.UNDISGUISE_SUCCESS));
} else { } else {
player.sendMessage(I18N.translate(player, I18NDict.Event.UNDISGUISE_FAIL.getKey())); player.sendMessage(I18N.translate(player, I18NDict.Event.UNDISGUISE_FAIL));
} }
} }
} }

View file

@ -26,9 +26,9 @@ public class I18N {
return ResourceBundle.getBundle("locale", getLocale(player)); return ResourceBundle.getBundle("locale", getLocale(player));
} }
public static String translate(Player player, String key, Object... arguments) { public static String translate(Player player, I18NDict key, Object... arguments) {
try { try {
formatter.applyPattern(getBundle(player).getString(key)); formatter.applyPattern(getBundle(player).getString(key.getKey()));
return NickoBukkit.getInstance().getNickoConfig().getPrefix() + formatter.format(arguments); return NickoBukkit.getInstance().getNickoConfig().getPrefix() + formatter.format(arguments);
} catch (Exception e) { } catch (Exception e) {
return NickoBukkit.getInstance().getNickoConfig().getPrefix() + key; return NickoBukkit.getInstance().getNickoConfig().getPrefix() + key;

View file

@ -1,40 +1,31 @@
package net.artelnatif.nicko.i18n; package net.artelnatif.nicko.i18n;
public class I18NDict { public class I18NDict {
public enum Event { private String key;
UNDISGUISE_SUCCESS("undisguise.success"),
UNDISGUISE_FAIL("undisguise.fail"),
UNDISGUISE_NOTACTIVE("undisguise.notactive"),
DISGUISE_SUCCESS("disguise.success"),
DISGUISE_FAIL("disguise.fail"),
PREVIOUS_SKIN_APPLIED("previous_skin_applied.success"),
PREVIOUS_SKIN_APPLY_FAIL("previous_skin_applied.fail");
private final String messageKey; public I18NDict(String key) {
this.key = key;
Event(String messageKey) {
this.messageKey = "event." + messageKey;
}
public String getKey() {
return messageKey;
}
} }
public enum Error { public static class Event {
SKIN_FAIL_MOJANG("couldnt_get_skin_from_mojang"), public static final I18NDict UNDISGUISE_SUCCESS = new I18NDict("undisguise.success");
SKIN_FAIL_CACHE("couldnt_get_skin_from_cache"), public static final I18NDict UNDISGUISE_FAIL = new I18NDict("undisguise.fail");
NAME_FAIL_MOJANG("couldnt_get_name_from_mojang"), public static final I18NDict UNDISGUISE_NOTACTIVE = new I18NDict("undisguise.notactive");
UNEXPECTED_ERROR("generic"); public static final I18NDict DISGUISE_SUCCESS = new I18NDict("disguise.success");
public static final I18NDict DISGUISE_FAIL = new I18NDict("disguise.fail");
public static final I18NDict PREVIOUS_SKIN_APPLIED = new I18NDict("previous_skin_applied.success");
public static final I18NDict PREVIOUS_SKIN_APPLY_FAIL = new I18NDict("previous_skin_applied.fail");
}
private final String messageKey; public static class Error {
public static final I18NDict SKIN_FAIL_MOJANG = new I18NDict("couldnt_get_skin_from_mojang");
public static final I18NDict SKIN_FAIL_CACHE = new I18NDict("couldnt_get_skin_from_cache");
public static final I18NDict NAME_FAIL_MOJANG = new I18NDict("couldnt_get_name_from_mojang");
public static final I18NDict INVALID_USERNAME = new I18NDict("invalid_username");
public static final I18NDict UNEXPECTED_ERROR = new I18NDict("generic");
}
Error(String messageKey) { public String getKey() {
this.messageKey = "error." + messageKey; return key;
}
public String getKey() {
return messageKey;
}
} }
} }

View file

@ -9,4 +9,5 @@ event.disguise.success=§aDisguise applied !
event.disguise.fail=§cUnable to apply your disguise. §7§o({0}) event.disguise.fail=§cUnable to apply your disguise. §7§o({0})
event.undisguise.success=§aDisguise removed. event.undisguise.success=§aDisguise removed.
event.undisguise.fail=§cUnable to remove your disguise. It will be set back to default on the next login. Sorry! event.undisguise.fail=§cUnable to remove your disguise. It will be set back to default on the next login. Sorry!
event.undisguise.notactive=§cYou do not have an active disguise. event.undisguise.notactive=§cYou do not have an active disguise.
error.invalid_username=§cThe specified username is not a valid Minecraft username.

View file

@ -9,4 +9,5 @@ event.disguise.success=§aDéguisement appliqué !
event.disguise.fail=§cImpossible d'appliquer votre déguisement. §7§o({0}) event.disguise.fail=§cImpossible d'appliquer votre déguisement. §7§o({0})
event.undisguise.success=§aDéguisement retiré. event.undisguise.success=§aDéguisement retiré.
event.undisguise.fail=§cImpossible de retier votre déguisement. Il sera remis par défaut à votre prochaine reconnexion. Désolé ! event.undisguise.fail=§cImpossible de retier votre déguisement. Il sera remis par défaut à votre prochaine reconnexion. Désolé !
event.undisguise.notactive=§cVous n'avez pas de déguisement. event.undisguise.notactive=§cVous n'avez pas de déguisement.
error.invalid_username=§cLe pseudo spécifié n'est pas un pseudo Minecraft valide.