From b83a3dd548ed48e59f0db6f668c5a1231f15c8f8 Mon Sep 17 00:00:00 2001 From: Auxilor Date: Tue, 5 Jan 2021 09:09:05 +0000 Subject: [PATCH] Fixed TelekinesisUtils and updated to 1.0.5 --- build.gradle | 2 +- .../drops/telekinesis/TelekinesisUtils.java | 40 +++++++++++++++++-- 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/build.gradle b/build.gradle index 327efb2b7..63325baee 100644 --- a/build.gradle +++ b/build.gradle @@ -98,5 +98,5 @@ build.dependsOn publishToMavenLocal group = 'com.willfp' archivesBaseName = project.name -version = '1.0.4' +version = '1.0.5' java.sourceCompatibility = JavaVersion.VERSION_1_8 \ No newline at end of file diff --git a/src/main/java/com/willfp/eco/util/drops/telekinesis/TelekinesisUtils.java b/src/main/java/com/willfp/eco/util/drops/telekinesis/TelekinesisUtils.java index f46881f58..c12b89715 100644 --- a/src/main/java/com/willfp/eco/util/drops/telekinesis/TelekinesisUtils.java +++ b/src/main/java/com/willfp/eco/util/drops/telekinesis/TelekinesisUtils.java @@ -7,14 +7,21 @@ import org.bukkit.plugin.ServicePriority; import org.jetbrains.annotations.NotNull; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; import java.util.function.Function; @UtilityClass public final class TelekinesisUtils { + private final Object instance; + private final Class clazz; + /** * The test service registered to bukkit. */ - private final TelekinesisTests tests; + private final Method testMethod; + + private final Method registerMethod; /** * Test the player for telekinesis. @@ -25,7 +32,13 @@ public final class TelekinesisUtils { * @return If the player is telekinetic. */ public boolean testPlayer(@NotNull final Player player) { - return tests.testPlayer(player); + try { + return (boolean) testMethod.invoke(instance, player); + } catch (IllegalAccessException | InvocationTargetException e) { + e.printStackTrace(); + } + + return false; } /** @@ -34,14 +47,33 @@ public boolean testPlayer(@NotNull final Player player) { * @param test The test to register, where the boolean output is if the player is telekinetic. */ public void registerTest(@NotNull final Function test) { - tests.registerTest(test); + try { + registerMethod.invoke(instance, test); + } catch (IllegalAccessException | InvocationTargetException e) { + e.printStackTrace(); + } } static { + Method testMethod1; + Method registerMethod1; if (Bukkit.getServicesManager().getKnownServices().stream().noneMatch(clazz -> clazz.getName().contains("TelekinesisTests"))) { Bukkit.getServicesManager().register(TelekinesisTests.class, new EcoTelekinesisTests(), AbstractEcoPlugin.getInstance(), ServicePriority.Normal); } - tests = (TelekinesisTests) Bukkit.getServicesManager().load(Bukkit.getServicesManager().getKnownServices().stream().filter(clazz -> clazz.getName().contains("TelekinesisTests")).findFirst().get()); + instance = Bukkit.getServicesManager().load(Bukkit.getServicesManager().getKnownServices().stream().filter(clazz -> clazz.getName().contains("TelekinesisTests")).findFirst().get()); + clazz = instance.getClass(); + + try { + testMethod1 = clazz.getDeclaredMethod("testPlayer", Player.class); + registerMethod1 = clazz.getDeclaredMethod("registerTest", Function.class); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + testMethod1 = null; + registerMethod1 = null; + } + + testMethod = testMethod1; + registerMethod = registerMethod1; } }