|
| 1 | +package net.azisaba.loreeditor.v1_21_11.chat; |
| 2 | + |
| 3 | +import com.google.gson.Gson; |
| 4 | +import com.mojang.serialization.JsonOps; |
| 5 | +import net.azisaba.loreeditor.common.chat.ChatModifier; |
| 6 | +import net.azisaba.loreeditor.common.chat.Component; |
| 7 | +import net.azisaba.loreeditor.common.util.Reflected; |
| 8 | +import net.minecraft.network.chat.ComponentSerialization; |
| 9 | +import net.minecraft.network.chat.MutableComponent; |
| 10 | +import net.minecraft.network.chat.Style; |
| 11 | +import org.jetbrains.annotations.Contract; |
| 12 | +import org.jetbrains.annotations.NotNull; |
| 13 | +import org.jetbrains.annotations.Nullable; |
| 14 | + |
| 15 | +import java.util.List; |
| 16 | +import java.util.Objects; |
| 17 | +import java.util.function.UnaryOperator; |
| 18 | + |
| 19 | +@Reflected |
| 20 | +public record ComponentImpl(MutableComponent handle) implements Component { |
| 21 | + private static final Gson GSON = new Gson(); |
| 22 | + |
| 23 | + public ComponentImpl(@Nullable net.minecraft.network.chat.Component handle) { |
| 24 | + this(handle == null ? null : handle.copy()); |
| 25 | + } |
| 26 | + |
| 27 | + @Contract(value = "_ -> new", pure = true) |
| 28 | + @Reflected |
| 29 | + public static @NotNull ComponentImpl getInstance(@Nullable Object component) { |
| 30 | + return new ComponentImpl((net.minecraft.network.chat.Component) component); |
| 31 | + } |
| 32 | + |
| 33 | + @Override |
| 34 | + public @NotNull MutableComponent handle() { |
| 35 | + return Objects.requireNonNull(handle, "cannot reference handle in static context"); |
| 36 | + } |
| 37 | + |
| 38 | + public static MutableComponent deserializeFromJson(@NotNull String input) { |
| 39 | + return ComponentSerialization.CODEC.decode(JsonOps.INSTANCE, GSON.toJsonTree(input)).getOrThrow().getFirst().copy(); |
| 40 | + } |
| 41 | + |
| 42 | + public static String serializeToJson(@NotNull net.minecraft.network.chat.Component component) { |
| 43 | + return GSON.toJson(ComponentSerialization.CODEC.encodeStart(JsonOps.INSTANCE, component).getOrThrow()); |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public @Nullable Component deserialize(@NotNull String input) { |
| 48 | + return getInstance(deserializeFromJson(input)); |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public @NotNull String serialize(@NotNull Component component) { |
| 53 | + return serializeToJson(((ComponentImpl) component).handle()); |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public @NotNull List<?> getSiblings() { |
| 58 | + return handle().getSiblings(); |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public void addSiblingText(@NotNull String text) { |
| 63 | + handle().append(deserializeFromJson(text)); |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public @NotNull Component modifyStyle(@NotNull UnaryOperator<ChatModifier> action) { |
| 68 | + ChatModifier cm = new ChatModifierImpl(handle().getStyle()); |
| 69 | + Style newChatModifier = ((ChatModifierImpl) action.apply(cm)).handle(); |
| 70 | + return getInstance(handle().setStyle(newChatModifier)); |
| 71 | + } |
| 72 | +} |
0 commit comments