Skip to content

Commit

Permalink
Add reflective checks on plugin enable to prevent script errors later
Browse files Browse the repository at this point in the history
  • Loading branch information
magicmq committed Jul 4, 2023
1 parent 03a9c8b commit 447362e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
19 changes: 19 additions & 0 deletions src/main/java/dev/magicmq/pyspigot/PySpigot.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -56,6 +57,8 @@ public class ScriptManager {
private final List<Script> scripts;
private final HashMap<String, PyObject> globalVariables;

private final BukkitTask startScriptTask;

private ScriptManager() {
this.systemState = new PySystemState();
systemState.setClassLoader(LibraryManager.get().getClassLoader());
Expand All @@ -71,13 +74,15 @@ private ScriptManager() {
if (!logs.exists())
logs.mkdir();

Bukkit.getScheduler().runTaskLater(PySpigot.get(), this::loadScripts, PluginConfig.getLoadScriptDelay());
startScriptTask = Bukkit.getScheduler().runTaskLater(PySpigot.get(), this::loadScripts, PluginConfig.getLoadScriptDelay());
}

/**
* Called on plugin unload or server shutdown. Gracefully stops and unloads all loaded and running scripts.
*/
public void shutdown() {
startScriptTask.cancel();

for (Script script : scripts) {
ScriptUnloadEvent event = new ScriptUnloadEvent(script, false);
Bukkit.getPluginManager().callEvent(event);
Expand Down

0 comments on commit 447362e

Please sign in to comment.