diff --git a/src/main/java/fr/zcraft/zlib/components/commands/Command.java b/src/main/java/fr/zcraft/zlib/components/commands/Command.java index 286c7603..6849892c 100644 --- a/src/main/java/fr/zcraft/zlib/components/commands/Command.java +++ b/src/main/java/fr/zcraft/zlib/components/commands/Command.java @@ -31,6 +31,9 @@ package fr.zcraft.zlib.components.commands; import fr.zcraft.zlib.components.commands.CommandException.Reason; +import fr.zcraft.zlib.components.i18n.I; +import fr.zcraft.zlib.components.i18n.I18n; +import fr.zcraft.zlib.components.i18n.LazyTranslation; import fr.zcraft.zlib.components.rawtext.RawText; import fr.zcraft.zlib.core.ZLib; import fr.zcraft.zlib.tools.text.RawMessage; @@ -44,10 +47,10 @@ import java.util.Collections; import java.util.HashSet; import java.util.List; +import java.util.Locale; import java.util.Set; import java.util.regex.Pattern; - abstract public class Command { private static final Pattern FLAG_PATTERN = Pattern.compile("(--?)[a-zA-Z0-9-]+"); @@ -63,6 +66,9 @@ abstract public class Command protected CommandSender sender; protected String[] args; + + private Locale senderLocale; + protected Set flags; /** @@ -158,12 +164,15 @@ protected List complete() throws CommandException public void execute(CommandSender sender, String[] args) { this.sender = sender; + this.senderLocale = null; + parseArgs(args); try { if (!canExecute(sender, args)) throw new CommandException(this, Reason.SENDER_NOT_AUTHORIZED); + run(); } catch (CommandException ex) @@ -172,6 +181,7 @@ public void execute(CommandSender sender, String[] args) } this.sender = null; + this.senderLocale = null; this.args = null; this.flags = null; } @@ -187,6 +197,7 @@ public List tabComplete(CommandSender sender, String[] args) List result = null; this.sender = sender; + this.senderLocale = null; parseArgs(args); try @@ -194,9 +205,10 @@ public List tabComplete(CommandSender sender, String[] args) if (canExecute(sender, args)) result = complete(); } - catch (CommandException ignored) {} + catch (CommandException ignored){} this.sender = null; + this.senderLocale = null; this.args = null; this.flags = null; @@ -379,6 +391,7 @@ private static boolean isValidFlag(Set acceptedFlags, String flag) } + ///////////// Common methods for commands ///////////// /** @@ -394,6 +407,12 @@ protected void throwInvalidArgument(String reason) throws CommandException throw new CommandException(this, Reason.INVALID_PARAMETERS, reason); } + protected void throwInvalidArgument(LazyTranslation reason, Object... parameters) throws CommandException + { + throwInvalidArgument(I.tl(getSenderLocale(), reason, parameters)); + } + + /** * Stops the command execution because the command usage is disallowed, and * displays an error message. @@ -408,7 +427,7 @@ protected void throwNotAuthorized() throws CommandException /** * Retrieves the {@link Player} who executed this command. If the command is * not executed by a player, aborts the execution and displays an error - * messagE. + * message. * * @return The player executing this command. * @throws CommandException If the sender is not a player. @@ -420,6 +439,32 @@ protected Player playerSender() throws CommandException return (Player) sender; } + /** + * Retrieves the sender's locale. If it cannot be retrieved for some reason, + * or if the sender is not a player (e.g. the console), the primary, or the + * fallback if no primary is defined, language is returned instead. + * + * @return The {@link Locale} to use for this sender. + */ + protected Locale getSenderLocale() + { + if(sender == null) return null; + + if(senderLocale != null) return senderLocale; + + if(sender instanceof Player) + senderLocale = I18n.getPlayerLocale((Player) sender); + + if(senderLocale == null) + senderLocale = I18n.getPrimaryLocale(); + + if(senderLocale == null) + senderLocale = I18n.getFallbackLocale(); + + return senderLocale; + } + + ///////////// Methods for command execution ///////////// @@ -444,6 +489,20 @@ protected void info(String message) info(sender, message); } + /** + * Displays a gray informational message to the sender. + * + * @param message The message to display. + * @param parameters The parameters of the I18n message. + * + * @see I#t(Locale, LazyTranslation, Object...) the underlying method used + * to retrieve the text and merge it with the parameters. + */ + protected void info(LazyTranslation message, Object... parameters) + { + info(I.tl(getSenderLocale(), message, parameters)); + } + /** * Displays a green success message. * @@ -465,6 +524,21 @@ protected void success(String message) success(sender, message); } + /** + * Displays a green success message to the sender. + * + * @param message The message to display. + * @param parameters The parameters of the I18n message. + * + * @see I#t(Locale, LazyTranslation, Object...) the underlying method used + * to retrieve the text and merge it with the parameters. + */ + protected void success(LazyTranslation message, Object... parameters) + { + success(I.tl(getSenderLocale(), message, parameters)); + } + + /** * Displays a red warning message. * @@ -486,22 +560,57 @@ protected void warning(String message) warning(sender, message); } + /** + * Displays a red warning message to the sender. + * + * @param message The message to display. + * @param parameters The parameters of the I18n message. + * + * @see I#t(Locale, LazyTranslation, Object...) the underlying method used + * to retrieve the text and merge it with the parameters. + */ + protected void warning(LazyTranslation message, Object... parameters) + { + warning(I.tl(getSenderLocale(), message, parameters)); + } + + /** * Aborts the execution and displays an error message. * * @param message The message. * - * @throws CommandException + * @throws CommandException This method always throws a {@link Reason#COMMAND_ERROR}'s + * {@link CommandException}, interrupting the execution of the command. */ protected void error(String message) throws CommandException { throw new CommandException(this, Reason.COMMAND_ERROR, message); } + /** + * Aborts the execution and displays an error message. + * + * @param message The message. + * @param parameters The parameters of the I18n message. + * + * @throws CommandException This method always throws a {@link Reason#COMMAND_ERROR}'s + * {@link CommandException}, interrupting the execution of the command. + * + * @see I#t(Locale, LazyTranslation, Object...) the underlying method used + * to retrieve the text and merge it with the parameters. + */ + protected void error(LazyTranslation message, Object... parameters) throws CommandException + { + error(I.tl(getSenderLocale(), message, parameters)); + } + + /** * Aborts the execution and displays a generic error message. * - * @throws CommandException + * @throws CommandException This method always throws a {@link Reason#COMMAND_ERROR}'s + * {@link CommandException}, interrupting the execution of the command. */ protected void error() throws CommandException { @@ -527,6 +636,9 @@ protected void tellRaw(String rawMessage) throws CommandException */ protected void send(RawText text) { + if(text.getLocale() == null) + text.locale(getSenderLocale()); + RawMessage.send(sender, text); } diff --git a/src/main/java/fr/zcraft/zlib/components/gui/ActionGui.java b/src/main/java/fr/zcraft/zlib/components/gui/ActionGui.java index 41870d8f..5001edab 100644 --- a/src/main/java/fr/zcraft/zlib/components/gui/ActionGui.java +++ b/src/main/java/fr/zcraft/zlib/components/gui/ActionGui.java @@ -130,10 +130,12 @@ protected void action(String name, int slot, ItemStack item, String title, List< * @param name The identifier of the action. * @param slot The slot the action will be placed on. * @param material The material used to represent the action. + * @return an {@link fr.zcraft.zlib.tools.items.ItemStackBuilder} ItemStackBuilder to build the representing item */ - protected void action(String name, int slot, Material material) + protected ItemStackBuilder action(String name, int slot, Material material) { - action(name, slot, GuiUtils.makeItem(material)); + return action(name, slot) + .material(material); } /** @@ -180,7 +182,7 @@ protected ItemStackBuilder action(String name, int slot, ItemStack item) action(action); - if(item == null) return action.updateItem(); + if(item == null) return action.updateItem().locale(getPlayerLocale()); return null; } @@ -243,7 +245,7 @@ protected ItemStackBuilder updateAction(String name, Material material) protected ItemStackBuilder updateAction(String name) { - return getAction(name).updateItem(); + return getAction(name).updateItem().locale(getPlayerLocale()); } /** diff --git a/src/main/java/fr/zcraft/zlib/components/gui/GuiBase.java b/src/main/java/fr/zcraft/zlib/components/gui/GuiBase.java index 57f45875..5d4c4d0a 100644 --- a/src/main/java/fr/zcraft/zlib/components/gui/GuiBase.java +++ b/src/main/java/fr/zcraft/zlib/components/gui/GuiBase.java @@ -30,7 +30,9 @@ package fr.zcraft.zlib.components.gui; +import fr.zcraft.zlib.components.i18n.I; import fr.zcraft.zlib.components.i18n.I18n; +import fr.zcraft.zlib.components.i18n.LazyTranslation; import fr.zcraft.zlib.core.ZLib; import java.util.Locale; import org.bukkit.entity.Player; @@ -164,6 +166,25 @@ void setParent(GuiBase parent) /** @return The locale used by the player this Gui instance is associated to. */ protected final Locale getPlayerLocale() { return playerLocale; } + /** + * Sends a message to the player this Gui instance is associated to. + * @param message The message to send to the player. + */ + protected void sendMessage(String message) + { + getPlayer().sendMessage(message); + } + + /** + * Sends a message to the player this Gui instance is associated to. + * @param message The message to send to the player. + * @param parameters Parameters for the translatable format text, if any. + */ + protected void sendMessage(LazyTranslation message, Object... parameters) + { + sendMessage(I.tl(getPlayerLocale(), message, parameters)); + } + protected boolean checkImmune() { if(!immune) return false; @@ -176,6 +197,4 @@ private void setImmune(boolean immune) this.immune = immune; } - /* ===== Static API ===== */ - } diff --git a/src/main/java/fr/zcraft/zlib/components/gui/InventoryGui.java b/src/main/java/fr/zcraft/zlib/components/gui/InventoryGui.java index 961d93f8..fc286c38 100644 --- a/src/main/java/fr/zcraft/zlib/components/gui/InventoryGui.java +++ b/src/main/java/fr/zcraft/zlib/components/gui/InventoryGui.java @@ -30,6 +30,8 @@ package fr.zcraft.zlib.components.gui; +import fr.zcraft.zlib.components.i18n.I; +import fr.zcraft.zlib.components.i18n.LazyTranslation; import fr.zcraft.zlib.tools.items.InventoryUtils; import fr.zcraft.zlib.tools.runners.RunTask; import org.bukkit.Bukkit; @@ -247,6 +249,17 @@ protected void setTitle(String title) this.title = title; } + /** + * Sets the new title of the inventory. + * It will be applied on the next GUI update. + * @param title The new title of the inventory + * @param parameters Parameters for the translatable format text, if any. + */ + protected void setTitle(LazyTranslation title, Object ...parameters) + { + setTitle(I.tl(getPlayerLocale(), title, parameters)); + } + /** @return The underlying inventory, or null if the Gui has not been opened yet. */ public Inventory getInventory() { return inventory; } diff --git a/src/main/java/fr/zcraft/zlib/components/i18n/I.java b/src/main/java/fr/zcraft/zlib/components/i18n/I.java index e3b3b3ef..a1de0dc1 100644 --- a/src/main/java/fr/zcraft/zlib/components/i18n/I.java +++ b/src/main/java/fr/zcraft/zlib/components/i18n/I.java @@ -87,6 +87,16 @@ public static String t(String text, Object... parameters) { return I18n.translate(null, null, text, null, null, parameters); } + + public static String tl(Locale locale, LazyTranslation text, Object... parameters) + { + return I18n.translate(locale, text, parameters); + } + + public static String tl(LazyTranslation text, Object... parameters) + { + return tl(null, text, parameters); + } /** * Translates the string with a plural. @@ -304,5 +314,24 @@ public static void sendTcn(Player player, String context, String singular, Strin { player.sendMessage(I18n.translate(I18n.getPlayerLocale(player), context, singular, plural, count, parameters)); } + + public static LazyTranslation l(String messageId) + { + return lcn(null, messageId, null, null); + } + public static LazyTranslation ln(String messageId, String pluralMessageId, Integer count) + { + return lcn(null, messageId, pluralMessageId, count); + } + + public static LazyTranslation lc(String messageId, Integer count) + { + return lcn(null, messageId, null, count); + } + + public static LazyTranslation lcn(String context, String messageId, String pluralMessageId, Integer count) + { + return new LazyTranslation(messageId, pluralMessageId, count, context); + } } diff --git a/src/main/java/fr/zcraft/zlib/components/i18n/I18n.java b/src/main/java/fr/zcraft/zlib/components/i18n/I18n.java index 7260613d..0ec960fc 100644 --- a/src/main/java/fr/zcraft/zlib/components/i18n/I18n.java +++ b/src/main/java/fr/zcraft/zlib/components/i18n/I18n.java @@ -647,6 +647,21 @@ public static String translate(String context, String messageId, String messageI return translate(null, context, messageId, messageIdPlural, count, parameters); } + /** + * Translates the given string. + * + * @param locale The locale to use to translate the string. + * @param text The text to translate + * @param parameters The parameters, replacing values like {@code {0}} in the translated + * string. + * + * @return The translated text, with the parameters replaced by their values. + */ + public static String translate(Locale locale, LazyTranslation text, Object... parameters) + { + return translate(locale, text.getContext(), text.getMessageId(), text.getPluralMessageId(), text.getCount(), parameters); + } + /** * Replaces some formatting codes into system codes. * diff --git a/src/main/java/fr/zcraft/zlib/components/i18n/LazyTranslation.java b/src/main/java/fr/zcraft/zlib/components/i18n/LazyTranslation.java new file mode 100644 index 00000000..222a09f5 --- /dev/null +++ b/src/main/java/fr/zcraft/zlib/components/i18n/LazyTranslation.java @@ -0,0 +1,84 @@ +/* + * Copyright or © or Copr. ZLib contributors (2015 - 2016) + * + * This software is governed by the CeCILL-B license under French law and + * abiding by the rules of distribution of free software. You can use, + * modify and/ or redistribute the software under the terms of the CeCILL-B + * license as circulated by CEA, CNRS and INRIA at the following URL + * "http://www.cecill.info". + * + * As a counterpart to the access to the source code and rights to copy, + * modify and redistribute granted by the license, users are provided only + * with a limited warranty and the software's author, the holder of the + * economic rights, and the successive licensors have only limited + * liability. + * + * In this respect, the user's attention is drawn to the risks associated + * with loading, using, modifying and/or developing or reproducing the + * software by the user in light of its specific status of free software, + * that may mean that it is complicated to manipulate, and that also + * therefore means that it is reserved for developers and experienced + * professionals having in-depth computer knowledge. Users are therefore + * encouraged to load and test the software's suitability as regards their + * requirements in conditions enabling the security of their systems and/or + * data to be ensured and, more generally, to use and operate it in the + * same conditions as regards security. + * + * The fact that you are presently reading this means that you have had + * knowledge of the CeCILL-B license and that you accept its terms. + */ + +package fr.zcraft.zlib.components.i18n; + +public class LazyTranslation +{ + private final String messageId; + private final String pluralMessageId; + private final Integer count; + private final String context; + + private Object[] parameters; + + public LazyTranslation(String messageId, String pluralMessageId, Integer count, String context) + { + this.messageId = messageId; + this.pluralMessageId = pluralMessageId; + this.count = count; + this.context = context; + } + + public LazyTranslation(String messageId) + { + this(messageId, null, null, null); + } + + public String getMessageId() + { + return messageId; + } + + public String getPluralMessageId() + { + return pluralMessageId; + } + + public Integer getCount() + { + return count; + } + + public String getContext() + { + return context; + } + + public Object[] getParameters() + { + return parameters; + } + + public void setParameters(Object[] parameters) + { + this.parameters = parameters; + } +} diff --git a/src/main/java/fr/zcraft/zlib/components/rawtext/RawTextPart.java b/src/main/java/fr/zcraft/zlib/components/rawtext/RawTextPart.java index 964385fc..7b6f99d4 100644 --- a/src/main/java/fr/zcraft/zlib/components/rawtext/RawTextPart.java +++ b/src/main/java/fr/zcraft/zlib/components/rawtext/RawTextPart.java @@ -28,13 +28,15 @@ * knowledge of the CeCILL-B license and that you accept its terms. */ - package fr.zcraft.zlib.components.rawtext; import com.google.common.base.CaseFormat; import fr.zcraft.zlib.components.commands.Command; import fr.zcraft.zlib.components.commands.Commands; +import fr.zcraft.zlib.components.i18n.I; +import fr.zcraft.zlib.components.i18n.LazyTranslation; import fr.zcraft.zlib.tools.PluginLogger; +import fr.zcraft.zlib.tools.items.ItemStackBuilder; import fr.zcraft.zlib.tools.items.ItemUtils; import fr.zcraft.zlib.tools.reflection.NMSException; import org.bukkit.Achievement; @@ -50,17 +52,19 @@ import java.net.URISyntaxException; import java.util.ArrayList; import java.util.Iterator; +import java.util.Locale; + public abstract class RawTextPart> implements Iterable, JSONAware { - static private enum ActionClick + private enum ActionClick { OPEN_URL, RUN_COMMAND, SUGGEST_COMMAND } - static private enum ActionHover + private enum ActionHover { SHOW_TEXT, SHOW_ACHIEVEMENT, @@ -68,8 +72,12 @@ static private enum ActionHover SHOW_ENTITY } - private String text; + private String text = null; + private LazyTranslation lazyTranslation = null; + private Object[] textParameters = null; private boolean translate = false; + + private Locale locale = null; private final RawTextPart parent; private final ArrayList extra = new ArrayList<>(); @@ -95,7 +103,7 @@ static private enum ActionHover RawTextPart() { - this(null); + this((String) null); } RawTextPart(String text) @@ -103,12 +111,25 @@ static private enum ActionHover this(text, null); } + RawTextPart(LazyTranslation text) + { + this(null, text); + } + RawTextPart(String text, RawTextPart parent) { this.text = text; this.parent = parent; } + RawTextPart(RawTextPart parent, LazyTranslation text, Object... parameters) + { + this.lazyTranslation = text; + this.parent = parent; + this.textParameters = parameters; + } + + /** * Starts a new text component with no predefined text. * @@ -117,7 +138,7 @@ static private enum ActionHover */ public RawTextPart then() { - return then(null); + return then((String) null); } /** @@ -134,6 +155,15 @@ public RawTextPart then(String text) return newPart; } + public RawTextPart then(LazyTranslation text, Object... parameters) + { + RawTextPart root = getRoot(); + RawTextPart newPart = new RawTextSubPart(root, text, parameters); + + root.extra.add(newPart); + return newPart; + } + /** * Sets the text of this component. * @@ -143,10 +173,22 @@ public RawTextPart then(String text) public T text(String text) { this.text = text; + this.lazyTranslation = null; + this.translate = false; + + return (T)this; + } + + public T text(LazyTranslation text, Object... parameters) + { + this.text = null; + this.lazyTranslation = text; this.translate = false; + this.textParameters = parameters; return (T)this; } + /** * Sets the text to be a translation key. This have to be a valid key in the Minecraft translation files. @@ -157,6 +199,7 @@ public T text(String text) public T translate(String text) { this.text = text; + this.lazyTranslation = null; this.translate = true; return (T)this; @@ -185,6 +228,20 @@ public T translate(ItemStack item) return translate(trName); } + public T locale(Locale locale) + { + this.locale = locale; + return (T)this; + } + + public Locale getLocale() + { + if(this.locale == null && this.parent != null) + return this.parent.getLocale(); + return this.locale; + } + + /** * Sets the color of this text component. * @@ -300,6 +357,9 @@ public T hover(String hoverText) */ public T hover(RawTextPart hoverText) { + if(hoverText.getLocale() == null) + hoverText.locale(getLocale()); + return hover(ActionHover.SHOW_TEXT, hoverText.build()); } @@ -349,6 +409,13 @@ public T hover(ItemStack item) return hover(ActionHover.SHOW_ITEM, RawText.toJSONString(item)); } + public T hover(ItemStackBuilder item) + { + if(item.getLocale() != null) + item.locale(getLocale()); + return hover(item.item()); + } + /** * Adds an hover entity to this component. * @@ -536,7 +603,7 @@ public JSONObject toJSON() } else { - obj.put("text", text); + obj.put("text", getText()); } if(!extra.isEmpty()) @@ -599,7 +666,7 @@ public String toPlainText() private void writePlainText(StringBuilder buf) { - buf.append(text); + buf.append(getText()); for(RawTextPart subPart : this) { @@ -626,14 +693,23 @@ private void writeFormattedText(StringBuilder buf) if(strikethrough) buf.append(ChatColor.STRIKETHROUGH); if(obfuscated) buf.append(ChatColor.MAGIC); - buf.append(text); + buf.append(getText()); buf.append(ChatColor.RESET); for(RawTextPart subPart : this) { subPart.writeFormattedText(buf); } + } + + private String getText() + { + if(lazyTranslation != null) + { + return I.tl(getLocale(), lazyTranslation, textParameters); + } + return text; } @Override diff --git a/src/main/java/fr/zcraft/zlib/components/rawtext/RawTextSubPart.java b/src/main/java/fr/zcraft/zlib/components/rawtext/RawTextSubPart.java index ee6788e2..9abbf335 100644 --- a/src/main/java/fr/zcraft/zlib/components/rawtext/RawTextSubPart.java +++ b/src/main/java/fr/zcraft/zlib/components/rawtext/RawTextSubPart.java @@ -30,6 +30,8 @@ package fr.zcraft.zlib.components.rawtext; +import fr.zcraft.zlib.components.i18n.LazyTranslation; + class RawTextSubPart extends RawTextPart { public RawTextSubPart(String text) @@ -37,8 +39,18 @@ public RawTextSubPart(String text) super(text); } + public RawTextSubPart(LazyTranslation text) + { + super(text); + } + public RawTextSubPart(String text, RawTextPart parent) { super(text, parent); } + + public RawTextSubPart(RawTextPart parent, LazyTranslation text, Object... parameters) + { + super(parent, text, parameters); + } } diff --git a/src/main/java/fr/zcraft/zlib/tools/items/ItemStackBuilder.java b/src/main/java/fr/zcraft/zlib/tools/items/ItemStackBuilder.java index 53bb78be..1c3b1972 100644 --- a/src/main/java/fr/zcraft/zlib/tools/items/ItemStackBuilder.java +++ b/src/main/java/fr/zcraft/zlib/tools/items/ItemStackBuilder.java @@ -31,6 +31,8 @@ package fr.zcraft.zlib.tools.items; import fr.zcraft.zlib.components.gui.GuiUtils; +import fr.zcraft.zlib.components.i18n.I; +import fr.zcraft.zlib.components.i18n.LazyTranslation; import fr.zcraft.zlib.components.nbt.NBT; import fr.zcraft.zlib.components.nbt.NBTCompound; import fr.zcraft.zlib.components.rawtext.RawText; @@ -48,8 +50,10 @@ import java.util.ArrayList; import java.util.Arrays; +import java.util.Collection; import java.util.HashMap; import java.util.List; +import java.util.Locale; import java.util.Map; import java.util.Map.Entry; @@ -135,6 +139,8 @@ public class ItemStackBuilder private DyeColor dye = null; + private Locale locale = null; + /** * Creates a new ItemStackBuilder. */ @@ -230,7 +236,12 @@ public ItemStack item() ItemMeta meta = newItemStack.getItemMeta(); if (title != null) + { + if(title.getLocale() == null) + title.locale(locale); + meta.setDisplayName(ChatColor.RESET + title.build().toFormattedText()); + } if (!loreLines.isEmpty() || resetLore) meta.setLore(loreLines); @@ -329,7 +340,7 @@ public ItemStackBuilder title(RawTextPart text) { if (this.title != null) throw new IllegalStateException("Title has already been defined."); - + this.title = text; return this; } @@ -359,6 +370,20 @@ public ItemStackBuilder title(ChatColor color, String... texts) return this; } + /** + * Sets the title of the ItemStack. If a text has already been defined, it + * will be appended to the already existing one. + * + * @param text The text. It will be translated using the locale currently set for the ItemStack. + * @param parameters Parameters for the translatable format text, if any. + * + * @return The current ItemStackBuilder instance, for methods chaining. + */ + public ItemStackBuilder title(LazyTranslation text, Object ...parameters) + { + return title(I.tl(locale, text, parameters)); + } + /** * Sets the title of the ItemStack. If a text has already been defined, it * will be appended to the already existing one. @@ -370,7 +395,7 @@ public ItemStackBuilder title(ChatColor color, String... texts) */ public ItemStackBuilder title(String... texts) { - return title(null, texts); + return title((ChatColor) null, texts); } /** @@ -393,7 +418,7 @@ public ItemStackBuilder lore(String... lines) * * @return The current ItemStackBuilder instance, for methods chaining. */ - public ItemStackBuilder lore(List lines) + public ItemStackBuilder lore(Collection lines) { loreLines.addAll(lines); return this; @@ -409,7 +434,34 @@ public ItemStackBuilder lore(List lines) */ public ItemStackBuilder loreLine(String... text) { - return loreLine(null, text); + return loreLine((ChatColor) null, text); + } + + /** + * Adds one line of lore to the ItemStack. + * + * @param text The lore line's text. It will be translated using the locale currently set for the ItemStack. + * @param parameters Parameters for the translatable format text, if any. + * + * @return The current ItemStackBuilder instance, for methods chaining. + */ + public ItemStackBuilder loreLine(LazyTranslation text, Object ...parameters) + { + return loreLine(I.tl(locale, text, parameters)); + } + + /** + * Adds one line of lore to the ItemStack. + * This method is an alias for {@link #loreLine(LazyTranslation, java.lang.Object...) }. + * + * @param text The lore line's text. It will be translated using the locale currently set for the ItemStack. + * @param parameters Parameters for the translatable format text, if any. + * + * @return The current ItemStackBuilder instance, for methods chaining. + */ + public ItemStackBuilder lore(LazyTranslation text, Object ...parameters) + { + return loreLine(text, parameters); } /** @@ -440,6 +492,9 @@ public ItemStackBuilder loreLine(ChatColor color, String... text) */ public ItemStackBuilder loreLine(RawTextPart rawText) { + if(rawText.getLocale() == null) + rawText.locale(locale); + return loreLine(rawText.toFormattedText()); } @@ -462,7 +517,7 @@ public ItemStackBuilder longLore(String text) * characters, so the tooltip is not too large. * * @param text The text. - * @param lineLength The max length of a line. + * @param lineLength The maximum length of a line. * * @return The current ItemStackBuilder instance, for methods chaining. * @see GuiUtils#generateLore(String, int) @@ -493,7 +548,7 @@ public ItemStackBuilder longLore(ChatColor color, String text) * * @param color The color for this line of lore. * @param text The text. - * @param lineLength The max length of a line. + * @param lineLength The maximum length of a line. * * @return The current ItemStackBuilder instance, for methods chaining. * @see GuiUtils#generateLore(String, int) @@ -502,6 +557,35 @@ public ItemStackBuilder longLore(ChatColor color, String text, int lineLength) { return longLore(color + text, lineLength); } + + /** + * Adds a line of lore and wraps it to lines of {@code lineLength} + * characters, so the tooltip is not too large. + * + * @param lineLength The maximum length of a line. + * @param text The lore line's text. It will be translated using the locale currently set for the ItemStack. + * @param parameters Parameters for the translatable format text, if any. + * + * @return The current ItemStackBuilder instance, for methods chaining. + */ + public ItemStackBuilder longLore(int lineLength, LazyTranslation text, Object ...parameters) + { + return longLore(I.tl(locale, text, parameters), lineLength); + } + + /** + * Adds a line of lore and wraps it to lines of {@code lineLength} + * characters, so the tooltip is not too large. + * + * @param text The lore line's text. It will be translated using the locale currently set for the ItemStack. + * @param parameters Parameters for the translatable format text, if any. + * + * @return The current ItemStackBuilder instance, for methods chaining. + */ + public ItemStackBuilder longLore(LazyTranslation text, Object ...parameters) + { + return longLore(I.tl(locale, text, parameters)); + } /** * Adds an empty line in the lore to act as a separator. @@ -684,6 +768,26 @@ public ItemStackBuilder replaceNBT() this.replaceNBT = true; return this; } + + /** + * Sets the locale used to translate different strings of the ItemStack. + * @param locale The locale to use. + * @return The current ItemStackBuilder instance, for methods chaining. + */ + public ItemStackBuilder locale(Locale locale) + { + this.locale = locale; + return this; + } + + /** + * Retreives the locale assigned to this ItemStackBuilder + * @return the locale assigned to this ItemStackBuilder, or null if not defined. + */ + public Locale getLocale() + { + return this.locale; + } /** * Concatenates a String[] to a single one. diff --git a/src/main/java/fr/zcraft/zlib/tools/items/ItemUtils.java b/src/main/java/fr/zcraft/zlib/tools/items/ItemUtils.java index b9ad8dae..30f3857e 100644 --- a/src/main/java/fr/zcraft/zlib/tools/items/ItemUtils.java +++ b/src/main/java/fr/zcraft/zlib/tools/items/ItemUtils.java @@ -29,6 +29,8 @@ */ package fr.zcraft.zlib.tools.items; +import fr.zcraft.zlib.components.i18n.I; +import fr.zcraft.zlib.components.i18n.LazyTranslation; import fr.zcraft.zlib.tools.PluginLogger; import fr.zcraft.zlib.tools.reflection.NMSException; import fr.zcraft.zlib.tools.reflection.Reflection; @@ -49,6 +51,7 @@ import java.lang.reflect.Type; import java.util.ArrayList; import java.util.List; +import java.util.Locale; import java.util.Random; //import org.bukkit.Sound; diff --git a/src/main/java/fr/zcraft/zlib/tools/text/ListHeaderFooter.java b/src/main/java/fr/zcraft/zlib/tools/text/ListHeaderFooter.java index 0c64ee3b..57c06071 100644 --- a/src/main/java/fr/zcraft/zlib/tools/text/ListHeaderFooter.java +++ b/src/main/java/fr/zcraft/zlib/tools/text/ListHeaderFooter.java @@ -29,6 +29,7 @@ */ package fr.zcraft.zlib.tools.text; +import fr.zcraft.zlib.components.i18n.I18n; import fr.zcraft.zlib.components.rawtext.RawText; import fr.zcraft.zlib.exceptions.IncompatibleMinecraftVersionException; import fr.zcraft.zlib.tools.reflection.NMSNetwork; @@ -37,6 +38,7 @@ import org.bukkit.entity.Player; import java.lang.reflect.InvocationTargetException; +import java.util.Locale; /** @@ -108,6 +110,17 @@ public static void sendListHeaderFooter(Player player, String header, String foo */ public static void sendListHeaderFooter(Player player, RawText header, RawText footer) { + Locale playerLocale = null; + + if(header != null || footer != null) + playerLocale = I18n.getPlayerLocale(player); + + if(header != null && header.getLocale() != null) + header.locale(playerLocale); + + if(footer != null && footer.getLocale() != null) + footer.locale(playerLocale); + sendRawListHeaderFooter( player, header != null ? header.toJSONString() : "{\"text\": \"\"}", diff --git a/src/main/java/fr/zcraft/zlib/tools/text/MessageSender.java b/src/main/java/fr/zcraft/zlib/tools/text/MessageSender.java index 31032323..a5c95354 100644 --- a/src/main/java/fr/zcraft/zlib/tools/text/MessageSender.java +++ b/src/main/java/fr/zcraft/zlib/tools/text/MessageSender.java @@ -29,6 +29,7 @@ */ package fr.zcraft.zlib.tools.text; +import fr.zcraft.zlib.components.i18n.I18n; import fr.zcraft.zlib.components.rawtext.RawText; import fr.zcraft.zlib.tools.PluginLogger; import fr.zcraft.zlib.tools.reflection.NMSNetwork; @@ -157,6 +158,8 @@ public static boolean sendMessage(Player receiver, String message, MessageType t */ public static boolean sendMessage(Player receiver, RawText message, MessageType type) { + if(message.getLocale() != null) + message.locale(I18n.getPlayerLocale(receiver)); return sendChatPacket(receiver, type.isJSON() ? message.toJSONString() : message.toFormattedText(), type); } diff --git a/src/main/java/fr/zcraft/zlib/tools/text/RawMessage.java b/src/main/java/fr/zcraft/zlib/tools/text/RawMessage.java index 0086831a..954d2017 100644 --- a/src/main/java/fr/zcraft/zlib/tools/text/RawMessage.java +++ b/src/main/java/fr/zcraft/zlib/tools/text/RawMessage.java @@ -29,6 +29,7 @@ */ package fr.zcraft.zlib.tools.text; +import fr.zcraft.zlib.components.i18n.I18n; import fr.zcraft.zlib.components.rawtext.RawText; import org.bukkit.Bukkit; import org.bukkit.command.CommandException; @@ -63,6 +64,9 @@ public static void send(CommandSender commandSender, RawText text) { if(commandSender instanceof Player) { + if(text.getLocale() == null) + text.locale(I18n.getPlayerLocale((Player) commandSender)); + send((Player) commandSender, text.toJSONString()); } else diff --git a/src/main/java/fr/zcraft/zlib/tools/text/Titles.java b/src/main/java/fr/zcraft/zlib/tools/text/Titles.java index 92ff8e71..c646d3d6 100644 --- a/src/main/java/fr/zcraft/zlib/tools/text/Titles.java +++ b/src/main/java/fr/zcraft/zlib/tools/text/Titles.java @@ -29,6 +29,7 @@ */ package fr.zcraft.zlib.tools.text; +import fr.zcraft.zlib.components.i18n.I18n; import fr.zcraft.zlib.components.rawtext.RawText; import fr.zcraft.zlib.exceptions.IncompatibleMinecraftVersionException; import fr.zcraft.zlib.tools.reflection.NMSNetwork; @@ -37,6 +38,7 @@ import org.bukkit.entity.Player; import java.lang.reflect.InvocationTargetException; +import java.util.Locale; /** @@ -144,6 +146,17 @@ public static void displayTitle(Player player, int fadeIn, int stay, int fadeOut */ public static void displayTitle(Player player, int fadeIn, int stay, int fadeOut, RawText title, RawText subtitle) { + Locale playerLocale = null; + + if(title != null || subtitle != null) + playerLocale = I18n.getPlayerLocale(player); + + if(title != null && title.getLocale() != null) + title.locale(playerLocale); + + if(subtitle != null && subtitle.getLocale() != null) + subtitle.locale(playerLocale); + displayRawTitle( player, fadeIn, stay, fadeOut, title != null ? title.toJSONString() : "{\"text\": \"\"}",