9
0
mirror of https://github.com/Xiao-MoMi/craft-engine.git synced 2025-12-28 19:39:11 +00:00

完善componentutils

This commit is contained in:
XiaoMoMi
2025-04-05 16:31:32 +08:00
parent e9ebfe1e35
commit 51dad2f602
7 changed files with 68 additions and 13 deletions

View File

@@ -3,6 +3,7 @@ package net.momirealms.craftengine.bukkit.font;
import io.papermc.paper.event.player.AsyncChatCommandDecorateEvent;
import io.papermc.paper.event.player.AsyncChatDecorateEvent;
import net.momirealms.craftengine.bukkit.plugin.BukkitCraftEngine;
import net.momirealms.craftengine.bukkit.util.ComponentUtils;
import net.momirealms.craftengine.bukkit.util.Reflections;
import net.momirealms.craftengine.core.font.AbstractFontManager;
import net.momirealms.craftengine.core.font.FontManager;
@@ -21,17 +22,10 @@ import java.util.function.Consumer;
public class BukkitFontManager extends AbstractFontManager implements Listener {
private final BukkitCraftEngine plugin;
private final Object serializer;
public BukkitFontManager(BukkitCraftEngine plugin) {
super(plugin);
this.plugin = plugin;
try {
Object builder = Reflections.method$GsonComponentSerializer$builder.invoke(null);
this.serializer = Reflections.method$GsonComponentSerializer$Builder$build.invoke(builder);
} catch (IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e);
}
}
@Override
@@ -79,16 +73,15 @@ public class BukkitFontManager extends AbstractFontManager implements Listener {
}
try {
Object originalMessage = Reflections.field$AsyncChatDecorateEvent$originalMessage.get(event);
String jsonMessage = (String) Reflections.method$ComponentSerializer$serialize.invoke(serializer, originalMessage);
runIfContainsIllegalCharacter(jsonMessage, (json) -> {
runIfContainsIllegalCharacter(ComponentUtils.paperAdventureToJson(originalMessage), (json) -> {
Object component = ComponentUtils.jsonToPaperAdventure(json);
try {
Object component = Reflections.method$ComponentSerializer$deserialize.invoke(serializer, json);
Reflections.method$AsyncChatDecorateEvent$result.invoke(event, component);
} catch (ReflectiveOperationException e) {
throw new RuntimeException(e);
}
});
} catch (IllegalAccessException | InvocationTargetException e) {
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}

View File

@@ -1,6 +1,8 @@
package net.momirealms.craftengine.bukkit.item;
import com.saicone.rtag.RtagItem;
import net.kyori.adventure.Adventure;
import net.kyori.adventure.text.NBTComponent;
import net.momirealms.craftengine.core.item.ItemWrapper;
import org.bukkit.inventory.ItemStack;

View File

@@ -11,7 +11,6 @@ import org.bukkit.inventory.ItemStack;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
@SuppressWarnings("UnstableApiUsage")

View File

@@ -3,6 +3,7 @@ package net.momirealms.craftengine.bukkit.util;
import com.google.gson.JsonElement;
import net.kyori.adventure.text.Component;
import net.momirealms.craftengine.bukkit.nms.FastNMS;
import net.momirealms.craftengine.core.plugin.CraftEngine;
import net.momirealms.craftengine.core.util.AdventureHelper;
public class ComponentUtils {
@@ -24,4 +25,21 @@ public class ComponentUtils {
public static String minecraftToJson(Object component) {
return FastNMS.INSTANCE.method$Component$Serializer$toJson(component);
}
public static String paperAdventureToJson(Object component) {
try {
return (String) Reflections.method$ComponentSerializer$serialize.invoke(Reflections.instance$GsonComponentSerializer, component);
} catch (ReflectiveOperationException e) {
CraftEngine.instance().logger().warn("Failed to serialize paper adventure component " + component, e);
return AdventureHelper.EMPTY_COMPONENT;
}
}
public static Object jsonToPaperAdventure(String json) {
try {
return Reflections.method$ComponentSerializer$deserialize.invoke(Reflections.instance$GsonComponentSerializer, json);
} catch (ReflectiveOperationException e) {
throw new RuntimeException("Failed to deserialize paper component from json", e);
}
}
}

View File

@@ -6146,4 +6146,15 @@ public class Reflections {
public static final Method method$PacketReport$serializePackets = Optional.ofNullable(clazz$PacketReport)
.map(it -> ReflectionUtils.getDeclaredMethod(it, JsonElement.class))
.orElse(null);
public static final Object instance$GsonComponentSerializer;
static {
try {
Object builder = Reflections.method$GsonComponentSerializer$builder.invoke(null);
instance$GsonComponentSerializer = Reflections.method$GsonComponentSerializer$Builder$build.invoke(builder);
} catch (ReflectiveOperationException e) {
throw new RuntimeException(e);
}
}
}