001package net.tnemc.plugincore.sponge.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.sponge.SpongePluginCore;
025import org.spongepowered.api.Sponge;
026import org.spongepowered.api.scheduler.TaskExecutorService;
027
028import java.util.concurrent.TimeUnit;
029
030/**
031 * SpongeScheduler
032 *
033 * @author creatorfromhell
034 * @since 0.1.2.0
035 */
036public class SpongeScheduler extends SchedulerProvider<SpongeChore> {
037  /**
038   * Used to create a task, which will execute after the specified delay.
039   *
040   * @param task  The task to run.
041   * @param delay The delay, in ticks.
042   * @param environment The execution environment for the task.
043   */
044  @Override
045  public void createDelayedTask(Runnable task, ChoreTime delay, ChoreExecution environment) {
046    //divide by 20 because the delay will be coming in tick for.
047    Sponge.asyncScheduler().executor(SpongePluginCore.instance().getContainer()).schedule(task, delay.asSeconds(), TimeUnit.SECONDS);
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 SpongeChore createRepeatingTask(Runnable task, ChoreTime delay, ChoreTime period, ChoreExecution environment) {
062
063    final TaskExecutorService service = Sponge.asyncScheduler().executor(SpongePluginCore.instance().getContainer());
064
065    //divide by 20 because the delay will be coming in tick for.
066    return new SpongeChore(service.scheduleAtFixedRate(task,
067                                                       delay.asSeconds(),
068                                                       period.asSeconds(),
069                                                       TimeUnit.SECONDS).task(), environment);
070  }
071}