ScruffyRules

Untitled

Nov 20th, 2016
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.83 KB | None | 0 0
  1. package net.minecraft.util.text;
  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. import net.minecraft.util.EnumTypeAdapterFactory;
  18. import net.minecraft.util.JsonUtils;
  19. import net.minecraftforge.fml.relauncher.Side;
  20. import net.minecraftforge.fml.relauncher.SideOnly;
  21.  
  22. public interface ITextComponent extends Iterable < ITextComponent > {
  23.     ITextComponent setStyle(Style style);
  24.  
  25.     Style getStyle();
  26.  
  27.     /**
  28.      * Appends the given text to the end of this component.
  29.      */
  30.     ITextComponent appendText(String text);
  31.  
  32.     /**
  33.      * Appends the given component to the end of this one.
  34.      */
  35.     ITextComponent appendSibling(ITextComponent component);
  36.  
  37.     /**
  38.      * Gets the text of this component, without any special formatting codes added, for chat.  TODO: why is this two
  39.      * different methods?
  40.      */
  41.     String getUnformattedComponentText();
  42.  
  43.     /**
  44.      * Get the text of this component, <em>and all child components</em>, with all special formatting codes removed.
  45.      */
  46.     String getUnformattedText();
  47.  
  48.     /**
  49.      * Gets the text of this component, with formatting codes added for rendering.
  50.      */
  51.     String getFormattedText();
  52.  
  53.     List < ITextComponent > getSiblings();
  54.  
  55.     /**
  56.      * Creates a copy of this component.  Almost a deep copy, except the style is shallow-copied.
  57.      */
  58.     ITextComponent createCopy();
  59.  
  60.     public static class Serializer implements JsonDeserializer < ITextComponent > ,
  61.     JsonSerializer < ITextComponent > {
  62.         private static final Gson GSON;
  63.  
  64.         public ITextComponent deserialize(JsonElement p_deserialize_1_, Type p_deserialize_2_, JsonDeserializationContext p_deserialize_3_) throws JsonParseException {
  65.             if (p_deserialize_1_.isJsonPrimitive()) {
  66.                 return new TextComponentString(p_deserialize_1_.getAsString());
  67.             } else if (!p_deserialize_1_.isJsonObject()) {
  68.                 if (p_deserialize_1_.isJsonArray()) {
  69.                     JsonArray jsonarray1 = p_deserialize_1_.getAsJsonArray();
  70.                     ITextComponent itextcomponent1 = null;
  71.  
  72.                     for (JsonElement jsonelement: jsonarray1) {
  73.                         ITextComponent itextcomponent2 = this.deserialize(jsonelement, jsonelement.getClass(), p_deserialize_3_);
  74.  
  75.                         if (itextcomponent1 == null) {
  76.                             itextcomponent1 = itextcomponent2;
  77.                         } else {
  78.                             itextcomponent1.appendSibling(itextcomponent2);
  79.                         }
  80.                     }
  81.  
  82.                     return itextcomponent1;
  83.                 } else {
  84.                     throw new JsonParseException("Don\'t know how to turn " + p_deserialize_1_ + " into a Component");
  85.                 }
  86.             } else {
  87.                 JsonObject jsonobject = p_deserialize_1_.getAsJsonObject();
  88.                 ITextComponent itextcomponent;
  89.  
  90.                 if (jsonobject.has("text")) {
  91.                     itextcomponent = new TextComponentString(jsonobject.get("text").getAsString());
  92.                 } else if (jsonobject.has("translate")) {
  93.                     String s = jsonobject.get("translate").getAsString();
  94.  
  95.                     if (jsonobject.has("with")) {
  96.                         JsonArray jsonarray = jsonobject.getAsJsonArray("with");
  97.                         Object[] aobject = new Object[jsonarray.size()];
  98.  
  99.                         for (int i = 0; i < aobject.length; ++i) {
  100.                             aobject[i] = this.deserialize(jsonarray.get(i), p_deserialize_2_, p_deserialize_3_);
  101.  
  102.                             if (aobject[i] instanceof TextComponentString) {
  103.                                 TextComponentString textcomponentstring = (TextComponentString) aobject[i];
  104.  
  105.                                 if (textcomponentstring.getStyle().isEmpty() && textcomponentstring.getSiblings().isEmpty()) {
  106.                                     aobject[i] = textcomponentstring.getText();
  107.                                 }
  108.                             }
  109.                         }
  110.  
  111.                         itextcomponent = new TextComponentTranslation(s, aobject);
  112.                     } else {
  113.                         itextcomponent = new TextComponentTranslation(s, new Object[0]);
  114.                     }
  115.                 } else if (jsonobject.has("score")) {
  116.                     JsonObject jsonobject1 = jsonobject.getAsJsonObject("score");
  117.  
  118.                     if (!jsonobject1.has("name") || !jsonobject1.has("objective")) {
  119.                         throw new JsonParseException("A score component needs a least a name and an objective");
  120.                     }
  121.  
  122.                     itextcomponent = new TextComponentScore(JsonUtils.getString(jsonobject1, "name"), JsonUtils.getString(jsonobject1, "objective"));
  123.  
  124.                     if (jsonobject1.has("value")) {
  125.                         ((TextComponentScore) itextcomponent).setValue(JsonUtils.getString(jsonobject1, "value"));
  126.                     }
  127.                 } else {
  128.                     if (!jsonobject.has("selector")) {
  129.                         throw new JsonParseException("Don\'t know how to turn " + p_deserialize_1_ + " into a Component");
  130.                     }
  131.  
  132.                     itextcomponent = new TextComponentSelector(JsonUtils.getString(jsonobject, "selector"));
  133.                 }
  134.  
  135.                 if (jsonobject.has("extra")) {
  136.                     JsonArray jsonarray2 = jsonobject.getAsJsonArray("extra");
  137.  
  138.                     if (jsonarray2.size() <= 0) {
  139.                         throw new JsonParseException("Unexpected empty array of components");
  140.                     }
  141.  
  142.                     for (int j = 0; j < jsonarray2.size(); ++j) {
  143.                         itextcomponent.appendSibling(this.deserialize(jsonarray2.get(j), p_deserialize_2_, p_deserialize_3_));
  144.                     }
  145.                 }
  146.  
  147.                 itextcomponent.setStyle((Style) p_deserialize_3_.deserialize(p_deserialize_1_, Style.class));
  148.                 return itextcomponent;
  149.             }
  150.         }
  151.  
  152.         private void serializeChatStyle(Style style, JsonObject object, JsonSerializationContext ctx) {
  153.             JsonElement jsonelement = ctx.serialize(style);
  154.  
  155.             if (jsonelement.isJsonObject()) {
  156.                 JsonObject jsonobject = (JsonObject) jsonelement;
  157.  
  158.                 for (Entry < String, JsonElement > entry: jsonobject.entrySet()) {
  159.                     object.add((String) entry.getKey(), (JsonElement) entry.getValue());
  160.                 }
  161.             }
  162.         }
  163.  
  164.         public JsonElement serialize(ITextComponent p_serialize_1_, Type p_serialize_2_, JsonSerializationContext p_serialize_3_) {
  165.             JsonObject jsonobject = new JsonObject();
  166.  
  167.             if (!p_serialize_1_.getStyle().isEmpty()) {
  168.                 this.serializeChatStyle(p_serialize_1_.getStyle(), jsonobject, p_serialize_3_);
  169.             }
  170.  
  171.             if (!p_serialize_1_.getSiblings().isEmpty()) {
  172.                 JsonArray jsonarray = new JsonArray();
  173.  
  174.                 for (ITextComponent itextcomponent: p_serialize_1_.getSiblings()) {
  175.                     jsonarray.add(this.serialize((ITextComponent) itextcomponent, itextcomponent.getClass(), p_serialize_3_));
  176.                 }
  177.  
  178.                 jsonobject.add("extra", jsonarray);
  179.             }
  180.  
  181.             if (p_serialize_1_ instanceof TextComponentString) {
  182.                 jsonobject.addProperty("text", ((TextComponentString) p_serialize_1_).getText());
  183.             } else if (p_serialize_1_ instanceof TextComponentTranslation) {
  184.                 TextComponentTranslation textcomponenttranslation = (TextComponentTranslation) p_serialize_1_;
  185.                 jsonobject.addProperty("translate", textcomponenttranslation.getKey());
  186.  
  187.                 if (textcomponenttranslation.getFormatArgs() != null && textcomponenttranslation.getFormatArgs().length > 0) {
  188.                     JsonArray jsonarray1 = new JsonArray();
  189.  
  190.                     for (Object object: textcomponenttranslation.getFormatArgs()) {
  191.                         if (object instanceof ITextComponent) {
  192.                             jsonarray1.add(this.serialize((ITextComponent)((ITextComponent) object), object.getClass(), p_serialize_3_));
  193.                         } else {
  194.                             jsonarray1.add(new JsonPrimitive(String.valueOf(object)));
  195.                         }
  196.                     }
  197.  
  198.                     jsonobject.add("with", jsonarray1);
  199.                 }
  200.             } else if (p_serialize_1_ instanceof TextComponentScore) {
  201.                 TextComponentScore textcomponentscore = (TextComponentScore) p_serialize_1_;
  202.                 JsonObject jsonobject1 = new JsonObject();
  203.                 jsonobject1.addProperty("name", textcomponentscore.getName());
  204.                 jsonobject1.addProperty("objective", textcomponentscore.getObjective());
  205.                 jsonobject1.addProperty("value", textcomponentscore.getUnformattedComponentText());
  206.                 jsonobject.add("score", jsonobject1);
  207.             } else {
  208.                 if (!(p_serialize_1_ instanceof TextComponentSelector)) {
  209.                     throw new IllegalArgumentException("Don\'t know how to serialize " + p_serialize_1_ + " as a Component");
  210.                 }
  211.  
  212.                 TextComponentSelector textcomponentselector = (TextComponentSelector) p_serialize_1_;
  213.                 jsonobject.addProperty("selector", textcomponentselector.getSelector());
  214.             }
  215.  
  216.             return jsonobject;
  217.         }
  218.  
  219.         public static String componentToJson(ITextComponent component) {
  220.             return GSON.toJson((Object) component);
  221.         }
  222.  
  223.         public static ITextComponent jsonToComponent(String json) {
  224.             return (ITextComponent) JsonUtils.gsonDeserialize(GSON, json, ITextComponent.class, false);
  225.         }
  226.  
  227.         public static ITextComponent fromJsonLenient(String json) {
  228.             return (ITextComponent) JsonUtils.gsonDeserialize(GSON, json, ITextComponent.class, true);
  229.         }
  230.  
  231.         static {
  232.             GsonBuilder gsonbuilder = new GsonBuilder();
  233.             gsonbuilder.registerTypeHierarchyAdapter(ITextComponent.class, new ITextComponent.Serializer());
  234.             gsonbuilder.registerTypeHierarchyAdapter(Style.class, new Style.Serializer());
  235.             gsonbuilder.registerTypeAdapterFactory(new EnumTypeAdapterFactory());
  236.             GSON = gsonbuilder.create();
  237.         }
  238.     }
  239. }
Advertisement
Add Comment
Please, Sign In to add comment