Skip to content

Commit

Permalink
* Fixed up old NMS reflection.
Browse files Browse the repository at this point in the history
Signed-off-by: BuildTools <[email protected]>
  • Loading branch information
XZot1K authored and BuildTools committed Mar 9, 2023
1 parent e0837f5 commit e2ca4b3
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 9 deletions.
12 changes: 11 additions & 1 deletion src/main/java/xzot1k/plugins/sp/core/packets/bar/ABH_Old.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,17 @@ public void sendActionBar(@NotNull Player player, @NotNull String message) {
final Object icbc = method.invoke(csClass, ("{\"text\": \""
+ SimplePortals.getPluginInstance().getManager().colorText(message) + "\"}"));

final Constructor<?> packetConstructor = packetChatClass.getConstructor(icbClass, Byte.class);
Constructor<?> packetConstructor = null;
for (Constructor<?> con : packetChatClass.getConstructors()) {

if (con.getParameterTypes().length != 2 || con.getParameterTypes()[0] != icbClass
|| con.getParameterTypes()[1] != byte.class) continue;

packetConstructor = con;
break;
}

if (packetConstructor == null) return;
final Object packet = packetConstructor.newInstance(icbc, (byte) 2);

final Object cPlayer = craftPlayerClass.cast(player);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@

public class Titles_Old implements TitleHandler {

private Class<?> csClass, titlePacketClass, etaClass, cpClass, packetClass;
private Class<?> csClass, cbcClass, titlePacketClass, etaClass, cpClass, packetClass;

public Titles_Old() {
try {
cbcClass = Class.forName("net.minecraft.server."
+ SimplePortals.getPluginInstance().getServerVersion() + ".IChatBaseComponent");

csClass = Class.forName("net.minecraft.server."
+ SimplePortals.getPluginInstance().getServerVersion() + ".IChatBaseComponent$ChatSerializer");

Expand Down Expand Up @@ -48,23 +51,55 @@ public void sendSubTitle(Player player, String text, int fadeIn, int displayTime

private void send(@NotNull Player player, @NotNull String action, @NotNull String text, int fadeIn, int displayTime, int fadeOut) {
try {
final Object titleAction = etaClass.getDeclaredField(action);
final Object titleAction = etaClass.getDeclaredField(action).get(null);

final Method aMethod = csClass.getDeclaredMethod("a", String.class);
final String textField = (String) aMethod.invoke(csClass, "{\"text\":\""
+ SimplePortals.getPluginInstance().getManager().colorText(text) + "\"}");

final Constructor<?> pConst = titlePacketClass.getConstructor(etaClass, String.class,
Integer.class, Integer.class, Integer.class);
final String coloredText = SimplePortals.getPluginInstance().getManager().colorText(text);
final Object textField = aMethod.invoke(csClass, "{\"text\":\"" + coloredText + "\"}");


Constructor<?> pConst = null;
for (Constructor<?> con : titlePacketClass.getConstructors()) {

if (con.getParameterTypes().length != 5) continue;

if (con.getParameterTypes()[0] != etaClass || con.getParameterTypes()[1] != cbcClass
&& con.getParameterTypes()[2] != int.class
&& con.getParameterTypes()[3] != int.class
&& con.getParameterTypes()[4] != int.class) continue;

pConst = con;
break;
}

boolean isOld = false;
if (pConst == null) {
for (Constructor<?> con : titlePacketClass.getConstructors()) {

if (con.getParameterTypes().length != 5) continue;

if (con.getParameterTypes()[0] != etaClass || con.getParameterTypes()[1] != String.class
&& con.getParameterTypes()[2] != int.class
&& con.getParameterTypes()[3] != int.class
&& con.getParameterTypes()[4] != int.class) continue;

pConst = con;
isOld = true;
break;
}
}

if (pConst == null) return;

final Object packet = pConst.newInstance(titleAction, textField, (fadeIn * 20), (displayTime * 20), (fadeOut * 20));
final Object packet = pConst.newInstance(titleAction, (!isOld ? textField : coloredText), (fadeIn * 20), (displayTime * 20), (fadeOut * 20));

final Object cPlayer = cpClass.cast(player);
final Object getHandle = cpClass.getDeclaredMethod("getHandle").invoke(cPlayer);
final Object pConnection = getHandle.getClass().getDeclaredField("playerConnection").get(getHandle);
final Method sendPacket = pConnection.getClass().getDeclaredMethod("sendPacket", packetClass);
sendPacket.invoke(pConnection, packet);
} catch (NoSuchFieldException | NoSuchMethodException | IllegalAccessException
} catch (NoSuchFieldException | NoSuchMethodException | IllegalAccessException | IllegalArgumentException
| InvocationTargetException | InstantiationException e) {e.printStackTrace();}
}

Expand Down

0 comments on commit e2ca4b3

Please sign in to comment.