Fixed formatting!

This commit is contained in:
Norbi Peti 2016-09-08 19:44:03 +02:00
parent 6648bc6901
commit dc7913ca14
5 changed files with 31 additions and 8 deletions

View file

@ -27,7 +27,12 @@ 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 CONSOLE_PING_PATTERN = Pattern.compile("(?i)" + Pattern.quote("@console"));
@ -35,7 +40,8 @@ public class ChatProcessing {
private static final Pattern URL_PATTERN = Pattern.compile("(http[\\w:/?=$\\-_.+!*'(),]+)");
private static final Pattern ENTIRE_MESSAGE_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 String[] RainbowPresserColors = new String[] { "red", "gold", "yellow", "green", "blue",
"dark_purple" }; // TODO
@ -235,7 +241,8 @@ public class ChatProcessing {
Gson gson = new GsonBuilder()
.registerTypeHierarchyAdapter(TellrawSerializableEnum.class, new TellrawSerializer.TwEnum())
.registerTypeHierarchyAdapter(Collection.class, new TellrawSerializer.TwCollection())
.disableHtmlEscaping().create();
.registerTypeAdapter(Boolean.class, new TellrawSerializer.TwBool())
.registerTypeAdapter(boolean.class, new TellrawSerializer.TwBool()).disableHtmlEscaping().create();
String jsonstr = gson.toJson(json);
if (jsonstr.length() >= 32767) {
sender.sendMessage(

View file

@ -146,7 +146,7 @@ public final class ChatFormatter {
replacewith = formatter.replacewith;
}
TellrawPart newtp = new TellrawPart("");
if (replacewith != null)
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);

View file

@ -4,7 +4,7 @@ import java.io.Serializable;
public final class TellrawEvent<T extends TellrawEvent.Action> implements Serializable {
private static final long serialVersionUID = -1681364161210561505L;
private boolean hoverEvent;
private transient boolean hoverEvent;
private T action;
private Object value;

View file

@ -4,11 +4,12 @@ import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
@SuppressWarnings("unused")
public final class TellrawPart implements Serializable {
private static final long serialVersionUID = 4125357644462144024L;
private ChatFormatter.Color color;
private transient ChatFormatter.Format format;
private boolean italics;
private boolean italic;
private boolean bold;
private boolean underlined;
private boolean strikethrough;
@ -37,13 +38,13 @@ public final class TellrawPart implements Serializable {
public TellrawPart setFormat(ChatFormatter.Format format) {
this.format = format;
this.italics = false;
this.italic = false;
this.bold = false;
this.underlined = false;
this.strikethrough = false;
this.obfuscated = false;
if (format.equals(ChatFormatter.Format.Italic))
this.italics = true;
this.italic = true;
else if (format.equals(ChatFormatter.Format.Bold))
this.bold = true;
else if (format.equals(ChatFormatter.Format.Underlined))
@ -52,7 +53,7 @@ public final class TellrawPart implements Serializable {
this.strikethrough = true;
else if (format.equals(ChatFormatter.Format.Obfuscated))
this.obfuscated = true;
else // TODO: Don't serialize false values, find out why is it bugging
else
throw new UnsupportedOperationException("Trying to set to an unknown format!");
return this;
}

View file

@ -39,4 +39,19 @@ public abstract class TellrawSerializer {
return array;
}
}
public static class TwBool extends TypeAdapter<Boolean> {
@Override
public Boolean read(JsonReader reader) throws IOException {
throw new UnsupportedOperationException();
}
@Override
public void write(JsonWriter writer, Boolean val) throws IOException {
if (val)
writer.value(val);
else
writer.nullValue();
}
}
}