Skip to content

Commit

Permalink
Merge branch 'master' into improve-listener-tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
Ingrim4 authored Jun 6, 2024
2 parents 4edaca4 + 76432f9 commit c7c7305
Show file tree
Hide file tree
Showing 6 changed files with 211 additions and 70 deletions.
18 changes: 9 additions & 9 deletions src/main/java/com/comphenix/protocol/ProtocolLib.java
Original file line number Diff line number Diff line change
Expand Up @@ -535,15 +535,15 @@ public void onDisable() {

// that reloading the server might break ProtocolLib / plugins depending on it
if (Util.isCurrentlyReloading()) {
logger.severe("╔══════════════════════════════════════════════════════════════════╗");
logger.severe("║ WARNING ║");
logger.severe("║ RELOADING THE SERVER WHILE PROTOCOL LIB IS ENABLED MIGHT ║");
logger.severe("║ LEAD TO UNEXPECTED ERRORS! ║");
logger.severe("║ ║");
logger.severe("║ Consider to cleanly restart your server if you encounter ║");
logger.severe("║ any issues related to Protocol Lib before opening an issue ║");
logger.severe("║ on GitHub! ║");
logger.severe("╚══════════════════════════════════════════════════════════════════╝");
highlyVisibleError(
" WARNING ",
" RELOADING THE SERVER WHILE PROTOCOLLIB IS ENABLED MIGHT ",
" LEAD TO UNEXPECTED ERRORS! ",
" ",
" Consider cleanly restarting your server if you encounter ",
" any issues related to ProtocolLib before opening an issue ",
" on GitHub! "
);
}

// Clean up
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@

import java.lang.reflect.Array;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Supplier;
import java.util.logging.Level;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -115,6 +117,8 @@ public final class MinecraftReflection {
private static MethodAccessor asCraftMirror = null;
private static MethodAccessor isEmpty = null;

private static boolean isMojangMapped = false;

private MinecraftReflection() {
// No need to make this constructable.
}
Expand Down Expand Up @@ -204,6 +208,9 @@ public static String getMinecraftPackage() {
}
}

// for now, we're going to say that it's Mojang mapped if the nms world was renamed to ServerLevel
isMojangMapped = getNmsWorldClass().getName().contains("ServerLevel");

return MINECRAFT_FULL_PACKAGE;
} catch (NoSuchMethodException exception) {
throw new IllegalStateException("Cannot find getHandle() in CraftEntity", exception);
Expand Down Expand Up @@ -1109,19 +1116,37 @@ public static Class<?> getPlayerInfoDataClass() {
}
}

static Class<?> getOrInferMinecraftClass(String className, Supplier<Class<?>> supplier) {
return getOptionalNMS(className).orElseGet(() -> {
Class<?> clazz = supplier.get();
return setMinecraftClass(className, clazz);
});
}

/**
* Retrieves the entity use action class in 1.17.
*
* @return The EntityUseAction class
*/
public static Class<?> getEnumEntityUseActionClass() {
Class<?> packetClass = PacketType.Play.Client.USE_ENTITY.getPacketClass();
FuzzyReflection fuzzyReflection = FuzzyReflection.fromClass(packetClass, true);
try {
return fuzzyReflection.getFieldByType("^.*(EnumEntityUseAction)").getType();
} catch (IllegalArgumentException ignored) {
return fuzzyReflection.getFieldByType("^.*(Action)").getType();
}
return getOrInferMinecraftClass("ServerboundInteractPacket.Action", () -> {
Class<?> packetClass = PacketType.Play.Client.USE_ENTITY.getPacketClass();
FuzzyReflection fuzzyReflection = FuzzyReflection.fromClass(packetClass, true);

Field field = fuzzyReflection.getField(FuzzyFieldContract.newBuilder()
.banModifier(Modifier.STATIC)
.typeDerivedOf(Object.class)
.build());
if (field != null) {
return field.getType();
}

try {
return fuzzyReflection.getFieldByType("^.*(EnumEntityUseAction)").getType();
} catch (IllegalArgumentException ignored) {
return fuzzyReflection.getFieldByType("^.*(Action)").getType();
}
});
}

/**
Expand Down Expand Up @@ -1763,4 +1788,8 @@ public static Class<?> getStreamCodecClass() {
public static Optional<Class<?>> getRegistryFriendlyByteBufClass() {
return getOptionalNMS("network.RegistryFriendlyByteBuf");
}

public static boolean isMojangMapped() {
return isMojangMapped;
}
}
13 changes: 11 additions & 2 deletions src/main/java/com/comphenix/protocol/wrappers/EnumWrappers.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@
import com.comphenix.protocol.reflect.ExactReflection;
import com.comphenix.protocol.reflect.FuzzyReflection;
import com.comphenix.protocol.reflect.accessors.Accessors;
import com.comphenix.protocol.reflect.fuzzy.FuzzyMatchers;
import com.comphenix.protocol.reflect.fuzzy.FuzzyMethodContract;
import com.comphenix.protocol.utility.MinecraftReflection;
import com.comphenix.protocol.utility.MinecraftVersion;
import org.apache.commons.lang.Validate;
import org.bukkit.GameMode;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.HashSet;
Expand Down Expand Up @@ -549,8 +552,14 @@ private static void initialize() {
// In 1.17 the hand and use action class is no longer a field in the packet
if (MinecraftVersion.CAVES_CLIFFS_1.atOrAbove()) {
HAND_CLASS = MinecraftReflection.getMinecraftClass("world.EnumHand", "world.InteractionHand");
// class is named 'b' in the packet but class order differs in spigot and paper so we can only use the first method's return type (safest way)
ENTITY_USE_ACTION_CLASS = MinecraftReflection.getEnumEntityUseActionClass().getMethods()[0].getReturnType();

FuzzyReflection fuzzy = FuzzyReflection.fromClass(MinecraftReflection.getEnumEntityUseActionClass(), true);
Method getType = fuzzy.getMethod(FuzzyMethodContract.newBuilder()
.parameterCount(0)
.returnTypeMatches(FuzzyMatchers.except(Void.class))
.build());

ENTITY_USE_ACTION_CLASS = getType.getReturnType();
} else {
HAND_CLASS = getEnum(PacketType.Play.Client.USE_ENTITY.getPacketClass(), 1);
ENTITY_USE_ACTION_CLASS = getEnum(PacketType.Play.Client.USE_ENTITY.getPacketClass(), 0);
Expand Down
Loading

0 comments on commit c7c7305

Please sign in to comment.