Guest User

TasksQueue.java

a guest
Oct 5th, 2022
310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.55 KB | Gaming | 0 0
  1. import org.bukkit.plugin.Plugin;
  2. import org.bukkit.scheduler.BukkitScheduler;
  3. import org.bukkit.scheduler.BukkitTask;
  4.  
  5. import java.util.ArrayList;
  6. import java.util.List;
  7. import java.util.concurrent.TimeUnit;
  8.  
  9. // Author: Dymeth.Ru
  10. public final class TasksQueue {
  11.     private final List<Runnable> actions = new ArrayList<>();
  12.     private int actionIndex = 0;
  13.     private long sleepTicks = 0;
  14.     private BukkitTask task = null;
  15.  
  16.     public TasksQueue action(Runnable runnable) {
  17.         if (this.isRunning()) {
  18.             throw new IllegalStateException("Unable to add actions to running " + this.getClass().getSimpleName());
  19.         }
  20.         this.actions.add(runnable);
  21.         return this;
  22.     }
  23.  
  24.     /**
  25.      * @param ticks Время ожидания в тиках после завершения предыдущей задачи
  26.      */
  27.     public TasksQueue sleepTicks(long ticks) {
  28.         if (ticks <= 0) {
  29.             throw new IllegalArgumentException("Ticks must be > 0");
  30.         }
  31.         return this.action(() -> this.sleepTicks = ticks);
  32.     }
  33.  
  34.     /**
  35.      * @param duration Значение продолжительности
  36.      * @param timeUnit Единицы измерения
  37.      */
  38.     public TasksQueue sleep(long duration, TimeUnit timeUnit) {
  39.         return this.sleepTicks(TimeUnit.MILLISECONDS.convert(duration, timeUnit) / 50);
  40.     }
  41.  
  42.     public boolean isRunning() {
  43.         return this.task != null;
  44.     }
  45.  
  46.     public void startAsync(Plugin plugin) {
  47.         this.start(plugin, true);
  48.     }
  49.  
  50.     public void start(Plugin plugin) {
  51.         this.start(plugin, false);
  52.     }
  53.  
  54.     private void start(Plugin plugin, boolean async) {
  55.         if (this.isRunning()) {
  56.             throw new IllegalStateException(this.getClass().getSimpleName() + " started running");
  57.         }
  58.         BukkitScheduler scheduler = plugin.getServer().getScheduler();
  59.         this.task = async
  60.                 ? scheduler.runTaskTimerAsynchronously(plugin, this::run, 1L, 1L)
  61.                 : scheduler.runTaskTimer(plugin, this::run, 1L, 1L);
  62.     }
  63.  
  64.     public void stop() {
  65.         if (!this.isRunning()) return;
  66.         this.task.cancel();
  67.         this.actionIndex = 0;
  68.         this.sleepTicks = 0;
  69.         this.task = null;
  70.     }
  71.  
  72.     private void run() {
  73.         while (this.sleepTicks == 0) {
  74.             this.actions.get(this.actionIndex++).run();
  75.             if (this.actionIndex == this.actions.size()) {
  76.                 this.stop();
  77.                 return;
  78.             }
  79.         }
  80.         this.sleepTicks--;
  81.     }
  82. }
  83.  
Advertisement
Add Comment
Please, Sign In to add comment