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

151 lines
5.7 KiB
Java
Raw Normal View History

2015-07-30 19:24:01 +02:00
package com.plotsquared.bukkit.chat;
2015-07-30 16:25:16 +02:00
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;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
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
/**
* 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 {
static final BiMap<ChatColor, String> stylesToNames;
static {
final ImmutableBiMap.Builder<ChatColor, String> builder = ImmutableBiMap.builder();
for (final ChatColor style : ChatColor.values()) {
if (!style.isFormat()) {
continue;
}
String styleName;
switch (style) {
case MAGIC:
styleName = "obfuscated";
break;
case UNDERLINE:
styleName = "underlined";
break;
default:
styleName = style.name().toLowerCase();
break;
}
builder.put(style, styleName);
}
stylesToNames = builder.build();
}
static {
ConfigurationSerialization.registerClass(MessagePart.class);
}
2015-09-11 12:09:22 +02:00
ChatColor color = ChatColor.WHITE;
2016-03-14 07:18:04 +01:00
ArrayList<ChatColor> styles = new ArrayList<>();
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;
2016-03-14 07:18:04 +01:00
ArrayList<JsonRepresentedObject> translationReplacements = new ArrayList<>();
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;
}
@SuppressWarnings("unchecked")
public static MessagePart deserialize(final Map<String, Object> serialized) {
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
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-11 12:09:22 +02:00
}
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);
}
if ((!translationReplacements.isEmpty()) && (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) {
2016-02-05 00:45:50 +01:00
Bukkit.getLogger().log(Level.WARNING, "A problem occurred during writing of JSON string", e);
2015-09-11 12:09:22 +02:00
}
}
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() {
2016-03-14 07:18:04 +01:00
final HashMap<String, Object> map = new HashMap<>();
2015-09-11 12:09:22 +02:00
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
}