FIXED #39! Mostly.

This commit is contained in:
Norbi Peti 2016-09-04 21:07:09 +02:00
parent 9d18604c25
commit 5cdbe61166
6 changed files with 149 additions and 101 deletions

View file

@ -15,41 +15,20 @@ public final class ChatFormatter {
private Predicate<String> onmatch;
private String openlink;
private Priority priority;
private String replacewith;
private static final String[] RainbowPresserColors = new String[] { "red", "gold", "yellow", "green", "blue",
"dark_purple" };
"dark_purple" }; // TODO
public ChatFormatter(Pattern regex, Format format) {
public ChatFormatter(Pattern regex, Format format, Color color, Predicate<String> onmatch, String openlink,
Priority priority, String replacewith) {
this.regex = regex;
this.format = format;
this.priority = Priority.High;
}
public ChatFormatter(Pattern regex, Format format, String openlink) {
this.regex = regex;
this.format = format;
this.openlink = openlink;
this.priority = Priority.High;
}
public ChatFormatter(Pattern regex, Color color, String openlink, Priority priority) {
this.regex = regex;
this.color = color;
this.openlink = openlink;
this.priority = priority;
}
public ChatFormatter(Pattern regex, Color color, Priority priority) {
this.regex = regex;
this.color = color;
this.priority = priority;
}
public ChatFormatter(Pattern regex, Color color, Predicate<String> onmatch, Priority priority) {
this.regex = regex;
this.color = color;
this.onmatch = onmatch;
this.priority = priority;
this.openlink = openlink;
this.priority = Priority.High;
this.replacewith = replacewith;
}
public static String Combine(List<ChatFormatter> formatters, String str) {
@ -67,8 +46,7 @@ public final class ChatFormatter {
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);
FormattedSection section = new FormattedSection(formatter, matcher.start(), matcher.end() - 1, groups);
sections.add(section);
}
}
@ -96,8 +74,8 @@ public final class ChatFormatter {
origend = origend2;
origend2 = tmp;
}
FormattedSection section = new FormattedSection(
sections.get(i - 1).Formatters, sections.get(i).Start, origend, sections.get(i - 1).Matches);
FormattedSection section = new FormattedSection(sections.get(i - 1).Formatters, sections.get(i).Start,
origend, sections.get(i - 1).Matches);
section.Formatters.addAll(sections.get(i).Formatters);
section.Matches.addAll(sections.get(i).Matches);
sections.add(i, section);
@ -124,10 +102,12 @@ public final class ChatFormatter {
sections.remove(i);
found = true;
}
if (i < sections.size() && sections.get(i).End < sections.get(i).Start) {
DebugCommand.SendDebugMessage("Removing section: " + sections.get(i));
sections.remove(i);
found = true;
for (int j = i - 1; j <= i + 1; j++) {
if (j < sections.size() && sections.get(j).End < sections.get(j).Start) {
DebugCommand.SendDebugMessage("Removing section: " + sections.get(j));
sections.remove(j);
found = true;
}
}
i = nextindex - 1;
i++;
@ -151,27 +131,31 @@ public final class ChatFormatter {
DebugCommand.SendDebugMessage("Applying section: " + section);
String originaltext = str.substring(section.Start, section.End + 1);
DebugCommand.SendDebugMessage("Originaltext: " + originaltext);
finalstring.append(",{\"text\":\""); // TODO: Bool replace - Replace with match $1?
finalstring.append(originaltext);
finalstring.append("\"");
Color color = null;
Format format = null;
String openlink = null;
Priority priority = null;
String replacewith = null;
section.Formatters.sort((cf1, cf2) -> cf1.priority.compareTo(cf2.priority));
for (ChatFormatter formatter : section.Formatters) {
DebugCommand.SendDebugMessage("Applying formatter: " + formatter);
if (formatter.onmatch == null || formatter.onmatch.test(originaltext)) {
if (priority == null || priority.GetValue() < formatter.priority.GetValue()) {
if (formatter.color != null)
color = formatter.color;
format = formatter.format; // TODO: Don't overwrite
// parts, and work until all
// of them are combined
if (formatter.format != null)
format = formatter.format;
if (formatter.openlink != null)
openlink = formatter.openlink;
priority = formatter.priority;
}
if (formatter.replacewith != null)
replacewith = formatter.replacewith;
} else
DebugCommand.SendDebugMessage("Onmatch predicate returned false.");
}
finalstring.append(",{\"text\":\"");
if (replacewith != null)
finalstring.append(replacewith.replace("$1", section.Matches.get(0)));
else
finalstring.append(originaltext);
finalstring.append("\"");
if (color != null) {
finalstring.append(",\"color\":\"");
finalstring.append(color.name);
@ -190,7 +174,8 @@ public final class ChatFormatter {
finalstring.append("}");
}
DebugCommand.SendDebugMessage("Finalstring: " + finalstring);
return finalstring.toString(); // TODO
return finalstring.toString();
}
@Override

View file

@ -0,0 +1,85 @@
package buttondevteam.thebuttonmcchat;
import java.util.function.Predicate;
import java.util.regex.Pattern;
import buttondevteam.thebuttonmcchat.ChatFormatter.Color;
import buttondevteam.thebuttonmcchat.ChatFormatter.Format;
import buttondevteam.thebuttonmcchat.ChatFormatter.Priority;
public class ChatFormatterBuilder {
private Pattern regex;
private Format format;
private Color color;
private Predicate<String> onmatch;
private String openlink;
private Priority priority;
private String replacewith;
public ChatFormatter build() {
return new ChatFormatter(regex, format, color, onmatch, openlink, priority, replacewith);
}
public Pattern getRegex() {
return regex;
}
public ChatFormatterBuilder setRegex(Pattern regex) {
this.regex = regex;
return this;
}
public Format getFormat() {
return format;
}
public ChatFormatterBuilder setFormat(Format format) {
this.format = format;
return this;
}
public Color getColor() {
return color;
}
public ChatFormatterBuilder setColor(Color color) {
this.color = color;
return this;
}
public Predicate<String> getOnmatch() {
return onmatch;
}
public ChatFormatterBuilder setOnmatch(Predicate<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;
return this;
}
public String getReplacewith() {
return replacewith;
}
public ChatFormatterBuilder setReplacewith(String replacewith) {
this.replacewith = replacewith;
return this;
}
}

View file

@ -69,7 +69,8 @@ public class ChatProcessing {
colormode = ChatFormatter.Color.Green;
// If greentext, ignore channel or player colors
formatters.add(new ChatFormatter(Pattern.compile(".+"), colormode, "", Priority.Low));
formatters.add(new ChatFormatterBuilder().setRegex(Pattern.compile(".+")).setColor(colormode)
.setPriority(Priority.Low).build());
String formattedmessage = message;
formattedmessage = formattedmessage.replace("\\", "\\\\");
@ -78,16 +79,19 @@ public class ChatProcessing {
String suggestmsg = formattedmessage;
formatters.add(new ChatFormatter(Pattern.compile("(?<!\\\\)\\*\\*((?:\\\\\\*|[^\\*])+[^\\*\\\\])\\*\\*"),
ChatFormatter.Format.Bold, "$1"));
formatters.add(new ChatFormatter(Pattern.compile("(?<!\\\\)\\*((?:\\\\\\*|[^\\*])+[^\\*\\\\])\\*"),
ChatFormatter.Format.Italic, "$1"));
formatters.add(new ChatFormatter(Pattern.compile("(?<!\\\\)\\_((?:\\\\\\_|[^\\_])+[^\\_\\\\])\\_"),
ChatFormatter.Format.Underlined, "$1"));
formatters.add(new ChatFormatterBuilder()
.setRegex(Pattern.compile("(?<!\\\\)\\*\\*((?:\\\\\\*|[^\\*])+[^\\*\\\\])\\*\\*"))
.setFormat(ChatFormatter.Format.Bold).setReplacewith("$1").build());
formatters.add(
new ChatFormatterBuilder().setRegex(Pattern.compile("(?<!\\\\)\\*((?:\\\\\\*|[^\\*])+[^\\*\\\\])\\*"))
.setFormat(ChatFormatter.Format.Italic).setReplacewith("$1").build());
formatters.add(
new ChatFormatterBuilder().setRegex(Pattern.compile("(?<!\\\\)\\_((?:\\\\\\_|[^\\_])+[^\\_\\\\])\\_"))
.setFormat(ChatFormatter.Format.Underlined).setReplacewith("$1").build());
// URLs + Rainbow text
formatters.add(new ChatFormatter(Pattern.compile("(http[\\w:/?=$\\-_.+!*'(),]+)"),
ChatFormatter.Format.Underlined, "$1"));
formatters.add(new ChatFormatterBuilder().setRegex(Pattern.compile("(http[\\w:/?=$\\-_.+!*'(),]+)"))
.setFormat(ChatFormatter.Format.Underlined).setReplacewith("$1").build());
/*
* formattedmessage = formattedmessage .replace( item, String.format(
* "\",\"color\":\"%s\"},{\"text\":\"%s\",\"color\":\"%s\",\"underlined\":\"true\",\"clickEvent\":{\"action\":\"open_url\",\"value\":\"%s\"},\"hoverEvent\":{\"action\":\"show_text\",\"value\":{\"text\":\"\",\"extra\":[{\"text\":\"Open URL\",\"color\":\"blue\"}]}}},{\"text\":\""
@ -102,8 +106,8 @@ public class ChatProcessing {
sb.deleteCharAt(sb.length() - 1);
sb.append(")");
formatters
.add(new ChatFormatter(Pattern.compile(sb.toString()), ChatFormatter.Color.Aqua, (String match) -> {
formatters.add(new ChatFormatterBuilder().setRegex(Pattern.compile(sb.toString()))
.setColor(ChatFormatter.Color.Aqua).setOnmatch((String match) -> {
Player p = Bukkit.getPlayer(match);
if (p == null) {
PluginMain.Instance.getLogger()
@ -119,10 +123,10 @@ public class ChatProcessing {
(float) PlayerListener.NotificationPitch);
String color = String.format("§%x", (mpp.GetFlairColor() == 0x00 ? 0xb : mpp.GetFlairColor()));
return true; // TODO
}, Priority.High));
}).setPriority(Priority.High).build());
formatters
.add(new ChatFormatter(Pattern.compile(sb.toString()), ChatFormatter.Color.Aqua, (String match) -> {
formatters.add(new ChatFormatterBuilder().setRegex(Pattern.compile(sb.toString()))
.setColor(ChatFormatter.Color.Aqua).setOnmatch((String match) -> {
for (String n : PlayerListener.nicknames.keySet()) {
String nwithoutformatting = new String(n);
int index;
@ -155,32 +159,27 @@ public class ChatProcessing {
(mpp.GetFlairColor() == 0x00 ? 0xb : mpp.GetFlairColor()));
}
return true; // TODO
}, Priority.High));
}).setPriority(Priority.High).build());
}
pingedconsole = false;
formatters.add(new ChatFormatter(Pattern.compile("(?i)" + Pattern.quote("@console")), ChatFormatter.Color.Aqua,
(String match) -> {
formatters.add(new ChatFormatterBuilder().setRegex(Pattern.compile("(?i)" + Pattern.quote("@console")))
.setColor(ChatFormatter.Color.Aqua).setOnmatch((String match) -> {
if (!pingedconsole) {
System.out.print("\007");
pingedconsole = true;
}
return true;
}, Priority.High));
}).setPriority(Priority.High).build());
formatters.add(new ChatFormatter(Pattern.compile("#(\\w+)"), ChatFormatter.Color.Blue,
"https://twitter.com/hashtag/$1", Priority.High));
formatters
.add(new ChatFormatterBuilder().setRegex(Pattern.compile("#(\\w+)")).setColor(ChatFormatter.Color.Blue)
.setOpenlink("https://twitter.com/hashtag/$1").setPriority(Priority.High).build());
/*
* if (!hadurls) {
*
* if (formattedmessage.matches("(?i).*" + Pattern.quote("@console") +
* ".*")) { formattedmessage = formattedmessage.replaceAll( "(?i)" +
* Pattern.quote("@console"), "§b@console§r"); formattedmessage =
* formattedmessage .replaceAll( "(?i)" + Pattern.quote("@console"),
* String.format(
* "\",\"color\":\"%s\"},{\"text\":\"§b@console§r\",\"color\":\"blue\"},{\"text\":\""
* , colormode)); System.out.println("\007"); } }
* if (!hadurls) { if (formattedmessage.matches("(?i).*" + Pattern.quote("@console") + ".*")) { formattedmessage = formattedmessage.replaceAll( "(?i)" + Pattern.quote("@console"),
* "§b@console§r"); formattedmessage = formattedmessage .replaceAll( "(?i)" + Pattern.quote("@console"), String.format(
* "\",\"color\":\"%s\"},{\"text\":\"§b@console§r\",\"color\":\"blue\"},{\"text\":\"" , colormode)); System.out.println("\007"); } }
*/
StringBuilder json = new StringBuilder();
@ -207,24 +206,16 @@ public class ChatProcessing {
json.append("{\"text\":\"> \",\"color\":\"white\"}");
/*
* int index = -1; ArrayList<String> list = new ArrayList<String>();
* while ((index = message.indexOf("#", index + 1)) != -1) { int index2
* = message.indexOf(" ", index + 1); if (index2 == -1) index2 =
* message.length(); int index3 = message.indexOf("#", index + 1); if
* (index3 != -1 && index3 < index2) // A # occurs before a // space
* index2 = index3; String original = message.substring(index + 1,
* index2); list.add(original); }
*
* if (!hadurls) { for (String original : list) // Hashtags
* formattedmessage = formattedmessage .replace( "#" + original,
* int index = -1; ArrayList<String> list = new ArrayList<String>(); while ((index = message.indexOf("#", index + 1)) != -1) { int index2 = message.indexOf(" ", index + 1); if (index2 == -1)
* index2 = message.length(); int index3 = message.indexOf("#", index + 1); if (index3 != -1 && index3 < index2) // A # occurs before a // space index2 = index3; String original =
* message.substring(index + 1, index2); list.add(original); } if (!hadurls) { for (String original : list) // Hashtags formattedmessage = formattedmessage .replace( "#" + original,
* String.format(
* "\",\"color\":\"%s\"},{\"text\":\"#%s\",\"color\":\"blue\",\"clickEvent\":{\"action\":\"open_url\",\"value\":\"https://twitter.com/hashtag/%s\"},\"hoverEvent\":{\"action\":\"show_text\",\"value\":{\"text\":\"\",\"extra\":[{\"text\":\"Open on Twitter\",\"color\":\"blue\"}]}}},{\"text\":\""
* , colormode, original, original)); }
*/
/*
* json.append(String.format("{\"text\":\"%s\",\"color\":\"%s\"}]",
* ChatFormatter.Combine(formatters, formattedmessage), colormode));
* json.append(String.format("{\"text\":\"%s\",\"color\":\"%s\"}]", ChatFormatter.Combine(formatters, formattedmessage), colormode));
*/
json.append(ChatFormatter.Combine(formatters, formattedmessage));
json.append("]");

View file

@ -24,7 +24,6 @@ import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.event.player.PlayerChatTabCompleteEvent;
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.event.player.PlayerPickupItemEvent;
import org.bukkit.event.player.PlayerQuitEvent;
@ -35,8 +34,6 @@ import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.SkullMeta;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import org.bukkit.util.Vector;
import au.com.mineauz.minigames.MinigamePlayer;
import au.com.mineauz.minigames.Minigames;
import buttondevteam.bucket.core.TBMCPlayer;

View file

@ -4,7 +4,6 @@ import net.milkbowl.vault.chat.Chat;
import net.milkbowl.vault.economy.Economy;
import net.milkbowl.vault.permission.Permission;
import org.apache.commons.io.IOUtils;
import org.bukkit.Bukkit;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.configuration.InvalidConfigurationException;

View file

@ -1,16 +1,7 @@
package buttondevteam.thebuttonmcchat.commands.ucmds.admin;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import org.apache.commons.io.FileUtils;
import org.bukkit.command.CommandSender;
import org.bukkit.plugin.Plugin;
import buttondevteam.bucket.core.TBMCCoreAPI;
import buttondevteam.thebuttonmcchat.PluginMain;
public class UpdatePlugin extends AdminCommandBase {