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

View File

@ -1,6 +1,5 @@
package pw.yumc.YumCore.tellraw;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@ -26,13 +25,7 @@ public class Tellraw implements Cloneable {
static {
if (Bukkit.getVersion().contains("Paper") || Bukkit.getVersion().contains("Torch")) {
try {
// 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) {
if (!C.init) {
Log.console("§c========== §4警 告 §c==========");
Log.console("§a 当前服务器为 §6Paper §a或 §6Torch ");
Log.console("§c 异步命令会刷报错 §b不影响使用");
@ -182,7 +175,11 @@ public class Tellraw implements Cloneable {
public void send(final CommandSender sender) {
final String json = toJsonString();
if (sender instanceof Player && json.getBytes().length < 32000) {
if (C.init) {
C.sendJson((Player) sender, json, 0);
} else {
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "tellraw " + sender.getName() + " " + json);
}
} else {
sender.sendMessage(toOldMessageFormat());
}