From 8c538dca19e208b45f371538b157b00b0fa93d43 Mon Sep 17 00:00:00 2001 From: Josh Roy <10731363+JRoy@users.noreply.github.com> Date: Tue, 28 Dec 2021 18:13:03 -0500 Subject: [PATCH] i hate this i hate this i hate this i hate this i hate this i hate this i hate this i hate this i hate this i hate this i hate this i hate this i hate this i hate this i hate this i hate this i hate this i hate this i hate this i hate this i hate this i hate this i hate this i hate this i hate this i hate this i hate this i hate this i hate this i hate this i hate this i hate this i hate this i hate this i hate this i hate this i hate this i hate this i hate this i hate this i hate this i hate this i hate this i hate this i hate this i hate this i hate this i hate this i hate this i hate this i hate this i hate this i hate this i hate this i hate this i hate this i hate this i hate this i hate this i hate this i hate this i hate this i hate this i hate this i hate this i hate this i hate this i hate this i hate this i hate this i hate this i hate this i hate this i hate this i hate this i hate this i hate this i hate this i hate this i hate this i hate this --- .../java/com/earth2me/essentials/Console.java | 4 +-- .../com/earth2me/essentials/Essentials.java | 24 +++++++++++---- .../java/com/earth2me/essentials/I18n.java | 29 ++++++++++++------- .../essentials/commands/Commandbroadcast.java | 4 +-- .../textreader/KeywordReplacer.java | 4 +-- 5 files changed, 40 insertions(+), 25 deletions(-) diff --git a/Essentials/src/main/java/com/earth2me/essentials/Console.java b/Essentials/src/main/java/com/earth2me/essentials/Console.java index dff940f3c38..da6543cc6a1 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/Console.java +++ b/Essentials/src/main/java/com/earth2me/essentials/Console.java @@ -8,11 +8,11 @@ import java.util.UUID; -import static com.earth2me.essentials.I18n.tl; +import static com.earth2me.essentials.I18n.tlLiteral; public final class Console implements IMessageRecipient { public static final String NAME = "Console"; - public static final String DISPLAY_NAME = tl("consoleName"); + public static final String DISPLAY_NAME = tlLiteral("consoleName"); private static Console instance; // Set in essentials private final IEssentials ess; diff --git a/Essentials/src/main/java/com/earth2me/essentials/Essentials.java b/Essentials/src/main/java/com/earth2me/essentials/Essentials.java index b099009f412..df64f6bf695 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/Essentials.java +++ b/Essentials/src/main/java/com/earth2me/essentials/Essentials.java @@ -1153,22 +1153,26 @@ private int broadcastMessage(final IUser sender, final String permission, final } @Override - public void broadcastTl(String tlKey, Object... args) { - broadcastTl(null, tlKey, args); + public void broadcastTl(final String tlKey, final Object... args) { + broadcastTl(null, null, true, tlKey, args); } @Override - public void broadcastTl(IUser sender, String tlKey, Object... args) { + public void broadcastTl(final IUser sender, final String tlKey, final Object... args) { broadcastTl(sender, (Predicate) null, tlKey, args); } @Override - public void broadcastTl(IUser sender, String permission, String tlKey, Object... args) { + public void broadcastTl(final IUser sender, final String permission, final String tlKey, final Object... args) { broadcastTl(sender, u -> !u.isAuthorized(permission), tlKey, args); } @Override - public void broadcastTl(IUser sender, Predicate shouldExclude, String tlKey, Object... args) { + public void broadcastTl(final IUser sender, final Predicate shouldExclude, final String tlKey, final Object... args) { + broadcastTl(sender, shouldExclude, false, tlKey, args); + } + + private void broadcastTl(IUser sender, Predicate shouldExclude, boolean parseKeywords, String tlKey, Object... args) { if (sender != null && sender.isHidden()) { return; } @@ -1182,7 +1186,15 @@ public void broadcastTl(IUser sender, Predicate shouldExclude, String tlK continue; } - user.sendTl(tlKey, args); + final Object[] processedArgs; + // FUCK THIS STUPID BULLSHIT + if (parseKeywords) { + processedArgs = I18n.mutateArgs(args, s -> new KeywordReplacer(new SimpleTextInput(s), new CommandSource(user.getBase()), this, false).getLines().get(0)); + } else { + processedArgs = args; + } + + user.sendTl(tlKey, processedArgs); } } diff --git a/Essentials/src/main/java/com/earth2me/essentials/I18n.java b/Essentials/src/main/java/com/earth2me/essentials/I18n.java index 7e367afe8d3..409462544d4 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/I18n.java +++ b/Essentials/src/main/java/com/earth2me/essentials/I18n.java @@ -23,6 +23,7 @@ import java.util.MissingResourceException; import java.util.PropertyResourceBundle; import java.util.ResourceBundle; +import java.util.function.Function; import java.util.logging.Level; import java.util.logging.Logger; import java.util.regex.Pattern; @@ -55,9 +56,11 @@ public I18n(final IEssentials ess) { localeBundle = defaultBundle; } + /* public static String tl(final String string, final Object... objects) { return tlLiteral(string, objects); } + */ public static String tlLiteral(final String string, final Object... objects) { if (instance == null) { @@ -152,17 +155,7 @@ public String format(final Locale locale, final String string, final Object... o final Object[] processedArgs; if (miniMessage) { - final Object[] args = new Object[objects.length]; - for (int i = 0; i < objects.length; i++) { - final Object object = objects[i]; - // MessageFormat will format these itself, troll face. - if (object instanceof Number || object instanceof Date) { - args[i] = object; - } else { - args[i] = object != null ? MiniMessage.miniMessage().escapeTokens(object.toString()) : null; - } - } - processedArgs = args; + processedArgs = mutateArgs(objects, s -> MiniMessage.miniMessage().escapeTokens(s)); } else { processedArgs = objects; } @@ -170,6 +163,20 @@ public String format(final Locale locale, final String string, final Object... o return messageFormat.format(processedArgs).replace(' ', ' '); // replace nbsp with a space } + public static Object[] mutateArgs(final Object[] objects, final Function mutator) { + final Object[] args = new Object[objects.length]; + for (int i = 0; i < objects.length; i++) { + final Object object = objects[i]; + // MessageFormat will format these itself, troll face. + if (object instanceof Number || object instanceof Date || object == null) { + args[i] = object; + continue; + } + args[i] = mutator.apply(object.toString()); + } + return args; + } + public void updateLocale(final String loc) { if (loc != null && !loc.isEmpty()) { currentLocale = getLocale(loc); diff --git a/Essentials/src/main/java/com/earth2me/essentials/commands/Commandbroadcast.java b/Essentials/src/main/java/com/earth2me/essentials/commands/Commandbroadcast.java index 4e566795117..029fe9f13b7 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/commands/Commandbroadcast.java +++ b/Essentials/src/main/java/com/earth2me/essentials/commands/Commandbroadcast.java @@ -1,8 +1,6 @@ package com.earth2me.essentials.commands; import com.earth2me.essentials.CommandSource; -import com.earth2me.essentials.textreader.KeywordReplacer; -import com.earth2me.essentials.textreader.SimpleTextInput; import com.earth2me.essentials.utils.FormatUtil; import org.bukkit.Server; @@ -17,6 +15,6 @@ public void run(final Server server, final CommandSource sender, final String co throw new NotEnoughArgumentsException(); } - ess.broadcastTl("broadcast", new KeywordReplacer(new SimpleTextInput(FormatUtil.replaceFormat(getFinalArg(args, 0)).replace("\\n", "\n")), sender, ess).getLines().get(0), sender.getDisplayName()); + ess.broadcastTl("broadcast", FormatUtil.replaceFormat(getFinalArg(args, 0)).replace("\\n", "\n"), sender.getDisplayName()); } } diff --git a/Essentials/src/main/java/com/earth2me/essentials/textreader/KeywordReplacer.java b/Essentials/src/main/java/com/earth2me/essentials/textreader/KeywordReplacer.java index 3bc099e0812..8f00e21c902 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/textreader/KeywordReplacer.java +++ b/Essentials/src/main/java/com/earth2me/essentials/textreader/KeywordReplacer.java @@ -30,8 +30,6 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; -import static com.earth2me.essentials.I18n.tl; - //When adding a keyword here, you also need to add the implementation above enum KeywordType { PLAYER(KeywordCachable.CACHEABLE), @@ -337,7 +335,7 @@ private String replaceLine(String line, final String fullMatch, final String[] m case COORDS: if (user != null) { final Location location = user.getLocation(); - replacer = tl("coordsKeyword", location.getBlockX(), location.getBlockY(), location.getBlockZ()); + replacer = user.playerTl("coordsKeyword", location.getBlockX(), location.getBlockY(), location.getBlockZ()); } break; case TPS: