1.15.2 Executing a method a certain time

Discussion in 'Spigot Plugin Development' started by Flaquito, Feb 14, 2020 at 6:18 PM.

  1. Hello guys

    I wonder why this code no works. I found it and try to put working but, no work.

    Code (Java):
    public void datetime() {
            DateFormat dateFormatter = new SimpleDateFormat("hh:mm:ss");
            Timer timer = new Timer();
            try {
                Date date = dateFormatter .parse("02:15:00");
                timer.schedule(new toExecute(), date);
               
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
       
        private static class toExecute extends TimerTask {
            public void run() {
                Bukkit.broadcastMessage("Ha comenzado");
                //worldinteractive.event_0_1();
                cancel();
            }
        }
    What do I have to do?
     
  2. The parameter is of type long and it’s the amount of milliseconds before the task is executed.
     
  3. Don't use the timer and task API that comes with Java. Instead, use Bukkit's scheduler and/or a BukkitRunnable, like so:
    Code (Java):
    new BukkitRunnable() {
        @Override
        public void run() {
            // do stuff
        }
    }.runTaskLater(plugin, wait);
    Doesn't necessarily have to be #runTaskLater, of course - if you want to repeat the task, you'd use #runTaskTimer, or #runTask if you just want it to run once.

    On an unrelated note though, class names should be written in PascalCase rather than camelCase (i.e. toExecute should be ToExecute, or better yet, something that describes its purpose).
     
  4. The problem with the code above is I want to execute the method "toExecute" at date I let before. But, when I run that nothing happen. When I reload the server the method "toExecute" executes and I don't know why, 'cause the code has to execute when the time is, no before.
     
  5. Well I already explained the reason. The schedule method only allows you to input a value of type “long” and not Date. The task only runs after some value of milliseconds instead of running at a specific date.
     
    #5 Plochem, Feb 15, 2020 at 7:23 PM
    Last edited: Feb 15, 2020 at 7:40 PM
  6. So, how can I fix it?
     
  7. I was checking the code and I found that the parameters that I have on "timer.schedule(new toExecute(), date);" is for "Date", no "Long".
     
  8. Yeah, so that's why it doesn't work. You're putting in the wrong parameters.

    Calculate the number of miliiseconds between a target Date and the current Date. That's the number for the 2nd parameter.
     
  9. It's a little bit complicated. Is there another way to execute a method in a exact time? 'Cause if I do what you mean it's gonna be so lazy to do every time I want to execute differents commands or methods with code.
     
  10. Check out LocalDate. Use LocalDate#of(year, month, day) to create the object with the correct parameters. Use LocalDate#until(Temporal, TemporalUnit) to get the difference between the 2 dates in the given time unit. Use LocalDate#now() to get current date.