9
0
mirror of https://github.com/Xiao-MoMi/Custom-Nameplates.git synced 2026-01-06 15:42:00 +00:00
This commit is contained in:
XiaoMoMi
2025-06-30 02:05:35 +08:00
parent 3cbf4698ed
commit 2ebb781721
20 changed files with 66 additions and 197 deletions

View File

@@ -297,11 +297,6 @@ public class BukkitNetworkManager implements PacketSender, PipelineInjector {
plugin.getPlatform().onPacketSend(player, event);
if (event.cancelled()) return;
super.write(context, packet, channelPromise);
channelPromise.addListener((p) -> {
for (Runnable task : event.getDelayedTasks()) {
task.run();
}
});
} catch (Throwable e) {
plugin.getPluginLogger().severe("An error occurred when reading packets", e);
super.write(context, packet, channelPromise);

View File

@@ -482,35 +482,19 @@ public class BukkitPlatform implements Platform {
@Override
public Object jsonToMinecraftComponent(String json) {
if (VersionHelper.isVersionNewerThan1_20_5()) {
try {
return Reflections.method$Component$Serializer$fromJson.invoke(null, json, Reflections.instance$MinecraftRegistry);
} catch (ReflectiveOperationException e) {
throw new RuntimeException(e);
}
} else {
try {
return Reflections.method$CraftChatMessage$fromJSON.invoke(null, json);
} catch (ReflectiveOperationException e) {
throw new RuntimeException(e);
}
try {
return Reflections.method$CraftChatMessage$fromJSON.invoke(null, json);
} catch (ReflectiveOperationException e) {
throw new RuntimeException(e);
}
}
@Override
public String minecraftComponentToJson(Object component) {
if (VersionHelper.isVersionNewerThan1_20_5()) {
try {
return (String) Reflections.method$Component$Serializer$toJson.invoke(null, component, Reflections.instance$MinecraftRegistry);
} catch (ReflectiveOperationException e) {
throw new RuntimeException(e);
}
} else {
try {
return (String) Reflections.method$CraftChatMessage$toJSON.invoke(null, component);
} catch (ReflectiveOperationException e) {
throw new RuntimeException(e);
}
try {
return (String) Reflections.method$CraftChatMessage$toJSON.invoke(null, component);
} catch (ReflectiveOperationException e) {
throw new RuntimeException(e);
}
}

View File

@@ -28,6 +28,8 @@ package net.momirealms.customnameplates.bukkit;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
import net.kyori.adventure.text.Component;
import net.momirealms.customnameplates.api.helper.AdventureHelper;
import net.momirealms.customnameplates.bukkit.util.Reflections;
import net.momirealms.customnameplates.common.sender.Sender;
import net.momirealms.customnameplates.common.sender.SenderFactory;
import net.momirealms.customnameplates.common.util.Tristate;
@@ -69,8 +71,14 @@ public class BukkitSenderFactory extends SenderFactory<BukkitCustomNameplates, C
@Override
protected void sendMessage(CommandSender sender, Component message) {
// we can safely send async for players and the console - otherwise, send it sync
if (sender instanceof Player || sender instanceof ConsoleCommandSender || sender instanceof RemoteConsoleCommandSender) {
if (sender instanceof Player player) {
try {
Object packet = Reflections.constructor$ClientboundSystemChatPacket.newInstance(getPlugin().getPlatform().jsonToMinecraftComponent(AdventureHelper.gson().serialize(message)), false);
getPlugin().getPacketSender().sendPacket(getPlugin().getPlayer(player.getUniqueId()), packet);
} catch (ReflectiveOperationException e) {
getPlugin().getPluginLogger().warn("Failed to send message to player " + sender.getName(), e);
}
} else if (sender instanceof ConsoleCommandSender || sender instanceof RemoteConsoleCommandSender) {
getAudience(sender).sendMessage(message);
} else {
getPlugin().getScheduler().executeSync(() -> getAudience(sender).sendMessage(message));

View File

@@ -124,6 +124,12 @@ public class Reflections {
clazz$ClientboundSystemChatPacket, String.class, 0
);
public static final Constructor<?> constructor$ClientboundSystemChatPacket = requireNonNull(
ReflectionUtils.getConstructor(
clazz$ClientboundSystemChatPacket, clazz$Component, boolean.class
)
);
public static final Class<?> clazz$CraftChatMessage = requireNonNull(
ReflectionUtils.getClazz(
BukkitReflectionUtils.assembleCBClass("util.CraftChatMessage")
@@ -317,30 +323,11 @@ public class Reflections {
}
}
public static final Class<?> clazz$Component$Serializer = requireNonNull(
ReflectionUtils.getClazz(
BukkitReflectionUtils.assembleMCClass("network.chat.Component$Serializer"),
BukkitReflectionUtils.assembleMCClass("network.chat.IChatBaseComponent$ChatSerializer")
)
);
public static final Class<?> clazz$HolderLookup$Provider = ReflectionUtils.getClazz(
BukkitReflectionUtils.assembleMCClass("core.HolderLookup$Provider"),
BukkitReflectionUtils.assembleMCClass(VersionHelper.isVersionNewerThan1_20_5() ? "core.HolderLookup$a" : "core.HolderLookup$b")
);
public static final Method method$Component$Serializer$fromJson = ReflectionUtils.getMethod(
clazz$Component$Serializer,
new String[] { "fromJson", "a" },
String.class, clazz$HolderLookup$Provider
);
public static final Method method$Component$Serializer$toJson = ReflectionUtils.getMethod(
clazz$Component$Serializer,
new String[] { "toJson", "a" },
clazz$Component, clazz$HolderLookup$Provider
);
public static final Class<?> clazz$ClientboundBundlePacket = requireNonNull(
ReflectionUtils.getClazz(
BukkitReflectionUtils.assembleMCClass("network.protocol.game.ClientboundBundlePacket")