Fixed chat formatting and Discord message hover info in MC #83
5 changed files with 38 additions and 35 deletions
|
@ -47,6 +47,7 @@ public class ChatProcessing {
|
||||||
private static final Pattern CODE_PATTERN = Pattern.compile("`");
|
private static final Pattern CODE_PATTERN = Pattern.compile("`");
|
||||||
private static final Pattern MASKED_LINK_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 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,
|
private static final Color[] RainbowPresserColors = new Color[]{Color.Red, Color.Gold, Color.Yellow, Color.Green,
|
||||||
Color.Blue, Color.DarkPurple};
|
Color.Blue, Color.DarkPurple};
|
||||||
private static boolean pingedconsole = false;
|
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(ITALIC_PATTERN).italic(true).removeCharCount((short) 1).range(true).build(),
|
||||||
ChatFormatter.builder().regex(UNDERLINED_PATTERN).underlined(true).removeCharCount((short) 1).range(true)
|
ChatFormatter.builder().regex(UNDERLINED_PATTERN).underlined(true).removeCharCount((short) 1).range(true)
|
||||||
.build(),
|
.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(),
|
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(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) -> {
|
ChatFormatter.builder().regex(CONSOLE_PING_PATTERN).color(Color.Aqua).onmatch((match, builder) -> {
|
||||||
|
@ -104,9 +107,11 @@ public class ChatProcessing {
|
||||||
|
|
||||||
doFunStuff(sender, e, message);
|
doFunStuff(sender, e, message);
|
||||||
|
|
||||||
ChatPlayer mp = null;
|
ChatPlayer mp;
|
||||||
if (player != null)
|
if (player != null)
|
||||||
mp = TBMCPlayerBase.getPlayer(player.getUniqueId(), ChatPlayer.class);
|
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;
|
Color colormode = channel.color;
|
||||||
if (mp != null && mp.OtherColorMode != null)
|
if (mp != null && mp.OtherColorMode != null)
|
||||||
|
@ -126,7 +131,7 @@ public class ChatProcessing {
|
||||||
pingedconsole = false; // Will set it to true onmatch (static constructor)
|
pingedconsole = false; // Will set it to true onmatch (static constructor)
|
||||||
final String channelidentifier = getChannelID(channel, sender);
|
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();
|
long combinetime = System.nanoTime();
|
||||||
ChatFormatter.Combine(formatters, message, json);
|
ChatFormatter.Combine(formatters, message, json);
|
||||||
combinetime = System.nanoTime() - combinetime;
|
combinetime = System.nanoTime() - combinetime;
|
||||||
|
@ -189,8 +194,8 @@ public class ChatProcessing {
|
||||||
return gson.toJson(json);
|
return gson.toJson(json);
|
||||||
}
|
}
|
||||||
|
|
||||||
static TellrawPart createTellraw(CommandSender sender, String message, @Nullable Player player, @Nullable ChatPlayer mp,
|
static TellrawPart createTellraw(CommandSender sender, String message, @Nullable Player player,
|
||||||
final String channelidentifier) {
|
@Nullable ChatPlayer mp, @Nullable ChromaGamerBase cg, final String channelidentifier) {
|
||||||
TellrawPart json = new TellrawPart("");
|
TellrawPart json = new TellrawPart("");
|
||||||
if (mp != null && mp.ChatOnly) {
|
if (mp != null && mp.ChatOnly) {
|
||||||
json.addExtra(new TellrawPart("[C]")
|
json.addExtra(new TellrawPart("[C]")
|
||||||
|
@ -211,8 +216,8 @@ public class ChatProcessing {
|
||||||
.setHoverEvent(TellrawEvent.create(TellrawEvent.HoverAction.SHOW_TEXT, "Gold Patreon supporter")));
|
.setHoverEvent(TellrawEvent.create(TellrawEvent.HoverAction.SHOW_TEXT, "Gold Patreon supporter")));
|
||||||
json.addExtra(new TellrawPart(" <"));
|
json.addExtra(new TellrawPart(" <"));
|
||||||
TellrawPart hovertp = new TellrawPart("");
|
TellrawPart hovertp = new TellrawPart("");
|
||||||
if (mp != null)
|
if (cg != null)
|
||||||
hovertp.addExtra(new TellrawPart(mp.getInfo(ChromaGamerBase.InfoTarget.MCHover)));
|
hovertp.addExtra(new TellrawPart(cg.getInfo(ChromaGamerBase.InfoTarget.MCHover)));
|
||||||
json.addExtra(new TellrawPart(getSenderName(sender, player))
|
json.addExtra(new TellrawPart(getSenderName(sender, player))
|
||||||
.setHoverEvent(TellrawEvent.create(TellrawEvent.HoverAction.SHOW_TEXT, hovertp)));
|
.setHoverEvent(TellrawEvent.create(TellrawEvent.HoverAction.SHOW_TEXT, hovertp)));
|
||||||
json.addExtra(new TellrawPart("> "));
|
json.addExtra(new TellrawPart("> "));
|
||||||
|
|
|
@ -2,14 +2,13 @@ package buttondevteam.chat.commands.appendtext;
|
||||||
|
|
||||||
import buttondevteam.chat.ChatPlayer;
|
import buttondevteam.chat.ChatPlayer;
|
||||||
import buttondevteam.chat.listener.PlayerListener;
|
import buttondevteam.chat.listener.PlayerListener;
|
||||||
import buttondevteam.lib.chat.Channel;
|
import buttondevteam.lib.chat.*;
|
||||||
import buttondevteam.lib.chat.CommandClass;
|
|
||||||
import buttondevteam.lib.chat.TBMCChatAPI;
|
|
||||||
import buttondevteam.lib.chat.TBMCCommandBase;
|
|
||||||
import buttondevteam.lib.player.TBMCPlayer;
|
import buttondevteam.lib.player.TBMCPlayer;
|
||||||
import org.bukkit.command.CommandSender;
|
import org.bukkit.command.CommandSender;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
|
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
@CommandClass(modOnly = false, excludeFromPath = true)
|
@CommandClass(modOnly = false, excludeFromPath = true)
|
||||||
public abstract class AppendTextCommandBase extends TBMCCommandBase {
|
public abstract class AppendTextCommandBase extends TBMCCommandBase {
|
||||||
|
|
||||||
|
@ -22,14 +21,17 @@ public abstract class AppendTextCommandBase extends TBMCCommandBase {
|
||||||
String msg = GetAppendedText();
|
String msg = GetAppendedText();
|
||||||
for (int i = args.length - 1; i >= 0; i--)
|
for (int i = args.length - 1; i >= 0; i--)
|
||||||
msg = args[i] + " " + msg;
|
msg = args[i] + " " + msg;
|
||||||
|
ChatPlayer cp;
|
||||||
if (sender instanceof Player)
|
if (sender instanceof Player)
|
||||||
TBMCChatAPI.SendChatMessage(
|
TBMCChatAPI.SendChatMessage(ChatMessage.builder(
|
||||||
TBMCPlayer.getPlayer(((Player) sender).getUniqueId(), ChatPlayer.class).CurrentChannel, sender,
|
(cp = TBMCPlayer.getPlayer(((Player) sender).getUniqueId(), ChatPlayer.class)).CurrentChannel, sender,
|
||||||
msg, true);
|
cp, msg).fromCommand(true).build());
|
||||||
else if (sender.isOp())
|
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
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -71,7 +71,7 @@ public final class ChatFormatter {
|
||||||
: Integer.compare(s1.Start, s2.Start));
|
: Integer.compare(s1.Start, s2.Start));
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 0: Start - 1: End
|
* 0: Start - 1: End index
|
||||||
*/
|
*/
|
||||||
val remchars = new ArrayList<int[]>();
|
val remchars = new ArrayList<int[]>();
|
||||||
|
|
||||||
|
@ -87,7 +87,7 @@ public final class ChatFormatter {
|
||||||
if (!section.IsRange) {
|
if (!section.IsRange) {
|
||||||
escaped = section.Formatters.contains(ChatProcessing.ESCAPE_FORMATTER) && !escaped; // Enable escaping on first \, disable on second
|
escaped = section.Formatters.contains(ChatProcessing.ESCAPE_FORMATTER) && !escaped; // Enable escaping on first \, disable on second
|
||||||
if (escaped) // Don't add the escape character
|
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 \
|
combined.add(section); // This will delete the \
|
||||||
DebugCommand.SendDebugMessage("Added " + (!escaped ? "not " : "") + "escaper section: " + section);
|
DebugCommand.SendDebugMessage("Added " + (!escaped ? "not " : "") + "escaper section: " + section);
|
||||||
sendMessageWithPointer(str, section.Start, section.End);
|
sendMessageWithPointer(str, section.Start, section.End);
|
||||||
|
@ -120,7 +120,8 @@ public final class ChatFormatter {
|
||||||
takenFormatter = section.Formatters.get(0);
|
takenFormatter = section.Formatters.get(0);
|
||||||
if (nextSection.containsKey(section.Formatters.get(0))) {
|
if (nextSection.containsKey(section.Formatters.get(0))) {
|
||||||
FormattedSection s = nextSection.remove(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
|
// s.IsRange = false; // IsRange means it's a 1 long section indicating a start or an end
|
||||||
combined.add(s);
|
combined.add(s);
|
||||||
DebugCommand.SendDebugMessage("Finished section: " + s);
|
DebugCommand.SendDebugMessage("Finished section: " + s);
|
||||||
|
@ -145,22 +146,22 @@ public final class ChatFormatter {
|
||||||
DebugCommand.SendDebugMessage("Finished unfinished section: " + sec);
|
DebugCommand.SendDebugMessage("Finished unfinished section: " + sec);
|
||||||
sendMessageWithPointer(str, sec.Start, sec.End);
|
sendMessageWithPointer(str, sec.Start, sec.End);
|
||||||
}
|
}
|
||||||
|
sections = combined;
|
||||||
|
|
||||||
header("Adding remove chars (RC)"); // Important to add after the range section conversion
|
header("Adding remove chars (RC)"); // Important to add after the range section conversion
|
||||||
sections.stream()
|
sections.stream()
|
||||||
.flatMap(fs -> fs.Formatters.stream().filter(cf -> cf.removeCharCount > 0)
|
.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));
|
.forEach(rc -> remchars.add(rc));
|
||||||
sections.stream()
|
sections.stream()
|
||||||
.flatMap(fs -> fs.Formatters.stream().filter(cf -> cf.removeCharCount > 0)
|
.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));
|
.forEach(rc -> remchars.add(rc));
|
||||||
DebugCommand.SendDebugMessage("Added remchars:");
|
DebugCommand.SendDebugMessage("Added remchars:");
|
||||||
DebugCommand
|
DebugCommand
|
||||||
.SendDebugMessage(remchars.stream().map(rc -> Arrays.toString(rc)).collect(Collectors.joining("; ")));
|
.SendDebugMessage(remchars.stream().map(rc -> Arrays.toString(rc)).collect(Collectors.joining("; ")));
|
||||||
|
|
||||||
header("Section combining");
|
header("Section combining");
|
||||||
sections = combined;
|
|
||||||
boolean cont = true;
|
boolean cont = true;
|
||||||
boolean found = false;
|
boolean found = false;
|
||||||
for (int i = 1; cont;) {
|
for (int i = 1; cont;) {
|
||||||
|
@ -190,7 +191,6 @@ public final class ChatFormatter {
|
||||||
origend = origend2;
|
origend = origend2;
|
||||||
origend2 = tmp;
|
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,
|
FormattedSection section = new FormattedSection(firstSection.Formatters, sections.get(i).Start, origend,
|
||||||
firstSection.Matches, false);
|
firstSection.Matches, false);
|
||||||
section.Formatters.addAll(sections.get(i).Formatters);
|
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();
|
val rce = remchars.stream().filter(rc -> rc[0] <= end && end <= rc[1]).findAny();
|
||||||
int s = start, e = end;
|
int s = start, e = end;
|
||||||
if (rcs.isPresent())
|
if (rcs.isPresent())
|
||||||
s = rcs.get()[1];
|
s = rcs.get()[1] + 1;
|
||||||
if (rce.isPresent())
|
if (rce.isPresent())
|
||||||
e = rce.get()[0];
|
e = rce.get()[0] - 1;
|
||||||
DebugCommand.SendDebugMessage("After RC - Start: " + s + " - End: " + e);
|
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
|
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) + ")");
|
DebugCommand.SendDebugMessage("Skipping section because of remchars (length would be " + (e - s + 1) + ")");
|
||||||
|
|
|
@ -5,10 +5,7 @@ import buttondevteam.chat.ChatProcessing;
|
||||||
import buttondevteam.chat.PluginMain;
|
import buttondevteam.chat.PluginMain;
|
||||||
import buttondevteam.lib.TBMCChatEvent;
|
import buttondevteam.lib.TBMCChatEvent;
|
||||||
import buttondevteam.lib.TBMCCoreAPI;
|
import buttondevteam.lib.TBMCCoreAPI;
|
||||||
import buttondevteam.lib.chat.Channel;
|
import buttondevteam.lib.chat.*;
|
||||||
import buttondevteam.lib.chat.ChatChannelRegisterEvent;
|
|
||||||
import buttondevteam.lib.chat.ChatRoom;
|
|
||||||
import buttondevteam.lib.chat.TBMCChatAPI;
|
|
||||||
import buttondevteam.lib.player.ChromaGamerBase.InfoTarget;
|
import buttondevteam.lib.player.ChromaGamerBase.InfoTarget;
|
||||||
import buttondevteam.lib.player.TBMCPlayer;
|
import buttondevteam.lib.player.TBMCPlayer;
|
||||||
import buttondevteam.lib.player.TBMCPlayerGetInfoEvent;
|
import buttondevteam.lib.player.TBMCPlayerGetInfoEvent;
|
||||||
|
@ -59,9 +56,8 @@ public class PlayerListener implements Listener {
|
||||||
public void onPlayerChat(AsyncPlayerChatEvent event) {
|
public void onPlayerChat(AsyncPlayerChatEvent event) {
|
||||||
if (event.isCancelled())
|
if (event.isCancelled())
|
||||||
return;
|
return;
|
||||||
TBMCChatAPI.SendChatMessage(
|
ChatPlayer cp = TBMCPlayer.getPlayer(event.getPlayer().getUniqueId(), ChatPlayer.class);
|
||||||
TBMCPlayer.getPlayer(event.getPlayer().getUniqueId(), ChatPlayer.class).CurrentChannel,
|
TBMCChatAPI.SendChatMessage(ChatMessage.builder(cp.CurrentChannel, event.getPlayer(), cp, event.getMessage()).build());
|
||||||
event.getPlayer(), event.getMessage());
|
|
||||||
event.setCancelled(true); // The custom event should only be cancelled when muted or similar
|
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)
|
if (sender instanceof Player)
|
||||||
mp = TBMCPlayer.getPlayer(((Player) sender).getUniqueId(), ChatPlayer.class);
|
mp = TBMCPlayer.getPlayer(((Player) sender).getUniqueId(), ChatPlayer.class);
|
||||||
else
|
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;
|
String cmd;
|
||||||
final BiPredicate<Channel, String> checkchid = (chan, cmd1) -> cmd1.equalsIgnoreCase(chan.ID) || (chan.IDs != null && Arrays.stream(chan.IDs).anyMatch(cmd1::equalsIgnoreCase));
|
final BiPredicate<Channel, String> 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
|
if (index == -1) { // Only the command is run
|
||||||
|
@ -138,7 +134,7 @@ public class PlayerListener implements Listener {
|
||||||
} else
|
} else
|
||||||
for (Channel channel : Channel.getChannels()) {
|
for (Channel channel : Channel.getChannels()) {
|
||||||
if (checkchid.test(channel, cmd)) { //Apparently method references don't require final variables
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -66,11 +66,11 @@ public class ChatFormatIT {
|
||||||
public void testMessage() {
|
public void testMessage() {
|
||||||
ArrayList<ChatFormatter> cfs = ChatProcessing.addFormatters(Color.White);
|
ArrayList<ChatFormatter> cfs = ChatProcessing.addFormatters(Color.White);
|
||||||
final String chid = ChatProcessing.getChannelID(Channel.GlobalChat, sender);
|
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);
|
ChatFormatter.Combine(cfs, message, tp);
|
||||||
System.out.println("Testing: " + message);
|
System.out.println("Testing: " + message);
|
||||||
// System.out.println(ChatProcessing.toJson(tp));
|
// 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));
|
// System.out.println("Raw: " + ChatProcessing.toJson(expectedtp));
|
||||||
for (TellrawPart extra : extras)
|
for (TellrawPart extra : extras)
|
||||||
expectedtp.addExtra(extra);
|
expectedtp.addExtra(extra);
|
||||||
|
|
Loading…
Reference in a new issue