package org.sgrewritten.stargate.property; import org.sgrewritten.stargate.Stargate; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.logging.Level; /** * An enum containing various new features that might be available */ public enum NonLegacyMethod { /** * The powered minecart pushX method * *
This was added to Paper to change a powered minecart's x-push
*/ PUSH_X(NonLegacyClass.POWERED_MINECART, "setPushX", double.class), /** * The powered minecart pushZ method * *This was added to Paper to change a powered minecart's z-push
*/ PUSH_Z(NonLegacyClass.POWERED_MINECART, "setPushZ", double.class), /** * The world get getMinHeight() method * *This was added as a cause of the cave update
*/ GET_WORLD_MIN(NonLegacyClass.WORLD, "getMinHeight"), /** * The world get getMaxHeight() method * *This was added as a cause of the cave update
*/ GET_WORLD_MAX(NonLegacyClass.WORLD, "getMaxHeight"), /** * The powered minecart getFuel method * *This was added to Paper to change a powered minecart's z-push
*/ GET_FUEL(NonLegacyClass.POWERED_MINECART, "getFuel"); private NonLegacyClass nonLegacyClass; private String methodInClassToCheckFor; private Class>[] parameters; private boolean isImplemented; /** * Instantiates a new non-legacy method * * @param classToCheckForThe class containing the method
* @param methodInClassToCheckForThe legacy method itself
* @param parameterTypesThe parameters expected by the method
*/ NonLegacyMethod(NonLegacyClass nonLegacyClass, String methodInClassToCheckFor, Class>... parameterTypes) { try { Class> aClass = nonLegacyClass.getRelatedClass(); aClass.getMethod(methodInClassToCheckFor, parameterTypes); this.nonLegacyClass = nonLegacyClass; this.methodInClassToCheckFor = methodInClassToCheckFor; this.parameters = parameterTypes; isImplemented = true; } catch (ClassNotFoundException | NoSuchMethodException ignored) { isImplemented = false; } } /** * Checks whether this non-legacy method is available * * @returnWhether this non-legacy method is available
*/ public boolean isImplemented() { return isImplemented; } /** * Invokes this non-legacy method * * @param objectThe object to invoke the method on
* @param parametersThe parameters required for the method
* @returnThe return value of the invocation
*/ @SuppressWarnings("UnusedReturnValue") public Object invoke(Object object, Object... parameters) { try { Class> aClass = nonLegacyClass.getRelatedClass(); Method method = aClass.getMethod(methodInClassToCheckFor, this.parameters); return method.invoke(object, parameters); } catch (ClassNotFoundException | NoSuchMethodException | InvocationTargetException | IllegalAccessException e) { Stargate.log(Level.FINE, "Unable to invoke " + this.name()); return null; } } }