Advertisement
Guest User

Untitled

a guest
May 21st, 2020
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 17.20 KB | None | 0 0
  1. MC Text:
  2. package net.minecraft.text;
  3.  
  4. import com.google.gson.Gson;
  5. import com.google.gson.GsonBuilder;
  6. import com.google.gson.JsonArray;
  7. import com.google.gson.JsonDeserializationContext;
  8. import com.google.gson.JsonDeserializer;
  9. import com.google.gson.JsonElement;
  10. import com.google.gson.JsonObject;
  11. import com.google.gson.JsonParseException;
  12. import com.google.gson.JsonPrimitive;
  13. import com.google.gson.JsonSerializationContext;
  14. import com.google.gson.JsonSerializer;
  15. import com.google.gson.stream.JsonReader;
  16. import com.mojang.brigadier.Message;
  17. import java.io.IOException;
  18. import java.io.StringReader;
  19. import java.lang.reflect.Field;
  20. import java.lang.reflect.Type;
  21. import java.util.Iterator;
  22. import java.util.List;
  23. import java.util.Map.Entry;
  24. import java.util.function.Consumer;
  25. import java.util.stream.Stream;
  26. import javax.annotation.Nullable;
  27. import net.minecraft.util.Formatting;
  28. import net.minecraft.util.Identifier;
  29. import net.minecraft.util.JsonHelper;
  30. import net.minecraft.util.LowercaseEnumTypeAdapterFactory;
  31. import net.minecraft.util.Util;
  32.  
  33. public interface Text extends Message, Iterable<Text> {
  34.    Text setStyle(Style style);
  35.  
  36.    Style getStyle();
  37.  
  38.    default Text append(String text) {
  39.       return this.append((Text)(new LiteralText(text)));
  40.    }
  41.  
  42.    Text append(Text text);
  43.  
  44.    String asString();
  45.  
  46.    default String getString() {
  47.       StringBuilder stringBuilder = new StringBuilder();
  48.       this.stream().forEach((text) -> {
  49.          stringBuilder.append(text.asString());
  50.       });
  51.       return stringBuilder.toString();
  52.    }
  53.  
  54.    default String asTruncatedString(int length) {
  55.       StringBuilder stringBuilder = new StringBuilder();
  56.       Iterator iterator = this.stream().iterator();
  57.  
  58.       while(iterator.hasNext()) {
  59.          int i = length - stringBuilder.length();
  60.          if (i <= 0) {
  61.             break;
  62.          }
  63.  
  64.          String string = ((Text)iterator.next()).asString();
  65.          stringBuilder.append(string.length() <= i ? string : string.substring(0, i));
  66.       }
  67.  
  68.       return stringBuilder.toString();
  69.    }
  70.  
  71.    default String asFormattedString() {
  72.       StringBuilder stringBuilder = new StringBuilder();
  73.       String string = "";
  74.       Iterator iterator = this.stream().iterator();
  75.  
  76.       while(iterator.hasNext()) {
  77.          Text text = (Text)iterator.next();
  78.          String string2 = text.asString();
  79.          if (!string2.isEmpty()) {
  80.             String string3 = text.getStyle().asString();
  81.             if (!string3.equals(string)) {
  82.                if (!string.isEmpty()) {
  83.                   stringBuilder.append(Formatting.RESET);
  84.                }
  85.  
  86.                stringBuilder.append(string3);
  87.                string = string3;
  88.             }
  89.  
  90.             stringBuilder.append(string2);
  91.          }
  92.       }
  93.  
  94.       if (!string.isEmpty()) {
  95.          stringBuilder.append(Formatting.RESET);
  96.       }
  97.  
  98.       return stringBuilder.toString();
  99.    }
  100.  
  101.    List<Text> getSiblings();
  102.  
  103.    Stream<Text> stream();
  104.  
  105.    default Stream<Text> streamCopied() {
  106.       return this.stream().map(Text::copyWithoutChildren);
  107.    }
  108.  
  109.    default Iterator<Text> iterator() {
  110.       return this.streamCopied().iterator();
  111.    }
  112.  
  113.    Text copy();
  114.  
  115.    default Text deepCopy() {
  116.       Text text = this.copy();
  117.       text.setStyle(this.getStyle().deepCopy());
  118.       Iterator var2 = this.getSiblings().iterator();
  119.  
  120.       while(var2.hasNext()) {
  121.          Text text2 = (Text)var2.next();
  122.          text.append(text2.deepCopy());
  123.       }
  124.  
  125.       return text;
  126.    }
  127.  
  128.    default Text styled(Consumer<Style> transformer) {
  129.       transformer.accept(this.getStyle());
  130.       return this;
  131.    }
  132.  
  133.    default Text formatted(Formatting... formatting) {
  134.       Formatting[] var2 = formatting;
  135.       int var3 = formatting.length;
  136.  
  137.       for(int var4 = 0; var4 < var3; ++var4) {
  138.          Formatting formatting2 = var2[var4];
  139.          this.formatted(formatting2);
  140.       }
  141.  
  142.       return this;
  143.    }
  144.  
  145.    default Text formatted(Formatting formatting) {
  146.       Style style = this.getStyle();
  147.       if (formatting.isColor()) {
  148.          style.setColor(formatting);
  149.       }
  150.  
  151.       if (formatting.isModifier()) {
  152.          switch(formatting) {
  153.          case OBFUSCATED:
  154.             style.setObfuscated(true);
  155.             break;
  156.          case BOLD:
  157.             style.setBold(true);
  158.             break;
  159.          case STRIKETHROUGH:
  160.             style.setStrikethrough(true);
  161.             break;
  162.          case UNDERLINE:
  163.             style.setUnderline(true);
  164.             break;
  165.          case ITALIC:
  166.             style.setItalic(true);
  167.          }
  168.       }
  169.  
  170.       return this;
  171.    }
  172.  
  173.    static Text copyWithoutChildren(Text text) {
  174.       Text text2 = text.copy();
  175.       text2.setStyle(text.getStyle().copy());
  176.       return text2;
  177.    }
  178.  
  179.    public static class Serializer implements JsonDeserializer<Text>, JsonSerializer<Text> {
  180.       private static final Gson GSON = (Gson)Util.make(() -> {
  181.          GsonBuilder gsonBuilder = new GsonBuilder();
  182.          gsonBuilder.disableHtmlEscaping();
  183.          gsonBuilder.registerTypeHierarchyAdapter(Text.class, new Text.Serializer());
  184.          gsonBuilder.registerTypeHierarchyAdapter(Style.class, new Style.Serializer());
  185.          gsonBuilder.registerTypeAdapterFactory(new LowercaseEnumTypeAdapterFactory());
  186.          return gsonBuilder.create();
  187.       });
  188.       private static final Field JSON_READER_POS = (Field)Util.make(() -> {
  189.          try {
  190.             new JsonReader(new StringReader(""));
  191.             Field field = JsonReader.class.getDeclaredField("pos");
  192.             field.setAccessible(true);
  193.             return field;
  194.          } catch (NoSuchFieldException var1) {
  195.             throw new IllegalStateException("Couldn't get field 'pos' for JsonReader", var1);
  196.          }
  197.       });
  198.       private static final Field JSON_READER_LINE_START = (Field)Util.make(() -> {
  199.          try {
  200.             new JsonReader(new StringReader(""));
  201.             Field field = JsonReader.class.getDeclaredField("lineStart");
  202.             field.setAccessible(true);
  203.             return field;
  204.          } catch (NoSuchFieldException var1) {
  205.             throw new IllegalStateException("Couldn't get field 'lineStart' for JsonReader", var1);
  206.          }
  207.       });
  208.  
  209.       public Text deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
  210.          if (jsonElement.isJsonPrimitive()) {
  211.             return new LiteralText(jsonElement.getAsString());
  212.          } else if (!jsonElement.isJsonObject()) {
  213.             if (jsonElement.isJsonArray()) {
  214.                JsonArray jsonArray3 = jsonElement.getAsJsonArray();
  215.                Text text13 = null;
  216.                Iterator var14 = jsonArray3.iterator();
  217.  
  218.                while(var14.hasNext()) {
  219.                   JsonElement jsonElement2 = (JsonElement)var14.next();
  220.                   Text text14 = this.deserialize(jsonElement2, jsonElement2.getClass(), jsonDeserializationContext);
  221.                   if (text13 == null) {
  222.                      text13 = text14;
  223.                   } else {
  224.                      text13.append(text14);
  225.                   }
  226.                }
  227.  
  228.                return text13;
  229.             } else {
  230.                throw new JsonParseException("Don't know how to turn " + jsonElement + " into a Component");
  231.             }
  232.          } else {
  233.             JsonObject jsonObject = jsonElement.getAsJsonObject();
  234.             Object text4;
  235.             if (jsonObject.has("text")) {
  236.                text4 = new LiteralText(JsonHelper.getString(jsonObject, "text"));
  237.             } else {
  238.                String string2;
  239.                if (jsonObject.has("translate")) {
  240.                   string2 = JsonHelper.getString(jsonObject, "translate");
  241.                   if (jsonObject.has("with")) {
  242.                      JsonArray jsonArray = JsonHelper.getArray(jsonObject, "with");
  243.                      Object[] objects = new Object[jsonArray.size()];
  244.  
  245.                      for(int i = 0; i < objects.length; ++i) {
  246.                         objects[i] = this.deserialize(jsonArray.get(i), type, jsonDeserializationContext);
  247.                         if (objects[i] instanceof LiteralText) {
  248.                            LiteralText literalText = (LiteralText)objects[i];
  249.                            if (literalText.getStyle().isEmpty() && literalText.getSiblings().isEmpty()) {
  250.                               objects[i] = literalText.getRawString();
  251.                            }
  252.                         }
  253.                      }
  254.  
  255.                      text4 = new TranslatableText(string2, objects);
  256.                   } else {
  257.                      text4 = new TranslatableText(string2, new Object[0]);
  258.                   }
  259.                } else if (jsonObject.has("score")) {
  260.                   JsonObject jsonObject2 = JsonHelper.getObject(jsonObject, "score");
  261.                   if (!jsonObject2.has("name") || !jsonObject2.has("objective")) {
  262.                      throw new JsonParseException("A score component needs a least a name and an objective");
  263.                   }
  264.  
  265.                   text4 = new ScoreText(JsonHelper.getString(jsonObject2, "name"), JsonHelper.getString(jsonObject2, "objective"));
  266.                   if (jsonObject2.has("value")) {
  267.                      ((ScoreText)text4).setScore(JsonHelper.getString(jsonObject2, "value"));
  268.                   }
  269.                } else if (jsonObject.has("selector")) {
  270.                   text4 = new SelectorText(JsonHelper.getString(jsonObject, "selector"));
  271.                } else if (jsonObject.has("keybind")) {
  272.                   text4 = new KeybindText(JsonHelper.getString(jsonObject, "keybind"));
  273.                } else {
  274.                   if (!jsonObject.has("nbt")) {
  275.                      throw new JsonParseException("Don't know how to turn " + jsonElement + " into a Component");
  276.                   }
  277.  
  278.                   string2 = JsonHelper.getString(jsonObject, "nbt");
  279.                   boolean bl = JsonHelper.getBoolean(jsonObject, "interpret", false);
  280.                   if (jsonObject.has("block")) {
  281.                      text4 = new NbtText.BlockNbtText(string2, bl, JsonHelper.getString(jsonObject, "block"));
  282.                   } else if (jsonObject.has("entity")) {
  283.                      text4 = new NbtText.EntityNbtText(string2, bl, JsonHelper.getString(jsonObject, "entity"));
  284.                   } else {
  285.                      if (!jsonObject.has("storage")) {
  286.                         throw new JsonParseException("Don't know how to turn " + jsonElement + " into a Component");
  287.                      }
  288.  
  289.                      text4 = new NbtText.StorageNbtText(string2, bl, new Identifier(JsonHelper.getString(jsonObject, "storage")));
  290.                   }
  291.                }
  292.             }
  293.  
  294.             if (jsonObject.has("extra")) {
  295.                JsonArray jsonArray2 = JsonHelper.getArray(jsonObject, "extra");
  296.                if (jsonArray2.size() <= 0) {
  297.                   throw new JsonParseException("Unexpected empty array of components");
  298.                }
  299.  
  300.                for(int j = 0; j < jsonArray2.size(); ++j) {
  301.                   ((Text)text4).append(this.deserialize(jsonArray2.get(j), type, jsonDeserializationContext));
  302.                }
  303.             }
  304.  
  305.             ((Text)text4).setStyle((Style)jsonDeserializationContext.deserialize(jsonElement, Style.class));
  306.             return (Text)text4;
  307.          }
  308.       }
  309.  
  310.       private void addStyle(Style style, JsonObject json, JsonSerializationContext context) {
  311.          JsonElement jsonElement = context.serialize(style);
  312.          if (jsonElement.isJsonObject()) {
  313.             JsonObject jsonObject = (JsonObject)jsonElement;
  314.             Iterator var6 = jsonObject.entrySet().iterator();
  315.  
  316.             while(var6.hasNext()) {
  317.                Entry<String, JsonElement> entry = (Entry)var6.next();
  318.                json.add((String)entry.getKey(), (JsonElement)entry.getValue());
  319.             }
  320.          }
  321.  
  322.       }
  323.  
  324.       public JsonElement serialize(Text text, Type type, JsonSerializationContext jsonSerializationContext) {
  325.          JsonObject jsonObject = new JsonObject();
  326.          if (!text.getStyle().isEmpty()) {
  327.             this.addStyle(text.getStyle(), jsonObject, jsonSerializationContext);
  328.          }
  329.  
  330.          if (!text.getSiblings().isEmpty()) {
  331.             JsonArray jsonArray = new JsonArray();
  332.             Iterator var6 = text.getSiblings().iterator();
  333.  
  334.             while(var6.hasNext()) {
  335.                Text text2 = (Text)var6.next();
  336.                jsonArray.add(this.serialize((Text)text2, text2.getClass(), jsonSerializationContext));
  337.             }
  338.  
  339.             jsonObject.add("extra", jsonArray);
  340.          }
  341.  
  342.          if (text instanceof LiteralText) {
  343.             jsonObject.addProperty("text", ((LiteralText)text).getRawString());
  344.          } else if (text instanceof TranslatableText) {
  345.             TranslatableText translatableText = (TranslatableText)text;
  346.             jsonObject.addProperty("translate", translatableText.getKey());
  347.             if (translatableText.getArgs() != null && translatableText.getArgs().length > 0) {
  348.                JsonArray jsonArray2 = new JsonArray();
  349.                Object[] var19 = translatableText.getArgs();
  350.                int var8 = var19.length;
  351.  
  352.                for(int var9 = 0; var9 < var8; ++var9) {
  353.                   Object object = var19[var9];
  354.                   if (object instanceof Text) {
  355.                      jsonArray2.add(this.serialize((Text)((Text)object), object.getClass(), jsonSerializationContext));
  356.                   } else {
  357.                      jsonArray2.add(new JsonPrimitive(String.valueOf(object)));
  358.                   }
  359.                }
  360.  
  361.                jsonObject.add("with", jsonArray2);
  362.             }
  363.          } else if (text instanceof ScoreText) {
  364.             ScoreText scoreText = (ScoreText)text;
  365.             JsonObject jsonObject2 = new JsonObject();
  366.             jsonObject2.addProperty("name", scoreText.getName());
  367.             jsonObject2.addProperty("objective", scoreText.getObjective());
  368.             jsonObject2.addProperty("value", scoreText.asString());
  369.             jsonObject.add("score", jsonObject2);
  370.          } else if (text instanceof SelectorText) {
  371.             SelectorText selectorText = (SelectorText)text;
  372.             jsonObject.addProperty("selector", selectorText.getPattern());
  373.          } else if (text instanceof KeybindText) {
  374.             KeybindText keybindText = (KeybindText)text;
  375.             jsonObject.addProperty("keybind", keybindText.getKey());
  376.          } else {
  377.             if (!(text instanceof NbtText)) {
  378.                throw new IllegalArgumentException("Don't know how to serialize " + text + " as a Component");
  379.             }
  380.  
  381.             NbtText nbtText = (NbtText)text;
  382.             jsonObject.addProperty("nbt", nbtText.getPath());
  383.             jsonObject.addProperty("interpret", nbtText.shouldInterpret());
  384.             if (text instanceof NbtText.BlockNbtText) {
  385.                NbtText.BlockNbtText blockNbtText = (NbtText.BlockNbtText)text;
  386.                jsonObject.addProperty("block", blockNbtText.getPos());
  387.             } else if (text instanceof NbtText.EntityNbtText) {
  388.                NbtText.EntityNbtText entityNbtText = (NbtText.EntityNbtText)text;
  389.                jsonObject.addProperty("entity", entityNbtText.getSelector());
  390.             } else {
  391.                if (!(text instanceof NbtText.StorageNbtText)) {
  392.                   throw new IllegalArgumentException("Don't know how to serialize " + text + " as a Component");
  393.                }
  394.  
  395.                NbtText.StorageNbtText storageNbtText = (NbtText.StorageNbtText)text;
  396.                jsonObject.addProperty("storage", storageNbtText.method_23728().toString());
  397.             }
  398.          }
  399.  
  400.          return jsonObject;
  401.       }
  402.  
  403.       public static String toJson(Text text) {
  404.          return GSON.toJson(text);
  405.       }
  406.  
  407.       public static JsonElement toJsonTree(Text text) {
  408.          return GSON.toJsonTree(text);
  409.       }
  410.  
  411.       @Nullable
  412.       public static Text fromJson(String json) {
  413.          return (Text)JsonHelper.deserialize(GSON, json, Text.class, false);
  414.       }
  415.  
  416.       @Nullable
  417.       public static Text fromJson(JsonElement json) {
  418.          return (Text)GSON.fromJson(json, Text.class);
  419.       }
  420.  
  421.       @Nullable
  422.       public static Text fromLenientJson(String json) {
  423.          return (Text)JsonHelper.deserialize(GSON, json, Text.class, true);
  424.       }
  425.  
  426.       public static Text fromJson(com.mojang.brigadier.StringReader reader) {
  427.          try {
  428.             JsonReader jsonReader = new JsonReader(new StringReader(reader.getRemaining()));
  429.             jsonReader.setLenient(false);
  430.             Text text = (Text)GSON.getAdapter(Text.class).read(jsonReader);
  431.             reader.setCursor(reader.getCursor() + getPosition(jsonReader));
  432.             return text;
  433.          } catch (StackOverflowError | IOException var3) {
  434.             throw new JsonParseException(var3);
  435.          }
  436.       }
  437.  
  438.       private static int getPosition(JsonReader reader) {
  439.          try {
  440.             return JSON_READER_POS.getInt(reader) - JSON_READER_LINE_START.getInt(reader) + 1;
  441.          } catch (IllegalAccessException var2) {
  442.             throw new IllegalStateException("Couldn't read position of JsonReader", var2);
  443.          }
  444.       }
  445.    }
  446. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement