feat(settings): bungeecord transfer settings (front only)

This commit is contained in:
aro 2022-12-07 11:30:04 +01:00
parent 6ad2a32d5d
commit e2ccdf9071
9 changed files with 200 additions and 24 deletions

View file

@ -1,18 +1,20 @@
package net.artelnatif.nicko.disguise; package net.artelnatif.nicko.disguise;
import java.util.Locale; import net.artelnatif.nicko.i18n.Locale;
public class NickoProfile implements Cloneable { 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 name;
private String skin; private String skin;
private Locale locale; 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.name = name;
this.skin = skin; this.skin = skin;
this.locale = locale; this.locale = locale;
this.bungeecordTransfer = bungeecordTransfer;
} }
public boolean isEmpty() { public boolean isEmpty() {
@ -39,11 +41,20 @@ public class NickoProfile implements Cloneable {
public void setLocale(Locale locale) { this.locale = locale; } public void setLocale(Locale locale) { this.locale = locale; }
public boolean isBungeecordTransfer() {
return bungeecordTransfer;
}
public void setBungeecordTransfer(boolean bungeecordTransfer) {
this.bungeecordTransfer = bungeecordTransfer;
}
@Override @Override
public String toString() { public String toString() {
return "NickoProfile{" + return "NickoProfile{" +
"name='" + name + '\'' + "name='" + name + '\'' +
", skin='" + skin + '\'' + ", skin='" + skin + '\'' +
", locale='" + locale + '\'' +
", empty='" + isEmpty() + '\'' + ", empty='" + isEmpty() + '\'' +
'}'; '}';
} }

View file

@ -11,13 +11,13 @@ import org.bukkit.entity.Player;
public class MainGUI { public class MainGUI {
private final Player player; private final Player player;
private final GUI gui; private final GUI gui;
private final String[] structureIngredients = new String[] private final String[] structureIngredients = new String[]{
{"# # # # # # # # #", "# # # # # # # # #",
"# % % % % % % % #", "# % % % % % % % #",
"# % # R B P # % #", "# % # R B P # % #",
"# % # N A S # % #", "# % # N A S # % #",
"# % % % % % % % #", "# % % % % % % % #",
"E # # # # # # # #"}; "E # # # # # # # #"};
public MainGUI(Player player) { public MainGUI(Player player) {
if (!player.hasPermission("nicko.admin") || !player.isOp()) { if (!player.hasPermission("nicko.admin") || !player.isOp()) {

View file

@ -3,22 +3,33 @@ package net.artelnatif.nicko.gui;
import de.studiocode.invui.gui.GUI; import de.studiocode.invui.gui.GUI;
import de.studiocode.invui.gui.builder.GUIBuilder; import de.studiocode.invui.gui.builder.GUIBuilder;
import de.studiocode.invui.gui.builder.guitype.GUIType; import de.studiocode.invui.gui.builder.guitype.GUIType;
import de.studiocode.invui.gui.structure.Structure;
import de.studiocode.invui.window.impl.single.SimpleWindow; 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.common.BackItem;
import net.artelnatif.nicko.gui.items.settings.BungeeCordCyclingItem;
import net.artelnatif.nicko.gui.items.settings.LanguageCyclingItem;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
public class SettingsGUI { public class SettingsGUI {
private final Player player; private final Player player;
private final GUI gui; private final GUI gui;
private final String[] structureIngredients = new String[]{
"# # # # # # # # #",
"# % % L # T % % #",
"B # # # # # # # #"
};
public SettingsGUI(Player player) { public SettingsGUI(Player player) {
final Structure structure = new Structure("# # # # # # # # #", if (!NickoBukkit.getInstance().getNickoConfig().isBungeecordEnabled()) {
"# % % M C R % % #", }
"B # # # # # # # #"); //structureIngredients[1] = structureIngredients[1].replace("T", "#");
structure.addIngredient('B', new BackItem(new MainGUI(player).getGUI()));
this.gui = new GUIBuilder<>(GUIType.NORMAL) 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(); .build();
this.player = player; this.player = player;
} }

View file

@ -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<NickoProfile> 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;
}
}

View file

@ -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<NickoProfile> 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;
}
}

View file

@ -2,10 +2,10 @@ package net.artelnatif.nicko.i18n;
import net.artelnatif.nicko.NickoBukkit; import net.artelnatif.nicko.NickoBukkit;
import net.artelnatif.nicko.disguise.NickoProfile; import net.artelnatif.nicko.disguise.NickoProfile;
import org.apache.commons.lang3.LocaleUtils;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import java.text.MessageFormat; import java.text.MessageFormat;
import java.util.Locale;
import java.util.Optional; import java.util.Optional;
import java.util.ResourceBundle; import java.util.ResourceBundle;
@ -21,16 +21,20 @@ public class I18N {
return profile.get().getLocale(); return profile.get().getLocale();
} }
} catch (IllegalArgumentException exception) { } 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(); return Locale.getDefault();
} }
} }
private static ResourceBundle getBundle(Player player) { 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) { 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 { try {
formatter.applyPattern(getBundle(player).getString(key.key())); formatter.applyPattern(getBundle(player).getString(key.key()));
return NickoBukkit.getInstance().getNickoConfig().getPrefix() + formatter.format(arguments); return NickoBukkit.getInstance().getNickoConfig().getPrefix() + formatter.format(arguments);

View file

@ -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<String, Locale> 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<String, Locale> 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;
}
}

View file

@ -1,10 +1,8 @@
package net.artelnatif.nicko.i18n; package net.artelnatif.nicko.i18n;
import net.artelnatif.nicko.NickoBukkit; import net.artelnatif.nicko.NickoBukkit;
import org.apache.commons.lang3.LocaleUtils;
import java.util.Arrays; import java.util.Arrays;
import java.util.Locale;
public class LocaleManager { public class LocaleManager {
private static final String[] supportedLocales = new String[]{"en", "fr", "custom"}; private static final String[] supportedLocales = new String[]{"en", "fr", "custom"};
@ -17,8 +15,8 @@ public class LocaleManager {
Locale.setDefault(Locale.ENGLISH); Locale.setDefault(Locale.ENGLISH);
return; return;
} }
final Locale defaultLocale = LocaleUtils.toLocale(locale); final Locale defaultLocale = Locale.fromCode(locale);
instance.getLogger().info("Default locale set to " + defaultLocale.getDisplayName() + "."); instance.getLogger().info("Default locale set to " + defaultLocale.getName() + ".");
Locale.setDefault(defaultLocale); Locale.setDefault(defaultLocale);
} catch (Exception e) { } catch (Exception e) {
instance.getLogger().severe(locale + " is not a valid locale, defaulting to English."); instance.getLogger().severe(locale + " is not a valid locale, defaulting to English.");

View file

@ -13,7 +13,7 @@ bungeecord:
ttl: -1 ttl: -1
# Localisation: # 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. # If that fails, the locale fallback is the one provided by this option.
# Accepted values: fr (French), en (English), custom (Custom language file). # Accepted values: fr (French), en (English), custom (Custom language file).
locale: "en" locale: "en"