Skip to content
This repository has been archived by the owner on May 26, 2024. It is now read-only.

Commit

Permalink
Version 1.1.8
Browse files Browse the repository at this point in the history
Création de SchedulerTaskRunnable qui est une inspiration de BukkitRunnable
  • Loading branch information
Euphillya committed Mar 18, 2024
1 parent 1e1c610 commit ee21c16
Show file tree
Hide file tree
Showing 5 changed files with 251 additions and 2 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
}

group = "fr.euphyllia"
version = "1.1.7"
version = "1.1.8"

repositories {
mavenCentral()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.plugin.Plugin;
import org.checkerframework.checker.units.qual.C;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package fr.euphyllia.energie.model;

import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public abstract class AbstractSchedulerTaskRunnable {
protected SchedulerTaskInter task;

/**
* Returns true if this task has been cancelled.
*
* @return true if the task has been cancelled
* @throws IllegalStateException if task was not scheduled yet
*/
protected synchronized boolean isCancelled() throws IllegalStateException {
checkScheduled();
return task.isCancelled();
}

/**
* Attempts to cancel this task.
*
* @throws IllegalStateException if task was not scheduled yet
*/
public abstract void cancel() throws IllegalStateException;

@NotNull
public abstract SchedulerTaskInter runTask(@NotNull Plugin plugin, SchedulerType schedulerType) throws IllegalArgumentException, IllegalStateException;

public abstract @NotNull SchedulerTaskInter runTask(@NotNull Plugin plugin, @NotNull SchedulerType schedulerType, MultipleRecords.WorldChunk worldChunk);

public abstract @NotNull SchedulerTaskInter runTask(@NotNull Plugin plugin, @NotNull SchedulerType schedulerType, Location location);

public abstract @NotNull SchedulerTaskInter runTask(@NotNull Plugin plugin, @NotNull SchedulerType schedulerType, Entity entity, @Nullable Runnable retired);

@NotNull
public abstract SchedulerTaskInter runDelayed(@NotNull Plugin plugin, SchedulerType schedulerType, long delay) throws IllegalArgumentException, IllegalStateException;

public abstract @NotNull SchedulerTaskInter runDelayed(@NotNull Plugin plugin, @NotNull SchedulerType schedulerType, MultipleRecords.WorldChunk worldChunk, long delayTicks);

public abstract @NotNull SchedulerTaskInter runDelayed(@NotNull Plugin plugin, @NotNull SchedulerType schedulerType, Location location, long delayTicks);

public abstract @NotNull SchedulerTaskInter runDelayed(@NotNull Plugin plugin, @NotNull SchedulerType schedulerType, Entity entity, @Nullable Runnable retired, long delayTicks);

@NotNull
public abstract SchedulerTaskInter runAtFixedRate(@NotNull Plugin plugin, SchedulerType schedulerType, long delay, long period) throws IllegalArgumentException, IllegalStateException;

public abstract @NotNull SchedulerTaskInter runAtFixedRate(@NotNull Plugin plugin, @NotNull SchedulerType schedulerType, MultipleRecords.WorldChunk worldChunk, long initialDelayTicks, long periodTicks);

public abstract @NotNull SchedulerTaskInter runAtFixedRate(@NotNull Plugin plugin, @NotNull SchedulerType schedulerType, Location location, long initialDelayTicks, long periodTicks);

public abstract @NotNull SchedulerTaskInter runAtFixedRate(@NotNull Plugin plugin, @NotNull SchedulerType schedulerType, Entity entity, @Nullable Runnable retired, long initialDelayTicks, long periodTicks);

/**
* Gets the task id for this runnable.
*
* @return the task id that this runnable was scheduled as
* @throws IllegalStateException if task was not scheduled yet
*/
protected synchronized int getTaskId() throws IllegalStateException {
checkScheduled();
return task.getTaskId();
}

protected void checkScheduled() {
if (task == null) {
throw new IllegalStateException("Not scheduled yet");
}
}

protected void checkNotYetScheduled() {
if (task != null) {
throw new IllegalStateException("Already scheduled as " + task.getTaskId());
}
}

@NotNull
protected SchedulerTaskInter setupTask(@NotNull final SchedulerTaskInter task) {
this.task = task;
return task;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package fr.euphyllia.energie.model;

public interface SchedulerRunnable {

void run();
}
159 changes: 159 additions & 0 deletions src/main/java/fr/euphyllia/energie/utils/SchedulerTaskRunnable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
package fr.euphyllia.energie.utils;

import fr.euphyllia.energie.Energie;
import fr.euphyllia.energie.folia.FoliaSchedulerTask;
import fr.euphyllia.energie.legacy.LegacySchedulerTask;
import fr.euphyllia.energie.model.*;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.concurrent.TimeUnit;

public abstract class SchedulerTaskRunnable extends AbstractSchedulerTaskRunnable implements SchedulerRunnable {

@Override
public void cancel() throws IllegalStateException {
task.cancel();
}

@Override
public @NotNull SchedulerTaskInter runTask(@NotNull Plugin plugin, SchedulerType schedulerType) throws IllegalArgumentException, IllegalStateException {
checkNotYetScheduled();
if (Energie.isFolia()) {
if (schedulerType.equals(SchedulerType.ASYNC)) {
return setupTask(new FoliaSchedulerTask(Bukkit.getAsyncScheduler().runNow(plugin, task1 -> this.run()), false));
} else {
return setupTask(new FoliaSchedulerTask(Bukkit.getGlobalRegionScheduler().run(plugin, task1 -> this.run()), true));
}
} else {
if (schedulerType.equals(SchedulerType.ASYNC)) {
return setupTask(new LegacySchedulerTask(Bukkit.getScheduler().runTaskAsynchronously(plugin, this::run)));
} else {
return setupTask(new LegacySchedulerTask(Bukkit.getScheduler().runTask(plugin, this::run)));
}
}
}

@Override
public @NotNull SchedulerTaskInter runTask(@NotNull Plugin plugin, @NotNull SchedulerType schedulerType, MultipleRecords.WorldChunk worldChunk) {
if (!Energie.isFolia() || schedulerType.equals(SchedulerType.ASYNC)) {
return runTask(plugin, schedulerType);
}
checkNotYetScheduled();
return setupTask(new FoliaSchedulerTask(Bukkit.getRegionScheduler().run(plugin, worldChunk.world(), worldChunk.chunkX(), worldChunk.chunkZ(), task1 -> this.run()), true));
}

@Override
public @NotNull SchedulerTaskInter runTask(@NotNull Plugin plugin, @NotNull SchedulerType schedulerType, Location location) {
if (!Energie.isFolia() || schedulerType.equals(SchedulerType.ASYNC)) {
return runTask(plugin, schedulerType);
}
checkNotYetScheduled();
return setupTask(new FoliaSchedulerTask(Bukkit.getRegionScheduler().run(plugin, location, task1 -> this.run()), true));
}

@Override
public @NotNull SchedulerTaskInter runTask(@NotNull Plugin plugin, @NotNull SchedulerType schedulerType, Entity entity, @Nullable Runnable retired) {
if (!Energie.isFolia() || schedulerType.equals(SchedulerType.ASYNC)) {
return runTask(plugin, schedulerType);
}
checkNotYetScheduled();
return setupTask(new FoliaSchedulerTask(entity.getScheduler().run(plugin, task1 -> this.run(), retired), true));
}

@Override
public @NotNull SchedulerTaskInter runDelayed(@NotNull Plugin plugin, SchedulerType schedulerType, long delay) throws IllegalArgumentException, IllegalStateException {
checkNotYetScheduled();
if (Energie.isFolia()) {
if (schedulerType.equals(SchedulerType.ASYNC)) {
return setupTask(new FoliaSchedulerTask(Bukkit.getAsyncScheduler().runDelayed(plugin, task1 -> this.run(), delay * 50, TimeUnit.MILLISECONDS), false));
} else {
return setupTask(new FoliaSchedulerTask(Bukkit.getGlobalRegionScheduler().runDelayed(plugin, task1 -> this.run(), delay), true));
}
} else {
if (schedulerType.equals(SchedulerType.ASYNC)) {
return setupTask(new LegacySchedulerTask(Bukkit.getScheduler().runTaskLaterAsynchronously(plugin, this::run, delay)));
} else {
return setupTask(new LegacySchedulerTask(Bukkit.getScheduler().runTaskLater(plugin, this::run, delay)));
}
}
}

@Override
public @NotNull SchedulerTaskInter runDelayed(@NotNull Plugin plugin, @NotNull SchedulerType schedulerType, MultipleRecords.WorldChunk worldChunk, long delayTicks) {
if (!Energie.isFolia() || schedulerType.equals(SchedulerType.ASYNC)) {
return runTask(plugin, schedulerType);
}
checkNotYetScheduled();
return setupTask(new FoliaSchedulerTask(Bukkit.getRegionScheduler().runDelayed(plugin, worldChunk.world(), worldChunk.chunkX(), worldChunk.chunkZ(), task1 -> this.run(), delayTicks), true));
}

@Override
public @NotNull SchedulerTaskInter runDelayed(@NotNull Plugin plugin, @NotNull SchedulerType schedulerType, Location location, long delayTicks) {
if (!Energie.isFolia() || schedulerType.equals(SchedulerType.ASYNC)) {
return runTask(plugin, schedulerType);
}
checkNotYetScheduled();
return setupTask(new FoliaSchedulerTask(Bukkit.getRegionScheduler().runDelayed(plugin, location, task1 -> this.run(), delayTicks), true));
}

@Override
public @NotNull SchedulerTaskInter runDelayed(@NotNull Plugin plugin, @NotNull SchedulerType schedulerType, Entity entity, @Nullable Runnable retired, long delayTicks) {
checkNotYetScheduled();
if (!Energie.isFolia() || schedulerType.equals(SchedulerType.ASYNC)) {
return runTask(plugin, schedulerType);
}
checkNotYetScheduled();
return setupTask(new FoliaSchedulerTask(entity.getScheduler().runDelayed(plugin, task1 -> this.run(), retired, delayTicks), true));
}

@Override
public @NotNull SchedulerTaskInter runAtFixedRate(@NotNull Plugin plugin, SchedulerType schedulerType, long delay, long period) throws IllegalArgumentException, IllegalStateException {
checkNotYetScheduled();
if (Energie.isFolia()) {
if (schedulerType.equals(SchedulerType.ASYNC)) {
return setupTask(new FoliaSchedulerTask(Bukkit.getAsyncScheduler().runAtFixedRate(plugin, task1 -> this.run(), delay * 50, period * 50, TimeUnit.MILLISECONDS), false));
} else {
return setupTask(new FoliaSchedulerTask(Bukkit.getGlobalRegionScheduler().runAtFixedRate(plugin, task1 -> this.run(), delay, period), true));
}
} else {
if (schedulerType.equals(SchedulerType.ASYNC)) {
return setupTask(new LegacySchedulerTask(Bukkit.getScheduler().runTaskTimerAsynchronously(plugin, this::run, delay, period)));
} else {
return setupTask(new LegacySchedulerTask(Bukkit.getScheduler().runTaskTimer(plugin, this::run, delay, period)));
}
}
}

@Override
public @NotNull SchedulerTaskInter runAtFixedRate(@NotNull Plugin plugin, @NotNull SchedulerType schedulerType, MultipleRecords.WorldChunk worldChunk, long initialDelayTicks, long periodTicks) {
if (!Energie.isFolia() || schedulerType.equals(SchedulerType.ASYNC)) {
return runTask(plugin, schedulerType);
}
checkNotYetScheduled();
return setupTask(new FoliaSchedulerTask(Bukkit.getRegionScheduler().runAtFixedRate(plugin, worldChunk.world(), worldChunk.chunkX(), worldChunk.chunkZ(), task1 -> this.run(), initialDelayTicks, periodTicks), true));
}

@Override
public @NotNull SchedulerTaskInter runAtFixedRate(@NotNull Plugin plugin, @NotNull SchedulerType schedulerType, Location location, long initialDelayTicks, long periodTicks) {
if (!Energie.isFolia() || schedulerType.equals(SchedulerType.ASYNC)) {
return runTask(plugin, schedulerType);
}
checkNotYetScheduled();
return setupTask(new FoliaSchedulerTask(Bukkit.getRegionScheduler().runAtFixedRate(plugin, location, task1 -> this.run(), initialDelayTicks, periodTicks), true));
}

@Override
public @NotNull SchedulerTaskInter runAtFixedRate(@NotNull Plugin plugin, @NotNull SchedulerType schedulerType, Entity entity, @Nullable Runnable retired, long initialDelayTicks, long periodTicks) {
checkNotYetScheduled();
if (!Energie.isFolia() || schedulerType.equals(SchedulerType.ASYNC)) {
return runTask(plugin, schedulerType);
}
checkNotYetScheduled();
return setupTask(new FoliaSchedulerTask(entity.getScheduler().runAtFixedRate(plugin, task1 -> this.run(), retired, initialDelayTicks, periodTicks), true));
}
}

0 comments on commit ee21c16

Please # to comment.