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

对兼容类添加1.11+,Uranium,Cauldron和1.7支持

This commit is contained in:
xjboss 2017-09-08 16:21:59 +08:00
parent 2ca0872f7c
commit ebdd574cda

View File

@ -48,41 +48,85 @@ public class C {
private static Object[] chatMessageTypes; private static Object[] chatMessageTypes;
public static boolean init; public static boolean init;
public static boolean cauldron;
public static boolean uranium;
public static boolean titlePAB;
static { static {
try { version=getNMSVersion();
version = getNMSVersion(); titlePAB=false;
newversion = Integer.parseInt(version.split("_")[1]) > 7; if(version.equals("v1_7_R4")&&Package.getPackage("net.minecraftforge.cauldron")!=null) {
nmsChatSerializer = Class.forName(a(newversion ? "IChatBaseComponent$ChatSerializer" : "ChatSerializer")); try{
chatSerializer = nmsChatSerializer.getMethod("a", String.class); Class.forName("cc.uraniummc.Uranium");
nmsIChatBaseComponent = Class.forName(a("IChatBaseComponent")); uranium=true;
packetType = Class.forName(a("PacketPlayOutChat")); }catch (Exception e){
Arrays.stream(packetType.getConstructors()).forEach(c -> { Log.d("检测到不是Uranium服务端");
if (c.getParameterTypes().length == 2) { }
packetTypeConstructor = c; try {
} cauldron=true;
}); nmsChatSerializer = Class.forName("net.minecraft.util.IChatComponent$Serializer");
nmsChatMessageTypeClass = packetTypeConstructor.getParameterTypes()[1]; chatSerializer = nmsChatSerializer.getMethod("func_150699_a", String.class);
if (nmsChatMessageTypeClass.isEnum()) { nmsIChatBaseComponent = Class.forName("net.minecraft.util.IChatComponent");
chatMessageTypes = nmsChatMessageTypeClass.getEnumConstants(); packetType = Class.forName("net.minecraft.network.play.server.S02PacketChat");
} else { Arrays.stream(packetType.getConstructors()).forEach(c -> {
switch (nmsChatMessageTypeClass.getName()) { if (c.getParameterTypes().length == 2) {
case "int": packetTypeConstructor = c;
nmsChatMessageTypeClass = Integer.class; }
case "byte": });
nmsChatMessageTypeClass = Byte.class; nmsChatMessageTypeClass = packetTypeConstructor.getParameterTypes()[1];
} if (nmsChatMessageTypeClass.isEnum()) {
nmsChatMessageTypeClassValueOf = nmsChatMessageTypeClass.getDeclaredMethod("valueOf", String.class); chatMessageTypes = nmsChatMessageTypeClass.getEnumConstants();
} else if(uranium) {
titlePAB=true;
}
Class<?> typeCraftPlayer = Class.forName(b("entity.CraftPlayer"));
Class<?> typeNMSPlayer = Class.forName("net.minecraft.entity.player.EntityPlayerMP");
Class<?> typePlayerConnection = Class.forName("net.minecraft.network.NetHandlerPlayServer");
getHandle = typeCraftPlayer.getMethod("getHandle");
playerConnection = typeNMSPlayer.getField("field_71135_a");
sendPacket = typePlayerConnection.getMethod("func_147359_a", Class.forName("net.minecraft.network.Packet"));
init = true;
} catch (Exception e) {
Log.w("C(Cauldron_1710) 兼容性工具初始化失败 可能造成部分功能不可用!");
Log.d(e);
}
}else {
try {
version = getNMSVersion();
newversion = Integer.parseInt(version.split("_")[1]) > 7;
nmsChatSerializer = Class.forName(a(newversion ? "IChatBaseComponent$ChatSerializer" : "ChatSerializer"));
chatSerializer = nmsChatSerializer.getMethod("a", String.class);
nmsIChatBaseComponent = Class.forName(a("IChatBaseComponent"));
packetType = Class.forName(a("PacketPlayOutChat"));
Arrays.stream(packetType.getConstructors()).forEach(c -> {
if (c.getParameterTypes().length == 2) {
packetTypeConstructor = c;
}
});
nmsChatMessageTypeClass = packetTypeConstructor.getParameterTypes()[1];
if (nmsChatMessageTypeClass.isEnum()) {
chatMessageTypes = nmsChatMessageTypeClass.getEnumConstants();
} else {
if(newversion) {
switch (nmsChatMessageTypeClass.getName()) {
case "int":
nmsChatMessageTypeClass = Integer.class;
case "byte":
nmsChatMessageTypeClass = Byte.class;
}
nmsChatMessageTypeClassValueOf = nmsChatMessageTypeClass.getDeclaredMethod("valueOf", String.class);
}
}
Class<?> typeCraftPlayer = Class.forName(b("entity.CraftPlayer"));
Class<?> typeNMSPlayer = Class.forName(a("EntityPlayer"));
Class<?> typePlayerConnection = Class.forName(a("PlayerConnection"));
getHandle = typeCraftPlayer.getMethod("getHandle");
playerConnection = typeNMSPlayer.getField("playerConnection");
sendPacket = typePlayerConnection.getMethod("sendPacket", Class.forName(a("Packet")));
init = true;
} catch (Exception e) {
Log.w("C 兼容性工具初始化失败 可能造成部分功能不可用!");
Log.d(e);
} }
Class<?> typeCraftPlayer = Class.forName(b("entity.CraftPlayer"));
Class<?> typeNMSPlayer = Class.forName(a("EntityPlayer"));
Class<?> typePlayerConnection = Class.forName(a("PlayerConnection"));
getHandle = typeCraftPlayer.getMethod("getHandle");
playerConnection = typeNMSPlayer.getField("playerConnection");
sendPacket = typePlayerConnection.getMethod("sendPacket", Class.forName(a("Packet")));
init = true;
} catch (Exception e) {
Log.w("C 兼容性工具初始化失败 可能造成部分功能不可用!");
Log.d(e);
} }
} }
@ -123,7 +167,17 @@ 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);
Object typeObj = chatMessageTypes == null ? nmsChatMessageTypeClassValueOf.invoke(null, String.valueOf(type)) : chatMessageTypes[type]; Object typeObj;
if(titlePAB){
sendPacket.invoke(connection,Title.packetTitleSendConstructor.newInstance(Title.actions[2],serialized));
return;
}else {
if (!newversion) {
typeObj = true;
} else {
typeObj = chatMessageTypes == null ? nmsChatMessageTypeClassValueOf.invoke(null, String.valueOf(type)) : chatMessageTypes[type];
}
}
sendPacket.invoke(connection, packetTypeConstructor.newInstance(serialized, typeObj)); sendPacket.invoke(connection, packetTypeConstructor.newInstance(serialized, typeObj));
} catch (Exception ex) { } catch (Exception ex) {
Log.d("Json发包错误 " + version, ex); Log.d("Json发包错误 " + version, ex);
@ -328,14 +382,35 @@ public class C {
private static Constructor<?> packetTitleSetTimeConstructor; private static Constructor<?> packetTitleSetTimeConstructor;
private static Object[] actions; private static Object[] actions;
static { static {
try { if(cauldron&&uranium) {
packetActions = Class.forName(a(newversion ? "PacketPlayOutTitle$EnumTitleAction" : "EnumTitleAction")); try {
packetTitle = Class.forName(a("PacketPlayOutTitle")); packetActions = Class.forName("cc.uraniummc.packet.S45PacketTitle$Type");
packetTitleSendConstructor = packetTitle.getConstructor(packetActions, nmsIChatBaseComponent); packetTitle = Class.forName("cc.uraniummc.packet.S45PacketTitle");
packetTitleSetTimeConstructor = packetTitle.getConstructor(packetActions, nmsIChatBaseComponent, Integer.TYPE, Integer.TYPE, Integer.TYPE); packetTitleSendConstructor = packetTitle.getConstructor(packetActions, nmsIChatBaseComponent);
actions = packetActions.getEnumConstants(); packetTitleSetTimeConstructor = packetTitle.getConstructor(packetActions, nmsIChatBaseComponent, Integer.TYPE, Integer.TYPE, Integer.TYPE);
} catch (Exception ignore) { actions = packetActions.getEnumConstants();
Log.w("Title 兼容性工具初始化失败 可能造成部分功能不可用!"); } catch (Exception ignore) {
titlePAB=false;
Log.w("Title(Uranium 1.7.10) 兼容性工具初始化失败 可能造成部分功能不可用!");
Log.d(ignore);
}
}else {
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();
for(Object data:actions){
Enum enum_=(Enum)data;
if(enum_.name().equals("ACTIONBAR")){
titlePAB=true;
}
}
} catch (Exception ignore) {
Log.w("Title 兼容性工具初始化失败 可能造成部分功能不可用!");
Log.d(ignore);
}
} }
} }
@ -406,7 +481,7 @@ 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 packet = packetTitleSendConstructor.newInstance(actions[4], null); Object packet = packetTitleSendConstructor.newInstance(titlePAB?actions[5]:actions[4], null);
sendPacket.invoke(connection, packet); sendPacket.invoke(connection, packet);
} }
@ -453,7 +528,7 @@ public class C {
Object packet; Object packet;
// Send if set // Send if set
if ((fadeInTime != -1) && (fadeOutTime != -1) && (stayTime != -1)) { if ((fadeInTime != -1) && (fadeOutTime != -1) && (stayTime != -1)) {
packet = packetTitleSetTimeConstructor.newInstance(actions[2], null, fadeInTime * 20, stayTime * 20, fadeOutTime * 20); packet = packetTitleSetTimeConstructor.newInstance(titlePAB?actions[3]:actions[2], null, fadeInTime * 20, stayTime * 20, fadeOutTime * 20);
sendPacket.invoke(connection, packet); sendPacket.invoke(connection, packet);
} }
// Send title // Send title