From 447362e3021efe94ef9210f6a62a38083a09cff5 Mon Sep 17 00:00:00 2001 From: Michael Date: Tue, 4 Jul 2023 16:08:29 -0500 Subject: [PATCH] Add reflective checks on plugin enable to prevent script errors later --- .../java/dev/magicmq/pyspigot/PySpigot.java | 19 +++++++++++++++++++ .../manager/script/ScriptManager.java | 7 ++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/main/java/dev/magicmq/pyspigot/PySpigot.java b/src/main/java/dev/magicmq/pyspigot/PySpigot.java index 1b498f8..48866f5 100644 --- a/src/main/java/dev/magicmq/pyspigot/PySpigot.java +++ b/src/main/java/dev/magicmq/pyspigot/PySpigot.java @@ -26,8 +26,13 @@ import dev.magicmq.pyspigot.manager.script.ScriptManager; import dev.magicmq.pyspigot.manager.task.TaskManager; import org.bukkit.Bukkit; +import org.bukkit.command.PluginCommand; +import org.bukkit.command.SimpleCommandMap; +import org.bukkit.plugin.Plugin; import org.bukkit.plugin.java.JavaPlugin; +import java.util.logging.Level; + /** * Main class of the plugin. */ @@ -81,6 +86,13 @@ public void onEnable() { if (isPlaceholderApiAvailable()) placeholder = PlaceholderManager.get(); + + try { + checkReflection(); + } catch (NoSuchMethodException | NoSuchFieldException e) { + getLogger().log(Level.SEVERE, "Error when accessing CraftBukkit (Are you on a supported MC version?), PySpigot will not work correctly."); + Bukkit.getPluginManager().disablePlugin(this); + } } @Override @@ -112,6 +124,13 @@ public boolean isPlaceholderApiAvailable() { return Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null; } + private void checkReflection() throws NoSuchMethodException, NoSuchFieldException { + //Check reflection for commands + PluginCommand.class.getDeclaredConstructor(String.class, Plugin.class); + Bukkit.getServer().getClass().getDeclaredField("commandMap"); + SimpleCommandMap.class.getDeclaredField("knownCommands"); + } + /** * Get the instance of this plugin. * @return The instance diff --git a/src/main/java/dev/magicmq/pyspigot/manager/script/ScriptManager.java b/src/main/java/dev/magicmq/pyspigot/manager/script/ScriptManager.java index 12351a9..d28ec28 100644 --- a/src/main/java/dev/magicmq/pyspigot/manager/script/ScriptManager.java +++ b/src/main/java/dev/magicmq/pyspigot/manager/script/ScriptManager.java @@ -29,6 +29,7 @@ import dev.magicmq.pyspigot.manager.protocol.ProtocolManager; import dev.magicmq.pyspigot.manager.task.TaskManager; import org.bukkit.Bukkit; +import org.bukkit.scheduler.BukkitTask; import org.python.core.*; import org.python.util.PythonInterpreter; @@ -56,6 +57,8 @@ public class ScriptManager { private final List