1
0
mirror of https://e.coding.net/circlecloud/YumCore.git synced 2024-11-22 01:48:50 +00:00

fix: 修复TellRaw发包发送

Signed-off-by: 502647092 <admin@yumc.pw>
This commit is contained in:
502647092 2017-07-24 23:39:29 +08:00
parent ef8883626f
commit e949e4677a
2 changed files with 23 additions and 13 deletions

View File

@ -31,6 +31,7 @@ public class C {
private static Class<?> nmsChatSerializer; private static Class<?> nmsChatSerializer;
private static Class<?> nmsIChatBaseComponent; private static Class<?> nmsIChatBaseComponent;
private static Class<?> packetType; private static Class<?> packetType;
private static Class<?> nmsChatMessageTypeClass;
private static Constructor<?> packetTypeConstructor; private static Constructor<?> packetTypeConstructor;
@ -43,6 +44,8 @@ public class C {
private static Field playerConnection; private static Field playerConnection;
private static Method sendPacket; private static Method sendPacket;
private static Object[] ChatMessageTypes;
public static boolean init; public static boolean init;
static { static {
try { try {
@ -52,7 +55,13 @@ public class C {
chatSerializer = nmsChatSerializer.getMethod("a", String.class); chatSerializer = nmsChatSerializer.getMethod("a", String.class);
nmsIChatBaseComponent = Class.forName(a("IChatBaseComponent")); nmsIChatBaseComponent = Class.forName(a("IChatBaseComponent"));
packetType = Class.forName(a("PacketPlayOutChat")); packetType = Class.forName(a("PacketPlayOutChat"));
packetTypeConstructor = packetType.getConstructor(nmsIChatBaseComponent, newversion ? int.class : byte.class); try {
nmsChatMessageTypeClass = Class.forName(a("ChatMessageType"));
ChatMessageTypes = nmsChatMessageTypeClass.getEnumConstants();
packetTypeConstructor = packetType.getConstructor(nmsIChatBaseComponent, nmsChatMessageTypeClass);
} catch (ClassNotFoundException ex) {
packetTypeConstructor = packetType.getConstructor(nmsIChatBaseComponent, newversion ? int.class : byte.class);
}
Class<?> typeCraftPlayer = Class.forName(b("entity.CraftPlayer")); Class<?> typeCraftPlayer = Class.forName(b("entity.CraftPlayer"));
Class<?> typeNMSPlayer = Class.forName(a("EntityPlayer")); Class<?> typeNMSPlayer = Class.forName(a("EntityPlayer"));
Class<?> typePlayerConnection = Class.forName(a("PlayerConnection")); Class<?> typePlayerConnection = Class.forName(a("PlayerConnection"));
@ -103,7 +112,11 @@ public class C {
Object serialized = chatSerializer.invoke(null, json); Object serialized = chatSerializer.invoke(null, json);
Object player = getHandle.invoke(receivingPacket); Object player = getHandle.invoke(receivingPacket);
Object connection = playerConnection.get(player); Object connection = playerConnection.get(player);
sendPacket.invoke(connection, packetTypeConstructor.newInstance(serialized, newversion ? type : (byte) type)); if (nmsChatMessageTypeClass != null) {
sendPacket.invoke(connection, packetTypeConstructor.newInstance(serialized, ChatMessageTypes[type]));
} else {
sendPacket.invoke(connection, packetTypeConstructor.newInstance(serialized, newversion ? type : (byte) type));
}
} catch (Exception ex) { } catch (Exception ex) {
Log.d("Json发包错误 " + version, ex); Log.d("Json发包错误 " + version, ex);
} }
@ -305,12 +318,14 @@ public class C {
private static Class<?> packetTitle; private static Class<?> packetTitle;
private static Constructor<?> packetTitleSendConstructor; private static Constructor<?> packetTitleSendConstructor;
private static Constructor<?> packetTitleSetTimeConstructor; private static Constructor<?> packetTitleSetTimeConstructor;
private static Object[] actions;
static { static {
try { try {
packetActions = Class.forName(a(newversion ? "PacketPlayOutTitle$EnumTitleAction" : "EnumTitleAction")); packetActions = Class.forName(a(newversion ? "PacketPlayOutTitle$EnumTitleAction" : "EnumTitleAction"));
packetTitle = Class.forName(a("PacketPlayOutTitle")); packetTitle = Class.forName(a("PacketPlayOutTitle"));
packetTitleSendConstructor = packetTitle.getConstructor(packetActions, nmsIChatBaseComponent); packetTitleSendConstructor = packetTitle.getConstructor(packetActions, nmsIChatBaseComponent);
packetTitleSetTimeConstructor = packetTitle.getConstructor(packetActions, nmsIChatBaseComponent, Integer.TYPE, Integer.TYPE, Integer.TYPE); packetTitleSetTimeConstructor = packetTitle.getConstructor(packetActions, nmsIChatBaseComponent, Integer.TYPE, Integer.TYPE, Integer.TYPE);
actions = packetActions.getEnumConstants();
} catch (Exception ignore) { } catch (Exception ignore) {
Log.w("Title 兼容性工具初始化失败 可能造成部分功能不可用!"); Log.w("Title 兼容性工具初始化失败 可能造成部分功能不可用!");
} }
@ -383,7 +398,6 @@ public class C {
// Send timings first // Send timings first
Object player = getHandle.invoke(recoverPlayer); Object player = getHandle.invoke(recoverPlayer);
Object connection = playerConnection.get(player); Object connection = playerConnection.get(player);
Object[] actions = packetActions.getEnumConstants();
Object packet = packetTitleSendConstructor.newInstance(actions[4], null); Object packet = packetTitleSendConstructor.newInstance(actions[4], null);
sendPacket.invoke(connection, packet); sendPacket.invoke(connection, packet);
} }
@ -428,7 +442,6 @@ public class C {
// Send timings first // Send timings first
Object player = getHandle.invoke(receivingPacket); Object player = getHandle.invoke(receivingPacket);
Object connection = playerConnection.get(player); Object connection = playerConnection.get(player);
Object[] actions = packetActions.getEnumConstants();
Object packet; Object packet;
// Send if set // Send if set
if ((fadeInTime != -1) && (fadeOutTime != -1) && (stayTime != -1)) { if ((fadeInTime != -1) && (fadeOutTime != -1) && (stayTime != -1)) {

View File

@ -1,6 +1,5 @@
package pw.yumc.YumCore.tellraw; package pw.yumc.YumCore.tellraw;
import java.lang.reflect.Field;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
@ -26,13 +25,7 @@ public class Tellraw implements Cloneable {
static { static {
if (Bukkit.getVersion().contains("Paper") || Bukkit.getVersion().contains("Torch")) { if (Bukkit.getVersion().contains("Paper") || Bukkit.getVersion().contains("Torch")) {
try { if (!C.init) {
// Paper 根据 org.spigotmc.AsyncCatcher.enabled 判断是否拦截异步命令
Class AsyncCatcherClass = Class.forName("org.spigotmc.AsyncCatcher");
Field enabledField = AsyncCatcherClass.getDeclaredField("enabled");
enabledField.setAccessible(true);
enabledField.set(null, false);
} catch (ClassNotFoundException | NoSuchFieldException | IllegalAccessException ex) {
Log.console("§c========== §4警 告 §c=========="); Log.console("§c========== §4警 告 §c==========");
Log.console("§a 当前服务器为 §6Paper §a或 §6Torch "); Log.console("§a 当前服务器为 §6Paper §a或 §6Torch ");
Log.console("§c 异步命令会刷报错 §b不影响使用"); Log.console("§c 异步命令会刷报错 §b不影响使用");
@ -182,7 +175,11 @@ public class Tellraw implements Cloneable {
public void send(final CommandSender sender) { public void send(final CommandSender sender) {
final String json = toJsonString(); final String json = toJsonString();
if (sender instanceof Player && json.getBytes().length < 32000) { if (sender instanceof Player && json.getBytes().length < 32000) {
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "tellraw " + sender.getName() + " " + json); if (C.init) {
C.sendJson((Player) sender, json, 0);
} else {
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "tellraw " + sender.getName() + " " + json);
}
} else { } else {
sender.sendMessage(toOldMessageFormat()); sender.sendMessage(toOldMessageFormat());
} }