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?
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; }
Code (Text): JavaPlugin.getProvidingPlugin(main.class) Code (Text): new BukkitRunnable() { public void run() { cancel(); } }.runTaskLater(JavaPlugin.getProvidingPlugin(main.class), delay);
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.
Yeah sure, but not giving some code is different from giving bad code. User called this.plugin when plugin is public static.
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; } }
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).
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); }
U know java always filters this bullshit? naming it main doesn't matter. YES its better to not name a class main. but it doesn't really effect anything...