001package net.tnemc.plugincore.paper.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 */
019
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 net.tnemc.plugincore.paper.PaperPluginCore;
025import org.bukkit.Bukkit;
026
027/**
028 * BukkitScheduler
029 *
030 * @author creatorfromhell
031 * @since 0.1.2.0
032 */
033public class PaperScheduler extends SchedulerProvider<PaperChore> {
034
035  /**
036   * Used to create a task, which will execute after the specified delay.
037   *
038   * @param task  The task to run.
039   * @param delay The delay, in ticks.
040   * @param environment The execution environment for the task.
041   */
042  @Override
043  public void createDelayedTask(Runnable task, ChoreTime delay, ChoreExecution environment) {
044    if(environment.equals(ChoreExecution.MAIN_THREAD)) {
045      Bukkit.getScheduler().runTaskLater(PaperPluginCore.instance().getPlugin(), task, delay.asTicks());
046      return;
047    }
048    Bukkit.getScheduler().runTaskLaterAsynchronously(PaperPluginCore.instance().getPlugin(), task, delay.asTicks());
049  }
050
051  /**
052   * Used to create a task, which repeats after a specified period.
053   *
054   * @param task The task to run.
055   * @param delay The delay to run the task, in ticks.
056   * @param period The period to run the task.
057   * @param environment The execution environment for the task.
058   *
059   * @return The associated {@link Chore} with this task.
060   */
061  @Override
062  public PaperChore createRepeatingTask(Runnable task, ChoreTime delay, ChoreTime period, ChoreExecution environment) {
063    if(environment.equals(ChoreExecution.MAIN_THREAD)) {
064      return new PaperChore(Bukkit.getScheduler().runTaskTimer(PaperPluginCore.instance().getPlugin(), task, delay.asTicks(), period.asTicks()), environment);
065    }
066    return new PaperChore(Bukkit.getScheduler().runTaskTimerAsynchronously(PaperPluginCore.instance().getPlugin(), task, delay.asTicks(), period.asTicks()), environment);
067  }
068}