From 050fd885605c068b191308990859b1517d6d26aa Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Thu, 12 Jul 2018 23:27:17 +0200 Subject: [PATCH 1/4] Added API changes --- .../appendtext/AppendTextCommandBase.java | 20 ++++++++++--------- .../chat/listener/PlayerListener.java | 14 +++++-------- 2 files changed, 16 insertions(+), 18 deletions(-) diff --git a/src/main/java/buttondevteam/chat/commands/appendtext/AppendTextCommandBase.java b/src/main/java/buttondevteam/chat/commands/appendtext/AppendTextCommandBase.java index 50afc8f..01255df 100644 --- a/src/main/java/buttondevteam/chat/commands/appendtext/AppendTextCommandBase.java +++ b/src/main/java/buttondevteam/chat/commands/appendtext/AppendTextCommandBase.java @@ -2,14 +2,13 @@ package buttondevteam.chat.commands.appendtext; import buttondevteam.chat.ChatPlayer; import buttondevteam.chat.listener.PlayerListener; -import buttondevteam.lib.chat.Channel; -import buttondevteam.lib.chat.CommandClass; -import buttondevteam.lib.chat.TBMCChatAPI; -import buttondevteam.lib.chat.TBMCCommandBase; +import buttondevteam.lib.chat.*; import buttondevteam.lib.player.TBMCPlayer; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; +import java.util.UUID; + @CommandClass(modOnly = false, excludeFromPath = true) public abstract class AppendTextCommandBase extends TBMCCommandBase { @@ -22,14 +21,17 @@ public abstract class AppendTextCommandBase extends TBMCCommandBase { String msg = GetAppendedText(); for (int i = args.length - 1; i >= 0; i--) msg = args[i] + " " + msg; + ChatPlayer cp; if (sender instanceof Player) - TBMCChatAPI.SendChatMessage( - TBMCPlayer.getPlayer(((Player) sender).getUniqueId(), ChatPlayer.class).CurrentChannel, sender, - msg, true); + TBMCChatAPI.SendChatMessage(ChatMessage.builder( + (cp = TBMCPlayer.getPlayer(((Player) sender).getUniqueId(), ChatPlayer.class)).CurrentChannel, sender, + cp, msg).fromCommand(true).build()); else if (sender.isOp()) - TBMCChatAPI.SendChatMessage(PlayerListener.ConsoleChannel, sender, msg); + TBMCChatAPI.SendChatMessage(ChatMessage.builder(PlayerListener.ConsoleChannel, sender, + (cp = TBMCPlayer.getPlayer(new UUID(0, 0), ChatPlayer.class)), msg).build()); else - TBMCChatAPI.SendChatMessage(Channel.GlobalChat, sender, msg); + TBMCChatAPI.SendChatMessage(ChatMessage.builder(Channel.GlobalChat, sender, + (cp = TBMCPlayer.getPlayer(new UUID(0, 0), ChatPlayer.class)), msg).build()); //TODO return true; } } diff --git a/src/main/java/buttondevteam/chat/listener/PlayerListener.java b/src/main/java/buttondevteam/chat/listener/PlayerListener.java index d4bd7a1..1c379a8 100644 --- a/src/main/java/buttondevteam/chat/listener/PlayerListener.java +++ b/src/main/java/buttondevteam/chat/listener/PlayerListener.java @@ -5,10 +5,7 @@ import buttondevteam.chat.ChatProcessing; import buttondevteam.chat.PluginMain; import buttondevteam.lib.TBMCChatEvent; import buttondevteam.lib.TBMCCoreAPI; -import buttondevteam.lib.chat.Channel; -import buttondevteam.lib.chat.ChatChannelRegisterEvent; -import buttondevteam.lib.chat.ChatRoom; -import buttondevteam.lib.chat.TBMCChatAPI; +import buttondevteam.lib.chat.*; import buttondevteam.lib.player.ChromaGamerBase.InfoTarget; import buttondevteam.lib.player.TBMCPlayer; import buttondevteam.lib.player.TBMCPlayerGetInfoEvent; @@ -59,9 +56,8 @@ public class PlayerListener implements Listener { public void onPlayerChat(AsyncPlayerChatEvent event) { if (event.isCancelled()) return; - TBMCChatAPI.SendChatMessage( - TBMCPlayer.getPlayer(event.getPlayer().getUniqueId(), ChatPlayer.class).CurrentChannel, - event.getPlayer(), event.getMessage()); + ChatPlayer cp = TBMCPlayer.getPlayer(event.getPlayer().getUniqueId(), ChatPlayer.class); + TBMCChatAPI.SendChatMessage(ChatMessage.builder(cp.CurrentChannel, event.getPlayer(), cp, event.getMessage()).build()); event.setCancelled(true); // The custom event should only be cancelled when muted or similar } @@ -79,7 +75,7 @@ public class PlayerListener implements Listener { if (sender instanceof Player) mp = TBMCPlayer.getPlayer(((Player) sender).getUniqueId(), ChatPlayer.class); else - mp = null; + mp = TBMCPlayer.getPlayer(new UUID(0, 0), ChatPlayer.class); //Use 0, 0 for console and whatever - TODO: Refactor console stuff String cmd; final BiPredicate checkchid = (chan, cmd1) -> cmd1.equalsIgnoreCase(chan.ID) || (chan.IDs != null && Arrays.stream(chan.IDs).anyMatch(cmd1::equalsIgnoreCase)); if (index == -1) { // Only the command is run @@ -138,7 +134,7 @@ public class PlayerListener implements Listener { } else for (Channel channel : Channel.getChannels()) { if (checkchid.test(channel, cmd)) { //Apparently method references don't require final variables - TBMCChatAPI.SendChatMessage(channel, sender, message.substring(index + 1)); + TBMCChatAPI.SendChatMessage(ChatMessage.builder(channel, sender, mp, message.substring(index + 1)).build()); return true; } } -- 2.30.2 From 01562bf7696273f6e6eb1081f90e3055c48f3f85 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Sun, 15 Jul 2018 02:22:40 +0200 Subject: [PATCH 2/4] Using the CG object on chat This means user data will be available (again) if talking from Discord, for example. --- src/main/java/buttondevteam/chat/ChatProcessing.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/java/buttondevteam/chat/ChatProcessing.java b/src/main/java/buttondevteam/chat/ChatProcessing.java index 59e64a8..c323ded 100644 --- a/src/main/java/buttondevteam/chat/ChatProcessing.java +++ b/src/main/java/buttondevteam/chat/ChatProcessing.java @@ -104,9 +104,11 @@ public class ChatProcessing { doFunStuff(sender, e, message); - ChatPlayer mp = null; + ChatPlayer mp; if (player != null) mp = TBMCPlayerBase.getPlayer(player.getUniqueId(), ChatPlayer.class); + else //Due to the online player map, getPlayer() can be more efficient than getAs() + mp = e.getUser().getAs(ChatPlayer.class); //May be null Color colormode = channel.color; if (mp != null && mp.OtherColorMode != null) -- 2.30.2 From e6ac18e0c7d8c9f2e178dc4b926132cbb9576b1b Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Tue, 17 Jul 2018 17:06:42 +0200 Subject: [PATCH 3/4] Fixed hover info for Discord --- src/main/java/buttondevteam/chat/ChatProcessing.java | 10 +++++----- src/test/java/buttondevteam/chat/ChatFormatIT.java | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/main/java/buttondevteam/chat/ChatProcessing.java b/src/main/java/buttondevteam/chat/ChatProcessing.java index c323ded..f363fb6 100644 --- a/src/main/java/buttondevteam/chat/ChatProcessing.java +++ b/src/main/java/buttondevteam/chat/ChatProcessing.java @@ -128,7 +128,7 @@ public class ChatProcessing { pingedconsole = false; // Will set it to true onmatch (static constructor) final String channelidentifier = getChannelID(channel, sender); - TellrawPart json = createTellraw(sender, message, player, mp, channelidentifier); + TellrawPart json = createTellraw(sender, message, player, mp, e.getUser(), channelidentifier); long combinetime = System.nanoTime(); ChatFormatter.Combine(formatters, message, json); combinetime = System.nanoTime() - combinetime; @@ -191,8 +191,8 @@ public class ChatProcessing { return gson.toJson(json); } - static TellrawPart createTellraw(CommandSender sender, String message, @Nullable Player player, @Nullable ChatPlayer mp, - final String channelidentifier) { + static TellrawPart createTellraw(CommandSender sender, String message, @Nullable Player player, + @Nullable ChatPlayer mp, @Nullable ChromaGamerBase cg, final String channelidentifier) { TellrawPart json = new TellrawPart(""); if (mp != null && mp.ChatOnly) { json.addExtra(new TellrawPart("[C]") @@ -213,8 +213,8 @@ public class ChatProcessing { .setHoverEvent(TellrawEvent.create(TellrawEvent.HoverAction.SHOW_TEXT, "Gold Patreon supporter"))); json.addExtra(new TellrawPart(" <")); TellrawPart hovertp = new TellrawPart(""); - if (mp != null) - hovertp.addExtra(new TellrawPart(mp.getInfo(ChromaGamerBase.InfoTarget.MCHover))); + if (cg != null) + hovertp.addExtra(new TellrawPart(cg.getInfo(ChromaGamerBase.InfoTarget.MCHover))); json.addExtra(new TellrawPart(getSenderName(sender, player)) .setHoverEvent(TellrawEvent.create(TellrawEvent.HoverAction.SHOW_TEXT, hovertp))); json.addExtra(new TellrawPart("> ")); diff --git a/src/test/java/buttondevteam/chat/ChatFormatIT.java b/src/test/java/buttondevteam/chat/ChatFormatIT.java index c59815b..af45011 100644 --- a/src/test/java/buttondevteam/chat/ChatFormatIT.java +++ b/src/test/java/buttondevteam/chat/ChatFormatIT.java @@ -66,11 +66,11 @@ public class ChatFormatIT { public void testMessage() { ArrayList cfs = ChatProcessing.addFormatters(Color.White); final String chid = ChatProcessing.getChannelID(Channel.GlobalChat, sender); - final TellrawPart tp = ChatProcessing.createTellraw(sender, message, null, null, chid); + final TellrawPart tp = ChatProcessing.createTellraw(sender, message, null, null, null, chid); ChatFormatter.Combine(cfs, message, tp); System.out.println("Testing: " + message); // System.out.println(ChatProcessing.toJson(tp)); - final TellrawPart expectedtp = ChatProcessing.createTellraw(sender, message, null, null, chid); + final TellrawPart expectedtp = ChatProcessing.createTellraw(sender, message, null, null, null, chid); // System.out.println("Raw: " + ChatProcessing.toJson(expectedtp)); for (TellrawPart extra : extras) expectedtp.addExtra(extra); -- 2.30.2 From 4f2abe41b8b253b798f75da2ba7e8c9fa83a553a Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Tue, 17 Jul 2018 18:29:55 +0200 Subject: [PATCH 4/4] F I X E D F O R M A T T I N G Except /shrug #71 Also added strikethrough --- .../buttondevteam/chat/ChatProcessing.java | 3 +++ .../chat/formatting/ChatFormatter.java | 18 +++++++++--------- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/src/main/java/buttondevteam/chat/ChatProcessing.java b/src/main/java/buttondevteam/chat/ChatProcessing.java index f363fb6..92ae3f2 100644 --- a/src/main/java/buttondevteam/chat/ChatProcessing.java +++ b/src/main/java/buttondevteam/chat/ChatProcessing.java @@ -47,6 +47,7 @@ public class ChatProcessing { private static final Pattern CODE_PATTERN = Pattern.compile("`"); private static final Pattern MASKED_LINK_PATTERN = Pattern.compile("\\[([^\\[\\]])\\]\\(([^()])\\)"); private static final Pattern SOMEONE_PATTERN = Pattern.compile("@someone"); //TODO + private static final Pattern STRIKETHROUGH_PATTERN = Pattern.compile("~~"); private static final Color[] RainbowPresserColors = new Color[]{Color.Red, Color.Gold, Color.Yellow, Color.Green, Color.Blue, Color.DarkPurple}; private static boolean pingedconsole = false; @@ -59,6 +60,8 @@ public class ChatProcessing { ChatFormatter.builder().regex(ITALIC_PATTERN).italic(true).removeCharCount((short) 1).range(true).build(), ChatFormatter.builder().regex(UNDERLINED_PATTERN).underlined(true).removeCharCount((short) 1).range(true) .build(), + ChatFormatter.builder().regex(STRIKETHROUGH_PATTERN).strikethrough(true).removeCharCount((short) 2).range(true) + .build(), ESCAPE_FORMATTER, ChatFormatter.builder().regex(URL_PATTERN).underlined(true).openlink("$1").build(), ChatFormatter.builder().regex(NULL_MENTION_PATTERN).color(Color.DarkRed).build(), // Properly added a bug as a feature ChatFormatter.builder().regex(CONSOLE_PING_PATTERN).color(Color.Aqua).onmatch((match, builder) -> { diff --git a/src/main/java/buttondevteam/chat/formatting/ChatFormatter.java b/src/main/java/buttondevteam/chat/formatting/ChatFormatter.java index 4eea954..619398e 100644 --- a/src/main/java/buttondevteam/chat/formatting/ChatFormatter.java +++ b/src/main/java/buttondevteam/chat/formatting/ChatFormatter.java @@ -71,7 +71,7 @@ public final class ChatFormatter { : Integer.compare(s1.Start, s2.Start)); /** - * 0: Start - 1: End + * 0: Start - 1: End index */ val remchars = new ArrayList(); @@ -87,7 +87,7 @@ public final class ChatFormatter { if (!section.IsRange) { escaped = section.Formatters.contains(ChatProcessing.ESCAPE_FORMATTER) && !escaped; // Enable escaping on first \, disable on second if (escaped) // Don't add the escape character - remchars.add(new int[] { section.Start, section.Start + 1 }); + remchars.add(new int[]{section.Start, section.Start}); combined.add(section); // This will delete the \ DebugCommand.SendDebugMessage("Added " + (!escaped ? "not " : "") + "escaper section: " + section); sendMessageWithPointer(str, section.Start, section.End); @@ -120,7 +120,8 @@ public final class ChatFormatter { takenFormatter = section.Formatters.get(0); if (nextSection.containsKey(section.Formatters.get(0))) { FormattedSection s = nextSection.remove(section.Formatters.get(0)); - s.End = section.Start; // section: the ending marker section - s: the to-be full section + // section: the ending marker section - s: the to-be full section + s.End = takenEnd - 1; //Take the remCharCount into account as well // s.IsRange = false; // IsRange means it's a 1 long section indicating a start or an end combined.add(s); DebugCommand.SendDebugMessage("Finished section: " + s); @@ -145,22 +146,22 @@ public final class ChatFormatter { DebugCommand.SendDebugMessage("Finished unfinished section: " + sec); sendMessageWithPointer(str, sec.Start, sec.End); } + sections = combined; header("Adding remove chars (RC)"); // Important to add after the range section conversion sections.stream() .flatMap(fs -> fs.Formatters.stream().filter(cf -> cf.removeCharCount > 0) - .mapToInt(cf -> cf.removeCharCount).mapToObj(rcc -> new int[] { fs.Start, fs.Start + rcc })) + .mapToInt(cf -> cf.removeCharCount).mapToObj(rcc -> new int[]{fs.Start, fs.Start + rcc - 1})) .forEach(rc -> remchars.add(rc)); sections.stream() .flatMap(fs -> fs.Formatters.stream().filter(cf -> cf.removeCharCount > 0) - .mapToInt(cf -> cf.removeCharCount).mapToObj(rcc -> new int[] { fs.End, fs.End - rcc })) + .mapToInt(cf -> cf.removeCharCount).mapToObj(rcc -> new int[]{fs.End - rcc + 1, fs.End})) .forEach(rc -> remchars.add(rc)); DebugCommand.SendDebugMessage("Added remchars:"); DebugCommand .SendDebugMessage(remchars.stream().map(rc -> Arrays.toString(rc)).collect(Collectors.joining("; "))); header("Section combining"); - sections = combined; boolean cont = true; boolean found = false; for (int i = 1; cont;) { @@ -190,7 +191,6 @@ public final class ChatFormatter { origend = origend2; origend2 = tmp; } - // int rc1start, rc1end, rc2start, rc2end, rc3start, rc3end; // Remove characters - TODO: Store positions FormattedSection section = new FormattedSection(firstSection.Formatters, sections.get(i).Start, origend, firstSection.Matches, false); section.Formatters.addAll(sections.get(i).Formatters); @@ -271,9 +271,9 @@ public final class ChatFormatter { val rce = remchars.stream().filter(rc -> rc[0] <= end && end <= rc[1]).findAny(); int s = start, e = end; if (rcs.isPresent()) - s = rcs.get()[1]; + s = rcs.get()[1] + 1; if (rce.isPresent()) - e = rce.get()[0]; + e = rce.get()[0] - 1; DebugCommand.SendDebugMessage("After RC - Start: " + s + " - End: " + e); if (e - s < 0) { //e-s==0 means the end char is the same as start char, so one char message DebugCommand.SendDebugMessage("Skipping section because of remchars (length would be " + (e - s + 1) + ")"); -- 2.30.2