| 1 | package net.minecraft.util;
|
| 2 |
|
| 3 | import com.google.gson.Gson;
|
| 4 | import com.google.gson.GsonBuilder;
|
| 5 | import com.google.gson.JsonArray;
|
| 6 | import com.google.gson.JsonDeserializationContext;
|
| 7 | import com.google.gson.JsonDeserializer;
|
| 8 | import com.google.gson.JsonElement;
|
| 9 | import com.google.gson.JsonObject;
|
| 10 | import com.google.gson.JsonParseException;
|
| 11 | import com.google.gson.JsonPrimitive;
|
| 12 | import com.google.gson.JsonSerializationContext;
|
| 13 | import com.google.gson.JsonSerializer;
|
| 14 | import java.lang.reflect.Type;
|
| 15 | import java.util.List;
|
| 16 | import java.util.Map.Entry;
|
| 17 |
|
| 18 | public interface IChatComponent extends Iterable<IChatComponent>
|
| 19 | {
|
| 20 | IChatComponent setChatStyle(ChatStyle style);
|
| 21 |
|
| 22 | ChatStyle getChatStyle();
|
| 23 |
|
| 24 | /**
|
| 25 | * Appends the given text to the end of this component.
|
| 26 | */
|
| 27 | IChatComponent appendText(String text);
|
| 28 |
|
| 29 | /**
|
| 30 | * Appends the given component to the end of this one.
|
| 31 | */
|
| 32 | IChatComponent appendSibling(IChatComponent component);
|
| 33 |
|
| 34 | /**
|
| 35 | * Gets the text of this component, without any special formatting codes added, for chat. TODO: why is this two
|
| 36 | * different methods?
|
| 37 | */
|
| 38 | String getUnformattedTextForChat();
|
| 39 |
|
| 40 | /**
|
| 41 | * Get the text of this component, <em>and all child components</em>, with all special formatting codes removed.
|
| 42 | */
|
| 43 | String getUnformattedText();
|
| 44 |
|
| 45 | /**
|
| 46 | * Gets the text of this component, with formatting codes added for rendering.
|
| 47 | */
|
| 48 | String getFormattedText();
|
| 49 |
|
| 50 | List<IChatComponent> getSiblings();
|
| 51 |
|
| 52 | /**
|
| 53 | * Creates a copy of this component. Almost a deep copy, except the style is shallow-copied.
|
| 54 | */
|
| 55 | IChatComponent createCopy();
|
| 56 |
|
| 57 | public static class Serializer implements JsonDeserializer<IChatComponent>, JsonSerializer<IChatComponent>
|
| 58 | {
|
| 59 | private static final Gson GSON;
|
| 60 |
|
| 61 | public IChatComponent deserialize(JsonElement p_deserialize_1_, Type p_deserialize_2_, JsonDeserializationContext p_deserialize_3_) throws JsonParseException
|
| 62 | {
|
| 63 | if (p_deserialize_1_.isJsonPrimitive())
|
| 64 | {
|
| 65 | return new ChatComponentText(p_deserialize_1_.getAsString());
|
| 66 | }
|
| 67 | else if (!p_deserialize_1_.isJsonObject())
|
| 68 | {
|
| 69 | if (p_deserialize_1_.isJsonArray())
|
| 70 | {
|
| 71 | JsonArray jsonarray1 = p_deserialize_1_.getAsJsonArray();
|
| 72 | IChatComponent ichatcomponent1 = null;
|
| 73 |
|
| 74 | for (JsonElement jsonelement : jsonarray1)
|
| 75 | {
|
| 76 | IChatComponent ichatcomponent2 = this.deserialize(jsonelement, jsonelement.getClass(), p_deserialize_3_);
|
| 77 |
|
| 78 | if (ichatcomponent1 == null)
|
| 79 | {
|
| 80 | ichatcomponent1 = ichatcomponent2;
|
| 81 | }
|
| 82 | else
|
| 83 | {
|
| 84 | ichatcomponent1.appendSibling(ichatcomponent2);
|
| 85 | }
|
| 86 | }
|
| 87 |
|
| 88 | return ichatcomponent1;
|
| 89 | }
|
| 90 | else
|
| 91 | {
|
| 92 | throw new JsonParseException("Don\'t know how to turn " + p_deserialize_1_.toString() + " into a Component");
|
| 93 | }
|
| 94 | }
|
| 95 | else
|
| 96 | {
|
| 97 | JsonObject jsonobject = p_deserialize_1_.getAsJsonObject();
|
| 98 | IChatComponent ichatcomponent;
|
| 99 |
|
| 100 | if (jsonobject.has("text"))
|
| 101 | {
|
| 102 | ichatcomponent = new ChatComponentText(jsonobject.get("text").getAsString());
|
| 103 | }
|
| 104 | else if (jsonobject.has("translate"))
|
| 105 | {
|
| 106 | String s = jsonobject.get("translate").getAsString();
|
| 107 |
|
| 108 | if (jsonobject.has("with"))
|
| 109 | {
|
| 110 | JsonArray jsonarray = jsonobject.getAsJsonArray("with");
|
| 111 | Object[] aobject = new Object[jsonarray.size()];
|
| 112 |
|
| 113 | for (int i = 0; i < aobject.length; ++i)
|
| 114 | {
|
| 115 | aobject[i] = this.deserialize(jsonarray.get(i), p_deserialize_2_, p_deserialize_3_);
|
| 116 |
|
| 117 | if (aobject[i] instanceof ChatComponentText)
|
| 118 | {
|
| 119 | ChatComponentText chatcomponenttext = (ChatComponentText)aobject[i];
|
| 120 |
|
| 121 | if (chatcomponenttext.getChatStyle().isEmpty() && chatcomponenttext.getSiblings().isEmpty())
|
| 122 | {
|
| 123 | aobject[i] = chatcomponenttext.getChatComponentText_TextValue();
|
| 124 | }
|
| 125 | }
|
| 126 | }
|
| 127 |
|
| 128 | ichatcomponent = new ChatComponentTranslation(s, aobject);
|
| 129 | }
|
| 130 | else
|
| 131 | {
|
| 132 | ichatcomponent = new ChatComponentTranslation(s, new Object[0]);
|
| 133 | }
|
| 134 | }
|
| 135 | else if (jsonobject.has("score"))
|
| 136 | {
|
| 137 | JsonObject jsonobject1 = jsonobject.getAsJsonObject("score");
|
| 138 |
|
| 139 | if (!jsonobject1.has("name") || !jsonobject1.has("objective"))
|
| 140 | {
|
| 141 | throw new JsonParseException("A score component needs a least a name and an objective");
|
| 142 | }
|
| 143 |
|
| 144 | ichatcomponent = new ChatComponentScore(JsonUtils.getString(jsonobject1, "name"), JsonUtils.getString(jsonobject1, "objective"));
|
| 145 |
|
| 146 | if (jsonobject1.has("value"))
|
| 147 | {
|
| 148 | ((ChatComponentScore)ichatcomponent).setValue(JsonUtils.getString(jsonobject1, "value"));
|
| 149 | }
|
| 150 | }
|
| 151 | else
|
| 152 | {
|
| 153 | if (!jsonobject.has("selector"))
|
| 154 | {
|
| 155 | throw new JsonParseException("Don\'t know how to turn " + p_deserialize_1_.toString() + " into a Component");
|
| 156 | }
|
| 157 |
|
| 158 | ichatcomponent = new ChatComponentSelector(JsonUtils.getString(jsonobject, "selector"));
|
| 159 | }
|
| 160 |
|
| 161 | if (jsonobject.has("extra"))
|
| 162 | {
|
| 163 | JsonArray jsonarray2 = jsonobject.getAsJsonArray("extra");
|
| 164 |
|
| 165 | if (jsonarray2.size() <= 0)
|
| 166 | {
|
| 167 | throw new JsonParseException("Unexpected empty array of components");
|
| 168 | }
|
| 169 |
|
| 170 | for (int j = 0; j < jsonarray2.size(); ++j)
|
| 171 | {
|
| 172 | ichatcomponent.appendSibling(this.deserialize(jsonarray2.get(j), p_deserialize_2_, p_deserialize_3_));
|
| 173 | }
|
| 174 | }
|
| 175 |
|
| 176 | ichatcomponent.setChatStyle((ChatStyle)p_deserialize_3_.deserialize(p_deserialize_1_, ChatStyle.class));
|
| 177 | return ichatcomponent;
|
| 178 | }
|
| 179 | }
|
| 180 |
|
| 181 | private void serializeChatStyle(ChatStyle style, JsonObject object, JsonSerializationContext ctx)
|
| 182 | {
|
| 183 | JsonElement jsonelement = ctx.serialize(style);
|
| 184 |
|
| 185 | if (jsonelement.isJsonObject())
|
| 186 | {
|
| 187 | JsonObject jsonobject = (JsonObject)jsonelement;
|
| 188 |
|
| 189 | for (Entry<String, JsonElement> entry : jsonobject.entrySet())
|
| 190 | {
|
| 191 | object.add((String)entry.getKey(), (JsonElement)entry.getValue());
|
| 192 | }
|
| 193 | }
|
| 194 | }
|
| 195 |
|
| 196 | public JsonElement serialize(IChatComponent p_serialize_1_, Type p_serialize_2_, JsonSerializationContext p_serialize_3_)
|
| 197 | {
|
| 198 | if (p_serialize_1_ instanceof ChatComponentText && p_serialize_1_.getChatStyle().isEmpty() && p_serialize_1_.getSiblings().isEmpty())
|
| 199 | {
|
| 200 | return new JsonPrimitive(((ChatComponentText)p_serialize_1_).getChatComponentText_TextValue());
|
| 201 | }
|
| 202 | else
|
| 203 | {
|
| 204 | JsonObject jsonobject = new JsonObject();
|
| 205 |
|
| 206 | if (!p_serialize_1_.getChatStyle().isEmpty())
|
| 207 | {
|
| 208 | this.serializeChatStyle(p_serialize_1_.getChatStyle(), jsonobject, p_serialize_3_);
|
| 209 | }
|
| 210 |
|
| 211 | if (!p_serialize_1_.getSiblings().isEmpty())
|
| 212 | {
|
| 213 | JsonArray jsonarray = new JsonArray();
|
| 214 |
|
| 215 | for (IChatComponent ichatcomponent : p_serialize_1_.getSiblings())
|
| 216 | {
|
| 217 | jsonarray.add(this.serialize((IChatComponent)ichatcomponent, ichatcomponent.getClass(), p_serialize_3_));
|
| 218 | }
|
| 219 |
|
| 220 | jsonobject.add("extra", jsonarray);
|
| 221 | }
|
| 222 |
|
| 223 | if (p_serialize_1_ instanceof ChatComponentText)
|
| 224 | {
|
| 225 | jsonobject.addProperty("text", ((ChatComponentText)p_serialize_1_).getChatComponentText_TextValue());
|
| 226 | }
|
| 227 | else if (p_serialize_1_ instanceof ChatComponentTranslation)
|
| 228 | {
|
| 229 | ChatComponentTranslation chatcomponenttranslation = (ChatComponentTranslation)p_serialize_1_;
|
| 230 | jsonobject.addProperty("translate", chatcomponenttranslation.getKey());
|
| 231 |
|
| 232 | if (chatcomponenttranslation.getFormatArgs() != null && chatcomponenttranslation.getFormatArgs().length > 0)
|
| 233 | {
|
| 234 | JsonArray jsonarray1 = new JsonArray();
|
| 235 |
|
| 236 | for (Object object : chatcomponenttranslation.getFormatArgs())
|
| 237 | {
|
| 238 | if (object instanceof IChatComponent)
|
| 239 | {
|
| 240 | jsonarray1.add(this.serialize((IChatComponent)((IChatComponent)object), object.getClass(), p_serialize_3_));
|
| 241 | }
|
| 242 | else
|
| 243 | {
|
| 244 | jsonarray1.add(new JsonPrimitive(String.valueOf(object)));
|
| 245 | }
|
| 246 | }
|
| 247 |
|
| 248 | jsonobject.add("with", jsonarray1);
|
| 249 | }
|
| 250 | }
|
| 251 | else if (p_serialize_1_ instanceof ChatComponentScore)
|
| 252 | {
|
| 253 | ChatComponentScore chatcomponentscore = (ChatComponentScore)p_serialize_1_;
|
| 254 | JsonObject jsonobject1 = new JsonObject();
|
| 255 | jsonobject1.addProperty("name", chatcomponentscore.getName());
|
| 256 | jsonobject1.addProperty("objective", chatcomponentscore.getObjective());
|
| 257 | jsonobject1.addProperty("value", chatcomponentscore.getUnformattedTextForChat());
|
| 258 | jsonobject.add("score", jsonobject1);
|
| 259 | }
|
| 260 | else
|
| 261 | {
|
| 262 | if (!(p_serialize_1_ instanceof ChatComponentSelector))
|
| 263 | {
|
| 264 | throw new IllegalArgumentException("Don\'t know how to serialize " + p_serialize_1_ + " as a Component");
|
| 265 | }
|
| 266 |
|
| 267 | ChatComponentSelector chatcomponentselector = (ChatComponentSelector)p_serialize_1_;
|
| 268 | jsonobject.addProperty("selector", chatcomponentselector.getSelector());
|
| 269 | }
|
| 270 |
|
| 271 | return jsonobject;
|
| 272 | }
|
| 273 | }
|
| 274 |
|
| 275 | public static String componentToJson(IChatComponent component)
|
| 276 | {
|
| 277 | return GSON.toJson((Object)component);
|
| 278 | }
|
| 279 |
|
| 280 | public static IChatComponent jsonToComponent(String json)
|
| 281 | {
|
| 282 | return (IChatComponent)GSON.fromJson(json, IChatComponent.class);
|
| 283 | }
|
| 284 |
|
| 285 | static
|
| 286 | {
|
| 287 | GsonBuilder gsonbuilder = new GsonBuilder();
|
| 288 | gsonbuilder.registerTypeHierarchyAdapter(IChatComponent.class, new IChatComponent.Serializer());
|
| 289 | gsonbuilder.registerTypeHierarchyAdapter(ChatStyle.class, new ChatStyle.Serializer());
|
| 290 | gsonbuilder.registerTypeAdapterFactory(new EnumTypeAdapterFactory());
|
| 291 | GSON = gsonbuilder.create();
|
| 292 | }
|
| 293 | }
|
| 294 | }
|
| 295 |
|