#Need help with reflection and spigot and addons
1 messages · Page 1 of 1 (latest)
And heres the addonmanager class ```java
package org.eternitystudios.plugins.cloud.utils;
public class AddonManager {
Cloud instance;
public AddonManager(Cloud instance) {
this.instance = instance;
}
public void registerAddons() {
File [] files = instance.af.listFiles();
for (File file : files) {
if (file.isFile()) { // this line weeds out other directories/folders
if (file.getName().endsWith(".jar")) {
registerJAR(file);
}
}
}
}
public void registerJAR(File jar) {
try {
URLClassLoader loader = new URLClassLoader(new URL[] { jar.toURI().toURL() },
Cloud.class.getClassLoader());
Class classToLoad = Class.forName("org.cloud.addon.Addon", true, loader);
System.out.println(classToLoad);
Class<?>[] type = {Cloud.class};
Object instance = classToLoad.newInstance();
Constructor<?> cons = classToLoad.getConstructor(type);
Object newInstanceObj = cons.newInstance(instance);
Method method = classToLoad.getDeclaredMethod("addonMain");
Object result = method.invoke(instance);
} catch (MalformedURLException | ClassNotFoundException | NoSuchMethodException | SecurityException
| InstantiationException | IllegalAccessException | IllegalArgumentException
| InvocationTargetException e) {
// TODO Auto-generated catch block
if (instance.getConfig().getBoolean("debugMode")) e.printStackTrace();
Bukkit.getLogger().warning("Could not load addon " + jar.getName());
}
}
}
package org.eternitystudios.plugins.cloud;
import org.bukkit.Server;
import org.bukkit.plugin.java.JavaPlugin;
import org.eternitystudios.plugins.cloud.utils.AddonManager;
import java.io.File;
public final class Cloud extends JavaPlugin {
public File af;
public AddonManager addonManager;
public Server server;
public Cloud() {
}
@Override
public void onEnable() {
// Set server
server = getServer();
// Save config
saveDefaultConfig();
// Instantiate addonManager
addonManager = new AddonManager(this);
// Save addons folder if It's not there.
String configPath = getDataFolder().getAbsolutePath();
File addonFolder = new File(configPath);
af = addonFolder;
if (!addonFolder.exists()) addonFolder.mkdir();
// Start searching for addons
addonManager.registerAddons();
}
@Override
public void onDisable() {
// Plugin shutdown logic
}
}
``` this is the main plugin
on the addon, i cant access any of the server functions, how can i fix this?
im using the server var on the instance
What happens when you try
look
Also use a paste site
your constructor is wonky
'''java
public Addon(Cloud instance) {
this.instance = instance;
}
'''
it should work tho
you need to get the plugin somehow
ye
you are just doing a weird way of setting it to itself, or null;
so all your server functions are null
heres the addonmanager
thats doing the reflection if that can help me figure it out
@oblique pilot
what I do is use a singleton in the main class
its different tho
the addon is a whole different jar
thats a public static reference to Main accessable from main
and the instance is being feeded through reflection
I'm doing a terrible job explaining
i can try that tho
ill find the function
okay
how do I get the code blocks in discord?
Cloud instance;
public Addon(Cloud instance) {
this.instance = JavaPlugin.getPlugin(Cloud.class);
}
declaration: package: org.bukkit.plugin.java, class: JavaPlugin
you just never initialize instance properly, all the other code I see as unaffected
thing is
i dont have javaplugin
spigot isnt added in the addon
and i dont know if it can be
I was expecting that
You're trying to access the no args constructor which doesn't exist
oh, i needa no args constructer?
No you should just use the one you created
wdym
And not use the no args one
In my main class I just make a public static JavaPlugin plugin.
In onEneable you set plugin = this
Then in any addon code you can statically call
Cloud.plugin.serverFunctionYouWant
Cloud.plugin.getServer()... actually
If you're going to do that at least use a getInstance method
Anyways di is the best approach
okay
ive used singletons before
and i was
im not a beginner
it just wasnt working for me
I realize, but you missed initializing a varibale
but javaplugin prob will
then when you call varible.function, the functions are not working
hopefully that will work
@oblique pilotproblem
javaplugin isnt a thing in the addon
cause it not exactly using spigot
Your plugin instance is the JavaPlugin
yeah ik
package org.cloud.addon;
import org.eternitystudios.plugins.cloud.Cloud;
public class Addon {
JavaPlugin instance;
public Addon(JavaPlugin instance) {
this.instance = instance;
}
public void addonMain() {
}
}
``` in the addon project
javaplugin is not a class
Import spigot to your project
too late for that
You can always migrate your project
in intelji? how?
I'd create a new project and move everything over