feat: chat compatible catserver

Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
MiaoWoo 2020-02-14 04:02:18 +08:00
parent b6f3a31eca
commit 1eefb729d7

View File

@ -5,6 +5,11 @@ var packetTypeConstructor;
var nmsChatMessageTypeClass; var nmsChatMessageTypeClass;
var chatMessageTypes; var chatMessageTypes;
var RemapUtils;
var playerConnectionFieldName;
var sendPacketMethod;
var downgrade = false; var downgrade = false;
/** /**
* NMS版本 * NMS版本
@ -18,13 +23,37 @@ function nmsCls(name) {
return base.getClass(['net.minecraft.server', nmsVersion, name].join('.')) return base.getClass(['net.minecraft.server', nmsVersion, name].join('.'))
} }
function init() { function remapMethod(clazz: any, origin: string, test: string, params) {
nmsChatSerializerClass = nmsCls(nmsVersion.split("_")[1] > 7 ? "IChatBaseComponent$ChatSerializer" : "ChatSerializer");
try { try {
nmsChatSerializerMethod = nmsChatSerializerClass.getMethod('a', base.getClass('java.lang.String')) return clazz.getMethod(origin, params)
} catch (ex) { } catch (ex) {
nmsChatSerializerMethod = nmsChatSerializerClass.getMethod('func_150699_a', base.getClass('java.lang.String')) if (RemapUtils) {
return clazz.getMethod(RemapUtils.mapMethod(clazz, origin, params), params)
} else {
return clazz.getMethod(test, params)
} }
}
}
function remapFieldName(clazz: any, origin: string, test: string) {
try {
return clazz.getField(origin)
} catch (ex) {
if (RemapUtils) {
return clazz.getField(RemapUtils.mapFieldName(clazz, origin))
} else {
return clazz.getField(test)
}
}
}
function init() {
try {
RemapUtils = Java.type('catserver.server.remapper.RemapUtils');
} catch (ex) {
}
nmsChatSerializerClass = nmsCls(nmsVersion.split("_")[1] > 7 ? "IChatBaseComponent$ChatSerializer" : "ChatSerializer");
nmsChatSerializerMethod = remapMethod(nmsChatSerializerClass, 'a', 'func_150699_a', base.getClass('java.lang.String'))
var packetTypeClass = nmsCls("PacketPlayOutChat"); var packetTypeClass = nmsCls("PacketPlayOutChat");
Java.from(packetTypeClass.constructors).forEach(function(c) { Java.from(packetTypeClass.constructors).forEach(function(c) {
if (c.parameterTypes.length === 2) { if (c.parameterTypes.length === 2) {
@ -46,6 +75,11 @@ function init() {
break; break;
} }
} }
var entityPlayerClass = nmsCls('EntityPlayer');
var packetClass = nmsCls('Packet');
var playerConnectionField = remapFieldName(entityPlayerClass, 'playerConnection', 'field_71135_a')
playerConnectionFieldName = playerConnectionField.getName()
sendPacketMethod = remapMethod(playerConnectionField.getType(), 'sendPacket', 'func_179290_a', packetClass)
} }
function json(sender, json) { function json(sender, json) {
@ -63,7 +97,7 @@ function send(sender, json, type) {
} }
function sendPacket(player, p) { function sendPacket(player, p) {
player.handle.playerConnection.sendPacket(p); sendPacketMethod.invoke(player.handle[playerConnectionFieldName], p)
} }
try { try {