Solved Help with Bukkit Runnables.

Discussion in 'Spigot Plugin Development' started by Orangemaster123, May 28, 2017.

  1. Im trying to create a bukkit runnable which will wait 5 seconds until changing the block material, but i dont really know how to use the runnables. Here's what i've tried so far but it didn't work.

    Code (Text):


    scheduler.scheduleSyncDelayedTask((Plugin) this, new Runnable() {

                @Override

                public void run() {

                    loc.getBlock().setType(blocktype);

                }

            }, 100L);
     
    Can someone tell me how to properly use the Bukkit Runnables?
     
  2. you're on the right track, however, you can't blindly cast Plugin to the class its within. you need to pass the instance of your main class to the plugin paramater in the bukkitscheduler.

    Code (Java):

    public class Main extends JavaPlugin {

         public static Main get() {
              return JavaPlugin.getPlugin(Main.class);
         }
    }

    public class WithRunnable {

         private void myMethod() {
               Bukkit.getScheduler().scheduleSyncDelayedTask(Main.get(), () -> loc.getBlock().setType(blocktype), 60); //i used a lambda expression here, you can use the runnable as well. you can read more on why to use lambdas on several guides and tutorials.
         }
    }
     
    • Like Like x 1
  3. Thanks for the help, it worked. :)