Merge pull request #83 from TBMCPlugins/dev

Fixed chat formatting and Discord message hover info in MC
This commit is contained in:
Norbi Peti 2018-07-20 14:22:12 +02:00 committed by GitHub
commit 44488a78ad
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 38 additions and 35 deletions

View file

@ -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) -> {
@ -104,9 +107,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)
@ -126,7 +131,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;
@ -189,8 +194,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]")
@ -211,8 +216,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("> "));

View file

@ -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;
}
}

View file

@ -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<int[]>();
@ -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) + ")");

View file

@ -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<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
@ -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;
}
}

View file

@ -66,11 +66,11 @@ public class ChatFormatIT {
public void testMessage() {
ArrayList<ChatFormatter> 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);