Guest User

ChangeablePeriodTask.java

a guest
Mar 2nd, 2023
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.64 KB | None | 0 0
  1. import org.bukkit.plugin.Plugin;
  2. import org.bukkit.scheduler.BukkitRunnable;
  3. import org.bukkit.scheduler.BukkitTask;
  4.  
  5. // Author: Dymeth.Ru
  6. public abstract class ChangeablePeriodTask extends BukkitRunnable {
  7.     public long periodTicks; // Количество тиков между циклами
  8.     private long delayTicks; // Количество тиков до отработки следующего цикла
  9.  
  10.     public ChangeablePeriodTask() {
  11.     }
  12.  
  13.     @Override
  14.     public final void run() {
  15.         if (--this.delayTicks > 0) return;
  16.         try {
  17.             this.runCycle();
  18.         } finally {
  19.             this.delayTicks = this.periodTicks;
  20.         }
  21.     }
  22.  
  23.     public abstract void runCycle();
  24.  
  25.     @Override
  26.     public BukkitTask runTask(Plugin plugin) throws IllegalArgumentException, IllegalStateException {
  27.         throw new UnsupportedOperationException("Timers supported only");
  28.     }
  29.  
  30.     @Override
  31.     public BukkitTask runTaskAsynchronously(Plugin plugin) throws IllegalArgumentException, IllegalStateException {
  32.         throw new UnsupportedOperationException("Timers supported only");
  33.     }
  34.  
  35.     @Override
  36.     public BukkitTask runTaskLater(Plugin plugin, long delay) throws IllegalArgumentException, IllegalStateException {
  37.         throw new UnsupportedOperationException("Timers supported only");
  38.     }
  39.  
  40.     @Override
  41.     public BukkitTask runTaskLaterAsynchronously(Plugin plugin, long delay) throws IllegalArgumentException, IllegalStateException {
  42.         throw new UnsupportedOperationException("Timers supported only");
  43.     }
  44.  
  45.     @Override
  46.     public synchronized BukkitTask runTaskTimer(Plugin plugin, long delay, long period) throws IllegalArgumentException, IllegalStateException {
  47.         this.setPeriodTicks(period);
  48.         this.setDelayTicks(delay + 1);
  49.         return super.runTaskTimer(plugin, 1L, 1L);
  50.     }
  51.  
  52.     @Override
  53.     public synchronized BukkitTask runTaskTimerAsynchronously(Plugin plugin, long delay, long period) throws IllegalArgumentException, IllegalStateException {
  54.         this.setPeriodTicks(period);
  55.         this.setDelayTicks(delay + 1);
  56.         return super.runTaskTimerAsynchronously(plugin, 1L, 1L);
  57.     }
  58.  
  59.     public long getPeriodTicks() {
  60.         return this.periodTicks;
  61.     }
  62.  
  63.     public void setPeriodTicks(long periodTicks) {
  64.         if (periodTicks <= 0) throw new IllegalArgumentException("periodTicks must be > 0");
  65.         this.periodTicks = periodTicks;
  66.     }
  67.  
  68.     private void setDelayTicks(long delayTicks) {
  69.         if (delayTicks < 0) throw new IllegalArgumentException("delayTicks must be >= 0");
  70.         this.delayTicks = delayTicks;
  71.     }
  72. }
  73.  
Advertisement
Add Comment
Please, Sign In to add comment