Solved Getting JavaPlugin from other class doesn't work?

Discussion in 'Spigot Plugin Development' started by ultracakebakery, May 30, 2016.

  1. Hello.
    I have a main class where i do the onEnable stuff and i have other classes (playerManager, databaseManager, listeners etc) and now i want to make a BukkitRunnable (delayed task) and for that i need the JavaPlugin.

    I have this code in the main to get the JavaPlugin:
    Code (Text):

    public static main plugin = (main)Bukkit.getPluginManager().getPlugin("main");

    public class main extends JavaPlugin {

    }

     
    And this for the runnable:
    Code (Text):

            new BukkitRunnable() {
                public void run() {
                    cancel();
                }
            }.runTaskLater(main.plugin, delay);

     
    Error:
    Code (Text):

    30.05 17:01:03 [Server] INFO at com.ultracakebakery.KnightCore.managers.playerManager.onPlayerInteract(playerManager.java:65) ~[?:?]
    30.05 17:01:03 [Server] INFO at com.ultracakebakery.KnightCore.managers.playerManager.activateCrate(playerManager.java:80) ~[?:?]
    30.05 17:01:03 [Server] INFO at com.ultracakebakery.KnightCore.managers.playerManager.crateStartInventory(playerManager.java:86) ~[?:?]
    30.05 17:01:03 [Server] INFO at com.ultracakebakery.KnightCore.managers.playerManager.crateStartFrame(playerManager.java:110) ~[?:?]
     30.05 17:01:03 [Server] INFO at org.bukkit.scheduler.BukkitRunnable.runTaskLater(BukkitRunnable.java:64) ~[custom.jar:git-Spigot-e6f93f4-935f18b]
     30.05 17:01:03 [Server] INFO at org.bukkit.craftbukkit.v1_9_R1.scheduler.CraftScheduler.runTaskLater(CraftScheduler.java:106) ~[custom.jar:git-Spigot-e6f93f4-935f18b]
     
    But that doesn't work and i don't know a other way.

    Do u guys have any tips to make get this to work / a other way around?
     
  2. My English is not on-point today, sorry for that.
     
  3. Maybe this line is executed too early:
    Code (Text):
    public static main plugin = (main)Bukkit.getPluginManager().getPlugin("main");
    Try something like this in your onEnable method:
    Code (Text):
    onEnable() {
      this.plugin = this;
    }
     
  4. Code (Text):
    JavaPlugin.getProvidingPlugin(main.class)


    Code (Text):
     
    new BukkitRunnable() {
      public void run() {
        cancel();
       }
    }.runTaskLater(JavaPlugin.getProvidingPlugin(main.class), delay);
     
    • Agree Agree x 2
  5. Sorry for the late reaction, but it works like a charm. thanks m8!
     
  6. That's incredibly slow in comparison to using good design like dependency injection. That forces a map lookup.

    Edit: Doesn't cause a map lookup anymore. Doesn't forsake design principles however.
     
    #6 1Rogue, May 30, 2016
    Last edited: May 31, 2016
    • Agree Agree x 2
  7. Wouldn't compile...
     
  8. It's not meant to, it's an example.
     
  9. Yeah sure, but not giving some code is different from giving bad code. User called this.plugin when plugin is public static.
     
  10. That is not how you are suppose to handel things!!!!! *shivers*

    Either do what @1Rogue said or pass the reference in a constructutor which I recommend.

    Code (Text):
    onEnable() {
        new Time(this);
    }

    public class Time {
        private final Plugin plugin;

        public Time(Plugin plugin) {
            this.plugin = plugin;
        }
    }
     
    • Agree Agree x 1
  11. I think that you should see the implementation.
     
  12. Implementation
    [​IMG]

    Perf test

    [​IMG]
     
  13. Read that wrong...

    Yes you can do that too I use that all the time :p
     
  14. Ah nice, that's smarter than what it used to be.
     
  15. Whats the problem with it? That's a normal singleton design and would compile perfectly fine (of course a getter method would be more appropriate rather than making it public).
     
  16. Code (Java):
    private static Main instance = null;
    public void onEnable() {
       instance = this;
    }

    public static Main getInstance() {
    return instance;
    }
    Code (Java):
    new BukkitRunnable() {
      public void run() {
        cancel();
       }
    }.runTaskLater(Main.getInstance(), delay);
    or

    Code (Java):
    // Main class
    public void onEnable() {
      OtherClass class = new OtherClass(this);
    }
    Code (Java):
    public class OtherClass {
      private Main plugin;
      public OtherClass(Main main) {
       plugin = main;
      }

        new BukkitRunnable() {
        public void run() {
         cancel();
        }
      }.runTaskLater(plugin, delay);

    }
     
  17. Ah, Spigot's favourite design pattern.
     
  18. It would, it's just a bit silly to be accessing a static variable using this.