From e2ccdf9071f2f6599b0ca54ce5d0475ed044121f Mon Sep 17 00:00:00 2001 From: aro Date: Wed, 7 Dec 2022 11:30:04 +0100 Subject: [PATCH] feat(settings): bungeecord transfer settings (front only) --- .../nicko/disguise/NickoProfile.java | 17 ++++-- .../net/artelnatif/nicko/gui/MainGUI.java | 14 ++--- .../net/artelnatif/nicko/gui/SettingsGUI.java | 23 +++++--- .../items/settings/BungeeCordCyclingItem.java | 49 +++++++++++++++++ .../items/settings/LanguageCyclingItem.java | 51 ++++++++++++++++++ .../java/net/artelnatif/nicko/i18n/I18N.java | 10 ++-- .../net/artelnatif/nicko/i18n/Locale.java | 52 +++++++++++++++++++ .../artelnatif/nicko/i18n/LocaleManager.java | 6 +-- nicko-core/src/main/resources/config.yml | 2 +- 9 files changed, 200 insertions(+), 24 deletions(-) create mode 100644 nicko-core/src/main/java/net/artelnatif/nicko/gui/items/settings/BungeeCordCyclingItem.java create mode 100644 nicko-core/src/main/java/net/artelnatif/nicko/gui/items/settings/LanguageCyclingItem.java create mode 100644 nicko-core/src/main/java/net/artelnatif/nicko/i18n/Locale.java diff --git a/nicko-core/src/main/java/net/artelnatif/nicko/disguise/NickoProfile.java b/nicko-core/src/main/java/net/artelnatif/nicko/disguise/NickoProfile.java index 6ce799a..23007c7 100644 --- a/nicko-core/src/main/java/net/artelnatif/nicko/disguise/NickoProfile.java +++ b/nicko-core/src/main/java/net/artelnatif/nicko/disguise/NickoProfile.java @@ -1,18 +1,20 @@ package net.artelnatif.nicko.disguise; -import java.util.Locale; +import net.artelnatif.nicko.i18n.Locale; public class NickoProfile implements Cloneable { - public static final NickoProfile EMPTY_PROFILE = new NickoProfile(null, null, Locale.ENGLISH); + public static final NickoProfile EMPTY_PROFILE = new NickoProfile(null, null, Locale.ENGLISH, true); private String name; private String skin; private Locale locale; + private boolean bungeecordTransfer; - public NickoProfile(String name, String skin, Locale locale) { + public NickoProfile(String name, String skin, Locale locale, boolean bungeecordTransfer) { this.name = name; this.skin = skin; this.locale = locale; + this.bungeecordTransfer = bungeecordTransfer; } public boolean isEmpty() { @@ -39,11 +41,20 @@ public class NickoProfile implements Cloneable { public void setLocale(Locale locale) { this.locale = locale; } + public boolean isBungeecordTransfer() { + return bungeecordTransfer; + } + + public void setBungeecordTransfer(boolean bungeecordTransfer) { + this.bungeecordTransfer = bungeecordTransfer; + } + @Override public String toString() { return "NickoProfile{" + "name='" + name + '\'' + ", skin='" + skin + '\'' + + ", locale='" + locale + '\'' + ", empty='" + isEmpty() + '\'' + '}'; } diff --git a/nicko-core/src/main/java/net/artelnatif/nicko/gui/MainGUI.java b/nicko-core/src/main/java/net/artelnatif/nicko/gui/MainGUI.java index 9185712..af38b3b 100644 --- a/nicko-core/src/main/java/net/artelnatif/nicko/gui/MainGUI.java +++ b/nicko-core/src/main/java/net/artelnatif/nicko/gui/MainGUI.java @@ -11,13 +11,13 @@ import org.bukkit.entity.Player; public class MainGUI { private final Player player; private final GUI gui; - private final String[] structureIngredients = new String[] - {"# # # # # # # # #", - "# % % % % % % % #", - "# % # R B P # % #", - "# % # N A S # % #", - "# % % % % % % % #", - "E # # # # # # # #"}; + private final String[] structureIngredients = new String[]{ + "# # # # # # # # #", + "# % % % % % % % #", + "# % # R B P # % #", + "# % # N A S # % #", + "# % % % % % % % #", + "E # # # # # # # #"}; public MainGUI(Player player) { if (!player.hasPermission("nicko.admin") || !player.isOp()) { diff --git a/nicko-core/src/main/java/net/artelnatif/nicko/gui/SettingsGUI.java b/nicko-core/src/main/java/net/artelnatif/nicko/gui/SettingsGUI.java index 7b2bc65..7e7d19f 100644 --- a/nicko-core/src/main/java/net/artelnatif/nicko/gui/SettingsGUI.java +++ b/nicko-core/src/main/java/net/artelnatif/nicko/gui/SettingsGUI.java @@ -3,22 +3,33 @@ package net.artelnatif.nicko.gui; import de.studiocode.invui.gui.GUI; import de.studiocode.invui.gui.builder.GUIBuilder; import de.studiocode.invui.gui.builder.guitype.GUIType; -import de.studiocode.invui.gui.structure.Structure; import de.studiocode.invui.window.impl.single.SimpleWindow; +import net.artelnatif.nicko.NickoBukkit; import net.artelnatif.nicko.gui.items.common.BackItem; +import net.artelnatif.nicko.gui.items.settings.BungeeCordCyclingItem; +import net.artelnatif.nicko.gui.items.settings.LanguageCyclingItem; import org.bukkit.entity.Player; public class SettingsGUI { private final Player player; private final GUI gui; + private final String[] structureIngredients = new String[]{ + "# # # # # # # # #", + "# % % L # T % % #", + "B # # # # # # # #" + }; + public SettingsGUI(Player player) { - final Structure structure = new Structure("# # # # # # # # #", - "# % % M C R % % #", - "B # # # # # # # #"); - structure.addIngredient('B', new BackItem(new MainGUI(player).getGUI())); + if (!NickoBukkit.getInstance().getNickoConfig().isBungeecordEnabled()) { + } + //structureIngredients[1] = structureIngredients[1].replace("T", "#"); + this.gui = new GUIBuilder<>(GUIType.NORMAL) - .setStructure(structure) + .setStructure(structureIngredients) + .addIngredient('B', new BackItem(new MainGUI(player).getGUI())) + .addIngredient('L', new LanguageCyclingItem().get(player)) + .addIngredient('T', new BungeeCordCyclingItem().get(player)) .build(); this.player = player; } diff --git a/nicko-core/src/main/java/net/artelnatif/nicko/gui/items/settings/BungeeCordCyclingItem.java b/nicko-core/src/main/java/net/artelnatif/nicko/gui/items/settings/BungeeCordCyclingItem.java new file mode 100644 index 0000000..e96ccd1 --- /dev/null +++ b/nicko-core/src/main/java/net/artelnatif/nicko/gui/items/settings/BungeeCordCyclingItem.java @@ -0,0 +1,49 @@ +package net.artelnatif.nicko.gui.items.settings; + +import de.studiocode.invui.item.ItemProvider; +import de.studiocode.invui.item.builder.ItemBuilder; +import de.studiocode.invui.item.impl.BaseItem; +import de.studiocode.invui.item.impl.CycleItem; +import de.studiocode.invui.item.impl.SimpleItem; +import net.artelnatif.nicko.NickoBukkit; +import net.artelnatif.nicko.disguise.NickoProfile; +import org.bukkit.Material; +import org.bukkit.Sound; +import org.bukkit.entity.Player; + +import java.util.Optional; + +public class BungeeCordCyclingItem { + private final ItemProvider[] possibleLocales = new ItemProvider[]{ + getItemProviderForValue(true), + getItemProviderForValue(false) + }; + + public BaseItem get(Player player) { + Optional profile = NickoBukkit.getInstance().getDataStore().getData(player.getUniqueId()); + if (profile.isPresent()) { + final NickoProfile nickoProfile = profile.get(); + int startingState = nickoProfile.isBungeecordTransfer() ? 0 : 1; + return CycleItem.withStateChangeHandler((observer, integer) -> { + nickoProfile.setBungeecordTransfer(integer != 1); + observer.playSound(player, Sound.UI_BUTTON_CLICK, 1f, 0.707107f); // 0.707107 ~= C + }, startingState, possibleLocales); + } + + return new SimpleItem(ItemProvider.EMPTY); + } + + private ItemProvider getItemProviderForValue(boolean enabled) { + final ItemBuilder builder = new ItemBuilder(Material.COMPASS); + builder.setDisplayName("§6BungeeCord transfer:"); + if (enabled) { + builder.addLoreLines("§7> §cDisabled"); + builder.addLoreLines("§6§l> §a§lEnabled"); + } else { + builder.addLoreLines("§6§l> §c§lDisabled"); + builder.addLoreLines("§7> §aEnabled"); + } + builder.addLoreLines("§7§oCycle through the values by", "§7§oleft and right clicking."); + return builder; + } +} diff --git a/nicko-core/src/main/java/net/artelnatif/nicko/gui/items/settings/LanguageCyclingItem.java b/nicko-core/src/main/java/net/artelnatif/nicko/gui/items/settings/LanguageCyclingItem.java new file mode 100644 index 0000000..58171a1 --- /dev/null +++ b/nicko-core/src/main/java/net/artelnatif/nicko/gui/items/settings/LanguageCyclingItem.java @@ -0,0 +1,51 @@ +package net.artelnatif.nicko.gui.items.settings; + +import de.studiocode.invui.item.ItemProvider; +import de.studiocode.invui.item.builder.ItemBuilder; +import de.studiocode.invui.item.impl.BaseItem; +import de.studiocode.invui.item.impl.CycleItem; +import de.studiocode.invui.item.impl.SimpleItem; +import net.artelnatif.nicko.NickoBukkit; +import net.artelnatif.nicko.disguise.NickoProfile; +import net.artelnatif.nicko.i18n.Locale; +import org.bukkit.Material; +import org.bukkit.Sound; +import org.bukkit.entity.Player; + +import java.util.Optional; + +public class LanguageCyclingItem { + private final ItemProvider[] possibleLocales = new ItemProvider[]{ + getItemProviderForLocale(Locale.ENGLISH), + getItemProviderForLocale(Locale.FRENCH), + getItemProviderForLocale(Locale.CUSTOM), + }; + + public BaseItem get(Player player) { + Optional profile = NickoBukkit.getInstance().getDataStore().getData(player.getUniqueId()); + if (profile.isPresent()) { + final NickoProfile nickoProfile = profile.get(); + int localeOrdinal = nickoProfile.getLocale().ordinal(); + return CycleItem.withStateChangeHandler((observer, integer) -> { + nickoProfile.setLocale(Locale.values()[integer]); + observer.playSound(player, Sound.UI_BUTTON_CLICK, 1f, 0.707107f); // 0.707107 ~= C + }, localeOrdinal, possibleLocales); + } + + return new SimpleItem(ItemProvider.EMPTY); + } + + private ItemProvider getItemProviderForLocale(Locale locale) { + final ItemBuilder builder = new ItemBuilder(Material.OAK_SIGN); + builder.setDisplayName("§6Select your language:"); + for (Locale value : Locale.values()) { + if (locale != value) { + builder.addLoreLines("§7> " + value.getName()); + } else { + builder.addLoreLines("§6§l> §f" + value.getName()); + } + } + builder.addLoreLines("§7§oCycle through the values by", "§7§oleft and right clicking."); + return builder; + } +} \ No newline at end of file diff --git a/nicko-core/src/main/java/net/artelnatif/nicko/i18n/I18N.java b/nicko-core/src/main/java/net/artelnatif/nicko/i18n/I18N.java index e54f21d..0655c06 100644 --- a/nicko-core/src/main/java/net/artelnatif/nicko/i18n/I18N.java +++ b/nicko-core/src/main/java/net/artelnatif/nicko/i18n/I18N.java @@ -2,10 +2,10 @@ package net.artelnatif.nicko.i18n; import net.artelnatif.nicko.NickoBukkit; import net.artelnatif.nicko.disguise.NickoProfile; +import org.apache.commons.lang3.LocaleUtils; import org.bukkit.entity.Player; import java.text.MessageFormat; -import java.util.Locale; import java.util.Optional; import java.util.ResourceBundle; @@ -21,16 +21,20 @@ public class I18N { return profile.get().getLocale(); } } catch (IllegalArgumentException exception) { - NickoBukkit.getInstance().getLogger().severe("Invalid locale provided, defaulting to " + Locale.getDefault().getDisplayName() + "."); + NickoBukkit.getInstance().getLogger().severe("Invalid locale provided, defaulting to " + Locale.getDefault().getCode() + "."); return Locale.getDefault(); } } private static ResourceBundle getBundle(Player player) { - return ResourceBundle.getBundle("locale", getLocale(player)); + return ResourceBundle.getBundle("locale", LocaleUtils.toLocale(getLocale(player).getCode())); } public static String translate(Player player, I18NDict key, Object... arguments) { + if (Locale.getDefault() == Locale.CUSTOM) { + // TODO: 12/6/22 Actually return from custom language file + return NickoBukkit.getInstance().getNickoConfig().getPrefix() + key.key(); + } try { formatter.applyPattern(getBundle(player).getString(key.key())); return NickoBukkit.getInstance().getNickoConfig().getPrefix() + formatter.format(arguments); diff --git a/nicko-core/src/main/java/net/artelnatif/nicko/i18n/Locale.java b/nicko-core/src/main/java/net/artelnatif/nicko/i18n/Locale.java new file mode 100644 index 0000000..49084e6 --- /dev/null +++ b/nicko-core/src/main/java/net/artelnatif/nicko/i18n/Locale.java @@ -0,0 +1,52 @@ +package net.artelnatif.nicko.i18n; + +import java.io.Serializable; +import java.util.HashMap; + +public enum Locale implements Serializable { + ENGLISH("en", "English"), + FRENCH("fr", "Français"), + CUSTOM("custom", "Server Custom"); + + private static HashMap locales; + private static Locale defaultLocale; + + private final String code; + private transient final String name; + + Locale(String code, String name) { + this.code = code; + this.name = name; + } + + public static HashMap getLocales() { + if (locales == null) { + return locales = new HashMap<>() {{ + for (Locale value : Locale.values()) { + put(value.getCode(), value); + } + }}; + } + return locales; + } + + public static Locale fromCode(String code) { + return getLocales().getOrDefault(code, defaultLocale); + } + + public static void setDefault(Locale defaultLocale) { + Locale.defaultLocale = defaultLocale; + } + + public static Locale getDefault() { + return defaultLocale; + } + + public String getCode() { + return code; + } + + public String getName() { + return name; + } +} diff --git a/nicko-core/src/main/java/net/artelnatif/nicko/i18n/LocaleManager.java b/nicko-core/src/main/java/net/artelnatif/nicko/i18n/LocaleManager.java index 8d32bce..a6bb2c6 100644 --- a/nicko-core/src/main/java/net/artelnatif/nicko/i18n/LocaleManager.java +++ b/nicko-core/src/main/java/net/artelnatif/nicko/i18n/LocaleManager.java @@ -1,10 +1,8 @@ package net.artelnatif.nicko.i18n; import net.artelnatif.nicko.NickoBukkit; -import org.apache.commons.lang3.LocaleUtils; import java.util.Arrays; -import java.util.Locale; public class LocaleManager { private static final String[] supportedLocales = new String[]{"en", "fr", "custom"}; @@ -17,8 +15,8 @@ public class LocaleManager { Locale.setDefault(Locale.ENGLISH); return; } - final Locale defaultLocale = LocaleUtils.toLocale(locale); - instance.getLogger().info("Default locale set to " + defaultLocale.getDisplayName() + "."); + final Locale defaultLocale = Locale.fromCode(locale); + instance.getLogger().info("Default locale set to " + defaultLocale.getName() + "."); Locale.setDefault(defaultLocale); } catch (Exception e) { instance.getLogger().severe(locale + " is not a valid locale, defaulting to English."); diff --git a/nicko-core/src/main/resources/config.yml b/nicko-core/src/main/resources/config.yml index df9b7ee..0286449 100644 --- a/nicko-core/src/main/resources/config.yml +++ b/nicko-core/src/main/resources/config.yml @@ -13,7 +13,7 @@ bungeecord: ttl: -1 # Localisation: -# By default, Nicko tries to get the locale from the player's options. +# By default, Nicko tries to get the locale from the player's preferences. # If that fails, the locale fallback is the one provided by this option. # Accepted values: fr (French), en (English), custom (Custom language file). locale: "en"