Fixed ann cmds, chatformat Lombok, testing fixing

Fixed announce commands
Using Lombok for ChatFormat
In the process of fixing the tests
Plus #52, #53
This commit is contained in:
Norbi Peti 2017-08-25 00:56:47 +02:00
parent d0a990e0f1
commit 91c22e650b
8 changed files with 88 additions and 227 deletions

View file

@ -92,12 +92,16 @@
<goal>integration-test</goal> <goal>integration-test</goal>
<goal>verify</goal> <goal>verify</goal>
</goals> </goals>
<phase>test</phase>
</execution> </execution>
</executions> </executions>
<configuration> <configuration>
<testFailureIgnore>true</testFailureIgnore> <testFailureIgnore>true</testFailureIgnore>
</configuration> </configuration>
</plugin> </plugin>
<!-- <plugin> <groupId>org.basepom.maven</groupId> <artifactId>duplicate-finder-maven-plugin</artifactId>
<version>1.2.1</version> <executions> <execution> <goals> <goal>check</goal>
</goals> </execution> </executions> </plugin> -->
</plugins> </plugins>
</build> </build>
<groupId>buttondevteam</groupId> <groupId>buttondevteam</groupId>

View file

@ -40,37 +40,38 @@ public class ChatProcessing {
private static final Pattern UNDERLINED_PATTERN = Pattern.compile("\\_"); private static final Pattern UNDERLINED_PATTERN = Pattern.compile("\\_");
private static final Pattern ITALIC_PATTERN = Pattern.compile("\\*"); private static final Pattern ITALIC_PATTERN = Pattern.compile("\\*");
private static final Pattern BOLD_PATTERN = Pattern.compile("\\*\\*"); private static final Pattern BOLD_PATTERN = Pattern.compile("\\*\\*");
private static final Pattern CODE_PATTERN = Pattern.compile("`");
private static final Pattern MASKED_LINK_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;
public static final ChatFormatter ESCAPE_FORMATTER = new ChatFormatterBuilder().setRegex(ESCAPE_PATTERN).build(); public static final ChatFormatter ESCAPE_FORMATTER = ChatFormatter.builder().regex(ESCAPE_PATTERN).build();
private static ArrayList<ChatFormatter> commonFormatters = Lists.newArrayList( private static ArrayList<ChatFormatter> commonFormatters = Lists.newArrayList(
new ChatFormatterBuilder().setRegex(BOLD_PATTERN).setBold(true).setRemoveCharCount((short) 2).setRange(true) ChatFormatter.builder().regex(BOLD_PATTERN).bold(true).removeCharCount((short) 2).range(true)
.setPriority(Priority.High).build(), .priority(Priority.High).build(),
new ChatFormatterBuilder().setRegex(ITALIC_PATTERN).setItalic(true).setRemoveCharCount((short) 1) ChatFormatter.builder().regex(ITALIC_PATTERN).italic(true).removeCharCount((short) 1).range(true).build(),
.setRange(true).build(), ChatFormatter.builder().regex(UNDERLINED_PATTERN).underlined(true).removeCharCount((short) 1).range(true)
new ChatFormatterBuilder().setRegex(UNDERLINED_PATTERN).setUnderlined(true).setRemoveCharCount((short) 1) .build(),
.setRange(true).build(), ESCAPE_FORMATTER, ChatFormatter.builder().regex(URL_PATTERN).underlined(true).openlink("$1").build(),
ESCAPE_FORMATTER, ChatFormatter.builder().regex(NULL_MENTION_PATTERN).color(Color.DarkRed).build(), // Properly added a bug as a feature
new ChatFormatterBuilder().setRegex(URL_PATTERN).setUnderlined(true).setOpenlink("$1").build(), ChatFormatter.builder().regex(CONSOLE_PING_PATTERN).color(Color.Aqua).onmatch((match, builder) -> {
new ChatFormatterBuilder().setRegex(NULL_MENTION_PATTERN).setColor(Color.DarkRed).build(), // Properly added a bug as a feature if (!pingedconsole) {
new ChatFormatterBuilder().setRegex(CONSOLE_PING_PATTERN).setColor(Color.Aqua) System.out.print("\007");
.setOnmatch((match, builder) -> pingedconsole = true; // Will set it to false in ProcessChat
}
return match;
}).priority(Priority.High).build(),
{ ChatFormatter.builder().regex(HASHTAG_PATTERN).color(Color.Blue).openlink("https://twitter.com/hashtag/$1")
if (!pingedconsole) { .priority(Priority.High).build(),
System.out.print("\007"); ChatFormatter.builder().regex(CYAN_PATTERN).color(Color.Aqua).build(), // #55
pingedconsole = true; // Will set it to false in ProcessChat ChatFormatter.builder().regex(CODE_PATTERN).color(Color.DarkGray).removeCharCount((short) 1).range(true)
} .build(),
return match; ChatFormatter.builder().regex(MASKED_LINK_PATTERN).underlined(true).onmatch((match, builder) -> {
}).setPriority(Priority.High).build(), return match; // TODO!
}).build());
new ChatFormatterBuilder().setRegex(HASHTAG_PATTERN).setColor(Color.Blue)
.setOpenlink("https://twitter.com/hashtag/$1").setPriority(Priority.High).build(),
new ChatFormatterBuilder().setRegex(CYAN_PATTERN).setColor(Color.Aqua).build() // #55
);
private static Gson gson = new GsonBuilder() private static Gson gson = new GsonBuilder()
.registerTypeHierarchyAdapter(TellrawSerializableEnum.class, new TellrawSerializer.TwEnum()) .registerTypeHierarchyAdapter(TellrawSerializableEnum.class, new TellrawSerializer.TwEnum())
.registerTypeHierarchyAdapter(Collection.class, new TellrawSerializer.TwCollection()) .registerTypeHierarchyAdapter(Collection.class, new TellrawSerializer.TwCollection())
@ -108,9 +109,8 @@ public class ChatProcessing {
ArrayList<ChatFormatter> formatters = addFormatters(colormode); ArrayList<ChatFormatter> formatters = addFormatters(colormode);
if (colormode == channel.color && mp != null && mp.RainbowPresserColorMode) { // Only overwrite channel color if (colormode == channel.color && mp != null && mp.RainbowPresserColorMode) { // Only overwrite channel color
final AtomicInteger rpc = new AtomicInteger(0); final AtomicInteger rpc = new AtomicInteger(0);
formatters.add(new ChatFormatterBuilder().setColor(colormode).setOnmatch((match, builder) -> { formatters.add(ChatFormatter.builder().color(colormode).onmatch((match, cf) -> {
builder.setColor( cf.setColor(RainbowPresserColors[rpc.getAndUpdate(i -> ++i < RainbowPresserColors.length ? i : 0)]);
RainbowPresserColors[rpc.getAndUpdate(i -> ++i < RainbowPresserColors.length ? i : 0)]);
return match; return match;
}).build()); }).build());
} }
@ -245,8 +245,8 @@ public class ChatProcessing {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
ArrayList<ChatFormatter> formatters = (ArrayList<ChatFormatter>) commonFormatters.clone(); ArrayList<ChatFormatter> formatters = (ArrayList<ChatFormatter>) commonFormatters.clone();
formatters.add(new ChatFormatterBuilder().setRegex(ENTIRE_MESSAGE_PATTERN).setColor(colormode) formatters.add(
.setPriority(Priority.Low).build()); ChatFormatter.builder().regex(ENTIRE_MESSAGE_PATTERN).color(colormode).priority(Priority.Low).build());
if (Bukkit.getOnlinePlayers().size() > 0) { if (Bukkit.getOnlinePlayers().size() > 0) {
StringBuilder namesb = new StringBuilder("(?i)("); StringBuilder namesb = new StringBuilder("(?i)(");
@ -271,8 +271,8 @@ public class ChatProcessing {
} }
nicksb.append(")"); nicksb.append(")");
formatters.add(new ChatFormatterBuilder().setRegex(Pattern.compile(namesb.toString())).setColor(Color.Aqua) formatters.add(ChatFormatter.builder().regex(Pattern.compile(namesb.toString())).color(Color.Aqua)
.setOnmatch((match, builder) -> { .onmatch((match, builder) -> {
Player p = Bukkit.getPlayer(match); Player p = Bukkit.getPlayer(match);
if (p == null) { if (p == null) {
PluginMain.Instance.getLogger() PluginMain.Instance.getLogger()
@ -287,11 +287,11 @@ public class ChatProcessing {
(float) PlayerListener.NotificationPitch); (float) PlayerListener.NotificationPitch);
String color = String.format("§%x", (mpp.GetFlairColor() == 0x00 ? 0xb : mpp.GetFlairColor())); String color = String.format("§%x", (mpp.GetFlairColor() == 0x00 ? 0xb : mpp.GetFlairColor()));
return color + p.getName() + "§r"; return color + p.getName() + "§r";
}).setPriority(Priority.High).build()); }).priority(Priority.High).build());
if (addNickFormatter) if (addNickFormatter)
formatters.add(new ChatFormatterBuilder().setRegex(Pattern.compile(nicksb.toString())) formatters.add(ChatFormatter.builder().regex((Pattern.compile(nicksb.toString()))).color(Color.Aqua)
.setColor(Color.Aqua).setOnmatch((match, builder) -> { .onmatch((match, builder) -> {
if (PlayerListener.nicknames.containsKey(match)) { if (PlayerListener.nicknames.containsKey(match)) {
Player p = Bukkit.getPlayer(PlayerListener.nicknames.get(match)); Player p = Bukkit.getPlayer(PlayerListener.nicknames.get(match));
if (p == null) { if (p == null) {
@ -309,7 +309,7 @@ public class ChatProcessing {
Bukkit.getServer().getLogger().warning("Player nicknamed " + match Bukkit.getServer().getLogger().warning("Player nicknamed " + match
+ " not found in nickname map but was reported as online."); + " not found in nickname map but was reported as online.");
return "§c" + match + "§r"; return "§c" + match + "§r";
}).setPriority(Priority.High).build()); }).priority(Priority.High).build());
} }
return formatters; return formatters;
} }

View file

@ -16,8 +16,7 @@ public final class IgnoreCommand extends UCommandBase {
@Override @Override
public String[] GetHelpText(String alias) { public String[] GetHelpText(String alias) {
return new String[] { "§6---- Ignore flair ----", return new String[] { "§6---- Ignore flair ----",
"Stop the \"write your name in the thread\" message from showing up", "Stop the \"write your name in the thread\" message from showing up" };
"Use /u ignore <username> if you commented from multiple accounts" };
} }
@Override @Override

View file

@ -23,7 +23,7 @@ public class AddCommand extends AnnounceCommandBase {
return false; return false;
} }
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
for (int i = 2; i < args.length; i++) { for (int i = 0; i < args.length; i++) {
sb.append(args[i]); sb.append(args[i]);
if (i != args.length - 1) if (i != args.length - 1)
sb.append(" "); sb.append(" ");

View file

@ -21,22 +21,22 @@ public class EditCommand extends AnnounceCommandBase {
sender.sendMessage("§cError: This command can only be used from a command block. You can use add and remove, though it's not recommended."); sender.sendMessage("§cError: This command can only be used from a command block. You can use add and remove, though it's not recommended.");
return true; return true;
} }
if (args.length < 4) { if (args.length < 1) {
return false; return false;
} }
StringBuilder sb1 = new StringBuilder(); StringBuilder sb1 = new StringBuilder();
for (int i1 = 3; i1 < args.length; i1++) { for (int i1 = 1; i1 < args.length; i1++) {
sb1.append(args[i1]); sb1.append(args[i1]);
if (i1 != args.length - 1) if (i1 != args.length - 1)
sb1.append(" "); sb1.append(" ");
} }
String finalmessage1 = sb1.toString().replace('&', '§'); String finalmessage1 = sb1.toString().replace('&', '§');
int index = Integer.parseInt(args[2]); int index = Integer.parseInt(args[0]);
if (index > 100) if (index > 100)
return false; return false;
while (PluginMain.AnnounceMessages.size() <= index) while (PluginMain.AnnounceMessages.size() <= index)
PluginMain.AnnounceMessages.add(""); PluginMain.AnnounceMessages.add("");
PluginMain.AnnounceMessages.set(Integer.parseInt(args[2]), PluginMain.AnnounceMessages.set(Integer.parseInt(args[0]),
finalmessage1); finalmessage1);
sender.sendMessage("Announcement edited."); sender.sendMessage("Announcement edited.");
return true; return true;

View file

@ -16,7 +16,7 @@ public class SetTimeCommand extends AnnounceCommandBase {
@Override @Override
public boolean OnCommand(CommandSender sender, String alias, public boolean OnCommand(CommandSender sender, String alias,
String[] args) { String[] args) {
if (args.length < 3) { if (args.length < 1) {
return false; return false;
} }
try { try {

View file

@ -4,12 +4,17 @@ import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.function.BiFunction;
import java.util.function.Function; import java.util.function.Function;
import java.util.function.Predicate; import java.util.function.Predicate;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern;
import buttondevteam.chat.ChatProcessing; import buttondevteam.chat.ChatProcessing;
import buttondevteam.chat.commands.ucmds.admin.DebugCommand; import buttondevteam.chat.commands.ucmds.admin.DebugCommand;
import buttondevteam.lib.chat.*; import buttondevteam.lib.chat.*;
import lombok.Builder;
import lombok.Data;
/** /**
* A {@link ChatFormatter} shows what formatting to use based on regular expressions. {@link ChatFormatter#Combine(List, String, TellrawPart)} is used to turn it into a {@link TellrawPart}, combining * A {@link ChatFormatter} shows what formatting to use based on regular expressions. {@link ChatFormatter#Combine(List, String, TellrawPart)} is used to turn it into a {@link TellrawPart}, combining
@ -19,12 +24,24 @@ import buttondevteam.lib.chat.*;
* @author NorbiPeti * @author NorbiPeti
* *
*/ */
@Data
@Builder
public final class ChatFormatter { public final class ChatFormatter {
private ChatFormatterBuilder builder; Pattern regex;
boolean italic;
public ChatFormatter(ChatFormatterBuilder builder) { boolean bold;
this.builder = builder; boolean underlined;
} boolean strikethrough;
boolean obfuscated;
Color color;
BiFunction<String, ChatFormatter, String> onmatch;
String openlink;
@Builder.Default
Priority priority = Priority.Normal;
@Builder.Default
short removeCharCount = 0;
@Builder.Default
boolean range = false;
public static void Combine(List<ChatFormatter> formatters, String str, TellrawPart tp) { public static void Combine(List<ChatFormatter> formatters, String str, TellrawPart tp) {
/* /*
@ -33,7 +50,7 @@ public final class ChatFormatter {
header("ChatFormatter.Combine begin"); header("ChatFormatter.Combine begin");
ArrayList<FormattedSection> sections = new ArrayList<FormattedSection>(); ArrayList<FormattedSection> sections = new ArrayList<FormattedSection>();
for (ChatFormatter formatter : formatters) { for (ChatFormatter formatter : formatters) {
Matcher matcher = formatter.builder.regex.matcher(str); Matcher matcher = formatter.regex.matcher(str);
while (matcher.find()) { while (matcher.find()) {
DebugCommand.SendDebugMessage("Found match from " + matcher.start() + " to " + (matcher.end() - 1)); DebugCommand.SendDebugMessage("Found match from " + matcher.start() + " to " + (matcher.end() - 1));
DebugCommand.SendDebugMessage("With formatter:" + formatter); DebugCommand.SendDebugMessage("With formatter:" + formatter);
@ -44,13 +61,13 @@ public final class ChatFormatter {
if (groups.size() > 0) if (groups.size() > 0)
DebugCommand.SendDebugMessage("First group: " + groups.get(0)); DebugCommand.SendDebugMessage("First group: " + groups.get(0));
FormattedSection section = new FormattedSection(formatter, matcher.start(), matcher.end() - 1, groups, FormattedSection section = new FormattedSection(formatter, matcher.start(), matcher.end() - 1, groups,
formatter.builder.removecharcount, formatter.builder.removecharcount, formatter.builder.range); formatter.removeCharCount, formatter.removeCharCount, formatter.range);
sections.add(section); sections.add(section);
} }
} }
sections.sort((s1, s2) -> s1.Start == s2.Start sections.sort((s1, s2) -> s1.Start == s2.Start
? s1.End == s2.End ? Integer.compare(s2.Formatters.get(0).builder.priority.GetValue(), ? s1.End == s2.End ? Integer.compare(s2.Formatters.get(0).priority.GetValue(),
s1.Formatters.get(0).builder.priority.GetValue()) : Integer.compare(s2.End, s1.End) s1.Formatters.get(0).priority.GetValue()) : Integer.compare(s2.End, s1.End)
: Integer.compare(s1.Start, s2.Start)); : Integer.compare(s1.Start, s2.Start));
header("Range section conversion"); header("Range section conversion");
@ -243,11 +260,10 @@ public final class ChatFormatter {
if (found) { if (found) {
i = 1; i = 1;
found = false; found = false;
sections.sort((s1, sections.sort((s1, s2) -> s1.Start == s2.Start
s2) -> s1.Start == s2.Start ? s1.End == s2.End ? s1.End == s2.End ? Integer.compare(s2.Formatters.get(0).priority.GetValue(),
? Integer.compare(s2.Formatters.get(0).builder.priority.GetValue(), s1.Formatters.get(0).priority.GetValue()) : Integer.compare(s2.End, s1.End)
s1.Formatters.get(0).builder.priority.GetValue()) : Integer.compare(s1.Start, s2.Start));
: Integer.compare(s2.End, s1.End) : Integer.compare(s1.Start, s2.Start));
} else } else
cont = false; cont = false;
} }
@ -266,25 +282,25 @@ public final class ChatFormatter {
Color color = null; Color color = null;
boolean bold = false, italic = false, underlined = false, strikethrough = false, obfuscated = false; boolean bold = false, italic = false, underlined = false, strikethrough = false, obfuscated = false;
String openlink = null; String openlink = null;
section.Formatters.sort((cf2, cf1) -> cf1.builder.priority.compareTo(cf2.builder.priority)); section.Formatters.sort((cf2, cf1) -> cf1.priority.compareTo(cf2.priority));
for (ChatFormatter formatter : section.Formatters) { for (ChatFormatter formatter : section.Formatters) {
DebugCommand.SendDebugMessage("Applying formatter: " + formatter); DebugCommand.SendDebugMessage("Applying formatter: " + formatter);
if (formatter.builder.onmatch != null) if (formatter.onmatch != null)
originaltext = formatter.builder.onmatch.apply(originaltext, formatter.builder); originaltext = formatter.onmatch.apply(originaltext, formatter);
if (formatter.builder.color != null) if (formatter.color != null)
color = formatter.builder.color; color = formatter.color;
if (formatter.builder.bold) if (formatter.bold)
bold = true; bold = true;
if (formatter.builder.italic) if (formatter.italic)
italic = true; italic = true;
if (formatter.builder.underlined) if (formatter.underlined)
underlined = true; underlined = true;
if (formatter.builder.strikethrough) if (formatter.strikethrough)
strikethrough = true; strikethrough = true;
if (formatter.builder.obfuscated) if (formatter.obfuscated)
obfuscated = true; obfuscated = true;
if (formatter.builder.openlink != null) if (formatter.openlink != null)
openlink = formatter.builder.openlink; openlink = formatter.openlink;
} }
TellrawPart newtp = new TellrawPart(""); TellrawPart newtp = new TellrawPart("");
newtp.setText(originaltext); newtp.setText(originaltext);
@ -306,16 +322,6 @@ public final class ChatFormatter {
header("ChatFormatter.Combine done"); header("ChatFormatter.Combine done");
} }
@Override
public String toString() {
return new StringBuilder("F(").append(builder.color).append(", ")
.append((builder.bold ? "bold" : "") + (builder.italic ? "italic" : "")
+ (builder.underlined ? "underlined" : "") + (builder.strikethrough ? "strikethrough" : "")
+ (builder.obfuscated ? "obfuscated" : ""))
.append(", ").append(builder.openlink).append(", ").append(builder.priority).append(", ")
.append(builder.regex).append(")").toString(); // TODO: Lombok
}
/** /**
* *
* @param str * @param str

View file

@ -1,148 +0,0 @@
package buttondevteam.chat.formatting;
import java.io.Serializable;
import java.util.function.BiFunction;
import java.util.regex.Pattern;
import buttondevteam.lib.chat.*;
import lombok.SneakyThrows;
public class ChatFormatterBuilder implements Serializable {
private static final long serialVersionUID = -6115913400749778686L;
Pattern regex;
boolean italic;
boolean bold;
boolean underlined;
boolean strikethrough;
boolean obfuscated;
Color color;
BiFunction<String, ChatFormatterBuilder, String> onmatch;
String openlink;
Priority priority = Priority.Normal;
short removecharcount = 0;
boolean range = false;
/**
* The returned object is backed by this builder. All changes made to this object affets the returned one.
*/
@SneakyThrows
public ChatFormatter build() {
return new ChatFormatter(this);
}
public Pattern getRegex() {
return regex;
}
public ChatFormatterBuilder setRegex(Pattern regex) {
this.regex = regex;
return this;
}
public boolean isItalic() {
return italic;
}
public ChatFormatterBuilder setItalic(boolean italic) {
this.italic = italic;
return this;
}
public boolean isBold() {
return bold;
}
public ChatFormatterBuilder setBold(boolean bold) {
this.bold = bold;
return this;
}
public boolean isUnderlined() {
return underlined;
}
public ChatFormatterBuilder setUnderlined(boolean underlined) {
this.underlined = underlined;
return this;
}
public boolean isStrikethrough() {
return strikethrough;
}
public ChatFormatterBuilder setStrikethrough(boolean strikethrough) {
this.strikethrough = strikethrough;
return this;
}
public boolean isObfuscated() {
return obfuscated;
}
public ChatFormatterBuilder setObfuscated(boolean obfuscated) {
this.obfuscated = obfuscated;
return this;
}
public Color getColor() {
return color;
}
public ChatFormatterBuilder setColor(Color color) {
this.color = color;
return this;
}
public BiFunction<String, ChatFormatterBuilder, String> getOnmatch() {
return onmatch;
}
/**
* Making any changes here using the builder will not affect the previous matches with the current design
*/
public ChatFormatterBuilder setOnmatch(BiFunction<String, ChatFormatterBuilder, String> onmatch) {
this.onmatch = onmatch;
return this;
}
public String getOpenlink() {
return openlink;
}
public ChatFormatterBuilder setOpenlink(String openlink) {
this.openlink = openlink;
return this;
}
public Priority getPriority() {
return priority;
}
public ChatFormatterBuilder setPriority(Priority priority) {
this.priority = priority == null ? Priority.Normal : priority;
return this;
}
public short getRemoveCharCount() {
return removecharcount;
}
/**
* Sets the amount of characters to be removed from the start and the end of the match.
*
* @return This instance
*/
public ChatFormatterBuilder setRemoveCharCount(short removecharcount) {
this.removecharcount = removecharcount;
return this;
}
public boolean isRange() {
return range;
}
public ChatFormatterBuilder setRange(boolean range) {
this.range = range;
return this;
}
}