fix/feat/refactor: yes

This commit is contained in:
aro 2023-01-12 00:51:35 +01:00
parent a674edc187
commit 51d6f0f6af
14 changed files with 68 additions and 85 deletions

View file

@ -34,16 +34,21 @@
<includes> <includes>
<include>net.wesjd:anvilgui</include> <include>net.wesjd:anvilgui</include>
<include>de.studiocode.invui:*</include> <include>de.studiocode.invui:*</include>
<include>com.github.jsixface:*</include>
</includes> </includes>
</artifactSet> </artifactSet>
<relocations> <relocations>
<relocation> <relocation>
<pattern>net.wesjd.anvilgui</pattern> <pattern>net.wesjd.anvilgui</pattern>
<shadedPattern>net.artelnatif.anvilgui</shadedPattern> <shadedPattern>net.artelnatif.libs.anvilgui</shadedPattern>
</relocation> </relocation>
<relocation> <relocation>
<pattern>de.studiocode.invui</pattern> <pattern>de.studiocode.invui</pattern>
<shadedPattern>net.artelnatif.invui</shadedPattern> <shadedPattern>net.artelnatif.libs.invui</shadedPattern>
</relocation>
<relocation>
<pattern>com.github.jsixface</pattern>
<shadedPattern>net.artelnatif.libs.yaml</shadedPattern>
</relocation> </relocation>
</relocations> </relocations>
<minimizeJar>false</minimizeJar> <minimizeJar>false</minimizeJar>

View file

@ -105,7 +105,12 @@
<artifactId>mariadb-java-client</artifactId> <artifactId>mariadb-java-client</artifactId>
<version>3.1.0</version> <version>3.1.0</version>
</dependency> </dependency>
<!-- YAML Reader -->
<dependency>
<groupId>com.github.jsixface</groupId>
<artifactId>yamlconfig</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies> </dependencies>
<build> <build>
@ -130,16 +135,21 @@
<includes> <includes>
<include>net.wesjd:anvilgui</include> <include>net.wesjd:anvilgui</include>
<include>de.studiocode.invui:*</include> <include>de.studiocode.invui:*</include>
<include>com.github.jsixface:*</include>
</includes> </includes>
</artifactSet> </artifactSet>
<relocations> <relocations>
<relocation> <relocation>
<pattern>net.wesjd.anvilgui</pattern> <pattern>net.wesjd.anvilgui</pattern>
<shadedPattern>net.artelnatif.anvilgui</shadedPattern> <shadedPattern>net.artelnatif.libs.anvilgui</shadedPattern>
</relocation> </relocation>
<relocation> <relocation>
<pattern>de.studiocode.invui</pattern> <pattern>de.studiocode.invui</pattern>
<shadedPattern>net.artelnatif.invui</shadedPattern> <shadedPattern>net.artelnatif.libs.invui</shadedPattern>
</relocation>
<relocation>
<pattern>com.github.jsixface</pattern>
<shadedPattern>net.artelnatif.libs.yaml</shadedPattern>
</relocation> </relocation>
</relocations> </relocations>
<!-- Prevents breaking AnvilGUI's VersionWrapper. --> <!-- Prevents breaking AnvilGUI's VersionWrapper. -->

View file

