Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 1.18 support #67

Merged
merged 2 commits into from
Dec 2, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
import static net.kyori.adventure.platform.bukkit.MinecraftReflection.lookup;
import static net.kyori.adventure.platform.bukkit.MinecraftReflection.needClass;
import static net.kyori.adventure.platform.bukkit.MinecraftReflection.needField;
import static net.kyori.adventure.platform.bukkit.MinecraftReflection.searchMethod;
import static net.kyori.adventure.platform.facet.Knob.isEnabled;
import static net.kyori.adventure.platform.facet.Knob.logError;

Expand Down Expand Up @@ -159,7 +160,7 @@ public boolean isSupported() {
}
}
}
playerConnectionSendPacket = findMethod(playerConnectionClass, new String[]{"sendPacket", "send"}, void.class, packetClass);
playerConnectionSendPacket = searchMethod(playerConnectionClass, new int[]{Modifier.PUBLIC}, new String[]{"sendPacket", "send"}, void.class, packetClass);
} catch (final Throwable error) {
logError(error, "Failed to initialize CraftBukkit sendPacket");
}
Expand Down Expand Up @@ -395,7 +396,7 @@ static class EntitySound extends PacketFacet<Player> implements Facet.EntitySoun
private static final MethodHandle NEW_CLIENTBOUND_CUSTOM_SOUND = findConstructor(CLASS_CLIENTBOUND_CUSTOM_SOUND, CLASS_RESOURCE_LOCATION, CLASS_SOUND_SOURCE, CLASS_VEC3, float.class, float.class);
private static final MethodHandle NEW_VEC3 = findConstructor(CLASS_VEC3, double.class, double.class, double.class);
private static final MethodHandle NEW_RESOURCE_LOCATION = findConstructor(CLASS_RESOURCE_LOCATION, String.class, String.class);
private static final MethodHandle REGISTRY_GET_OPTIONAL = findMethod(CLASS_REGISTRY, "getOptional", Optional.class, CLASS_RESOURCE_LOCATION);
private static final MethodHandle REGISTRY_GET_OPTIONAL = searchMethod(CLASS_REGISTRY, new int[]{Modifier.PUBLIC}, "getOptional", Optional.class, CLASS_RESOURCE_LOCATION);
private static final MethodHandle SOUND_SOURCE_GET_NAME;
private static final Object REGISTRY_SOUND_EVENT;

Expand Down Expand Up @@ -806,8 +807,7 @@ public InputStream toInputStream() {
findMcClassName("world.item.ItemStack")
);

private static final MethodHandle MC_ITEMSTACK_SET_TAG = findMethod(CLASS_MC_ITEMSTACK, "setTag", void.class, CLASS_NBT_TAG_COMPOUND);
private static final MethodHandle MC_ITEMSTACK_GET_TAG = findMethod(CLASS_MC_ITEMSTACK, "getTag", CLASS_NBT_TAG_COMPOUND);
private static final MethodHandle MC_ITEMSTACK_SET_TAG = searchMethod(CLASS_MC_ITEMSTACK, new int[]{Modifier.PUBLIC}, "setTag", void.class, CLASS_NBT_TAG_COMPOUND);

private static final MethodHandle CRAFT_ITEMSTACK_NMS_COPY = findStaticMethod(CLASS_CRAFT_ITEMSTACK, "asNMSCopy", CLASS_MC_ITEMSTACK, ItemStack.class);
private static final MethodHandle CRAFT_ITEMSTACK_CRAFT_MIRROR = findStaticMethod(CLASS_CRAFT_ITEMSTACK, "asCraftMirror", CLASS_CRAFT_ITEMSTACK, CLASS_MC_ITEMSTACK);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Arrays;
import org.bukkit.Bukkit;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -141,6 +142,65 @@ public static boolean hasClass(final @NotNull String... classNames) {
return null;
}

/**
* Search a handle for a class method.
*
* @param holderClass a class
* @param methodModifiers method modifiers
* @param methodName a method name
* @param returnClass a method return class
* @param parameterClasses an array of method parameter classes
* @return a method handle or {@code null} if not found
*/
public static MethodHandle searchMethod(final @Nullable Class<?> holderClass, final @Nullable int[] methodModifiers, final String methodName, final @Nullable Class<?> returnClass, final Class<?>... parameterClasses) {
return searchMethod(holderClass, methodModifiers, new String[] {methodName}, returnClass, parameterClasses);
}

/**
* Search a handle for a class method.
*
* @param holderClass a class
* @param methodModifiers method modifiers
* @param methodNames a method names
* @param returnClass a method return class
* @param parameterClasses an array of method parameter classes
* @return a method handle or {@code null} if not found
*/
public static MethodHandle searchMethod(final @Nullable Class<?> holderClass, final @Nullable int[] methodModifiers, final @Nullable String@NotNull[] methodNames, final @Nullable Class<?> returnClass, final Class<?>... parameterClasses) {
if (holderClass == null || returnClass == null) return null;
for (final Class<?> parameterClass : parameterClasses) {
if (parameterClass == null) return null;
}

for (final String methodName : methodNames) {
if (methodName == null) continue;
try {
return LOOKUP.findVirtual(holderClass, methodName, MethodType.methodType(returnClass, parameterClasses));
} catch (final NoSuchMethodException | IllegalAccessException e) {
}
}

for (final Method method : holderClass.getDeclaredMethods()) {
if (!matchModifiers(method.getModifiers(), methodModifiers)
|| !Arrays.equals(method.getParameterTypes(), parameterClasses)) continue;
try {
return LOOKUP.findVirtual(holderClass, method.getName(), MethodType.methodType(returnClass, parameterClasses));
} catch (final NoSuchMethodException | IllegalAccessException e) {
}
}
return null;
}

private static boolean matchModifiers(final int modifier, final @Nullable int[] modifiers) {
zml2008 marked this conversation as resolved.
Show resolved Hide resolved
if (modifiers == null || modifiers.length == 0) return true;
for (final Integer mod : modifiers) {
if ((mod & modifier) == 0) {
return false;
}
}
return true;
}

/**
* Gets a handle for a class method.
*
Expand Down