Skip to content

Commit

Permalink
Merge pull request #67 from Foorcee/dev/1.18
Browse files Browse the repository at this point in the history
  • Loading branch information
zml2008 authored Dec 2, 2021
2 parents f7b1731 + 9f8f16f commit 18bef6f
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 4 deletions.
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, 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, 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, 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,55 @@ public static boolean hasClass(final @NotNull String... classNames) {
return null;
}

/**
* Search a handle for a class method.
*
* @param holderClass a class
* @param modifier 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 Integer modifier, final String methodName, final @Nullable Class<?> returnClass, final Class<?>... parameterClasses) {
return searchMethod(holderClass, modifier, new String[] {methodName}, returnClass, parameterClasses);
}

/**
* Search a handle for a class method.
*
* @param holderClass a class
* @param modifier 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 Integer modifier, 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 ((modifier == null || (method.getModifiers() & modifier) == 0)
|| !Arrays.equals(method.getParameterTypes(), parameterClasses)) continue;
try {
return LOOKUP.findVirtual(holderClass, method.getName(), MethodType.methodType(returnClass, parameterClasses));
} catch (final NoSuchMethodException | IllegalAccessException e) {
}
}
return null;
}

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

0 comments on commit 18bef6f

Please sign in to comment.