feat(i18n): cache admin

This commit is contained in:
ineanto 2023-09-11 12:18:43 +02:00
parent b24202e8ab
commit 597ed23dbc
10 changed files with 131 additions and 39 deletions

View file

@ -3,30 +3,39 @@ package xyz.atnrch.nicko.gui;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import xyz.atnrch.nicko.gui.items.admin.cache.CacheStatisticsItem; import xyz.atnrch.nicko.gui.items.admin.cache.CacheStatisticsItem;
import xyz.atnrch.nicko.gui.items.admin.cache.InvalidateCacheItem; import xyz.atnrch.nicko.gui.items.admin.cache.InvalidateCacheItem;
import xyz.atnrch.nicko.gui.items.admin.cache.InvalidateEntryItem; import xyz.atnrch.nicko.gui.items.admin.cache.InvalidateSkinItem;
import xyz.atnrch.nicko.gui.items.common.GoBackItem; import xyz.atnrch.nicko.gui.items.common.GoBackItem;
import xyz.atnrch.nicko.i18n.I18N;
import xyz.atnrch.nicko.i18n.I18NDict;
import xyz.xenondevs.invui.gui.Gui; import xyz.xenondevs.invui.gui.Gui;
import xyz.xenondevs.invui.window.Window; import xyz.xenondevs.invui.window.Window;
public class CacheManagementGUI { public class CacheManagementGUI {
private final String title = "Nicko > Admin... > Cache";
private final Player player; private final Player player;
private final Gui gui; private final Gui gui;
private final String title;
public CacheManagementGUI(Player player) { public CacheManagementGUI(Player player) {
final I18N i18n = new I18N(player);
this.title = i18n.translatePrefixless(I18NDict.GUI.Admin.Cache.TITLE);
final AdminGUI parent = new AdminGUI(player); final AdminGUI parent = new AdminGUI(player);
final GoBackItem backItem = new GoBackItem(player); final GoBackItem backItem = new GoBackItem(player);
final CacheStatisticsItem cacheStatisticsItem = new CacheStatisticsItem(player);
final InvalidateCacheItem invalidateCacheItem = new InvalidateCacheItem(player);
final InvalidateSkinItem invalidateSkinItem = new InvalidateSkinItem(player);
this.gui = Gui.normal() this.gui = Gui.normal()
.setStructure( .setStructure(
"# # # # # # # # #", "# # # # # # # # #",
"# # # S A D # # #", "# # # S C E # # #",
"B # # # # # # # #" "B # # # # # # # #"
) )
.addIngredient('B', backItem.get(parent.getGUI(), parent.getTitle())) .addIngredient('B', backItem.get(parent.getGUI(), parent.getTitle()))
.addIngredient('S', new CacheStatisticsItem()) .addIngredient('S', cacheStatisticsItem.get())
.addIngredient('A', new InvalidateCacheItem()) .addIngredient('C', invalidateCacheItem.get())
.addIngredient('D', new InvalidateEntryItem()) .addIngredient('E', invalidateSkinItem.get())
.build(); .build();
this.player = player; this.player = player;
} }

View file

@ -2,7 +2,11 @@ package xyz.atnrch.nicko.gui.items.admin.cache;
import com.google.common.cache.CacheStats; import com.google.common.cache.CacheStats;
import com.google.common.cache.LoadingCache; import com.google.common.cache.LoadingCache;
import org.bukkit.entity.Player;
import xyz.atnrch.nicko.NickoBukkit; import xyz.atnrch.nicko.NickoBukkit;
import xyz.atnrch.nicko.i18n.I18N;
import xyz.atnrch.nicko.i18n.I18NDict;
import xyz.atnrch.nicko.i18n.ItemTranslation;
import xyz.atnrch.nicko.mojang.MojangSkin; import xyz.atnrch.nicko.mojang.MojangSkin;
import org.bukkit.Material; import org.bukkit.Material;
import xyz.xenondevs.invui.item.builder.ItemBuilder; import xyz.xenondevs.invui.item.builder.ItemBuilder;
@ -10,17 +14,27 @@ import xyz.xenondevs.invui.item.impl.SuppliedItem;
import java.util.Optional; import java.util.Optional;
public class CacheStatisticsItem extends SuppliedItem { public class CacheStatisticsItem {
public CacheStatisticsItem() { private final I18N i18n;
super(() -> {
public CacheStatisticsItem(Player player) {
this.i18n = new I18N(player);
}
public SuppliedItem get() {
return new SuppliedItem(() -> {
final ItemBuilder builder = new ItemBuilder(Material.BOOK); final ItemBuilder builder = new ItemBuilder(Material.BOOK);
final LoadingCache<String, Optional<MojangSkin>> cache = NickoBukkit.getInstance().getMojangAPI().getSkinCache(); final LoadingCache<String, Optional<MojangSkin>> cache = NickoBukkit.getInstance().getMojangAPI().getSkinCache();
final CacheStats stats = cache.stats(); final CacheStats stats = cache.stats();
builder.setDisplayName("Statistics");
builder.addLoreLines( final ItemTranslation translation = i18n.translateItem(I18NDict.GUI.Admin.Cache.STATISTICS,
"Request Count: §b" + stats.requestCount(), stats.requestCount(),
"Skin Cached: §b" + Math.round(cache.size()), Math.round(cache.size())
"§8§oCache is cleared every 24 hours."); );
translation.getLore().forEach(System.out::println);
// TODO (Ineanto, 9/11/23): This doesn't work.
builder.setDisplayName(translation.getName());
translation.getLore().forEach(builder::addLoreLines);
return builder; return builder;
}, (event) -> true); }, (event) -> true);
} }

View file

@ -6,18 +6,23 @@ import xyz.atnrch.nicko.i18n.I18NDict;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.inventory.ClickType; import org.bukkit.event.inventory.ClickType;
import xyz.atnrch.nicko.i18n.ItemTranslation;
import xyz.xenondevs.invui.item.builder.ItemBuilder; import xyz.xenondevs.invui.item.builder.ItemBuilder;
import xyz.xenondevs.invui.item.impl.SuppliedItem; import xyz.xenondevs.invui.item.impl.SuppliedItem;
public class InvalidateCacheItem extends SuppliedItem { public class InvalidateCacheItem {
public InvalidateCacheItem() { private final I18N i18n;
super(() -> {
public InvalidateCacheItem(Player player) {
this.i18n = new I18N(player);
}
public SuppliedItem get() {
return new SuppliedItem(() -> {
final ItemBuilder builder = new ItemBuilder(Material.TNT); final ItemBuilder builder = new ItemBuilder(Material.TNT);
builder.setDisplayName("Invalidate cache"); final ItemTranslation translation = i18n.translateItem(I18NDict.GUI.Admin.Cache.INVALIDATE_CACHE);
builder.addLoreLines( builder.setDisplayName(translation.getName());
"§c§oNOT RECOMMENDED", translation.getLore().forEach(builder::addLoreLines);
"§7Invalidates every skin entry present in the cache.",
"§7Does not reset player disguises.");
return builder; return builder;
}, (click) -> { }, (click) -> {
final ClickType clickType = click.getClickType(); final ClickType clickType = click.getClickType();

View file

@ -1,19 +1,28 @@
package xyz.atnrch.nicko.gui.items.admin.cache; package xyz.atnrch.nicko.gui.items.admin.cache;
import xyz.atnrch.nicko.gui.CacheDetailedGUI;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.ClickType; import org.bukkit.event.inventory.ClickType;
import xyz.atnrch.nicko.gui.CacheDetailedGUI;
import xyz.atnrch.nicko.i18n.I18N;
import xyz.atnrch.nicko.i18n.I18NDict;
import xyz.atnrch.nicko.i18n.ItemTranslation;
import xyz.xenondevs.invui.item.builder.ItemBuilder; import xyz.xenondevs.invui.item.builder.ItemBuilder;
import xyz.xenondevs.invui.item.impl.SuppliedItem; import xyz.xenondevs.invui.item.impl.SuppliedItem;
public class InvalidateEntryItem extends SuppliedItem { public class InvalidateSkinItem {
public InvalidateEntryItem() { private final I18N i18n;
super(() -> {
public InvalidateSkinItem(Player player) {
this.i18n = new I18N(player);
}
public SuppliedItem get() {
return new SuppliedItem(() -> {
final ItemBuilder builder = new ItemBuilder(Material.PAPER); final ItemBuilder builder = new ItemBuilder(Material.PAPER);
builder.setDisplayName("Invalidate specific entry"); final ItemTranslation translation = i18n.translateItem(I18NDict.GUI.Admin.Cache.INVALIDATE_SKIN);
builder.addLoreLines("§7Select a specific skin to invalidate.", builder.setDisplayName(translation.getName());
"§7Useful if a skin has been updated", translation.getLore().forEach(builder::addLoreLines);
"§7recently and the cache no longer up-to-date.");
return builder; return builder;
}, (click) -> { }, (click) -> {
final ClickType clickType = click.getClickType(); final ClickType clickType = click.getClickType();

View file

@ -35,7 +35,7 @@ public class I18N {
this.yamlConfig = getYamlConfig(); this.yamlConfig = getYamlConfig();
} }
public ItemTranslation translateItem(String key, String... args) { public ItemTranslation translateItem(String key, Object... args) {
final String nameKey = key + ".name"; final String nameKey = key + ".name";
final String loreKey = key + ".lore"; final String loreKey = key + ".lore";
final String name = readString(nameKey); final String name = readString(nameKey);
@ -74,8 +74,9 @@ public class I18N {
// If it does, replace the content with the args at position replacementIndex // If it does, replace the content with the args at position replacementIndex
if (replacementIndex < args.length && args[replacementIndex] != null) { if (replacementIndex < args.length && args[replacementIndex] != null) {
System.out.println("replacing...");
// Replace with the corresponding varargs index // Replace with the corresponding varargs index
toTranslate.set(lineIndex, currentLine.replace("{" + replacementIndex + "}", args[replacementIndex])); toTranslate.set(lineIndex, currentLine.replace("{" + replacementIndex + "}", args[replacementIndex].toString()));
replacementIndex++; replacementIndex++;
} }

View file

@ -104,6 +104,15 @@ public class I18NDict {
public static final String TITLE = ADMIN_KEY + "title"; public static final String TITLE = ADMIN_KEY + "title";
public static final String MANAGE_CACHE = ADMIN_KEY + "manage_cache"; public static final String MANAGE_CACHE = ADMIN_KEY + "manage_cache";
public static final String MANAGE_PLAYER = ADMIN_KEY + "manage_player"; public static final String MANAGE_PLAYER = ADMIN_KEY + "manage_player";
public static class Cache {
private static final String CACHE_KEY = ADMIN_KEY + "cache.";
public static final String TITLE = CACHE_KEY + "title";
public static final String STATISTICS = CACHE_KEY + "statistics";
public static final String INVALIDATE_CACHE = CACHE_KEY + "invalidate_cache";
public static final String INVALIDATE_SKIN = CACHE_KEY + "invalidate_skin";
}
} }
} }
} }

View file

@ -51,10 +51,15 @@ public class NickoExpansion extends PlaceholderExpansion {
final Optional<NickoProfile> optionalProfile = instance.getDataStore().getData(player.getUniqueId()); final Optional<NickoProfile> optionalProfile = instance.getDataStore().getData(player.getUniqueId());
if (optionalProfile.isPresent()) { if (optionalProfile.isPresent()) {
final NickoProfile profile = optionalProfile.get(); final NickoProfile profile = optionalProfile.get();
if (!profile.hasData()) { if (profile.hasData()) {
if (profile.getName() != null) {
name = profile.getName(); name = profile.getName();
}
if (profile.getSkin() != null) {
skin = profile.getSkin(); skin = profile.getSkin();
} }
}
locale = profile.getLocale().getName(); locale = profile.getLocale().getName();
bungeecord = profile.isBungeecordTransfer(); bungeecord = profile.isBungeecordTransfer();
} }

View file

@ -73,6 +73,25 @@ gui:
name: "Check a player..." name: "Check a player..."
lore: lore:
- "§7See players' disguise information." - "§7See players' disguise information."
cache:
title: "Nicko > Admin... > Cache"
statistics:
name: "Statistics"
lore:
- "§fRequest count: §b{0}"
- "§fNumber of skin cached: §b{1}"
- "§8§oCache is cleared every 24 hours."
invalidate_cache:
name: "Invalidate cache"
lore:
- "§c§oNOT RECOMMENDED"
- "§7Invalidate the entirety of the skin cache."
- "§7This doesn't reset player's disguises."
invalidate_skin:
name: "Invalidate a skin..."
lore:
- "§7Select a specific skin to invalidate."
- "§7Useful if a skin has been recently updated."
settings: settings:
title: "Nicko > Settings" title: "Nicko > Settings"
language: language:

View file

@ -66,13 +66,34 @@ gui:
admin: admin:
title: "Nicko > Administration" title: "Nicko > Administration"
manage_cache: manage_cache:
name: "Gérer le cache d'§6apparences..." name: "Gérer le cache d'§6skin..."
lore: lore:
- "§7Accédez à la gestion du cache d'apparences." - "§7Accédez à la gestion du cache de skin."
manage_player: manage_player:
name: "Vérifier un joueur..." name: "Vérifier un joueur..."
lore: lore:
- "§7Vérifiez les informations de déguisement d'un joueur." - "§7Vérifiez les informations de déguisement d'un joueur."
cache:
title: "Nicko > Admin... > Cache"
statistics:
name: "Statistiques"
lore:
- "§fNombre de requêtes: §b{0}"
- "§fNb. de skin dans le cache: §b{1}"
- "§8§oLe cache est vidé toutes les 24 heures."
invalidate_cache:
name: "Invalider le cache"
lore:
- "§c§oDÉCONSEILLÉ"
- "§7Invalide l'entièreté du cache des skin."
- "§7Ne retire pas les déguisements"
- "§7pour les joueurs en disposant."
invalidate_skin:
name: "Invalider un skin..."
lore:
- "§7Sélectionnez une apparence spécifique à"
- "§7invalider. Utile dans le cas où un skin"
- "§7a récemment été mis à jour."
settings: settings:
title: "Nicko > Paramètres" title: "Nicko > Paramètres"
language: language:

View file

@ -44,8 +44,8 @@ public class I18NItemTranslationTest {
@DisplayName("Translate Item") @DisplayName("Translate Item")
public void translateItemLore() { public void translateItemLore() {
final I18N i18n = new I18N(Locale.FRENCH); final I18N i18n = new I18N(Locale.FRENCH);
final ItemTranslation translation = i18n.translateItem(I18NDict.GUI.Settings.BUNGEECORD, "Test"); final String translation = i18n.translatePrefixless(I18NDict.Event.Settings.ERROR, "Test!");
assertEquals("§7§oParcourez les valeurs", translation.getLore().get(0)); assertEquals("§cImpossible de mettre à jour vos paramètres. §7§o(Test!)", translation);
} }
@AfterAll @AfterAll