Skip to content

Commit

Permalink
fix: injector disconnect for 1.21 and above
Browse files Browse the repository at this point in the history
  • Loading branch information
Ingrim4 committed Jun 15, 2024
1 parent b75bba8 commit 5cb6b81
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import com.comphenix.protocol.reflect.FuzzyReflection;
import com.comphenix.protocol.reflect.accessors.Accessors;
import com.comphenix.protocol.reflect.accessors.FieldAccessor;
import com.comphenix.protocol.reflect.accessors.MethodAccessor;
import com.comphenix.protocol.reflect.fuzzy.FuzzyFieldContract;
import com.comphenix.protocol.utility.ByteBuddyGenerated;
import com.comphenix.protocol.utility.MinecraftFields;
Expand Down Expand Up @@ -366,7 +367,15 @@ public void disconnect(String message) {
// try to use the player connection if possible
if (playerConnection != null) {
// this method prefers the main thread so no event loop here
MinecraftMethods.getPlayerConnectionDisconnectMethod().invoke(playerConnection, message);
MethodAccessor accessor = MinecraftMethods.getPlayerConnectionDisconnectMethod();

// check if the parameter is a chat component
if (MinecraftReflection.isIChatBaseComponent(accessor.getMethod().getParameters()[0].getClass())) {
Object component = WrappedChatComponent.fromText(message).getHandle();
accessor.invoke(playerConnection, component);
} else {
accessor.invoke(playerConnection, message);
}
} else {
Object component = WrappedChatComponent.fromText(message).getHandle();
MinecraftMethods.getNetworkManagerDisconnectMethod().invoke(this.networkManager, component);
Expand Down
23 changes: 16 additions & 7 deletions src/main/java/com/comphenix/protocol/utility/MinecraftMethods.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,17 +109,26 @@ public static MethodAccessor getPlayerConnectionSendMethod() {
public static MethodAccessor getPlayerConnectionDisconnectMethod() {
if (playerConnectionDisconnectMethod == null) {
FuzzyReflection playerConnectionClass = FuzzyReflection.fromClass(MinecraftReflection.getPlayerConnectionClass());
try {

if (MinecraftVersion.v1_21_0.atOrAbove()) {
playerConnectionDisconnectMethod = Accessors.getMethodAccessor(playerConnectionClass.getMethod(FuzzyMethodContract.newBuilder()
.returnTypeVoid()
.nameRegex("disconnect.*")
.parameterCount(1)
.parameterExactType(String.class, 0)
.parameterExactType(MinecraftReflection.getIChatBaseComponentClass(), 0)
.build()));
} catch (IllegalArgumentException e) {
// Just assume it's the first String method
Method disconnect = playerConnectionClass.getMethodByParameters("disconnect", String.class);
playerConnectionDisconnectMethod = Accessors.getMethodAccessor(disconnect);
} else {
try {
playerConnectionDisconnectMethod = Accessors.getMethodAccessor(playerConnectionClass.getMethod(FuzzyMethodContract.newBuilder()
.returnTypeVoid()
.nameRegex("disconnect.*")
.parameterCount(1)
.parameterExactType(String.class, 0)
.build()));
} catch (IllegalArgumentException e) {
// Just assume it's the first String method
Method disconnect = playerConnectionClass.getMethodByParameters("disconnect", String.class);
playerConnectionDisconnectMethod = Accessors.getMethodAccessor(disconnect);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,11 @@ public static boolean is(Class<?> clazz, Object object) {
if (clazz == null || object == null) {
return false;
}

// check for accidential class objects
if (object instanceof Class) {
return clazz.isAssignableFrom((Class<?>) object);
}

return clazz.isAssignableFrom(object.getClass());
}
Expand Down Expand Up @@ -502,6 +507,10 @@ public static boolean isCraftItemStack(Object obj) {
return is(getCraftItemStackClass(), obj);
}

public static boolean isIChatBaseComponent(Class<?> target) {
return is(getIChatBaseComponentClass(), target);
}

/**
* Retrieve the EntityPlayer (NMS) class.
*
Expand Down

0 comments on commit 5cb6b81

Please sign in to comment.