Started fixing #46

This commit is contained in:
Norbi Peti 2016-09-24 13:18:09 +02:00
parent 78a0a12d26
commit cb91125028
4 changed files with 46 additions and 42 deletions

View file

@ -27,14 +27,10 @@ import buttondevteam.chat.formatting.TellrawPart;
import buttondevteam.chat.formatting.TellrawSerializableEnum;
import buttondevteam.chat.formatting.TellrawSerializer;
import buttondevteam.chat.formatting.ChatFormatter.Color;
import buttondevteam.chat.formatting.ChatFormatter.Format;
import buttondevteam.chat.formatting.ChatFormatter.Priority;
import buttondevteam.chat.formatting.TellrawEvent.ClickAction;
import buttondevteam.chat.formatting.TellrawEvent.HoverAction;
import buttondevteam.chat.formatting.TellrawSerializer.TwCollection;
import buttondevteam.chat.formatting.TellrawSerializer.TwEnum;
public class ChatProcessing {
private static final Pattern ESCAPE_PATTERN = Pattern.compile("\\\\([\\*\\_\\\\])");
private static final Pattern CONSOLE_PING_PATTERN = Pattern.compile("(?i)" + Pattern.quote("@console"));
private static final Pattern HASHTAG_PATTERN = Pattern.compile("#(\\w+)");
private static final Pattern URL_PATTERN = Pattern.compile("(http[\\w:/?=$\\-_.+!*'(),]+)");
@ -103,17 +99,16 @@ public class ChatProcessing {
String suggestmsg = formattedmessage;
formatters.add(new ChatFormatterBuilder().setRegex(BOLD_PATTERN).setFormat(ChatFormatter.Format.Bold)
.setReplacewith("$1").build());
.setRemoveCharCount((short) 2).build());
formatters.add(new ChatFormatterBuilder().setRegex(ITALIC_PATTERN).setFormat(ChatFormatter.Format.Italic)
.setReplacewith("$1").build());
.setRemoveCharCount((short) 1).build());
formatters.add(new ChatFormatterBuilder().setRegex(UNDERLINED_PATTERN)
.setFormat(ChatFormatter.Format.Underlined).setReplacewith("$1").build());
formatters.add(new ChatFormatterBuilder().setRegex(Pattern.compile("\\\\([\\*\\_\\\\])")).setReplacewith("$1")
.build());
.setFormat(ChatFormatter.Format.Underlined).setRemoveCharCount((short) 1).build());
formatters.add(new ChatFormatterBuilder().setRegex(ESCAPE_PATTERN).setRemoveCharPos((short) 0).build());
// URLs + Rainbow text
formatters.add(new ChatFormatterBuilder().setRegex(URL_PATTERN).setFormat(ChatFormatter.Format.Underlined)
.setReplacewith("$1").build());
.setOpenlink("$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\":\""

View file

@ -28,10 +28,8 @@ import com.palmergames.bukkit.towny.object.TownyUniverse;
import java.io.*;
import java.lang.String;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.net.URLConnection;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
@ -340,19 +338,6 @@ public class PluginMain extends JavaPlugin { // Translated to Java: 2015.07.15.
}
}
private void addClassPath(final URL url) throws IOException {
final URLClassLoader sysloader = (URLClassLoader) ClassLoader.getSystemClassLoader();
final Class<URLClassLoader> sysclass = URLClassLoader.class;
try {
final Method method = sysclass.getDeclaredMethod("addURL", new Class[] { URL.class });
method.setAccessible(true);
method.invoke(sysloader, new Object[] { url });
} catch (final Throwable t) {
t.printStackTrace();
throw new IOException("Error adding " + url + " to system classloader");
}
}
public static Permission permission = null;
public static Economy economy = null;
public static Chat chat = null;

View file

@ -15,17 +15,19 @@ public final class ChatFormatter {
private Function<String, String> onmatch;
private String openlink;
private Priority priority;
private String replacewith;
private short removecharcount = 0;
private short removecharpos = -1;
public ChatFormatter(Pattern regex, Format format, Color color, Function<String, String> onmatch, String openlink,
Priority priority, String replacewith) {
Priority priority, short removecharcount, short removecharpos) {
this.regex = regex;
this.format = format;
this.color = color;
this.onmatch = onmatch;
this.openlink = openlink;
this.priority = Priority.High;
this.replacewith = replacewith;
this.removecharcount = removecharcount;
this.removecharpos = removecharpos;
}
public static void Combine(List<ChatFormatter> formatters, String str, TellrawPart tp) {
@ -130,7 +132,7 @@ public final class ChatFormatter {
Color color = null;
Format format = null;
String openlink = null;
String replacewith = null;
List<Integer> removecharpositions = new ArrayList<Integer>();
section.Formatters.sort((cf2, cf1) -> cf1.priority.compareTo(cf2.priority));
for (ChatFormatter formatter : section.Formatters) {
DebugCommand.SendDebugMessage("Applying formatter: " + formatter);
@ -142,14 +144,16 @@ public final class ChatFormatter {
format = formatter.format;
if (formatter.openlink != null)
openlink = formatter.openlink;
if (formatter.replacewith != null)
replacewith = formatter.replacewith;
if (formatter.removecharcount != 0) {
removecharpositions.add(section.Start + formatter.removecharcount); // TODO
removecharpositions.add(section.End - formatter.removecharcount);
}
if (formatter.removecharpos != -1)
removecharpositions.add((int) formatter.removecharpos);
}
TellrawPart newtp = new TellrawPart("");
if (replacewith != null) // TODO: The ranges may change when formattings are nested, and this can't handle that
newtp.setText(replacewith.replace("$1", section.Matches.get(0)));
else
newtp.setText(originaltext);
StringBuilder origtextsb = new StringBuilder(originaltext);
newtp.setText(origtextsb.toString());
if (color != null)
newtp.setColor(color);
if (format != null)

View file

@ -14,10 +14,11 @@ public class ChatFormatterBuilder {
private Function<String, String> onmatch;
private String openlink;
private Priority priority;
private String replacewith;
private short removecharcount = 0;
private short removecharpos = -1;
public ChatFormatter build() {
return new ChatFormatter(regex, format, color, onmatch, openlink, priority, replacewith);
return new ChatFormatter(regex, format, color, onmatch, openlink, priority, removecharcount, removecharpos);
}
public Pattern getRegex() {
@ -74,12 +75,31 @@ public class ChatFormatterBuilder {
return this;
}
public String getReplacewith() {
return replacewith;
public short getRemoveCharCount() {
return removecharcount;
}
public ChatFormatterBuilder setReplacewith(String replacewith) {
this.replacewith = replacewith;
/**
* 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 short getRemoveCharPos() {
return removecharpos;
}
/**
* Sets the position where a single character should be removed. Setting -1 will disable it.
*
* @return This instance
*/
public ChatFormatterBuilder setRemoveCharPos(short removecharpos) {
this.removecharpos = removecharpos;
return this;
}
}