1.12.2 How do i create a method that makes delayed tasks more simple

Discussion in 'Spigot Plugin Development' started by FlameFOxYT, Feb 12, 2020.

  1. public static void set(Object object, long time) {
    Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
    public void run() {
    Object arguments;
    arguments = object;

    }}, time);
    }
    but when i use
    set(sender.sendMessage("test"), 10l);
    it sends an error
     
  2. thats a noble try. you cant pass a method call as an argument, thats simply not how it works. change your object argument to a runnable, and pass the runnable instead, then when you use set, you can use
    Code (Java):
    set(() -> sender.sendMessage("test"), 10L);
    /e
    Code (Text):
    () -> sender...
    is a lambda construction of a runnable. this is the same as doing new Runnable() { run() { ... } }
    then instead of creating a runnable in your set method, just use this runnable that youre passing as an argument
     
    • Like Like x 1
  3. You need to make that method for a certain use, e.g. sending a message
    replace object with a string and it will send a message later
    As said above you can’t put call a method in an argument so you will have to do it this way or make a runnable each time
     
  4. You cannot pass a void method as an argument.