From 73343f8d20d6d3fbd005c6d172ac2eae2a107e8e Mon Sep 17 00:00:00 2001 From: Rsl1122 <24460436+Rsl1122@users.noreply.github.com> Date: Fri, 24 Jan 2020 10:41:04 +0200 Subject: [PATCH] Change false assumption about paper methods Some paper methods are not available in old versions of Paper, leading to exceptions. Method for checking the existence of the methods was added Affects issues: - Fixed #1304 --- .../plan/gathering/BukkitSensor.java | 25 +++++++++++++++---- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/Plan/bukkit/src/main/java/com/djrapitops/plan/gathering/BukkitSensor.java b/Plan/bukkit/src/main/java/com/djrapitops/plan/gathering/BukkitSensor.java index 461f3d759..6df7a0e23 100644 --- a/Plan/bukkit/src/main/java/com/djrapitops/plan/gathering/BukkitSensor.java +++ b/Plan/bukkit/src/main/java/com/djrapitops/plan/gathering/BukkitSensor.java @@ -18,6 +18,7 @@ package com.djrapitops.plan.gathering; import com.djrapitops.plan.Plan; import com.djrapitops.plugin.api.Check; +import org.bukkit.Server; import org.bukkit.World; import javax.inject.Inject; @@ -27,19 +28,25 @@ import javax.inject.Singleton; public class BukkitSensor implements ServerSensor { private final Plan plugin; - private final boolean hasPaper; + + private final boolean hasTPSMethod; + private final boolean hasEntityCountMethod; + private final boolean hasChunkCountMethod; @Inject public BukkitSensor( Plan plugin ) { this.plugin = plugin; - hasPaper = Check.isPaperAvailable(); + boolean hasPaper = Check.isPaperAvailable(); + hasTPSMethod = hasPaper && hasPaperMethod(Server.class, "getTPS"); + hasEntityCountMethod = hasPaper && hasPaperMethod(World.class, "getEntityCount"); + hasChunkCountMethod = hasPaper && hasPaperMethod(World.class, "getChunkCount"); } @Override public boolean supportsDirectTPS() { - return hasPaper; + return hasTPSMethod; } @Override @@ -49,7 +56,7 @@ public class BukkitSensor implements ServerSensor { @Override public int getChunkCount(World world) { - if (hasPaper) { + if (hasChunkCountMethod) { try { return getChunkCountPaperWay(world); } catch (BootstrapMethodError | NoSuchMethodError e) { @@ -69,7 +76,7 @@ public class BukkitSensor implements ServerSensor { @Override public int getEntityCount(World world) { - if (hasPaper) { + if (hasEntityCountMethod) { try { return getEntitiesPaperWay(world); } catch (BootstrapMethodError | NoSuchMethodError e) { @@ -96,4 +103,12 @@ public class BukkitSensor implements ServerSensor { public Iterable getWorlds() { return plugin.getServer().getWorlds(); } + + private boolean hasPaperMethod(Class clazz, String methodName) { + try { + return clazz.getMethod(methodName) != null; + } catch (NoSuchMethodException e) { + return false; + } + } }