refactor(i18n): optimizations

This commit is contained in:
ineanto 2023-12-28 21:02:09 +01:00
parent ba9f39659b
commit aa9b71bc18
19 changed files with 46 additions and 54 deletions

View file

@ -45,7 +45,7 @@ repositories {
}
dependencies {
implementation("io.papermc.paper:paper-api:1.20.4-R0.1-SNAPSHOT")
implementation("io.papermc.paper:paper-api:1.20.2-R0.1-SNAPSHOT")
implementation("com.comphenix.protocol:ProtocolLib:5.1.1-SNAPSHOT")
shadowImplementation("me.clip:placeholderapi:2.11.4")
@ -132,6 +132,8 @@ tasks {
}
runServer {
dependsOn(shadowJar)
downloadPlugins {
url("https://download.luckperms.net/1526/bukkit/loader/LuckPerms-Bukkit-5.4.113.jar")
url("https://ci.dmulloy2.net/job/ProtocolLib/lastSuccessfulBuild/artifact/build/libs/ProtocolLib.jar")

View file

@ -115,9 +115,14 @@ public class AnvilManager {
final I18N i18n = new I18N(player);
final ActionResult actionResult = appearanceManager.updatePlayer(skinChange, false);
if (!actionResult.isError()) {
player.sendMessage(i18n.translateString(I18NDict.Event.Appearance.Set.OK));
player.sendMessage(i18n.translate(I18NDict.Event.Appearance.Set.OK, true));
} else {
player.sendMessage(i18n.translateString(I18NDict.Event.Appearance.Set.ERROR, i18n.translateStringWithoutPrefix(actionResult.getErrorKey())));
player.sendMessage(
i18n.translate(
I18NDict.Event.Appearance.Set.ERROR,
true,
i18n.translate(actionResult.getErrorKey(), false)
));
}
return Collections.singletonList(AnvilGUI.ResponseAction.close());
}

View file

@ -52,9 +52,13 @@ public class PlayerJoinListener implements Listener {
final boolean needsASkinChange = profile.getSkin() != null && !profile.getSkin().equals(player.getName());
final ActionResult actionResult = appearanceManager.updatePlayer(needsASkinChange, false);
if (!actionResult.isError()) {
player.sendMessage(i18n.translateString(I18NDict.Event.Appearance.Restore.OK));
player.sendMessage(i18n.translate(I18NDict.Event.Appearance.Restore.OK, true));
} else {
player.sendMessage(i18n.translateString(I18NDict.Event.Appearance.Restore.ERROR, i18n.translateStringWithoutPrefix(actionResult.getErrorKey())));
player.sendMessage(
i18n.translate(I18NDict.Event.Appearance.Restore.ERROR,
true,
i18n.translate(actionResult.getErrorKey(), false)
));
}
}
});

View file

@ -18,7 +18,7 @@ public class AdminGUI {
public AdminGUI(Player player) {
final I18N i18n = new I18N(player);
this.title = i18n.translateStringWithoutPrefix(I18NDict.GUI.Titles.ADMIN);
this.title = i18n.translate(I18NDict.GUI.Titles.ADMIN, false);
final HomeGUI parent = new HomeGUI(player);
final GoBackItem backItem = new GoBackItem(player);

View file

@ -17,7 +17,7 @@ public class CacheManagementGUI {
public CacheManagementGUI(Player player) {
final I18N i18n = new I18N(player);
this.title = i18n.translateStringWithoutPrefix(I18NDict.GUI.Titles.CACHE);
this.title = i18n.translate(I18NDict.GUI.Titles.CACHE, false);
final AdminGUI parent = new AdminGUI(player);
final GoBackItem backItem = new GoBackItem(player);

View file

@ -22,7 +22,7 @@ public class ChoiceGUI {
final ConfirmItem confirmItem = new ConfirmItem(player, callback);
final CancelItem cancelItem = new CancelItem(player, callback);
this.title = i18n.translateStringWithoutPrefix(I18NDict.GUI.Titles.CONFIRM);
this.title = i18n.translate(I18NDict.GUI.Titles.CONFIRM, false);
this.gui = Gui.normal()
.setStructure(
"@ @ @ @ % & & & &",

View file

@ -26,7 +26,7 @@ public class HomeGUI {
}
final I18N i18n = new I18N(player);
this.title = i18n.translateStringWithoutPrefix(I18NDict.GUI.Titles.HOME);
this.title = i18n.translate(I18NDict.GUI.Titles.HOME, false);
final ExitItem exitItem = new ExitItem(player);
final ResetItem resetItem = new ResetItem(player);

View file

@ -28,7 +28,7 @@ public class InvalidateSkinGUI {
public InvalidateSkinGUI(Player player) {
final I18N i18n = new I18N(player);
this.title = i18n.translateStringWithoutPrefix(I18NDict.GUI.Titles.INVALIDATE_SKIN);
this.title = i18n.translate(I18NDict.GUI.Titles.INVALIDATE_SKIN, false);
final ConcurrentMap<String, Optional<MojangSkin>> skins = NickoBukkit.getInstance().getMojangAPI().getSkinCache().asMap();
final List<String> loadedSkins = skins.entrySet().stream()

View file

@ -27,7 +27,7 @@ public class PlayerCheckGUI {
public PlayerCheckGUI(Player player, Collection<? extends Player> players) {
final I18N i18n = new I18N(player);
this.title = i18n.translateStringWithoutPrefix(I18NDict.GUI.Titles.CHECK);
this.title = i18n.translate(I18NDict.GUI.Titles.CHECK, false);
final List<Item> items = players.stream()
.map(Entity::getUniqueId)

View file

@ -22,7 +22,7 @@ public class SettingsGUI {
};
final I18N i18n = new I18N(player);
this.title = i18n.translateStringWithoutPrefix(I18NDict.GUI.Titles.SETTINGS);
this.title = i18n.translate(I18NDict.GUI.Titles.SETTINGS, false);
final HomeGUI parent = new HomeGUI(player);
final LanguageCyclingItem languageItem = new LanguageCyclingItem(player);

View file

@ -29,7 +29,9 @@ public class CacheEntryItem extends AsyncItem {
public CacheEntryItem(I18N i18n, String uuid) {
super(new ItemBuilder(Material.PAINTING)
.setDisplayName(Component.text(i18n.translateStringWithoutPrefix(I18NDict.GUI.LOADING)).content()),
.setDisplayName(
Component.text(i18n.translate(I18NDict.GUI.LOADING, false)).content()
),
() -> {
final String dashedUuid = uuid.replaceAll("(.{8})(.{4})(.{4})(.{4})(.+)", "$1-$2-$3-$4-$5");
final UUID uuidObject = UUID.fromString(dashedUuid);
@ -53,7 +55,7 @@ public class CacheEntryItem extends AsyncItem {
@Override
public void onConfirm() {
final I18N i18n = new I18N(player);
player.sendMessage(i18n.translateString(I18NDict.Event.Admin.Cache.INVALIDATE_ENTRY, name));
player.sendMessage(i18n.translate(I18NDict.Event.Admin.Cache.INVALIDATE_ENTRY, true, name));
mojangAPI.eraseFromCache(uuid);
}

View file

@ -78,7 +78,7 @@ public class PlayerInformationItem extends AsyncItem {
public void onConfirm() {
final AppearanceManager appearanceManager = new AppearanceManager(target);
appearanceManager.reset();
player.sendMessage(i18n.translateString(I18NDict.Event.Admin.Check.REMOVE_SKIN, target.getName()));
player.sendMessage(i18n.translate(I18NDict.Event.Admin.Check.REMOVE_SKIN, true, target.getName()));
}
@Override

View file

@ -23,7 +23,7 @@ public class ScrollDownItem extends ScrollItem {
@Override
public ItemProvider getItemProvider(ScrollGui gui) {
final ItemBuilder builder = new ItemBuilder(Material.GREEN_STAINED_GLASS_PANE);
final Translation translation = i18n.translate(I18NDict.GUI.SCROLL_DOWN);
final Translation translation = i18n.translateAndReplace(I18NDict.GUI.SCROLL_DOWN);
builder.setDisplayName(Component.text(translation.name()).content());
if (!gui.canScroll(1)) {
// Lore serialization

View file

@ -23,7 +23,7 @@ public class ScrollUpItem extends ScrollItem {
@Override
public ItemProvider getItemProvider(ScrollGui gui) {
final ItemBuilder builder = new ItemBuilder(Material.RED_STAINED_GLASS_PANE);
final Translation translation = i18n.translate(I18NDict.GUI.SCROLL_UP);
final Translation translation = i18n.translateAndReplace(I18NDict.GUI.SCROLL_UP);
builder.setDisplayName(Component.text(translation.name()).content());
if (!gui.canScroll(-1)) {
// Lore serialization

View file

@ -59,8 +59,8 @@ public class LanguageCyclingItem {
private ItemProvider generateItem(Locale locale, List<Locale> locales) {
final ItemBuilder builder = new ItemBuilder(Material.OAK_SIGN);
final Translation translation = i18n.translate(I18NDict.GUI.Settings.LANGUAGE);
final Translation cyclingChoicesTranslation = i18n.translate(I18NDict.GUI.Settings.CYCLING_CHOICES);
final Translation translation = i18n.translateAndReplace(I18NDict.GUI.Settings.LANGUAGE);
final Translation cyclingChoicesTranslation = i18n.translateAndReplace(I18NDict.GUI.Settings.CYCLING_CHOICES);
builder.setDisplayName(Component.text(translation.name()).content());
for (Locale value : locales) {

View file

@ -55,12 +55,12 @@ public class RandomSkinCyclingItem {
private ItemProvider getItemProviderForValue(boolean enabled) {
final SkullBuilder.HeadTexture texture = new SkullBuilder.HeadTexture("eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvYzgzMTEzOGMyMDYxMWQzMDJjNDIzZmEzMjM3MWE3NDNkMTc0MzdhMTg5NzNjMzUxOTczNDQ3MGE3YWJiNCJ9fX0=");
final SkullBuilder builder = new SkullBuilder(texture);
final Translation randomSkinTranslation = i18n.translate(I18NDict.GUI.Settings.RANDOM_SKIN);
final Translation toggleableTranslation = i18n.translate(I18NDict.GUI.Settings.TOGGLEABLE_BUTTON,
final Translation randomSkinTranslation = i18n.translateAndReplace(I18NDict.GUI.Settings.RANDOM_SKIN);
final Translation toggleableTranslation = i18n.translateAndReplace(I18NDict.GUI.Settings.TOGGLEABLE_BUTTON,
(enabled ? "§7>§c" : "§6§l>§c§l"),
(enabled ? "§6§l>§a§l" : "§7>§a")
);
final Translation cyclingChoicesTranslation = i18n.translate(I18NDict.GUI.Settings.CYCLING_CHOICES);
final Translation cyclingChoicesTranslation = i18n.translateAndReplace(I18NDict.GUI.Settings.CYCLING_CHOICES);
builder.setDisplayName(randomSkinTranslation.name());
toggleableTranslation.lore().forEach(builder::addLoreLines);

View file

@ -22,23 +22,21 @@ public class I18N {
private final NickoBukkit instance = NickoBukkit.getInstance();
private final Pattern replacementPattern = Pattern.compile("(?ms)\\{\\d+}");
private final YamlConfig yamlConfig;
private final Player player;
private final Locale playerLocale;
public I18N(Player player) {
this.player = player;
this.playerLocale = getPlayerLocale();
final Optional<NickoProfile> optionalProfile = NickoProfile.get(player);
this.playerLocale = optionalProfile.map(NickoProfile::getLocale).orElse(Locale.ENGLISH);
this.yamlConfig = getYamlConfig();
}
public I18N(Locale locale) {
this.player = null;
this.playerLocale = locale;
this.yamlConfig = getYamlConfig();
}
public AbstractItemBuilder<?> translateItem(AbstractItemBuilder<?> item, String key, Object... args) {
final Translation translation = translate(key, args);
final Translation translation = translateAndReplace(key, args);
// Name serialization
final Component deserializedName = MiniMessage.miniMessage().deserialize(translation.name());
@ -55,7 +53,7 @@ public class I18N {
return item;
}
public Translation translate(String key, Object... args) {
public Translation translateAndReplace(String key, Object... args) {
final String nameKey = key + ".name";
final String loreKey = key + ".lore";
final String name = readString(nameKey);
@ -115,23 +113,13 @@ public class I18N {
}
}
public String translateString(String key, Object... arguments) {
public String translate(String key, boolean prefix, Object... arguments) {
final String translation = readString(key);
try {
formatter.applyPattern(translation);
return instance.getNickoConfig().getPrefix() + formatter.format(arguments);
return (prefix ? instance.getNickoConfig().getPrefix() : "") + formatter.format(arguments);
} catch (Exception e) {
return instance.getNickoConfig().getPrefix() + key;
}
}
public String translateStringWithoutPrefix(String key, Object... arguments) {
final String translation = readString(key);
try {
formatter.applyPattern(translation);
return formatter.format(arguments);
} catch (Exception e) {
return key;
return (prefix ? instance.getNickoConfig().getPrefix() : "") + key;
}
}
@ -151,13 +139,4 @@ public class I18N {
return new YamlConfig(resource);
}
}
public Locale getPlayerLocale() {
final Optional<NickoProfile> optionalProfile = NickoProfile.get(player);
if (optionalProfile.isPresent()) {
return optionalProfile.get().getLocale();
} else {
return Locale.ENGLISH;
}
}
}

View file

@ -29,7 +29,7 @@ public class ItemTranslationTest {
@DisplayName("Translate Item Without Lore")
public void translateItemTranslationWithoutLore() {
final I18N i18n = new I18N(Locale.FRENCH);
final Translation translation = i18n.translate(I18NDict.GUI.GO_BACK);
final Translation translation = i18n.translateAndReplace(I18NDict.GUI.GO_BACK);
assertTrue(translation.lore().isEmpty());
assertEquals(translation.name(), "Retour");
}
@ -39,10 +39,10 @@ public class ItemTranslationTest {
public void translateItemLore() {
final I18N i18n = new I18N(Locale.FRENCH);
final Translation test = i18n.translate(I18NDict.GUI.Settings.TOGGLEABLE_BUTTON, "EST", "EST");
final Translation test = i18n.translateAndReplace(I18NDict.GUI.Settings.TOGGLEABLE_BUTTON, "EST", "EST");
test.lore().forEach(System.out::println);
final Translation translation = i18n.translate(I18NDict.GUI.Admin.Cache.STATISTICS, "1", "1");
final Translation translation = i18n.translateAndReplace(I18NDict.GUI.Admin.Cache.STATISTICS, "1", "1");
assertFalse(translation.lore().isEmpty());
assertEquals("Nombre de requêtes: <aqua>1</aqua>", translation.lore().get(0));
assertEquals("Nb. de skin dans le cache: <aqua>1</aqua>", translation.lore().get(1));

View file

@ -25,7 +25,7 @@ public class TranslationTest {
@DisplayName("Translate Line With Replacement")
public void translateItemTranslationWithoutLore() {
final I18N i18n = new I18N(Locale.FRENCH);
final String translation = i18n.translateStringWithoutPrefix(I18NDict.Event.Settings.ERROR, "Test");
final String translation = i18n.translate(I18NDict.Event.Settings.ERROR, false, "Test");
assertEquals("§cImpossible de mettre à jour vos paramètres. §7§o(Test)", translation);
}