Skip to content

Commit

Permalink
fix chat serialize
Browse files Browse the repository at this point in the history
  • Loading branch information
Faithcaio committed Nov 26, 2023
1 parent 340f495 commit 10fe337
Showing 1 changed file with 9 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.Redirect;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;
import org.spongepowered.common.adventure.AdventureTextComponent;
Expand Down Expand Up @@ -62,24 +62,24 @@ public abstract class Component_SerializerMixin {
}

// inject into the anonymous function to build a gson instance
@SuppressWarnings("UnresolvedMixinReference")
@Inject(method = "*()Lcom/google/gson/Gson;", at = @At(value = "INVOKE_ASSIGN", target = "com/google/gson/GsonBuilder.disableHtmlEscaping()Lcom/google/gson/GsonBuilder;", remap = false),
locals = LocalCapture.CAPTURE_FAILEXCEPTION, remap = false)
private static void impl$injectAdventureGson(final CallbackInfoReturnable<Gson> cir, final GsonBuilder gson) {
@Redirect(method = "<clinit>", at = @At(value = "INVOKE", target = "com/google/gson/GsonBuilder.disableHtmlEscaping()Lcom/google/gson/GsonBuilder;", remap = false), remap = false)
private static GsonBuilder impl$injectAdventureGson(final GsonBuilder gson) {
gson.disableHtmlEscaping();
GsonComponentSerializer.gson().populator().apply(gson);
return gson;
}

@Inject(method = "toJson", at = @At("HEAD"), cancellable = true)
private static void impl$redirectSerialization(final net.minecraft.network.chat.Component component, final CallbackInfoReturnable<String> cir) {
if (component instanceof AdventureTextComponent) {
cir.setReturnValue(GSON.toJson(((AdventureTextComponent) component).wrapped()));
if (component instanceof AdventureTextComponent atc) {
cir.setReturnValue(GSON.toJson(atc.wrapped()));
}
}

@Inject(method = "toJsonTree", at = @At("HEAD"), cancellable = true)
private static void impl$redirectTreeSerialization(final net.minecraft.network.chat.Component component, final CallbackInfoReturnable<JsonElement> cir) {
if (component instanceof AdventureTextComponent) {
cir.setReturnValue(GSON.toJsonTree(((AdventureTextComponent) component).wrapped()));
if (component instanceof AdventureTextComponent atc) {
cir.setReturnValue(GSON.toJsonTree(atc.wrapped()));
}
}

Expand Down

0 comments on commit 10fe337

Please sign in to comment.