PlotSquared/src/main/java/com/plotsquared/bukkit/chat/MessagePart.java

152 lines
5.8 KiB
Java
Raw Normal View History

2015-07-30 19:24:01 +02:00
package com.plotsquared.bukkit.chat;
2015-07-27 19:50:04 +02:00
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
2015-07-30 16:25:16 +02:00
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import com.google.common.collect.BiMap;
import com.google.common.collect.ImmutableBiMap;
import com.google.gson.stream.JsonWriter;
import com.intellectualcrafters.configuration.serialization.ConfigurationSerializable;
import com.intellectualcrafters.configuration.serialization.ConfigurationSerialization;
/**
* Internal class: Represents a component of a JSON-serializable {@link FancyMessage}.
*/
2015-09-13 06:04:31 +02:00
final class MessagePart implements JsonRepresentedObject, ConfigurationSerializable, Cloneable {
2015-09-11 12:09:22 +02:00
ChatColor color = ChatColor.WHITE;
ArrayList<ChatColor> styles = new ArrayList<ChatColor>();
2015-09-13 06:04:31 +02:00
String clickActionName = null, clickActionData = null, hoverActionName = null;
2015-09-11 12:09:22 +02:00
JsonRepresentedObject hoverActionData = null;
TextualComponent text = null;
String insertionData = null;
ArrayList<JsonRepresentedObject> translationReplacements = new ArrayList<JsonRepresentedObject>();
2015-09-13 06:04:31 +02:00
MessagePart(final TextualComponent text) {
2015-09-11 12:09:22 +02:00
this.text = text;
}
2015-09-13 06:04:31 +02:00
MessagePart() {
2015-09-11 12:09:22 +02:00
text = null;
}
2015-09-13 06:04:31 +02:00
boolean hasText() {
2015-09-11 12:09:22 +02:00
return text != null;
}
2015-09-13 06:04:31 +02:00
2015-09-11 12:09:22 +02:00
@Override
@SuppressWarnings("unchecked")
2015-09-13 06:04:31 +02:00
public MessagePart clone() throws CloneNotSupportedException {
2015-09-11 12:09:22 +02:00
final MessagePart obj = (MessagePart) super.clone();
obj.styles = (ArrayList<ChatColor>) styles.clone();
2015-09-13 06:04:31 +02:00
if (hoverActionData instanceof JsonString) {
2015-09-11 12:09:22 +02:00
obj.hoverActionData = new JsonString(((JsonString) hoverActionData).getValue());
2015-09-13 06:04:31 +02:00
} else if (hoverActionData instanceof FancyMessage) {
2015-09-11 12:09:22 +02:00
obj.hoverActionData = ((FancyMessage) hoverActionData).clone();
}
obj.translationReplacements = (ArrayList<JsonRepresentedObject>) translationReplacements.clone();
return obj;
2015-09-13 06:04:31 +02:00
2015-09-11 12:09:22 +02:00
}
2015-09-13 06:04:31 +02:00
2015-09-11 12:09:22 +02:00
static final BiMap<ChatColor, String> stylesToNames;
2015-09-13 06:04:31 +02:00
static {
2015-09-11 12:09:22 +02:00
final ImmutableBiMap.Builder<ChatColor, String> builder = ImmutableBiMap.builder();
2015-09-13 06:04:31 +02:00
for (final ChatColor style : ChatColor.values()) {
if (!style.isFormat()) {
2015-09-11 12:09:22 +02:00
continue;
}
2015-09-13 06:04:31 +02:00
2015-09-11 12:09:22 +02:00
String styleName;
2015-09-13 06:04:31 +02:00
switch (style) {
2015-09-11 12:09:22 +02:00
case MAGIC:
styleName = "obfuscated";
break;
case UNDERLINE:
styleName = "underlined";
break;
default:
styleName = style.name().toLowerCase();
break;
}
2015-09-13 06:04:31 +02:00
2015-09-11 12:09:22 +02:00
builder.put(style, styleName);
}
stylesToNames = builder.build();
}
2015-09-13 06:04:31 +02:00
2015-09-11 12:09:22 +02:00
@Override
2015-09-13 06:04:31 +02:00
public void writeJson(final JsonWriter json) {
try {
2015-09-11 12:09:22 +02:00
json.beginObject();
text.writeJson(json);
json.name("color").value(color.name().toLowerCase());
2015-09-13 06:04:31 +02:00
for (final ChatColor style : styles) {
2015-09-11 12:09:22 +02:00
json.name(stylesToNames.get(style)).value(true);
}
2015-09-13 06:04:31 +02:00
if ((clickActionName != null) && (clickActionData != null)) {
json.name("clickEvent").beginObject().name("action").value(clickActionName).name("value").value(clickActionData).endObject();
2015-09-11 12:09:22 +02:00
}
2015-09-13 06:04:31 +02:00
if ((hoverActionName != null) && (hoverActionData != null)) {
json.name("hoverEvent").beginObject().name("action").value(hoverActionName).name("value");
2015-09-11 12:09:22 +02:00
hoverActionData.writeJson(json);
json.endObject();
}
2015-09-13 06:04:31 +02:00
if (insertionData != null) {
2015-09-11 12:09:22 +02:00
json.name("insertion").value(insertionData);
}
2015-09-13 06:04:31 +02:00
if ((translationReplacements.size() > 0) && (text != null) && TextualComponent.isTranslatableText(text)) {
2015-09-11 12:09:22 +02:00
json.name("with").beginArray();
2015-09-13 06:04:31 +02:00
for (final JsonRepresentedObject obj : translationReplacements) {
2015-09-11 12:09:22 +02:00
obj.writeJson(json);
}
json.endArray();
}
json.endObject();
2015-09-13 06:04:31 +02:00
} catch (final IOException e) {
2015-09-11 12:09:22 +02:00
Bukkit.getLogger().log(Level.WARNING, "A problem occured during writing of JSON string", e);
}
}
2015-09-13 06:04:31 +02:00
2015-09-11 12:09:22 +02:00
@Override
2015-09-13 06:04:31 +02:00
public Map<String, Object> serialize() {
2015-09-11 12:09:22 +02:00
final HashMap<String, Object> map = new HashMap<String, Object>();
map.put("text", text);
map.put("styles", styles);
map.put("color", color.getChar());
map.put("hoverActionName", hoverActionName);
map.put("hoverActionData", hoverActionData);
map.put("clickActionName", clickActionName);
map.put("clickActionData", clickActionData);
map.put("insertion", insertionData);
map.put("translationReplacements", translationReplacements);
return map;
}
2015-09-13 06:04:31 +02:00
2015-09-11 12:09:22 +02:00
@SuppressWarnings("unchecked")
2015-09-13 06:04:31 +02:00
public static MessagePart deserialize(final Map<String, Object> serialized) {
2015-09-11 12:09:22 +02:00
final MessagePart part = new MessagePart((TextualComponent) serialized.get("text"));
part.styles = (ArrayList<ChatColor>) serialized.get("styles");
part.color = ChatColor.getByChar(serialized.get("color").toString());
part.hoverActionName = (String) serialized.get("hoverActionName");
part.hoverActionData = (JsonRepresentedObject) serialized.get("hoverActionData");
part.clickActionName = (String) serialized.get("clickActionName");
part.clickActionData = (String) serialized.get("clickActionData");
part.insertionData = (String) serialized.get("insertion");
part.translationReplacements = (ArrayList<JsonRepresentedObject>) serialized.get("translationReplacements");
return part;
}
2015-09-13 06:04:31 +02:00
static {
2015-09-11 12:09:22 +02:00
ConfigurationSerialization.registerClass(MessagePart.class);
}
2015-09-13 06:04:31 +02:00
}