#Need help with reflection and spigot and addons

1 messages · Page 1 of 1 (latest)

rotund hearth
#

hey so heres my code for the addon

package org.cloud.addon;

import org.eternitystudios.plugins.cloud.Cloud;

public class Addon {

    Cloud instance;

    public Addon(Cloud instance) {
        this.instance = instance;
    }

    public void addonMain() {
        instance.server
    }
}
#

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

rapid pendant
rotund hearth
rapid pendant
#

Also use a paste site

rotund hearth
#

okay

#

the server var is not a variable

#

@rapid pendant

rapid pendant
#

So uh why do you need that

#

Just use Bukkit#<what you need>

rotund hearth
#

its not a javaplugin tho

#

its another class

#

that im loading using reflection

oblique pilot
#

your constructor is wonky
'''java
public Addon(Cloud instance) {
this.instance = instance;
}
'''

oblique pilot
#

you need to get the plugin somehow

rotund hearth
#

ye

oblique pilot
#

you are just doing a weird way of setting it to itself, or null;

#

so all your server functions are null

rotund hearth
#

heres the addonmanager

#

thats doing the reflection if that can help me figure it out

#

@oblique pilot

oblique pilot
#

what I do is use a singleton in the main class

rotund hearth
#

the addon is a whole different jar

oblique pilot
#

thats a public static reference to Main accessable from main

rotund hearth
#

and the instance is being feeded through reflection

oblique pilot
#

I'm doing a terrible job explaining

rotund hearth
#

i can try that tho

oblique pilot
#

ill find the function

rotund hearth
#

okay

oblique pilot
#

how do I get the code blocks in discord?

#

you just never initialize instance properly, all the other code I see as unaffected

rotund hearth
#

thing is

#

i dont have javaplugin

#

spigot isnt added in the addon

#

and i dont know if it can be

rapid pendant
#

?

#

Just do what ForbiddenSoul said

rotund hearth
#

@oblique pilotokay

#

so now im getting an instantsionerror

rapid pendant
#

I was expecting that

#

You're trying to access the no args constructor which doesn't exist

rotund hearth
#

oh, i needa no args constructer?

rapid pendant
#

No you should just use the one you created

rotund hearth
#

wdym

rapid pendant
#

And not use the no args one

rotund hearth
#

@rapid pendantwdym

#

did i just fix it lol

oblique pilot
#

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

rapid pendant
#

If you're going to do that at least use a getInstance method

#

Anyways di is the best approach

rotund hearth
#

okay

rotund hearth
#

ive used singletons before

#

and i was

#

im not a beginner

#

it just wasnt working for me

oblique pilot
#

I realize, but you missed initializing a varibale

rotund hearth
#

but javaplugin prob will

oblique pilot
#

then when you call varible.function, the functions are not working

rotund hearth
#

hopefully that will work

#

@oblique pilotproblem

#

javaplugin isnt a thing in the addon

#

cause it not exactly using spigot

rapid pendant
#

Your plugin instance is the JavaPlugin

rotund hearth
#
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

rapid pendant
#

Import spigot to your project

rotund hearth
#

i dont know where to download the jar to add as libary

#

library

rapid pendant
#

Use maven or gradle

#

Adding jars directly is a bad idea

rotund hearth
#

too late for that

rapid pendant
#

You can always migrate your project

rotund hearth
#

in intelji? how?

rapid pendant
#

I'd create a new project and move everything over