@ -107,9 +107,7 @@ public class NickoBukkit extends JavaPlugin {
localeFileManager = new LocaleFileManager(); localeFileManager = new LocaleFileManager();
if (config.isCustomLocale()) { if (config.isCustomLocale()) {
if (localeFileManager.dumpFromLocale(Locale.ENGLISH)) { if (localeFileManager.dumpFromLocale(Locale.ENGLISH)) {
if (localeFileManager.loadValues()) {
getLogger().info("Successfully loaded custom language file."); getLogger().info("Successfully loaded custom language file.");
}
} else { } else {
getLogger().warning("Failed to load custom language file!"); getLogger().warning("Failed to load custom language file!");
} }

View file

@ -60,9 +60,9 @@ public class AnvilManager {
appearanceManager.setName(response); appearanceManager.setName(response);
final ActionResult actionResult = appearanceManager.updatePlayer(false); final ActionResult actionResult = appearanceManager.updatePlayer(false);
if (!actionResult.isError()) { if (!actionResult.isError()) {
player.sendMessage(I18N.translate(player, I18NDict.Event.DISGUISE_SUCCESS)); player.sendMessage(I18N.translate(player, I18NDict.Event.Disguise.SUCCESS));
} else { } else {
player.sendMessage(I18N.translate(player, I18NDict.Event.DISGUISE_FAIL, I18N.translateWithoutPrefix(player, actionResult.getErrorMessage()))); player.sendMessage(I18N.translate(player, I18NDict.Event.Disguise.FAIL, I18N.translateWithoutPrefix(player, actionResult.getErrorMessage())));
} }
return AnvilGUI.Response.close(); return AnvilGUI.Response.close();
} }
@ -81,9 +81,9 @@ public class AnvilManager {
appearanceManager.setSkin(response); appearanceManager.setSkin(response);
final ActionResult actionResult = appearanceManager.updatePlayer(true); final ActionResult actionResult = appearanceManager.updatePlayer(true);
if (!actionResult.isError()) { if (!actionResult.isError()) {
player.sendMessage(I18N.translate(player, I18NDict.Event.DISGUISE_SUCCESS)); player.sendMessage(I18N.translate(player, I18NDict.Event.Disguise.SUCCESS));
} else { } else {
player.sendMessage(I18N.translate(player, I18NDict.Event.DISGUISE_FAIL, I18N.translateWithoutPrefix(player, actionResult.getErrorMessage()))); player.sendMessage(I18N.translate(player, I18NDict.Event.Disguise.FAIL, I18N.translateWithoutPrefix(player, actionResult.getErrorMessage())));
} }
return AnvilGUI.Response.close(); return AnvilGUI.Response.close();
} }

View file

@ -24,9 +24,9 @@ public class PlayerJoinListener implements Listener {
if (appearanceManager.hasData()) { if (appearanceManager.hasData()) {
final ActionResult actionResult = appearanceManager.updatePlayer(appearanceManager.needsASkinChange()); final ActionResult actionResult = appearanceManager.updatePlayer(appearanceManager.needsASkinChange());
if (!actionResult.isError()) { if (!actionResult.isError()) {
player.sendMessage(I18N.translate(player, I18NDict.Event.PREVIOUS_SKIN_APPLIED)); player.sendMessage(I18N.translate(player, I18NDict.Event.PreviousSkin.SUCCESS));
} else { } else {
player.sendMessage(I18N.translate(player, I18NDict.Event.PREVIOUS_SKIN_APPLY_FAIL, I18N.translate(player, actionResult.getErrorMessage()))); player.sendMessage(I18N.translate(player, I18NDict.Event.PreviousSkin.FAIL, I18N.translate(player, actionResult.getErrorMessage())));
} }
} }
}, 20L); }, 20L);

View file

@ -5,7 +5,6 @@ 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.gui.structure.Structure;
import de.studiocode.invui.window.impl.single.SimpleWindow; import de.studiocode.invui.window.impl.single.SimpleWindow;
import net.artelnatif.nicko.gui.items.admin.ReloadLanguageFileItem;
import net.artelnatif.nicko.gui.items.common.BackItem; import net.artelnatif.nicko.gui.items.common.BackItem;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -15,12 +14,11 @@ public class AdminPanelGUI {
public AdminPanelGUI(Player player) { public AdminPanelGUI(Player player) {
final Structure structure = new Structure("# # # # # # # # #", final Structure structure = new Structure("# # # # # # # # #",
"# % % P U L % % #", "# % % P U # % % #",
"B # # # # # # # #"); "B # # # # # # # #");
structure.addIngredient('B', new BackItem(new MainGUI(player).getGUI())); structure.addIngredient('B', new BackItem(new MainGUI(player).getGUI()));
this.gui = new GUIBuilder<>(GUIType.NORMAL) this.gui = new GUIBuilder<>(GUIType.NORMAL)
.setStructure(structure) .setStructure(structure)
.addIngredient('L', new ReloadLanguageFileItem())
.build(); .build();
this.player = player; this.player = player;
} }

View file

@ -1,38 +0,0 @@
package net.artelnatif.nicko.gui.items.admin;
import de.studiocode.invui.item.ItemProvider;
import de.studiocode.invui.item.builder.ItemBuilder;
import de.studiocode.invui.item.impl.BaseItem;
import net.artelnatif.nicko.NickoBukkit;
import net.artelnatif.nicko.i18n.I18N;
import net.artelnatif.nicko.i18n.I18NDict;
import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.ItemFlag;
import org.jetbrains.annotations.NotNull;
public class ReloadLanguageFileItem extends BaseItem {
@Override
public ItemProvider getItemProvider() {
return new ItemBuilder(Material.BOOK)
.addEnchantment(Enchantment.DAMAGE_ALL, 1, false)
.addItemFlags(ItemFlag.HIDE_ENCHANTS)
.setDisplayName("§fReload Language File");
}
@Override
public void handleClick(@NotNull ClickType clickType, @NotNull Player player, @NotNull InventoryClickEvent event) {
if (clickType.isLeftClick() || clickType.isRightClick()) {
event.getView().close();
final boolean success = NickoBukkit.getInstance().getLocaleFileManager().loadValues();
if (success) {
player.sendMessage(I18N.translate(player, I18NDict.Plugin.CUSTOM_LANGUAGE_RELOAD_SUCCESS));
} else {
player.sendMessage(I18N.translate(player, I18NDict.Plugin.CUSTOM_LANGUAGE_RELOAD_FAIL));
}
}
}
}

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)); player.sendMessage(I18N.translate(player, I18NDict.Event.Undisguise.NONE));
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)); player.sendMessage(I18N.translate(player, I18NDict.Event.Undisguise.SUCCESS));
} else { } else {
player.sendMessage(I18N.translate(player, I18NDict.Event.UNDISGUISE_FAIL)); player.sendMessage(I18N.translate(player, I18NDict.Event.Undisguise.FAIL));
} }
} }
} }

