From 8735a8e1a3cef7d43eac5d3654c04802311b140f Mon Sep 17 00:00:00 2001 From: TheBusyBiscuit Date: Fri, 10 Jan 2020 21:43:49 +0100 Subject: [PATCH] [Ci skip] Super secret feature --- .../core/commands/SlimefunCommand.java | 5 +- .../slimefun4/core/commands/SubCommand.java | 13 +- .../core/guide/ChestSlimefunGuide.java | 22 +-- .../core/services/LocalizationService.java | 139 +++++++++++++++++- .../SlimefunItem/items/FortuneCookie.java | 2 +- src/main/resources/languages/messages_en.yml | 7 +- 6 files changed, 166 insertions(+), 22 deletions(-) diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/core/commands/SlimefunCommand.java b/src/main/java/io/github/thebusybiscuit/slimefun4/core/commands/SlimefunCommand.java index 753ca033d..f79874ef1 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/core/commands/SlimefunCommand.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/core/commands/SlimefunCommand.java @@ -14,6 +14,7 @@ import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerCommandPreprocessEvent; +import io.github.thebusybiscuit.cscorelib2.chat.ChatColors; import io.github.thebusybiscuit.slimefun4.core.commands.subcommands.CheatCommand; import io.github.thebusybiscuit.slimefun4.core.commands.subcommands.GiveCommand; import io.github.thebusybiscuit.slimefun4.core.commands.subcommands.GuideCommand; @@ -101,11 +102,11 @@ public class SlimefunCommand implements CommandExecutor, Listener { public void sendHelp(CommandSender sender) { sender.sendMessage(""); - sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&aSlimefun &2v" + Slimefun.getVersion())); + sender.sendMessage(ChatColors.color("&aSlimefun &2v" + Slimefun.getVersion())); sender.sendMessage(""); for (SubCommand cmd : commands) { - sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&3/sf " + cmd.getName() + " &b") + cmd.getDescription()); + sender.sendMessage(ChatColors.color("&3/sf " + cmd.getName() + " &b") + cmd.getDescription(sender)); } } diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/core/commands/SubCommand.java b/src/main/java/io/github/thebusybiscuit/slimefun4/core/commands/SubCommand.java index 179b72576..4bccce03c 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/core/commands/SubCommand.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/core/commands/SubCommand.java @@ -1,6 +1,7 @@ package io.github.thebusybiscuit.slimefun4.core.commands; import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; import me.mrCookieSlime.Slimefun.SlimefunPlugin; @@ -8,13 +9,10 @@ public abstract class SubCommand { protected final SlimefunPlugin plugin; protected final SlimefunCommand cmd; - private final String description; protected SubCommand(SlimefunPlugin plugin, SlimefunCommand cmd) { this.plugin = plugin; this.cmd = cmd; - - this.description = SlimefunPlugin.getLocal().getMessage(getDescriptionPath()); } public abstract String getName(); @@ -25,8 +23,13 @@ public abstract class SubCommand { return "commands." + getName(); } - public String getDescription() { - return description; + public String getDescription(CommandSender sender) { + if (sender instanceof Player) { + return SlimefunPlugin.getLocal().getMessage((Player) sender, getDescriptionPath()); + } + else { + return SlimefunPlugin.getLocal().getMessage(getDescriptionPath()); + } } } diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/core/guide/ChestSlimefunGuide.java b/src/main/java/io/github/thebusybiscuit/slimefun4/core/guide/ChestSlimefunGuide.java index 6aa56aca3..033210d1e 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/core/guide/ChestSlimefunGuide.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/core/guide/ChestSlimefunGuide.java @@ -74,7 +74,7 @@ public class ChestSlimefunGuide implements ISlimefunGuide { int index = 9; int pages = (categories.size() + handlers.size() - 1) / CATEGORY_SIZE + 1; - fillInv(profile, menu, survival); + fillInv(p, profile, menu, survival); int target = (CATEGORY_SIZE * (page - 1)) - 1; @@ -176,7 +176,7 @@ public class ChestSlimefunGuide implements ISlimefunGuide { } ChestMenu menu = create(); - fillInv(profile, menu, survival); + fillInv(p, profile, menu, survival); menu.addItem(1, new CustomItem(ChestMenuUtils.getBackButton(), meta -> meta.setLore(Arrays.asList("", ChatColors.color("&rLeft Click: &7Go back to Main Menu"))))); menu.addMenuClickHandler(1, (pl, s, is, action) -> { @@ -289,7 +289,7 @@ public class ChestSlimefunGuide implements ISlimefunGuide { } menu.setEmptySlotsClickable(false); - fillInv(profile, menu, survival); + fillInv(p, profile, menu, survival); addBackButton(menu, 1, profile, survival); int index = 9; @@ -501,15 +501,15 @@ public class ChestSlimefunGuide implements ISlimefunGuide { menu.addItem(16, output, ChestMenuUtils.getEmptyClickHandler()); } - private void fillInv(PlayerProfile profile, ChestMenu menu, boolean survival) { + private void fillInv(Player p, PlayerProfile profile, ChestMenu menu, boolean survival) { for (int i = 0; i < 9; i++) { menu.addItem(i, ChestMenuUtils.getBackground(), ChestMenuUtils.getEmptyClickHandler()); } // Settings Panel menu.addItem(1, ChestMenuUtils.getMenuButton()); - menu.addMenuClickHandler(1, (p, slot, item, action) -> { - GuideSettings.openSettings(p, p.getInventory().getItemInMainHand()); + menu.addMenuClickHandler(1, (pl, slot, item, action) -> { + GuideSettings.openSettings(pl, pl.getInventory().getItemInMainHand()); return false; }); @@ -524,12 +524,12 @@ public class ChestSlimefunGuide implements ISlimefunGuide { */ // Search feature! - menu.addItem(7, new CustomItem(ChestMenuUtils.getSearchButton(), SlimefunPlugin.getLocal().getMessage("guide.search.name"), SlimefunPlugin.getLocal().getMessagesArray("guide.search.lore"))); - menu.addMenuClickHandler(7, (p, slot, item, action) -> { - p.closeInventory(); - SlimefunPlugin.getLocal().sendMessage(p, "search.message"); + menu.addItem(7, new CustomItem(ChestMenuUtils.getSearchButton(), SlimefunPlugin.getLocal().getMessage(p, "guide.search.name"), SlimefunPlugin.getLocal().getMessages(p, "guide.search.lore").toArray(new String[0]))); + menu.addMenuClickHandler(7, (pl, slot, item, action) -> { + pl.closeInventory(); + SlimefunPlugin.getLocal().sendMessage(pl, "search.message"); - ChatInput.waitForPlayer(SlimefunPlugin.instance, p, msg -> + ChatInput.waitForPlayer(SlimefunPlugin.instance, pl, msg -> SlimefunGuide.openSearch(profile, msg, survival, true) ); diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/core/services/LocalizationService.java b/src/main/java/io/github/thebusybiscuit/slimefun4/core/services/LocalizationService.java index e90c7c127..410c0d209 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/core/services/LocalizationService.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/core/services/LocalizationService.java @@ -3,24 +3,40 @@ package io.github.thebusybiscuit.slimefun4.core.services; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.function.UnaryOperator; import java.util.logging.Level; +import org.bukkit.ChatColor; +import org.bukkit.Keyed; +import org.bukkit.NamespacedKey; +import org.bukkit.command.CommandSender; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.YamlConfiguration; +import org.bukkit.entity.Player; +import io.github.thebusybiscuit.cscorelib2.chat.ChatColors; import io.github.thebusybiscuit.cscorelib2.config.Localization; +import io.github.thebusybiscuit.cscorelib2.data.PersistentDataAPI; import me.mrCookieSlime.Slimefun.SlimefunPlugin; import me.mrCookieSlime.Slimefun.api.Slimefun; -public class LocalizationService extends Localization { +public class LocalizationService extends Localization implements Keyed { private static final String LANGUAGE_PATH = "language"; - + + private final Map languages = new HashMap<>(); private final SlimefunPlugin plugin; + private final NamespacedKey languageKey; public LocalizationService(SlimefunPlugin plugin) { super(plugin); + this.plugin = plugin; + languageKey = new NamespacedKey(plugin, LANGUAGE_PATH); String selectedLanguage = SlimefunPlugin.getSelectedLanguage(); String language = getLanguage(); @@ -36,6 +52,107 @@ public class LocalizationService extends Localization { save(); } + public String getPrefix() { + return getMessage("prefix"); + } + + public String getMessage(Player p, String key) { + Optional language = PersistentDataAPI.getOptionalString(p, languageKey); + + if (language.isPresent()) { + FileConfiguration cfg = languages.computeIfAbsent(language.get(), this::loadLanguage); + return cfg.getString(key); + } + else { + return getMessage(key); + } + } + + public List getMessages(Player p, String key) { + Optional language = PersistentDataAPI.getOptionalString(p, languageKey); + + if (language.isPresent()) { + FileConfiguration cfg = languages.computeIfAbsent(language.get(), this::loadLanguage); + return cfg.getStringList(key); + } + else { + return getMessages(key); + } + } + + @Override + public void sendMessage(CommandSender sender, String key) { + String prefix = getPrefix(); + + if (sender instanceof Player) { + sender.sendMessage(ChatColors.color(prefix + getMessage((Player) sender, key))); + } + else { + sender.sendMessage(ChatColor.stripColor(ChatColors.color(prefix + getMessage(key)))); + } + } + + @Override + public void sendMessage(CommandSender sender, String key, boolean addPrefix) { + sendMessage(sender, key); + } + + public void sendMessage(CommandSender sender, String key, UnaryOperator function) { + String prefix = getPrefix(); + + if (sender instanceof Player) { + sender.sendMessage(ChatColors.color(prefix + function.apply(getMessage((Player) sender, key)))); + } + else { + sender.sendMessage(ChatColor.stripColor(ChatColors.color(prefix + function.apply(getMessage(key))))); + } + } + + @Override + public void sendMessage(CommandSender sender, String key, boolean addPrefix, UnaryOperator function) { + sendMessage(sender, key, function); + } + + @Override + public void sendMessages(CommandSender sender, String key) { + String prefix = getPrefix(); + + if (sender instanceof Player) { + for (String translation : getMessages((Player) sender, key)) { + String message = ChatColors.color(prefix + translation); + sender.sendMessage(message); + } + } + else { + for (String translation : getMessages(key)) { + String message = ChatColors.color(prefix + translation); + sender.sendMessage(ChatColor.stripColor(message)); + } + } + } + + @Override + public void sendMessages(CommandSender sender, String key, boolean addPrefix, UnaryOperator function) { + sendMessages(sender, key, function); + } + + public void sendMessages(CommandSender sender, String key, UnaryOperator function) { + String prefix = getPrefix(); + + if (sender instanceof Player) { + for (String translation : getMessages((Player) sender, key)) { + String message = ChatColors.color(prefix + function.apply(translation)); + sender.sendMessage(message); + } + } + else { + for (String translation : getMessages(key)) { + String message = ChatColors.color(prefix + function.apply(translation)); + sender.sendMessage(ChatColor.stripColor(message)); + } + } + } + private String getLanguage() { String language = getConfig().getString(LANGUAGE_PATH); return language == null ? "en": language; @@ -62,7 +179,25 @@ public class LocalizationService extends Localization { save(); } + private FileConfiguration loadLanguage(String id) { + if (!hasLanguage(id)) { + return getConfig().getConfiguration(); + } + + try (BufferedReader reader = new BufferedReader(new InputStreamReader(plugin.getClass().getResourceAsStream("/languages/messages_" + id + ".yml")))) { + return YamlConfiguration.loadConfiguration(reader); + } catch (IOException e) { + Slimefun.getLogger().log(Level.SEVERE, "Failed to load language file into memory: \"messages_" + id + ".yml\"", e); + return getConfig().getConfiguration(); + } + } + public boolean hasLanguage(String language) { return plugin.getClass().getResource("/languages/messages_" + language + ".yml") != null; } + + @Override + public NamespacedKey getKey() { + return languageKey; + } } diff --git a/src/main/java/me/mrCookieSlime/Slimefun/Objects/SlimefunItem/items/FortuneCookie.java b/src/main/java/me/mrCookieSlime/Slimefun/Objects/SlimefunItem/items/FortuneCookie.java index d4343392a..80702d213 100644 --- a/src/main/java/me/mrCookieSlime/Slimefun/Objects/SlimefunItem/items/FortuneCookie.java +++ b/src/main/java/me/mrCookieSlime/Slimefun/Objects/SlimefunItem/items/FortuneCookie.java @@ -23,7 +23,7 @@ public class FortuneCookie extends SimpleSlimefunItem { public ItemConsumptionHandler getItemHandler() { return (e, p, item) -> { if (isItem(item)) { - List messages = SlimefunPlugin.getLocal().getMessages("messages.fortune-cookie"); + List messages = SlimefunPlugin.getLocal().getMessages(p, "messages.fortune-cookie"); String message = messages.get(ThreadLocalRandom.current().nextInt(messages.size())); p.sendMessage(ChatColor.translateAlternateColorCodes('&', message)); diff --git a/src/main/resources/languages/messages_en.yml b/src/main/resources/languages/messages_en.yml index 1350e0240..d04bdc552 100644 --- a/src/main/resources/languages/messages_en.yml +++ b/src/main/resources/languages/messages_en.yml @@ -162,4 +162,9 @@ android: rating: own: '&4You cannot rate your own Script!' - already: '&4You have already left a Rating for this Script!' \ No newline at end of file + already: '&4You have already left a Rating for this Script!' + +languages: + en: 'English' + de: 'German' + fr: 'French' \ No newline at end of file