Getting the main class/JavaPlugin in other classes

Discussion in 'Plugin Development' started by danielmiles, Mar 12, 2013.

Thread Status:
Not open for further replies.
  1. Offline


    is there a way to make an instance of your plugin in or main class inside of a different class that IS NOT this..
            private main plugin;
        public CommandHi(main plugin) {
            this.plugin = plugin;
    because then the class requires an instance of the plugin again to call any methods/ functions inside of it..
    and this doesnt work either gives a StackOverflow
    private main m = new main()
    bassically the reason i want this is i have a class that stores a lot of functions and objects that is called a lot in other classes, and i want to run a bukkitRunnable which requires "JavaPlugin" as a param. pls help :)
  2. Offline


    My two cents:
    Just keep it all in the main class when you're new to java.

    Also, why wouldn't you want to use a constructor for the purpose it was designed for?
  3. Offline


    well i dont want to use constructer like that because then when i make an instance of the class somewhere else ill need the param JavaPlugin, which in turn means that the class im making the instance in turn needs to have the the plugin in its constructer meaning i need JavaPlugin its params whenever i call it... Do you use a constructer with the instance of the plugin in all of your classes?

    edit: btw i dont need any methods from the main class just the instance of it
  4. Offline


    hello try this,

    main class:
    public class myplugin extends JavaPlugin {
        Logger log = Logger.getLogger("Minecraft");
      private myEvent event = new myEvent(this); //MyEvent represents MyEvent.class
        public void onEnable() {
  "[myplugin] has been enabled");
            this.getServer().getPluginManager().RegisterEvents(event, this);
        public void onDisable() {
  "[myplugin] has been disabled!");
    myEvent.class (with Constructor)

    public class myEvent implements Listener {
          myplugin plugin;
          public myEvent(myplugin instance) {
                this.plugin = instance; //now you can access plugin
        public void myTestEvent(callyourevent p) {
    now you can access the JavaPlugin into your myEvent.class to write configs as example the instances in myEvent is basicly a constructor.

    also another way I found to hooking other classes outside your plugin is this:
    private GeoIPLookup getGeoIPLookup() {
        Plugin pl = plugin.getServer().getPluginManager().getPlugin("GeoIPTools");
        if(pl != null) {
            return ((GeoIPTools) pl).getGeoIPLookup();
        } else {
            return null;
  5. Offline


    is there a way to make an instance of your plugin in or main class inside of a different class that IS NOT this..
    private main plugin;
    public CommandHi(main plugin) {
    this.plugin = plugin;

  6. Offline


    Why do you need the instance if you're not going to use any methods from it?
  7. Offline

    Sagacious_Zed Bukkit Docs

    I think he just wants to keep passing the reference down until he can construct a BukkitRunnable, which requires a reference to a plugin to schedule.
  8. Offline


    Ok... but that still needs methods from the main class...
  9. Offline

    Sagacious_Zed Bukkit Docs

    not really, you just need the instance. since a BukkitRunnable can schedule itself given the instance of your plugin.
  10. Offline


    exactly what i need it for -.- is it not posibble without using a constructer in pretty much all of my class as a result?

    i may have found a way to work it out in a method of my liking unfortunately i get an error for some odd reasons.. here is my stack trace
    [SEVERE] Could not pass event PlayerMoveEvent to TestGrounds v1.00.0
        at org.bukkit.plugin.RegisteredListener.callEvent(
        at org.bukkit.plugin.SimplePluginManager.fireEvent(
        at org.bukkit.plugin.SimplePluginManager.callEvent(
        at net.minecraft.server.v1_4_R1.PlayerConnection.a(
        at net.minecraft.server.v1_4_R1.Packet10Flying.handle(SourceFile:136)
        at net.minecraft.server.v1_4_R1.NetworkManager.b(
        at net.minecraft.server.v1_4_R1.PlayerConnection.d(
        at net.minecraft.server.v1_4_R1.ServerConnection.b(SourceFile:39)
        at net.minecraft.server.v1_4_R1.DedicatedServerConnection.b(SourceFile:30)
        at net.minecraft.server.v1_4_R1.MinecraftServer.r(
        at net.minecraft.server.v1_4_R1.DedicatedServer.r(
        at net.minecraft.server.v1_4_R1.MinecraftServer.q(
    Caused by: java.lang.IllegalArgumentException: Plugin cannot be null
        at org.apache.commons.lang.Validate.notNull(
        at org.bukkit.craftbukkit.v1_4_R1.scheduler.CraftScheduler.validate(
        at org.bukkit.craftbukkit.v1_4_R1.scheduler.CraftScheduler.runTaskTimer(
        at me.desty.plugin.Timer.resumeit(
        at me.desty.plugin.PlayerManager.PlayerFell(
        at me.desty.plugin.EntityDamageEvents.PlayerMoved(
        at sun.reflect.GeneratedMethodAccessor11.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        ... 14 more
        private TestGrounds plugin;
        private int id = -1;
        public Timer(TestGrounds instance) {
            plugin = instance;
    //line 54 \/
    id = Bukkit.getServer().getScheduler().runTaskTimer(plugin, this, 0L, 40L).getTaskId();
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
    Last edited by a moderator: May 31, 2016
  11. Offline


    You need to schedule it after the constructor runs
  12. Offline


    After much pain and sweat, I finally came up with a result in my modding and customizations...
    NOTE: Does not use what you specified but it's the only solution I've found to work, and works quite well. I'm actually using a separate plugin to do my SQL stuff

    First, add the plugin to the build path, whether that be included when compiling or finds it in the plugins/ folder. -- this is how I prefer, to save some space.

    You then import this in your code
    import net.aptitech.lordaragos.ChildPlugin
    Then grab it from the server and cast it to
    Plugin emptyplugin = this.getServer().getPluginManager().getPlugin("ChildPlugin");
    ChildPlugin cpplugin = (ChildPlugin) emptyplugin;
    If Bukkit cannot find the server specified, it will return null; be sure to do a null check. -- I actually prefer to shutdown the ParentPlugin if it can't find it just for continuity-sake.

    You then can do however you desire
    cpplugin.getCommandManager().executeCommand("slap", this.targetplayer);

    Hope this helps,
  13. Offline


    By far worst advice: keep everything in one class

    No, organize everything
  14. Offline


    If you have one listener and only one command, there is no need to make 3 different classes for a beginner.
  15. Offline


    This simplest way I found to do this is to do+
    private static <classname> plugin = (<classname>)Bukkit.getPluginManager().getPlugin("<classname>");
    You probably don't need it anymore but I thought I would put it there for anyone else's future reference.
    shano_dekono and sgavster like this.
Thread Status:
Not open for further replies.

Share This Page