Skip to content

Commit

Permalink
feat(minecraft/scheduler): Add entity scheduler wrapper for folia.
Browse files Browse the repository at this point in the history
  • Loading branch information
GeorgeV220 committed Feb 6, 2024
1 parent 160aa85 commit e4f59f1
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 7 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
group = com.georgev22.library
libName = MartexLibrary
author = GeorgeV22
mcVersion = 1.20.1
mcVersion = 1.20.2
version = 11.7.0-beta.10
gsonVersion = 2.10.1
guavaVersion = 32.0.0-jre
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
import org.bukkit.Chunk;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.Entity;
import org.bukkit.plugin.Plugin;
import org.bukkit.scheduler.BukkitTask;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;

@ApiStatus.NonExtendable
public class MinecraftBukkitScheduler implements MinecraftScheduler<Plugin, Location, World, Chunk> {
public class MinecraftBukkitScheduler implements MinecraftScheduler<Plugin, Location, World, Chunk, Entity> {

/**
* Schedules a task to be executed synchronously on the server's main thread.
Expand Down Expand Up @@ -118,6 +119,21 @@ public SchedulerTask createDelayedForLocation(Plugin plugin, Runnable task, Loca
return new BukkitSchedulerTask(Bukkit.getScheduler().runTaskLater(plugin, task, delay));
}

/**
* Creates a delayed task for a specific location.
*
* @param plugin The plugin that owns this task.
* @param task The runnable task to execute.
* @param retired Retire callback to run if the entity is retired before the run callback can be invoked, may be null.
* @param entity The entity in which the task will be executed.
* @param delay The delay in ticks before the task is executed.
* @return A SchedulerTask representing the created task.
*/
@Override
public SchedulerTask createDelayedForEntity(Plugin plugin, Runnable task, Runnable retired, Entity entity, long delay) {
return new BukkitSchedulerTask(Bukkit.getScheduler().runTaskLater(plugin, task, delay));
}

/**
* Creates a task for a specific world and chunk.
*
Expand Down Expand Up @@ -145,6 +161,20 @@ public SchedulerTask createTaskForLocation(Plugin plugin, Runnable task, Locatio
return new BukkitSchedulerTask(Bukkit.getScheduler().runTask(plugin, task));
}

/**
* Creates a task for a specific location.
*
* @param plugin The plugin that owns this task.
* @param task The runnable task to execute.
* @param retired Retire callback to run if the entity is retired before the run callback can be invoked, may be null.
* @param entity The entity in which the task will be executed.
* @return A SchedulerTask representing the created task.
*/
@Override
public SchedulerTask createTaskForEntity(Plugin plugin, Runnable task, Runnable retired, Entity entity) {
return new BukkitSchedulerTask(Bukkit.getScheduler().runTask(plugin, task));
}

/**
* Creates a repeating task for a specific world and chunk.
*
Expand Down Expand Up @@ -176,6 +206,22 @@ public SchedulerTask createRepeatingTaskForLocation(Plugin plugin, Runnable task
return new BukkitSchedulerTask(Bukkit.getScheduler().runTaskTimer(plugin, task, delay, period));
}

/**
* Creates a repeating task for a specific location.
*
* @param plugin The plugin that owns this task.
* @param task The runnable task to execute.
* @param retired Retire callback to run if the entity is retired before the run callback can be invoked, may be null.
* @param entity The entity in which the task will be executed.
* @param delay The initial delay in ticks before the first execution.
* @param period The period in ticks between consecutive executions.
* @return A SchedulerTask representing the created task.
*/
@Override
public SchedulerTask createRepeatingTaskForEntity(Plugin plugin, Runnable task, Runnable retired, Entity entity, long delay, long period) {
return new BukkitSchedulerTask(Bukkit.getScheduler().runTaskTimer(plugin, task, delay, period));
}

/**
* Cancels all tasks associated with the given `plugin`.
*
Expand All @@ -194,7 +240,7 @@ public void cancelTasks(Plugin plugin) {
* @return The scheduler instance for this class (i.e., this).
*/
@Override
public MinecraftScheduler<Plugin, Location, World, Chunk> getScheduler() {
public MinecraftScheduler<Plugin, Location, World, Chunk, Entity> getScheduler() {
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
import org.bukkit.Chunk;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.Entity;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;

import java.util.concurrent.TimeUnit;

@ApiStatus.NonExtendable
public class MinecraftFoliaScheduler implements MinecraftScheduler<Plugin, Location, World, Chunk> {
public class MinecraftFoliaScheduler implements MinecraftScheduler<Plugin, Location, World, Chunk, Entity> {

/**
* Schedules a task to be executed synchronously on the server's main thread.
Expand Down Expand Up @@ -118,6 +119,21 @@ public SchedulerTask createDelayedForLocation(Plugin plugin, Runnable task, Loca
return new FoliaSchedulerTask(Bukkit.getRegionScheduler().runDelayed(plugin, location, (scheduledTask) -> task.run(), delay));
}

/**
* Creates a delayed task for a specific location.
*
* @param plugin The plugin that owns this task.
* @param task The runnable task to execute.
* @param retired – Retire callback to run if the entity is retired before the run callback can be invoked, may be null.
* @param entity The entity in which the task will be executed.
* @param delay The delay in ticks before the task is executed.
* @return A SchedulerTask representing the created task.
*/
@Override
public SchedulerTask createDelayedForEntity(Plugin plugin, Runnable task, Runnable retired, @NotNull Entity entity, long delay) {
return new FoliaSchedulerTask(entity.getScheduler().runDelayed(plugin, (scheduledTask) -> task.run(), retired, delay));
}

/**
* Creates a task for a specific world and chunk.
*
Expand All @@ -143,6 +159,20 @@ public SchedulerTask createTaskForLocation(Plugin plugin, Runnable task, Locatio
return new FoliaSchedulerTask(Bukkit.getRegionScheduler().run(plugin, location, (scheduledTask) -> task.run()));
}

/**
* Creates a task for a specific location.
*
* @param plugin The plugin that owns this task.
* @param task The runnable task to execute.
* @param retired Retire callback to run if the entity is retired before the run callback can be invoked, may be null.
* @param entity The entity in which the task will be executed.
* @return A SchedulerTask representing the created task.
*/
@Override
public SchedulerTask createTaskForEntity(Plugin plugin, Runnable task, Runnable retired, @NotNull Entity entity) {
return new FoliaSchedulerTask(entity.getScheduler().run(plugin, (scheduledTask) -> task.run(), retired));
}

/**
* Creates a repeating task for a specific world and chunk.
*
Expand Down Expand Up @@ -172,6 +202,22 @@ public SchedulerTask createRepeatingTaskForLocation(Plugin plugin, Runnable task
return new FoliaSchedulerTask(Bukkit.getRegionScheduler().runAtFixedRate(plugin, location, (scheduledTask) -> task.run(), delay, period));
}

/**
* Creates a repeating task for a specific location.
*
* @param plugin The plugin that owns this task.
* @param task The runnable task to execute.
* @param retired Retire callback to run if the entity is retired before the run callback can be invoked, may be null.
* @param entity The entity in which the task will be executed.
* @param delay The initial delay in ticks before the first execution.
* @param period The period in ticks between consecutive executions.
* @return A SchedulerTask representing the created task.
*/
@Override
public SchedulerTask createRepeatingTaskForEntity(Plugin plugin, Runnable task, Runnable retired, @NotNull Entity entity, long delay, long period) {
return new FoliaSchedulerTask(entity.getScheduler().runAtFixedRate(plugin, (scheduledTask) -> task.run(), retired, delay, period));
}

/**
* Cancels all tasks associated with the given `plugin`.
*
Expand All @@ -190,7 +236,7 @@ public void cancelTasks(Plugin plugin) {
* @return The scheduler instance for this class (i.e., this).
*/
@Override
public MinecraftScheduler<Plugin, Location, World, Chunk> getScheduler() {
public MinecraftScheduler<Plugin, Location, World, Chunk, Entity> getScheduler() {
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/**
* A non-extendable interface representing a scheduler for task scheduling and cancellation.
*/
public interface MinecraftScheduler<Plugin, Location, World, Chunk> {
public interface MinecraftScheduler<Plugin, Location, World, Chunk, Entity> {

/**
* Schedules a task to be executed synchronously on the server's main thread.
Expand Down Expand Up @@ -89,6 +89,18 @@ public interface MinecraftScheduler<Plugin, Location, World, Chunk> {
*/
SchedulerTask createDelayedForLocation(Plugin plugin, Runnable task, Location location, long delay);

/**
* Creates a delayed task for a specific location.
*
* @param plugin The plugin that owns this task.
* @param task The runnable task to execute.
* @param retired Retire callback to run if the entity is retired before the run callback can be invoked, may be null.
* @param entity The entity in which the task will be executed.
* @param delay The delay in ticks before the task is executed.
* @return A SchedulerTask representing the created task.
*/
SchedulerTask createDelayedForEntity(Plugin plugin, Runnable task, Runnable retired, Entity entity, long delay);

/**
* Creates a task for a specific world and chunk.
*
Expand All @@ -110,6 +122,17 @@ public interface MinecraftScheduler<Plugin, Location, World, Chunk> {
*/
SchedulerTask createTaskForLocation(Plugin plugin, Runnable task, Location location);

/**
* Creates a task for a specific location.
*
* @param plugin The plugin that owns this task.
* @param task The runnable task to execute.
* @param retired Retire callback to run if the entity is retired before the run callback can be invoked, may be null.
* @param entity The entity in which the task will be executed.
* @return A SchedulerTask representing the created task.
*/
SchedulerTask createTaskForEntity(Plugin plugin, Runnable task, Runnable retired, Entity entity);

/**
* Creates a repeating task for a specific world and chunk.
*
Expand All @@ -135,6 +158,19 @@ public interface MinecraftScheduler<Plugin, Location, World, Chunk> {
*/
SchedulerTask createRepeatingTaskForLocation(Plugin plugin, Runnable task, Location location, long delay, long period);

/**
* Creates a repeating task for a specific location.
*
* @param plugin The plugin that owns this task.
* @param task The runnable task to execute.
* @param retired Retire callback to run if the entity is retired before the run callback can be invoked, may be null.
* @param entity The entity in which the task will be executed.
* @param delay The initial delay in ticks before the first execution.
* @param period The period in ticks between consecutive executions.
* @return A SchedulerTask representing the created task.
*/
SchedulerTask createRepeatingTaskForEntity(Plugin plugin, Runnable task, Runnable retired, Entity entity, long delay, long period);

/**
* Cancels all tasks associated with the given `plugin`.
*
Expand All @@ -147,6 +183,6 @@ public interface MinecraftScheduler<Plugin, Location, World, Chunk> {
*
* @return The scheduler
*/
MinecraftScheduler<Plugin, Location, World, Chunk> getScheduler();
MinecraftScheduler<Plugin, Location, World, Chunk, Entity> getScheduler();

}

0 comments on commit e4f59f1

Please sign in to comment.