2015-07-30 19:24:01 +02:00
|
|
|
package com.plotsquared.bukkit.chat;
|
2015-06-23 23:44:44 +02:00
|
|
|
|
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;
|
2016-02-14 02:01:18 +01:00
|
|
|
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
|
|
|
|
2015-06-23 23:44:44 +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 {
|
2016-02-14 02:01:18 +01:00
|
|
|
|
|
|
|
static final BiMap<ChatColor, String> stylesToNames;
|
|
|
|
|
|
|
|
static {
|
2016-03-23 02:41:37 +01:00
|
|
|
ImmutableBiMap.Builder<ChatColor, String> builder = ImmutableBiMap.builder();
|
|
|
|
for (ChatColor style : ChatColor.values()) {
|
2016-02-14 02:01:18 +01:00
|
|
|
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<>();
|
2016-03-23 02:41:37 +01:00
|
|
|
|
|
|
|
MessagePart(TextualComponent text) {
|
2015-09-11 12:09:22 +02:00
|
|
|
this.text = text;
|
|
|
|
}
|
2016-03-23 02:41:37 +01:00
|
|
|
|
2015-09-13 06:04:31 +02:00
|
|
|
MessagePart() {
|
2016-03-23 02:41:37 +01:00
|
|
|
this.text = null;
|
2015-09-11 12:09:22 +02:00
|
|
|
}
|
2016-02-14 02:01:18 +01:00
|
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
2016-03-23 02:41:37 +01:00
|
|
|
public static MessagePart deserialize(Map<String, Object> serialized) {
|
|
|
|
MessagePart part = new MessagePart((TextualComponent) serialized.get("text"));
|
2016-02-14 02:01:18 +01:00
|
|
|
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() {
|
2016-03-23 02:41:37 +01:00
|
|
|
return this.text != null;
|
2015-09-11 12:09:22 +02:00
|
|
|
}
|
2016-03-23 02:41:37 +01: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 {
|
2016-03-23 02:41:37 +01:00
|
|
|
MessagePart obj = (MessagePart) super.clone();
|
|
|
|
obj.styles = (ArrayList<ChatColor>) this.styles.clone();
|
|
|
|
if (this.hoverActionData instanceof JsonString) {
|
|
|
|
obj.hoverActionData = new JsonString(((JsonString) this.hoverActionData).getValue());
|
|
|
|
} else if (this.hoverActionData instanceof FancyMessage) {
|
|
|
|
obj.hoverActionData = ((FancyMessage) this.hoverActionData).clone();
|
2015-09-11 12:09:22 +02:00
|
|
|
}
|
2016-03-23 02:41:37 +01:00
|
|
|
obj.translationReplacements = (ArrayList<JsonRepresentedObject>) this.translationReplacements.clone();
|
2015-09-11 12:09:22 +02:00
|
|
|
return obj;
|
2016-02-14 02:01:18 +01:00
|
|
|
|
2015-09-11 12:09:22 +02:00
|
|
|
}
|
2016-03-23 02:41:37 +01:00
|
|
|
|
2015-09-11 12:09:22 +02:00
|
|
|
@Override
|
2016-03-23 02:41:37 +01:00
|
|
|
public void writeJson(JsonWriter json) {
|
2015-09-13 06:04:31 +02:00
|
|
|
try {
|
2015-09-11 12:09:22 +02:00
|
|
|
json.beginObject();
|
2016-03-23 02:41:37 +01:00
|
|
|
this.text.writeJson(json);
|
|
|
|
json.name("color").value(this.color.name().toLowerCase());
|
|
|
|
for (ChatColor style : this.styles) {
|
2015-09-11 12:09:22 +02:00
|
|
|
json.name(stylesToNames.get(style)).value(true);
|
|
|
|
}
|
2016-03-23 02:41:37 +01:00
|
|
|
if ((this.clickActionName != null) && (this.clickActionData != null)) {
|
|
|
|
json.name("clickEvent").beginObject().name("action").value(this.clickActionName).name("value").value(this.clickActionData)
|
|
|
|
.endObject();
|
2015-09-11 12:09:22 +02:00
|
|
|
}
|
2016-03-23 02:41:37 +01:00
|
|
|
if ((this.hoverActionName != null) && (this.hoverActionData != null)) {
|
|
|
|
json.name("hoverEvent").beginObject().name("action").value(this.hoverActionName).name("value");
|
|
|
|
this.hoverActionData.writeJson(json);
|
2015-09-11 12:09:22 +02:00
|
|
|
json.endObject();
|
|
|
|
}
|
2016-03-23 02:41:37 +01:00
|
|
|
if (this.insertionData != null) {
|
|
|
|
json.name("insertion").value(this.insertionData);
|
2015-09-11 12:09:22 +02:00
|
|
|
}
|
2016-03-23 02:41:37 +01:00
|
|
|
if (!this.translationReplacements.isEmpty() && (this.text != null) && TextualComponent.isTranslatableText(this.text)) {
|
2015-09-11 12:09:22 +02:00
|
|
|
json.name("with").beginArray();
|
2016-03-23 02:41:37 +01:00
|
|
|
for (JsonRepresentedObject obj : this.translationReplacements) {
|
2015-09-11 12:09:22 +02:00
|
|
|
obj.writeJson(json);
|
|
|
|
}
|
|
|
|
json.endArray();
|
|
|
|
}
|
|
|
|
json.endObject();
|
2016-03-23 02:41:37 +01:00
|
|
|
} catch (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
|
|
|
}
|
|
|
|
}
|
2016-03-23 02:41:37 +01: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-23 02:41:37 +01:00
|
|
|
HashMap<String, Object> map = new HashMap<>();
|
|
|
|
map.put("text", this.text);
|
|
|
|
map.put("styles", this.styles);
|
|
|
|
map.put("color", this.color.getChar());
|
|
|
|
map.put("hoverActionName", this.hoverActionName);
|
|
|
|
map.put("hoverActionData", this.hoverActionData);
|
|
|
|
map.put("clickActionName", this.clickActionName);
|
|
|
|
map.put("clickActionData", this.clickActionData);
|
|
|
|
map.put("insertion", this.insertionData);
|
|
|
|
map.put("translationReplacements", this.translationReplacements);
|
2015-09-11 12:09:22 +02:00
|
|
|
return map;
|
|
|
|
}
|
2016-03-23 02:41:37 +01:00
|
|
|
|
2015-06-23 23:44:44 +02:00
|
|
|
}
|