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

    danielmiles

    is there a way to make an instance of your plugin in or main class inside of a different class that IS NOT this..
    Code:
            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
    Code:
    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

    gomeow

    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

    danielmiles

    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

    xize

    hello try this,

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

    Code:
    public class myEvent implements Listener {
          myplugin plugin;
          public myEvent(myplugin instance) {
                this.plugin = instance; //now you can access plugin
        }
        @EventHandler
        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:
    Code:
    private GeoIPLookup getGeoIPLookup() {
        Plugin pl = plugin.getServer().getPluginManager().getPlugin("GeoIPTools");
        if(pl != null) {
            return ((GeoIPTools) pl).getGeoIPLookup();
        } else {
            return null;
        }
    
     
  5. Offline

    danielmiles


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

    }
     
  6. Offline

    gomeow

    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

    gomeow

    Sagacious_Zed
    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

    danielmiles

    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
    Code:
    [SEVERE] Could not pass event PlayerMoveEvent to TestGrounds v1.00.0
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:427)
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62)
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:477)
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:462)
        at net.minecraft.server.v1_4_R1.PlayerConnection.a(PlayerConnection.java:220)
        at net.minecraft.server.v1_4_R1.Packet10Flying.handle(SourceFile:136)
        at net.minecraft.server.v1_4_R1.NetworkManager.b(NetworkManager.java:290)
        at net.minecraft.server.v1_4_R1.PlayerConnection.d(PlayerConnection.java:113)
        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(MinecraftServer.java:598)
        at net.minecraft.server.v1_4_R1.DedicatedServer.r(DedicatedServer.java:224)
        at net.minecraft.server.v1_4_R1.MinecraftServer.q(MinecraftServer.java:494)
        at net.minecraft.server.v1_4_R1.MinecraftServer.run(MinecraftServer.java:427)
        at net.minecraft.server.v1_4_R1.ThreadServerApplication.run(SourceFile:849)
    Caused by: java.lang.IllegalArgumentException: Plugin cannot be null
        at org.apache.commons.lang.Validate.notNull(Validate.java:203)
        at org.bukkit.craftbukkit.v1_4_R1.scheduler.CraftScheduler.validate(CraftScheduler.java:390)
        at org.bukkit.craftbukkit.v1_4_R1.scheduler.CraftScheduler.runTaskTimer(CraftScheduler.java:119)
        at me.desty.plugin.Timer.resumeit(Timer.java:35)
        at me.desty.plugin.PlayerManager.PlayerFell(PlayerManager.java:42)
        at me.desty.plugin.EntityDamageEvents.PlayerMoved(EntityDamageEvents.java:28)
        at sun.reflect.GeneratedMethodAccessor11.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:425)
        ... 14 more
    Timer.java:35
    Code:
        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();
    PlayerManager.java:42
    Code:
                timer.resumeit();
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  11. Offline

    gomeow

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

    lordaragos

    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
    Code:
    import net.aptitech.lordaragos.ChildPlugin
    Then grab it from the server and cast it to
    Code:
    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
    Code:
    cpplugin.getCommandManager().executeCommand("slap", this.targetplayer);

    Hope this helps,
     
  13. Offline

    Tehmaker

    By far worst advice: keep everything in one class

    No, organize everything
     
  14. Offline

    HackintoshMan

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

    firecombat4

    This simplest way I found to do this is to do+
    Code:
    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