Get onEnable working for java plugin
This commit is contained in:
parent
228c508df0
commit
ea1f6df029
@ -0,0 +1,88 @@
|
||||
package org.patchbukkit.loader;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.regex.Pattern;
|
||||
import org.bukkit.event.Event;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.plugin.EventExecutor;
|
||||
import org.bukkit.plugin.InvalidDescriptionException;
|
||||
import org.bukkit.plugin.InvalidPluginException;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
import org.bukkit.plugin.PluginDescriptionFile;
|
||||
import org.bukkit.plugin.PluginLoader;
|
||||
import org.bukkit.plugin.RegisteredListener;
|
||||
import org.bukkit.plugin.UnknownDependencyException;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
public class PatchBukkitPluginLoader implements PluginLoader {
|
||||
|
||||
public static JavaPlugin createPlugin(String jarPath, String mainClass) {
|
||||
try {
|
||||
File jarFile = new File(jarPath);
|
||||
if (!jarFile.exists()) {
|
||||
System.err.println(
|
||||
"[PatchBukkit] Plugin file does not exist: " + jarPath
|
||||
);
|
||||
return null;
|
||||
}
|
||||
|
||||
PatchBukkitPluginClassLoader classLoader =
|
||||
new PatchBukkitPluginClassLoader(
|
||||
PatchBukkitPluginLoader.class.getClassLoader(),
|
||||
jarFile
|
||||
);
|
||||
|
||||
Class<?> jarClass = Class.forName(mainClass, true, classLoader);
|
||||
return (JavaPlugin) jarClass.getDeclaredConstructor().newInstance();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Plugin loadPlugin(File file)
|
||||
throws InvalidPluginException, UnknownDependencyException {
|
||||
throw new UnsupportedOperationException("Use createPlugin() instead");
|
||||
}
|
||||
|
||||
@Override
|
||||
public PluginDescriptionFile getPluginDescription(File file)
|
||||
throws InvalidDescriptionException {
|
||||
throw new UnsupportedOperationException(
|
||||
"Use PatchBukkitPluginClassLoader.getDescription() instead"
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Pattern[] getPluginFileFilters() {
|
||||
return new Pattern[] { Pattern.compile("\\.jar$") };
|
||||
}
|
||||
|
||||
@Override
|
||||
public void enablePlugin(Plugin plugin) {
|
||||
if (plugin instanceof JavaPlugin javaPlugin) {
|
||||
javaPlugin.setEnabled(true);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void disablePlugin(Plugin plugin) {
|
||||
if (plugin instanceof JavaPlugin javaPlugin) {
|
||||
javaPlugin.setEnabled(false);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<
|
||||
Class<? extends Event>,
|
||||
Set<RegisteredListener>
|
||||
> createRegisteredListeners(Listener listener, Plugin plugin) {
|
||||
// TODO: Implement event listener registration when event system is needed
|
||||
// For now, return empty map - plugins won't be able to register events
|
||||
return Map.of();
|
||||
}
|
||||
}
|
||||
@ -1,39 +0,0 @@
|
||||
package org.patchbukkit.loader;
|
||||
|
||||
import io.papermc.paper.plugin.provider.util.ProviderUtil;
|
||||
import java.io.File;
|
||||
import java.net.URL;
|
||||
import java.net.URLClassLoader;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
public class PluginLoader {
|
||||
|
||||
private static final Logger LOGGER = Logger.getLogger("PatchBukkit");
|
||||
|
||||
public static JavaPlugin createPlugin(String jarPath, String mainClass) {
|
||||
try {
|
||||
File jarFile = new File(jarPath);
|
||||
if (!jarFile.exists()) {
|
||||
LOGGER.severe("Plugin file does not exist: " + jarPath);
|
||||
return null;
|
||||
}
|
||||
|
||||
PatchBukkitPluginClassLoader classLoader =
|
||||
new PatchBukkitPluginClassLoader(
|
||||
PluginLoader.class.getClassLoader(),
|
||||
jarFile
|
||||
);
|
||||
|
||||
return ProviderUtil.loadClass(
|
||||
mainClass,
|
||||
JavaPlugin.class,
|
||||
classLoader
|
||||
);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1 @@
|
||||
org.patchbukkit.loader.PatchBukkitPluginLoader
|
||||
@ -150,13 +150,9 @@ impl PluginManager {
|
||||
}
|
||||
|
||||
pub fn load_all_plugins(&mut self, jvm: &Jvm) -> Result<()> {
|
||||
let loader = jvm.create_instance(
|
||||
"org.patchbukkit.loader.PluginLoader",
|
||||
InvocationArg::empty(),
|
||||
)?;
|
||||
for plugin in &mut self.plugins {
|
||||
let result = jvm.invoke(
|
||||
&loader,
|
||||
let result = jvm.invoke_static(
|
||||
"org.patchbukkit.loader.PatchBukkitPluginLoader",
|
||||
"createPlugin",
|
||||
&[
|
||||
InvocationArg::try_from(&plugin.path.to_string_lossy().to_string())?,
|
||||
|
||||
Loading…
Reference in New Issue
Block a user