001package net.tnemc.plugincore.bukkit.impl.scheduler;
002/*
003 * The New Plugin Core
004 * Copyright (C) 2022 - 2024 Daniel "creatorfromhell" Vidmar
005 *
006 * This program is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU Affero General Public License as published by
008 * the Free Software Foundation, either version 3 of the License, or
009 * (at your option) any later version.
010 *
011 * This program is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014 * GNU Affero General Public License for more details.
015 *
016 * You should have received a copy of the GNU Affero General Public License
017 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
018 */
019import net.tnemc.plugincore.bukkit.BukkitPluginCore;
020import net.tnemc.plugincore.core.compatibility.scheduler.Chore;
021import net.tnemc.plugincore.core.compatibility.scheduler.ChoreExecution;
022import net.tnemc.plugincore.core.compatibility.scheduler.ChoreTime;
023import net.tnemc.plugincore.core.compatibility.scheduler.SchedulerProvider;
024import org.bukkit.Bukkit;
025
026/**
027 * BukkitScheduler
028 *
029 * @author creatorfromhell
030 * @since 0.1.2.0
031 */
032public class BukkitScheduler extends SchedulerProvider<BukkitChore> {
033
034  /**
035   * Used to create a task, which will execute after the specified delay.
036   *
037   * @param task  The task to run.
038   * @param delay The delay, in ticks.
039   * @param environment The execution environment for the task.
040   */
041  @Override
042  public void createDelayedTask(Runnable task, ChoreTime delay, ChoreExecution environment) {
043    if(environment.equals(ChoreExecution.MAIN_THREAD)) {
044      Bukkit.getScheduler().runTaskLater(BukkitPluginCore.instance().getPlugin(), task, delay.asTicks());
045      return;
046    }
047    Bukkit.getScheduler().runTaskLaterAsynchronously(BukkitPluginCore.instance().getPlugin(), task, delay.asTicks());
048  }
049
050  /**
051   * Used to create a task, which repeats after a specified period.
052   *
053   * @param task The task to run.
054   * @param delay The delay to run the task, in ticks.
055   * @param period The period to run the task.
056   * @param environment The execution environment for the task.
057   *
058   * @return The associated {@link Chore} with this task.
059   */
060  @Override
061  public BukkitChore createRepeatingTask(Runnable task, ChoreTime delay, ChoreTime period, ChoreExecution environment) {
062    if(environment.equals(ChoreExecution.MAIN_THREAD)) {
063      return new BukkitChore(Bukkit.getScheduler().runTaskTimer(BukkitPluginCore.instance().getPlugin(), task, delay.asTicks(), period.asTicks()), environment);
064    }
065    return new BukkitChore(Bukkit.getScheduler().runTaskTimerAsynchronously(BukkitPluginCore.instance().getPlugin(), task, delay.asTicks(), period.asTicks()), environment);
066  }
067}