Skip to content

Commit

Permalink
fix method modifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
Foorcee committed Dec 1, 2021
1 parent f7cc043 commit 9f8f16f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public boolean isSupported() {
}
}
}
playerConnectionSendPacket = searchMethod(playerConnectionClass, new int[]{Modifier.PUBLIC}, 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 @@ -396,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 = searchMethod(CLASS_REGISTRY, new int[]{Modifier.PUBLIC}, "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 @@ -807,7 +807,7 @@ public InputStream toInputStream() {
findMcClassName("world.item.ItemStack")
);

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 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 @@ -146,27 +146,27 @@ public static boolean hasClass(final @NotNull String... classNames) {
* Search a handle for a class method.
*
* @param holderClass a class
* @param methodModifiers method modifiers
* @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 int[] methodModifiers, final String methodName, final @Nullable Class<?> returnClass, final Class<?>... parameterClasses) {
return searchMethod(holderClass, methodModifiers, new String[] {methodName}, returnClass, parameterClasses);
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 methodModifiers method modifiers
* @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 int[] methodModifiers, final @Nullable String@NotNull[] methodNames, final @Nullable Class<?> returnClass, final Class<?>... parameterClasses) {
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;
Expand All @@ -181,7 +181,7 @@ public static MethodHandle searchMethod(final @Nullable Class<?> holderClass, fi
}

for (final Method method : holderClass.getDeclaredMethods()) {
if (!matchModifiers(method.getModifiers(), methodModifiers)
if ((modifier == null || (method.getModifiers() & modifier) == 0)
|| !Arrays.equals(method.getParameterTypes(), parameterClasses)) continue;
try {
return LOOKUP.findVirtual(holderClass, method.getName(), MethodType.methodType(returnClass, parameterClasses));
Expand All @@ -191,16 +191,6 @@ public static MethodHandle searchMethod(final @Nullable Class<?> holderClass, fi
return null;
}

private static boolean matchModifiers(final int modifier, final @Nullable int[] modifiers) {
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

0 comments on commit 9f8f16f

Please sign in to comment.