|
| 1 | +/* |
| 2 | + * WorldEdit, a Minecraft world manipulation toolkit |
| 3 | + * Copyright (C) sk89q <http://www.sk89q.com> |
| 4 | + * Copyright (C) WorldEdit team and contributors |
| 5 | + * |
| 6 | + * This program is free software: you can redistribute it and/or modify |
| 7 | + * it under the terms of the GNU General Public License as published by |
| 8 | + * the Free Software Foundation, either version 3 of the License, or |
| 9 | + * (at your option) any later version. |
| 10 | + * |
| 11 | + * This program is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + * GNU General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU General Public License |
| 17 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 18 | + */ |
| 19 | + |
| 20 | +package com.sk89q.worldedit.bukkit.adapter.ext.fawe.v1_21_6; |
| 21 | + |
| 22 | +import com.google.gson.Gson; |
| 23 | +import com.google.gson.GsonBuilder; |
| 24 | +import com.google.gson.JsonElement; |
| 25 | +import com.google.gson.JsonParseException; |
| 26 | +import com.google.gson.JsonParser; |
| 27 | +import com.google.gson.Strictness; |
| 28 | +import com.google.gson.stream.JsonReader; |
| 29 | +import com.mojang.serialization.JsonOps; |
| 30 | +import net.minecraft.core.HolderLookup; |
| 31 | +import net.minecraft.network.chat.Component; |
| 32 | +import net.minecraft.network.chat.ComponentSerialization; |
| 33 | +import net.minecraft.network.chat.MutableComponent; |
| 34 | + |
| 35 | +import java.io.StringReader; |
| 36 | +import javax.annotation.Nullable; |
| 37 | + |
| 38 | +public class ComponentConverter { |
| 39 | + |
| 40 | + public static class Serializer { |
| 41 | + private static final Gson GSON = (new GsonBuilder()).disableHtmlEscaping().create(); |
| 42 | + |
| 43 | + private Serializer() { |
| 44 | + } |
| 45 | + |
| 46 | + static MutableComponent deserialize(JsonElement json, HolderLookup.Provider registries) { |
| 47 | + return (MutableComponent) ComponentSerialization.CODEC.parse(registries.createSerializationContext(JsonOps.INSTANCE), json).getOrThrow(JsonParseException::new); |
| 48 | + } |
| 49 | + |
| 50 | + static JsonElement serialize(Component text, HolderLookup.Provider registries) { |
| 51 | + return ComponentSerialization.CODEC.encodeStart(registries.createSerializationContext(JsonOps.INSTANCE), text).getOrThrow(JsonParseException::new); |
| 52 | + } |
| 53 | + |
| 54 | + public static String toJson(Component text, HolderLookup.Provider registries) { |
| 55 | + return GSON.toJson(serialize(text, registries)); |
| 56 | + } |
| 57 | + |
| 58 | + @Nullable |
| 59 | + public static MutableComponent fromJson(String json, HolderLookup.Provider registries) { |
| 60 | + JsonElement jsonelement = JsonParser.parseString(json); |
| 61 | + return jsonelement == null ? null : deserialize(jsonelement, registries); |
| 62 | + } |
| 63 | + |
| 64 | + @Nullable |
| 65 | + public static MutableComponent fromJson(@Nullable JsonElement json, HolderLookup.Provider registries) { |
| 66 | + return json == null ? null : deserialize(json, registries); |
| 67 | + } |
| 68 | + |
| 69 | + @Nullable |
| 70 | + public static MutableComponent fromJsonLenient(String json, HolderLookup.Provider registries) { |
| 71 | + JsonReader jsonreader = new JsonReader(new StringReader(json)); |
| 72 | + jsonreader.setStrictness(Strictness.LENIENT); |
| 73 | + JsonElement jsonelement = JsonParser.parseReader(jsonreader); |
| 74 | + return jsonelement == null ? null : deserialize(jsonelement, registries); |
| 75 | + } |
| 76 | + } |
| 77 | +} |
0 commit comments