Multiple classes extends JavaPlugin

Discussion in 'Spigot Plugin Development' started by DanHraje770, Jun 2, 2017.

  1. Hello,

    I have one problem. I created Main.java and OtherClass.java. I want extends JavaPlugin in OtherClass.java. How to extends in OtherClass.java?
     
  2. Why? What are you trying to do?

    (You only make your main class extend JavaPlugin, no other)
     
    • Agree Agree x 1
  3. "public class OtherClass extends JavaPlugin"
     
    • Optimistic Optimistic x 2
    • Like Like x 1
    • Funny Funny x 1
  4. You have to create an instance of the class what extends JavaPlugin. Get the instance then from OtherClass.java
     
  5. XY Problem, what are you actually trying to accomplish? @DanHraje770
     
    • Agree Agree x 1
  6. How would having two JavaPlugin classes protect your plugin?
     
  7. You other class seems to be a SQLHandler of sorts, I would just rename it to SQLHandler and use it from the normal Main class
     
  8. This is not the place for spoon-feeding :)
     
  9. Ok, and how to extends Java Plugin in class AntiLeak?
     
  10. I extends JavaPlugin in AntiLeak. Error code in console:
    Code (Text):
    [21:06:14 ERROR]: Error occurred while enabling Annihilation v2.1 (Is it up to date?)
    java.lang.IllegalArgumentException: Plugin already initialized!
            at org.bukkit.plugin.java.PluginClassLoader.initialize(PluginClassLoader.java:122) ~[server.jar:git-Spigot-21fe707-e1ebe52]
            at org.bukkit.plugin.java.JavaPlugin.<init>(JavaPlugin.java:66) ~[server.jar:git-Spigot-21fe707-e1ebe52]
            at cz._HeroPwP.Annihilation.AntiLeak.<init>(AntiLeak.java:11) ~[?:?]
            at cz._HeroPwP.Annihilation.Main.onEnable(Main.java:178) ~[?:?]
            at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:321) ~[server.jar:git-Spigot-21fe707-e1ebe52]
            at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:340) [server.jar:git-Spigot-21fe707-e1ebe52]
            at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:405) [server.jar:git-Spigot-21fe707-e1ebe52]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.loadPlugin(CraftServer.java:357) [server.jar:git-Spigot-21fe707-e1ebe52]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.enablePlugins(CraftServer.java:317) [server.jar:git-Spigot-21fe707-e1ebe52]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.reload(CraftServer.java:741) [server.jar:git-Spigot-21fe707-e1ebe52]
            at org.bukkit.Bukkit.reload(Bukkit.java:535) [server.jar:git-Spigot-21fe707-e1ebe52]
            at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:25) [server.jar:git-Spigot-21fe707-e1ebe52]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) [server.jar:git-Spigot-21fe707-e1ebe52]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:641) [server.jar:git-Spigot-21fe707-e1ebe52]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchServerCommand(CraftServer.java:627) [server.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.DedicatedServer.aO(DedicatedServer.java:412) [server.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:375) [server.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [server.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [server.jar:git-Spigot-21fe707-e1ebe52]
            at java.lang.Thread.run(Thread.java:748) [?:1.8.0_131]
    Caused by: java.lang.IllegalStateException: Initial initialization
            at org.bukkit.plugin.java.PluginClassLoader.initialize(PluginClassLoader.java:125) ~[server.jar:git-Spigot-21fe707-e1ebe52]
            at org.bukkit.plugin.java.JavaPlugin.<init>(JavaPlugin.java:66) ~[server.jar:git-Spigot-21fe707-e1ebe52]
            at cz._HeroPwP.Annihilation.Main.<init>(Main.java:78) ~[?:?]
            at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[?:1.8.0_131]
            at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[?:1.8.0_131]
            at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[?:1.8.0_131]
            at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[?:1.8.0_131]
            at java.lang.Class.newInstance(Class.java:442) ~[?:1.8.0_131]
            at org.bukkit.plugin.java.PluginClassLoader.<init>(PluginClassLoader.java:76) ~[server.jar:git-Spigot-21fe707-e1ebe52]
            at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:131) ~[server.jar:git-Spigot-21fe707-e1ebe52]
            at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:329) ~[server.jar:git-Spigot-21fe707-e1ebe52]
            at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:251) ~[server.jar:git-Spigot-21fe707-e1ebe52]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.loadPlugins(CraftServer.java:292) ~[server.jar:git-Spigot-21fe707-e1ebe52]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.reload(CraftServer.java:739) ~[server.jar:git-Spigot-21fe707-e1ebe52]
            ... 10 more
     
     
  11. If you want to do something with the plugin class pass it as one of the parameters in the constructor of the other class, don't try to extend JavaPlugin in the other class.
     
  12. How to use constructor for extend JavaPlugin?
     
  13. Code (Java):
    private Main plugin;

    public OtherClass(Main plugin)
    {
        this.plugin = plugin
    }
    Then you use the plugin field instead of trying to extend JavaPlugin.

    If this doesn't help you then you have to get a better understanding of Java before doing this.
     
    • Like Like x 1
  14. Apready said it but I'll just got ignored...
     
  15. Mas

    Mas

    Learn how classes, objects and constructors work. Ideally you should know Java basics before starting to work with the Spigot API.
    Only your plugin's main class can extend JavaPlugin. To use methods inherited from JavaPlugin in your main class, you will need to pass an instance of your main class to the constructor of your AnitiLeak class, as statede by others.
    https://docs.oracle.com/javase/tutorial/java/javaOO/
    http://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html
     
    #19 Mas, Jun 3, 2017
    Last edited by a moderator: Jun 4, 2017
  16. md_5

    Administrator Developer

    Removed links to code because OP included passwords in them.... You should be more careful
     
    • Friendly Friendly x 4
    • Like Like x 3
    • Informative Informative x 1