ScruffyRules

fuck yeah

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