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  /**
039   * Used to create a task, which will execute after the specified delay.
040   *
041   * @param task        The task to run.
042   * @param delay       The delay, in ticks.
043   * @param environment The execution environment for the task.
044   */
045  @Override
046  public void createDelayedTask(final Runnable task, final ChoreTime delay, final ChoreExecution environment) {
047    //divide by 20 because the delay will be coming in tick for.
048    Sponge.asyncScheduler().executor(SpongePluginCore.instance().getContainer()).schedule(task, delay.asSeconds(), TimeUnit.SECONDS);
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 SpongeChore createRepeatingTask(final Runnable task, final ChoreTime delay, final ChoreTime period, final ChoreExecution environment) {
063
064    final TaskExecutorService service = Sponge.asyncScheduler().executor(SpongePluginCore.instance().getContainer());
065
066    //divide by 20 because the delay will be coming in tick for.
067    return new SpongeChore(service.scheduleAtFixedRate(task,
068                                                       delay.asSeconds(),
069                                                       period.asSeconds(),
070                                                       TimeUnit.SECONDS).task(), environment);
071  }
072}