1.14 support, fixes #105
3 changed files with 180 additions and 175 deletions
|
@ -178,15 +178,14 @@ public class ChatProcessing {
|
|||
if (score < 0) // Never send messages to score below 0
|
||||
sender.sendMessage("§cYou don't have permission to send this message or something went wrong");
|
||||
else {
|
||||
PluginMain.Instance.getServer().dispatchCommand(PluginMain.Console,
|
||||
ChatUtils.dispatchConsoleCommand(
|
||||
String.format("tellraw @a[score_%s=%d,score_%s_min=%d] %s", channel.ID, score, channel.ID,
|
||||
score, jsonstr));
|
||||
score, jsonstr), true);
|
||||
val tc = ComponentManager.getIfEnabled(TownyComponent.class);
|
||||
if (tc != null) tc.handleSpies(channel, json, ChatProcessing::toJson);
|
||||
}
|
||||
} else
|
||||
PluginMain.Instance.getServer().dispatchCommand(PluginMain.Console,
|
||||
String.format("tellraw @a %s", jsonstr));
|
||||
ChatUtils.dispatchConsoleCommand(String.format("tellraw @a %s", jsonstr), true);
|
||||
} catch (Exception ex) {
|
||||
TBMCCoreAPI.SendException("An error occured while sending a chat message!", ex);
|
||||
sender.sendMessage("§cAn error occured while sending the message.");
|
||||
|
|
20
src/main/java/buttondevteam/chat/ChatUtils.java
Normal file
20
src/main/java/buttondevteam/chat/ChatUtils.java
Normal file
|
@ -0,0 +1,20 @@
|
|||
package buttondevteam.chat;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
|
||||
public final class ChatUtils {
|
||||
private ChatUtils() {}
|
||||
|
||||
/**
|
||||
* Dispatch a console command.
|
||||
*
|
||||
* @param command The command
|
||||
* @param async Whether the caller is async
|
||||
*/
|
||||
public static void dispatchConsoleCommand(String command, boolean async) {
|
||||
if (async)
|
||||
Bukkit.getScheduler().runTask(PluginMain.Instance, () -> Bukkit.dispatchCommand(PluginMain.Console, command));
|
||||
else
|
||||
Bukkit.dispatchCommand(PluginMain.Console, command);
|
||||
}
|
||||
}
|
|
@ -20,7 +20,6 @@ import java.util.stream.Collectors;
|
|||
* again.
|
||||
*
|
||||
* @author NorbiPeti
|
||||
*
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
|
@ -64,75 +63,68 @@ public final class ChatFormatter {
|
|||
* This method assumes that there is always a global formatter
|
||||
*/
|
||||
header("ChatFormatter.Combine begin");
|
||||
ArrayList<FormattedSection> sections = new ArrayList<FormattedSection>();
|
||||
ArrayList<FormattedSection> sections = new ArrayList<>();
|
||||
|
||||
for (ChatFormatter formatter : formatters) {
|
||||
if (formatter.type != Type.Excluder)
|
||||
continue;
|
||||
Matcher matcher = formatter.regex.matcher(str);
|
||||
while (matcher.find()) {
|
||||
DebugCommand.SendDebugMessage("Found match from " + matcher.start() + " to " + (matcher.end() - 1));
|
||||
DebugCommand.SendDebugMessage("With excluder formatter:" + formatter);
|
||||
sendMessageWithPointer(str, matcher.start(), matcher.end() - 1);
|
||||
if (formatter.regex != ChatProcessing.ENTIRE_MESSAGE_PATTERN && sections.stream().anyMatch(fs -> fs.type == Type.Excluder && (fs.End >= matcher.start() && fs.Start <= matcher.end() - 1))) {
|
||||
DebugCommand.SendDebugMessage("Ignoring formatter because of an excluder");
|
||||
continue; //Exclude areas matched by excluders - Range sections are correctly handled afterwards
|
||||
}
|
||||
ArrayList<String> groups = new ArrayList<String>();
|
||||
for (int i = 0; i < matcher.groupCount(); i++)
|
||||
groups.add(matcher.group(i + 1));
|
||||
if (groups.size() > 0)
|
||||
DebugCommand.SendDebugMessage("First group: " + groups.get(0));
|
||||
FormattedSection section = new FormattedSection(formatter, matcher.start(), matcher.end() - 1, groups,
|
||||
formatter.type);
|
||||
sections.add(section);
|
||||
}
|
||||
}
|
||||
createSections(formatters, str, sections, true);
|
||||
|
||||
header("Section creation (excluders done)");
|
||||
for (ChatFormatter formatter : formatters) {
|
||||
if (formatter.type == Type.Excluder)
|
||||
continue;
|
||||
Matcher matcher = formatter.regex.matcher(str);
|
||||
while (matcher.find()) {
|
||||
DebugCommand.SendDebugMessage("Found match from " + matcher.start() + " to " + (matcher.end() - 1));
|
||||
DebugCommand.SendDebugMessage("With formatter:" + formatter);
|
||||
sendMessageWithPointer(str, matcher.start(), matcher.end() - 1);
|
||||
if (formatter.regex != ChatProcessing.ENTIRE_MESSAGE_PATTERN && sections.stream().anyMatch(fs -> fs.type == Type.Excluder && (fs.End >= matcher.start() && fs.Start <= matcher.end() - 1))) {
|
||||
DebugCommand.SendDebugMessage("Ignoring formatter because of an excluder");
|
||||
continue; //Exclude areas matched by excluders - Range sections are correctly handled afterwards
|
||||
}
|
||||
ArrayList<String> groups = new ArrayList<String>();
|
||||
for (int i = 0; i < matcher.groupCount(); i++)
|
||||
groups.add(matcher.group(i + 1));
|
||||
if (groups.size() > 0)
|
||||
DebugCommand.SendDebugMessage("First group: " + groups.get(0));
|
||||
FormattedSection section = new FormattedSection(formatter, matcher.start(), matcher.end() - 1, groups,
|
||||
formatter.type);
|
||||
sections.add(section);
|
||||
}
|
||||
}
|
||||
sections.sort(
|
||||
(s1, s2) -> s1.Start == s2.Start
|
||||
? s1.End == s2.End ? Integer.compare(s2.Formatters.get(0).priority.GetValue(),
|
||||
s1.Formatters.get(0).priority.GetValue()) : Integer.compare(s2.End, s1.End)
|
||||
: Integer.compare(s1.Start, s2.Start));
|
||||
createSections(formatters, str, sections, false);
|
||||
sortSections(sections);
|
||||
|
||||
/**
|
||||
/*
|
||||
* 0: Start - 1: End index
|
||||
*/
|
||||
val remchars = new ArrayList<int[]>();
|
||||
|
||||
header("Range section conversion");
|
||||
sections = convertRangeSections(str, sections, remchars);
|
||||
|
||||
header("Adding remove chars (RC)"); // Important to add after the range section conversion
|
||||
addRemChars(sections, remchars);
|
||||
|
||||
header("Section combining");
|
||||
combineSections(str, sections);
|
||||
|
||||
header("Section applying");
|
||||
applySections(str, tp, sections, remchars);
|
||||
header("ChatFormatter.Combine done");
|
||||
}
|
||||
|
||||
private static void createSections(List<ChatFormatter> formatters, String str, ArrayList<FormattedSection> sections,
|
||||
boolean excluders) {
|
||||
for (ChatFormatter formatter : formatters) {
|
||||
if (excluders == (formatter.type != Type.Excluder))
|
||||
continue; //If we're looking at excluders and this isn't one, skip - or vica-versa
|
||||
Matcher matcher = formatter.regex.matcher(str);
|
||||
while (matcher.find()) {
|
||||
DebugCommand.SendDebugMessage("Found match from " + matcher.start() + " to " + (matcher.end() - 1));
|
||||
DebugCommand.SendDebugMessage("With " + (excluders ? "excluder " : "") + "formatter: " + formatter);
|
||||
sendMessageWithPointer(str, matcher.start(), matcher.end() - 1);
|
||||
if (formatter.regex != ChatProcessing.ENTIRE_MESSAGE_PATTERN && sections.stream().anyMatch(fs -> fs.type == Type.Excluder && (fs.End >= matcher.start() && fs.Start <= matcher.end() - 1))) {
|
||||
DebugCommand.SendDebugMessage("Ignoring formatter because of an excluder");
|
||||
continue; //Exclude areas matched by excluders - Range sections are correctly handled afterwards
|
||||
}
|
||||
ArrayList<String> groups = new ArrayList<>();
|
||||
for (int i = 0; i < matcher.groupCount(); i++)
|
||||
groups.add(matcher.group(i + 1));
|
||||
if (groups.size() > 0)
|
||||
DebugCommand.SendDebugMessage("First group: " + groups.get(0));
|
||||
FormattedSection section = new FormattedSection(formatter, matcher.start(), matcher.end() - 1, groups,
|
||||
formatter.type);
|
||||
sections.add(section);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static ArrayList<FormattedSection> convertRangeSections(String str, ArrayList<FormattedSection> sections, ArrayList<int[]> remchars) {
|
||||
ArrayList<FormattedSection> combined = new ArrayList<>();
|
||||
Map<ChatFormatter, FormattedSection> nextSection = new HashMap<>();
|
||||
boolean escaped = false;
|
||||
int takenStart = -1, takenEnd = -1;
|
||||
ChatFormatter takenFormatter = null;
|
||||
for (int i = 0; i < sections.size(); i++) {
|
||||
for (final FormattedSection section : sections) {
|
||||
// Set ending to -1 until closed with another 1 long "section" - only do this if IsRange is true
|
||||
final FormattedSection section = sections.get(i);
|
||||
if (section.type!=Type.Range) {
|
||||
if (section.type != Type.Range) {
|
||||
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});
|
||||
|
@ -145,12 +137,6 @@ public final class ChatFormatter {
|
|||
continue;
|
||||
}
|
||||
if (!escaped) {
|
||||
if (combined.stream().anyMatch(s -> section.type != Type.Range && (s.Start == section.Start
|
||||
|| (s.Start < section.Start ? s.End >= section.Start : s.Start <= section.End)))) {
|
||||
DebugCommand.SendDebugMessage("Range " + section + " overlaps with a combined section, ignoring.");
|
||||
sendMessageWithPointer(str, section.Start, section.End);
|
||||
continue;
|
||||
}
|
||||
if (section.Start == takenStart || (section.Start > takenStart && section.Start < takenEnd)) {
|
||||
/*
|
||||
* if (nextSection.containsKey(section.Formatters.get(0)) ? section.RemCharFromStart <= takenEnd - takenStart : section.RemCharFromStart > takenEnd - takenStart) {
|
||||
|
@ -193,24 +179,27 @@ public final class ChatFormatter {
|
|||
}
|
||||
//Do not finish unfinished sections, ignore them
|
||||
sections = combined;
|
||||
return sections;
|
||||
}
|
||||
|
||||
header("Adding remove chars (RC)"); // Important to add after the range section conversion
|
||||
private static void addRemChars(ArrayList<FormattedSection> sections, ArrayList<int[]> remchars) {
|
||||
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 - 1}))
|
||||
.forEach(rc -> remchars.add(rc));
|
||||
.forEach(remchars::add);
|
||||
sections.stream()
|
||||
.flatMap(fs -> fs.Formatters.stream().filter(cf -> cf.removeCharCount > 0)
|
||||
.mapToInt(cf -> cf.removeCharCount).mapToObj(rcc -> new int[]{fs.End - rcc + 1, fs.End}))
|
||||
.forEach(rc -> remchars.add(rc));
|
||||
.forEach(remchars::add);
|
||||
DebugCommand.SendDebugMessage("Added remchars:");
|
||||
DebugCommand
|
||||
.SendDebugMessage(remchars.stream().map(rc -> Arrays.toString(rc)).collect(Collectors.joining("; ")));
|
||||
.SendDebugMessage(remchars.stream().map(Arrays::toString).collect(Collectors.joining("; ")));
|
||||
}
|
||||
|
||||
header("Section combining");
|
||||
private static void combineSections(String str, ArrayList<FormattedSection> sections) {
|
||||
boolean cont = true;
|
||||
boolean found = false;
|
||||
for (int i = 1; cont;) {
|
||||
for (int i = 1; cont; ) {
|
||||
int nextindex = i + 1;
|
||||
if (sections.size() < 2)
|
||||
break;
|
||||
|
@ -218,47 +207,40 @@ public final class ChatFormatter {
|
|||
FormattedSection firstSection = sections.get(i - 1);
|
||||
DebugCommand.SendDebugMessage("Combining sections " + firstSection);
|
||||
sendMessageWithPointer(str, firstSection.Start, firstSection.End);
|
||||
DebugCommand.SendDebugMessage(" and " + sections.get(i));
|
||||
sendMessageWithPointer(str, sections.get(i).Start, sections.get(i).End);
|
||||
if (firstSection.Start == sections.get(i).Start && firstSection.End == sections.get(i).End) {
|
||||
firstSection.Formatters.addAll(sections.get(i).Formatters);
|
||||
firstSection.Matches.addAll(sections.get(i).Matches);
|
||||
final FormattedSection lastSection = sections.get(i);
|
||||
DebugCommand.SendDebugMessage(" and " + lastSection);
|
||||
sendMessageWithPointer(str, lastSection.Start, lastSection.End);
|
||||
if (firstSection.Start == lastSection.Start && firstSection.End == lastSection.End) {
|
||||
firstSection.Formatters.addAll(lastSection.Formatters);
|
||||
firstSection.Matches.addAll(lastSection.Matches);
|
||||
DebugCommand.SendDebugMessage("To section " + firstSection);
|
||||
sendMessageWithPointer(str, firstSection.Start, firstSection.End);
|
||||
sections.remove(i);
|
||||
found = true;
|
||||
} else if (firstSection.End > sections.get(i).Start && firstSection.Start < sections.get(i).End) {
|
||||
int origend = firstSection.End;
|
||||
firstSection.End = sections.get(i).Start - 1;
|
||||
int origend2 = sections.get(i).End;
|
||||
boolean switchends;
|
||||
if (switchends = origend2 < origend) {
|
||||
int tmp = origend;
|
||||
origend = origend2;
|
||||
origend2 = tmp;
|
||||
}
|
||||
FormattedSection section = new FormattedSection(firstSection.Formatters, sections.get(i).Start, origend,
|
||||
} else if (firstSection.End > lastSection.Start && firstSection.Start < lastSection.End) {
|
||||
int origend2 = firstSection.End;
|
||||
firstSection.End = lastSection.Start - 1;
|
||||
int origend = lastSection.End;
|
||||
FormattedSection section = new FormattedSection(firstSection.Formatters, lastSection.Start, origend,
|
||||
firstSection.Matches, Type.Normal);
|
||||
section.Formatters.addAll(sections.get(i).Formatters);
|
||||
section.Matches.addAll(sections.get(i).Matches); // TODO: Clean
|
||||
section.Formatters.addAll(lastSection.Formatters);
|
||||
section.Matches.addAll(lastSection.Matches); // TODO: Clean
|
||||
sections.add(i, section);
|
||||
nextindex++;
|
||||
FormattedSection thirdFormattedSection = sections.get(i + 1);
|
||||
if (switchends) { // Use the properties of the first section not the second one
|
||||
thirdFormattedSection.Formatters.clear();
|
||||
thirdFormattedSection.Formatters.addAll(firstSection.Formatters);
|
||||
thirdFormattedSection.Matches.clear();
|
||||
thirdFormattedSection.Matches.addAll(firstSection.Matches);
|
||||
}
|
||||
thirdFormattedSection.Start = origend + 1;
|
||||
thirdFormattedSection.End = origend2;
|
||||
// Use the properties of the first section not the second one
|
||||
lastSection.Formatters.clear();
|
||||
lastSection.Formatters.addAll(firstSection.Formatters);
|
||||
lastSection.Matches.clear();
|
||||
lastSection.Matches.addAll(firstSection.Matches);
|
||||
|
||||
lastSection.Start = origend + 1;
|
||||
lastSection.End = origend2;
|
||||
|
||||
ArrayList<FormattedSection> sts = sections;
|
||||
Predicate<FormattedSection> removeIfNeeded = s -> {
|
||||
if (s.Start < 0 || s.End < 0 || s.Start > s.End) {
|
||||
DebugCommand.SendDebugMessage("Removing section: " + s);
|
||||
sendMessageWithPointer(str, s.Start, s.End);
|
||||
sts.remove(s);
|
||||
sections.remove(s);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
|
@ -273,9 +255,9 @@ public final class ChatFormatter {
|
|||
DebugCommand.SendDebugMessage(" 2:" + section + "");
|
||||
sendMessageWithPointer(str, section.Start, section.End);
|
||||
}
|
||||
if (!removeIfNeeded.test(thirdFormattedSection)) {
|
||||
DebugCommand.SendDebugMessage(" 3:" + thirdFormattedSection);
|
||||
sendMessageWithPointer(str, thirdFormattedSection.Start, thirdFormattedSection.End);
|
||||
if (!removeIfNeeded.test(lastSection)) {
|
||||
DebugCommand.SendDebugMessage(" 3:" + lastSection);
|
||||
sendMessageWithPointer(str, lastSection.Start, lastSection.End);
|
||||
}
|
||||
found = true;
|
||||
}
|
||||
|
@ -294,20 +276,16 @@ public final class ChatFormatter {
|
|||
if (found) {
|
||||
i = 1;
|
||||
found = false;
|
||||
sections.sort(
|
||||
(s1, s2) -> s1.Start == s2.Start
|
||||
? s1.End == s2.End
|
||||
? Integer.compare(s2.Formatters.get(0).priority.GetValue(),
|
||||
s1.Formatters.get(0).priority.GetValue())
|
||||
: Integer.compare(s2.End, s1.End)
|
||||
: Integer.compare(s1.Start, s2.Start));
|
||||
sortSections(sections);
|
||||
} else
|
||||
cont = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
header("Section applying");
|
||||
TellrawPart lasttp = null; String lastlink = null;
|
||||
private static void applySections(String str, TellrawPart tp, ArrayList<FormattedSection> sections, ArrayList<int[]> remchars) {
|
||||
TellrawPart lasttp = null;
|
||||
String lastlink = null;
|
||||
for (FormattedSection section : sections) {
|
||||
DebugCommand.SendDebugMessage("Applying section: " + section);
|
||||
String originaltext;
|
||||
|
@ -343,15 +321,15 @@ public final class ChatFormatter {
|
|||
if (formatter.color != null)
|
||||
newtp.setColor(formatter.color);
|
||||
if (formatter.bold)
|
||||
newtp.setBold(formatter.bold);
|
||||
newtp.setBold(true);
|
||||
if (formatter.italic)
|
||||
newtp.setItalic(formatter.italic);
|
||||
newtp.setItalic(true);
|
||||
if (formatter.underlined)
|
||||
newtp.setUnderlined(formatter.underlined);
|
||||
newtp.setUnderlined(true);
|
||||
if (formatter.strikethrough)
|
||||
newtp.setStrikethrough(formatter.strikethrough);
|
||||
newtp.setStrikethrough(true);
|
||||
if (formatter.obfuscated)
|
||||
newtp.setObfuscated(formatter.obfuscated);
|
||||
newtp.setObfuscated(true);
|
||||
if (formatter.openlink != null)
|
||||
openlink = formatter.openlink;
|
||||
if (formatter.hoverText != null)
|
||||
|
@ -368,6 +346,7 @@ public final class ChatFormatter {
|
|||
lasttp.setText(lasttp.getText() + originaltext);
|
||||
continue; //Combine parts with the same properties
|
||||
}
|
||||
lastlink = openlink;
|
||||
newtp.setText(originaltext);
|
||||
if (openlink != null && openlink.length() > 0) {
|
||||
newtp.setClickEvent(TellrawEvent.create(TellrawEvent.ClickAction.OPEN_URL,
|
||||
|
@ -378,7 +357,14 @@ public final class ChatFormatter {
|
|||
tp.addExtra(newtp);
|
||||
lasttp = newtp;
|
||||
}
|
||||
header("ChatFormatter.Combine done");
|
||||
}
|
||||
|
||||
private static void sortSections(ArrayList<FormattedSection> sections) {
|
||||
sections.sort(
|
||||
(s1, s2) -> s1.Start == s2.Start
|
||||
? s1.End == s2.End ? Integer.compare(s2.Formatters.get(0).priority.GetValue(),
|
||||
s1.Formatters.get(0).priority.GetValue()) : Integer.compare(s2.End, s1.End)
|
||||
: Integer.compare(s1.Start, s2.Start));
|
||||
}
|
||||
|
||||
private static void sendMessageWithPointer(String str, int... pointer) {
|
||||
|
|
Loading…
Reference in a new issue