Delay While Loop for 5 mins without stopping execution of plugin

Discussion in 'Plugin Development' started by Clickau, Aug 16, 2017.

Thread Status:
Not open for further replies.
  1. Offline

    Clickau

    So currently I'm trying to make a spigot plugin to run commands based on system time (on Monday for example). I have this method which checks if the current day is the one specified in the config.yml file. But I only want to check the time every 5 mins or so, without pausing the entire plugin/server. How can I do that.

    Code:
    Code:
    private void type1(String commName)
        {
            while (true)
            {
                Calendar calendar = Calendar.getInstance();
                calendar.setTimeInMillis(System.currentTimeMillis());
                int currDay = calendar.get(Calendar.DAY_OF_WEEK);
                String day = getConfig().getString("commands." + commName + ".day");
                String[] correspondents = {"a", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
                if (Arrays.asList(correspondents).indexOf(day) == currDay)
                {
                    getServer().dispatchCommand(getServer().getConsoleSender(), getConfig().getString("commands." + commName + ".command"));
                }
            }
        }
     
  2. Offline

    timtower Administrator Administrator Moderator

    @Clickau Put it in a BukkitRunnable and run it every 5 minutes.
     
Thread Status:
Not open for further replies.

Share This Page