View file

@ -21,15 +21,21 @@ public class LanguageCyclingItem {
getItemProviderForLocale(Locale.CUSTOM), getItemProviderForLocale(Locale.CUSTOM),
}; };
private final ItemProvider[] providersNoCustom = new ItemProvider[]{
getItemProviderForLocale(Locale.ENGLISH),
getItemProviderForLocale(Locale.FRENCH)
};
public BaseItem get(Player player) { public BaseItem get(Player player) {
Optional<NickoProfile> profile = NickoBukkit.getInstance().getDataStore().getData(player.getUniqueId()); final NickoBukkit instance = NickoBukkit.getInstance();
Optional<NickoProfile> profile = instance.getDataStore().getData(player.getUniqueId());
if (profile.isPresent()) { if (profile.isPresent()) {
final NickoProfile nickoProfile = profile.get(); final NickoProfile nickoProfile = profile.get();
int localeOrdinal = nickoProfile.getLocale().ordinal(); int localeOrdinal = nickoProfile.getLocale().ordinal();
return CycleItem.withStateChangeHandler((observer, integer) -> { return CycleItem.withStateChangeHandler((observer, integer) -> {
nickoProfile.setLocale(Locale.values()[integer]); nickoProfile.setLocale(Locale.values()[integer]);
observer.playSound(player, Sound.UI_BUTTON_CLICK, 1f, 0.707107f); // 0.707107 ~= C observer.playSound(player, Sound.UI_BUTTON_CLICK, 1f, 0.707107f); // 0.707107 ~= C
}, localeOrdinal, providers); }, localeOrdinal, instance.getNickoConfig().isCustomLocale() ? providers : providersNoCustom);
} }
return new SimpleItem(ItemProvider.EMPTY); return new SimpleItem(ItemProvider.EMPTY);

View file

@ -1,12 +1,13 @@
package net.artelnatif.nicko.i18n; package net.artelnatif.nicko.i18n;
import com.github.jsixface.YamlConfig;
import net.artelnatif.nicko.NickoBukkit; import net.artelnatif.nicko.NickoBukkit;
import net.artelnatif.nicko.disguise.NickoProfile; import net.artelnatif.nicko.disguise.NickoProfile;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.yaml.snakeyaml.Yaml; import org.yaml.snakeyaml.Yaml;
import java.io.InputStream;
import java.text.MessageFormat; import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Optional; import java.util.Optional;
public class I18N { public class I18N {
@ -51,10 +52,11 @@ public class I18N {
final Locale locale = getLocale(player); final Locale locale = getLocale(player);
String translation; String translation;
if (locale == Locale.CUSTOM) { if (locale == Locale.CUSTOM) {
translation = instance.getLocaleFileManager().getFromFile(key.key()); translation = instance.getLocaleFileManager().get(key.key());
} else { } else {
final HashMap<String, String> values = yaml.load(instance.getResource(locale.getCode() + ".yml")); final InputStream resource = instance.getResource(locale.getCode() + ".yml");
translation = values.getOrDefault(key.key(), key.key()); final YamlConfig yamlConfig = YamlConfig.load(resource);
translation = yamlConfig.getString(key.key());
} }
return translation; return translation;

View file

@ -2,13 +2,21 @@ package net.artelnatif.nicko.i18n;
public record I18NDict(String key) { public record I18NDict(String key) {
public static class Event { public static class Event {
public static final I18NDict UNDISGUISE_SUCCESS = new I18NDict("event.undisguise.success"); public static class Disguise {
public static final I18NDict UNDISGUISE_FAIL = new I18NDict("event.undisguise.fail"); public static final I18NDict SUCCESS = new I18NDict("event.disguise.success");
public static final I18NDict UNDISGUISE_NOTACTIVE = new I18NDict("event.undisguise.notactive"); public static final I18NDict FAIL = new I18NDict("event.disguise.fail");
public static final I18NDict DISGUISE_SUCCESS = new I18NDict("event.disguise.success"); }
public static final I18NDict DISGUISE_FAIL = new I18NDict("event.disguise.fail");
public static final I18NDict PREVIOUS_SKIN_APPLIED = new I18NDict("event.previous_skin_applied.success"); public static class Undisguise {
public static final I18NDict PREVIOUS_SKIN_APPLY_FAIL = new I18NDict("event.previous_skin_applied.fail"); public static final I18NDict SUCCESS = new I18NDict("event.undisguise.success");
public static final I18NDict FAIL = new I18NDict("event.undisguise.fail");
public static final I18NDict NONE = new I18NDict("event.undisguise.none");
}
public static class PreviousSkin {
public static final I18NDict SUCCESS = new I18NDict("event.previous_skin_applied.success");
public static final I18NDict FAIL = new I18NDict("event.previous_skin_applied.fail");
}
} }
public static class Plugin { public static class Plugin {

View file

@ -1,5 +1,6 @@
package net.artelnatif.nicko.i18n; package net.artelnatif.nicko.i18n;
import com.github.jsixface.YamlConfig;
import net.artelnatif.nicko.NickoBukkit; import net.artelnatif.nicko.NickoBukkit;
import org.yaml.snakeyaml.Yaml; import org.yaml.snakeyaml.Yaml;
@ -11,23 +12,16 @@ public class LocaleFileManager {
private final File folder = new File(NickoBukkit.getInstance().getDataFolder() + "/lang/"); private final File folder = new File(NickoBukkit.getInstance().getDataFolder() + "/lang/");
private final File file = new File(folder, "lang.yml"); private final File file = new File(folder, "lang.yml");
private HashMap<String, String> data = new HashMap<>(); public String get(String key) {
if (!file.exists()) return key;
public boolean loadValues() {
if (!file.exists()) return true;
try (BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(file))) { try (BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(file))) {
data = yaml.load(inputStream); final YamlConfig yamlConfig = YamlConfig.load(inputStream);
return true; return yamlConfig.getString(key);
} catch (IOException e) { } catch (IOException e) {
return false; return key;
} }
} }
public String getFromFile(String key) {
if (!file.exists() || data.isEmpty()) return key;
return data.get(key);
}
public boolean dumpFromLocale(Locale locale) { public boolean dumpFromLocale(Locale locale) {
if (locale == Locale.CUSTOM) return true; if (locale == Locale.CUSTOM) return true;
if (file.exists()) return true; if (file.exists()) return true;

View file

@ -17,5 +17,5 @@ event:
success: "§aYour previous active disguise has been applied back." success: "§aYour previous active disguise has been applied back."
undisguise: undisguise:
fail: "§cUnable to remove your disguise. It will be set back to default on your next login. Sorry!" fail: "§cUnable to remove your disguise. It will be set back to default on your next login. Sorry!"
notactive: "§cYou do not have an active disguise." none: "§cYou do not have an active disguise."
success: "§aDisguise removed." success: "§aDisguise removed."

View file

@ -17,5 +17,5 @@ event:
success: "§aVotre précédent déguisement a été réappliqué." success: "§aVotre précédent déguisement a été réappliqué."
undisguise: undisguise:
fail: "§cImpossible de retier votre déguisement. Il sera remis par défaut à votre prochaine reconnexion. Désolé !" fail: "§cImpossible de retier votre déguisement. Il sera remis par défaut à votre prochaine reconnexion. Désolé !"
notactive: "§cVous n'avez pas de déguisement." none: "§cVous n'avez pas de déguisement."
success: "§aDéguisement retiré." success: "§aDéguisement retiré."