0

I have heard of ways to run code later like run task later and other scheduler methods but I want to do it inside of an event which I am having trouble with. Here is some code for an example of where I want to run it:

public class InventoryEvents implements Listener {
    @EventHandler
    public void onOpen(InventoryOpenEvent e) {
        // Run later code here
    }
}

When I try using code for something like run task later here or outside of it or even in another class I get a lot of errors relating to the class not extending JavaPlugin or me trying to use it in an event.

Any help is appreciated, thanks :)

Crafty
  • 11
  • 5

1 Answers1

0

You will need to create your scheduled task inside the onOpen method that's handling the InventoryOpenEvent.

Bukkit.getScheduler().runTaskLater(main, new Runnable() {
    @Override
    public void run() {
        //Run your delayed code in here
    }
}, 100L);//replace 100 with how many ticks you want to wait before the code executes

The main is where your errors are coming from. You need to use an instance of whatever class extends JavaPlugin (this would be your main class). I recommend passing the instance of your main into the Listener class in the constructor so you can use it as needed. An example would be as follows:

public class NamedListener implements Listener {
    private Main main;
    public NamedListener(Main main){
        this.main = main;
    }
}
Blue Dev
  • 142
  • 8