From 2f385589e9a9bccecd6123d4526cf9eac2a0f02b 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 | 27 ++++++++++-------- .../com/earth2me/essentials/IEssentials.java | 2 +- .../essentials/commands/Commandbroadcast.java | 4 +-- .../commands/Commandbroadcastworld.java | 28 ++----------------- .../textreader/KeywordReplacer.java | 4 +-- 7 files changed, 40 insertions(+), 53 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..c72b56b8cc4 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/Essentials.java +++ b/Essentials/src/main/java/com/earth2me/essentials/Essentials.java @@ -1153,22 +1153,22 @@ 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) { - broadcastTl(sender, (Predicate) null, tlKey, args); + public void broadcastTl(final IUser sender, final String tlKey, final Object... args) { + broadcastTl(sender, null, false, tlKey, args); } @Override - public void broadcastTl(IUser sender, String permission, String tlKey, Object... args) { - broadcastTl(sender, u -> !u.isAuthorized(permission), tlKey, args); + public void broadcastTl(final IUser sender, final String permission, final String tlKey, final Object... args) { + broadcastTl(sender, u -> !u.isAuthorized(permission), false, tlKey, args); } @Override - public void broadcastTl(IUser sender, Predicate shouldExclude, String tlKey, Object... args) { + public void broadcastTl(final IUser sender, final Predicate shouldExclude, final boolean parseKeywords, final String tlKey, final Object... args) { if (sender != null && sender.isHidden()) { return; } @@ -1182,7 +1182,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..08dfb91e866 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; @@ -152,17 +153,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 +161,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/IEssentials.java b/Essentials/src/main/java/com/earth2me/essentials/IEssentials.java index ce278bb0d34..24dc3f6b001 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/IEssentials.java +++ b/Essentials/src/main/java/com/earth2me/essentials/IEssentials.java @@ -83,7 +83,7 @@ public interface IEssentials extends Plugin { void broadcastTl(IUser sender, String tlKey, Object... args); - void broadcastTl(IUser sender, Predicate shouldExclude, String tlKey, Object... args); + void broadcastTl(IUser sender, Predicate shouldExclude, boolean parseKeywords, String tlKey, Object... args); void broadcastTl(IUser sender, String permission, String tlKey, Object... args); 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/commands/Commandbroadcastworld.java b/Essentials/src/main/java/com/earth2me/essentials/commands/Commandbroadcastworld.java index b062824b4f5..e01239a5ab2 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/commands/Commandbroadcastworld.java +++ b/Essentials/src/main/java/com/earth2me/essentials/commands/Commandbroadcastworld.java @@ -2,21 +2,14 @@ import com.earth2me.essentials.CommandSource; import com.earth2me.essentials.User; -import com.earth2me.essentials.textreader.IText; -import com.earth2me.essentials.textreader.KeywordReplacer; -import com.earth2me.essentials.textreader.SimpleTextInput; -import com.earth2me.essentials.utils.FormatUtil; import com.google.common.collect.Lists; +import net.ess3.api.TranslatableException; import org.bukkit.Server; import org.bukkit.World; -import org.bukkit.entity.Player; -import java.util.Collection; import java.util.Collections; import java.util.List; -import static com.earth2me.essentials.I18n.tl; - public class Commandbroadcastworld extends EssentialsCommand { public Commandbroadcastworld() { @@ -50,7 +43,7 @@ public void run(final Server server, final CommandSource sender, final String co final World world = ess.getWorld(args[0]); if (world == null) { - throw new Exception(tl("invalidWorld")); + throw new TranslatableException("invalidWorld"); } sendBroadcast(world, sender.getSender().getName(), getFinalArg(args, 1)); } @@ -59,22 +52,7 @@ private void sendBroadcast(final World world, final String name, final String me if (message.isEmpty()) { throw new NotEnoughArgumentsException(); } - sendToWorld(world, tl("broadcast", FormatUtil.replaceFormat(message).replace("\\n", "\n"), name)); - } - - private void sendToWorld(final World world, final String message) { - IText broadcast = new SimpleTextInput(message); - final Collection players = ess.getOnlinePlayers(); - - for (final Player player : players) { - if (player.getWorld().equals(world)) { - final User user = ess.getUser(player); - broadcast = new KeywordReplacer(broadcast, new CommandSource(player), ess, false); - for (final String messageText : broadcast.getLines()) { - user.sendMessage(messageText); - } - } - } + ess.broadcastTl(null, u -> u.getBase().getWorld().equals(world), true, "broadcast", message, name); } @Override 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: