Revert "Refactor TaskWrapper into interface and implementation classes"

This commit is contained in:
retrooper authored and GitHub committed 2025-07-08 19:39:19 +02:00
1 parent 2b0ec87da6
commit 69cbe1e405
8 files changed
+114 -187

No files matched your search

@@ -18,9 +18,6 @@
package io.github.retrooper.packetevents.util.folia;
import io.github.retrooper.packetevents.util.folia.task.BukkitTaskWrapper;
import io.github.retrooper.packetevents.util.folia.task.FoliaTaskWrapper;
import io.github.retrooper.packetevents.util.folia.task.TaskWrapper;
import org.bukkit.Bukkit;
import org.bukkit.plugin.Plugin;
import org.bukkit.scheduler.BukkitScheduler;
@@ -54,10 +51,10 @@ public class AsyncScheduler {
*/
public TaskWrapper runNow(@NotNull Plugin plugin, @NotNull Consumer<Object> task) {
if (!FoliaScheduler.isFolia) {
return new BukkitTaskWrapper(bukkitScheduler.runTaskAsynchronously(plugin, () -> task.accept(null)));
return new TaskWrapper(bukkitScheduler.runTaskAsynchronously(plugin, () -> task.accept(null)));
}
return new FoliaTaskWrapper(asyncScheduler.runNow(plugin, (o) -> task.accept(null)));
return new TaskWrapper(asyncScheduler.runNow(plugin, (o) -> task.accept(null)));
}
/**
@@ -71,10 +68,10 @@ public class AsyncScheduler {
*/
public TaskWrapper runDelayed(@NotNull Plugin plugin, @NotNull Consumer<Object> task, long delay, @NotNull TimeUnit timeUnit) {
if (!FoliaScheduler.isFolia) {
return new BukkitTaskWrapper(bukkitScheduler.runTaskLaterAsynchronously(plugin, () -> task.accept(null), convertTimeToTicks(delay, timeUnit)));
return new TaskWrapper(bukkitScheduler.runTaskLaterAsynchronously(plugin, () -> task.accept(null), convertTimeToTicks(delay, timeUnit)));
}
return new FoliaTaskWrapper(asyncScheduler.runDelayed(plugin, (o) -> task.accept(null), delay, timeUnit));
return new TaskWrapper(asyncScheduler.runDelayed(plugin, (o) -> task.accept(null), delay, timeUnit));
}
/**
@@ -91,10 +88,10 @@ public class AsyncScheduler {
if (period < 1) period = 1;
if (!FoliaScheduler.isFolia) {
return new BukkitTaskWrapper(bukkitScheduler.runTaskTimerAsynchronously(plugin, () -> task.accept(null), convertTimeToTicks(delay, timeUnit), convertTimeToTicks(period, timeUnit)));
return new TaskWrapper(bukkitScheduler.runTaskTimerAsynchronously(plugin, () -> task.accept(null), convertTimeToTicks(delay, timeUnit), convertTimeToTicks(period, timeUnit)));
}
return new FoliaTaskWrapper(asyncScheduler.runAtFixedRate(plugin, (o) -> task.accept(null), delay, period, timeUnit));
return new TaskWrapper(asyncScheduler.runAtFixedRate(plugin, (o) -> task.accept(null), delay, period, timeUnit));
}
/**
@@ -110,10 +107,10 @@ public class AsyncScheduler {
if (periodTicks < 1) periodTicks = 1;
if (!FoliaScheduler.isFolia) {
return new BukkitTaskWrapper(bukkitScheduler.runTaskTimerAsynchronously(plugin, () -> task.accept(null), initialDelayTicks, periodTicks));
return new TaskWrapper(bukkitScheduler.runTaskTimerAsynchronously(plugin, () -> task.accept(null), initialDelayTicks, periodTicks));
}
return new FoliaTaskWrapper(asyncScheduler.runAtFixedRate(plugin, (o) -> task.accept(null), initialDelayTicks * 50, periodTicks * 50, TimeUnit.MILLISECONDS));
return new TaskWrapper(asyncScheduler.runAtFixedRate(plugin, (o) -> task.accept(null), initialDelayTicks * 50, periodTicks * 50, TimeUnit.MILLISECONDS));
}
/**
@@ -18,9 +18,6 @@
package io.github.retrooper.packetevents.util.folia;
import io.github.retrooper.packetevents.util.folia.task.BukkitTaskWrapper;
import io.github.retrooper.packetevents.util.folia.task.FoliaTaskWrapper;
import io.github.retrooper.packetevents.util.folia.task.TaskWrapper;
import org.bukkit.Bukkit;
import org.bukkit.entity.Entity;
import org.bukkit.plugin.Plugin;
@@ -80,10 +77,10 @@ public class EntityScheduler {
*/
public TaskWrapper run(@NotNull Entity entity, @NotNull Plugin plugin, @NotNull Consumer<Object> task, @Nullable Runnable retired) {
if (!FoliaScheduler.isFolia) {
return new BukkitTaskWrapper(bukkitScheduler.runTask(plugin, () -> task.accept(null)));
return new TaskWrapper(bukkitScheduler.runTask(plugin, () -> task.accept(null)));
}
return new FoliaTaskWrapper(entity.getScheduler().run(plugin, (o) -> task.accept(null), retired));
return new TaskWrapper(entity.getScheduler().run(plugin, (o) -> task.accept(null), retired));
}
/**
@@ -104,10 +101,10 @@ public class EntityScheduler {
if (delayTicks < 1) delayTicks = 1;
if (!FoliaScheduler.isFolia) {
return new BukkitTaskWrapper(bukkitScheduler.runTaskLater(plugin, () -> task.accept(null), delayTicks));
return new TaskWrapper(bukkitScheduler.runTaskLater(plugin, () -> task.accept(null), delayTicks));
}
return new FoliaTaskWrapper(entity.getScheduler().runDelayed(plugin, (o) -> task.accept(null), retired, delayTicks));
return new TaskWrapper(entity.getScheduler().runDelayed(plugin, (o) -> task.accept(null), retired, delayTicks));
}
/**
@@ -130,9 +127,9 @@ public class EntityScheduler {
if (periodTicks < 1) periodTicks = 1;
if (!FoliaScheduler.isFolia) {
return new BukkitTaskWrapper(bukkitScheduler.runTaskTimer(plugin, () -> task.accept(null), initialDelayTicks, periodTicks));
return new TaskWrapper(bukkitScheduler.runTaskTimer(plugin, () -> task.accept(null), initialDelayTicks, periodTicks));
}
return new FoliaTaskWrapper(entity.getScheduler().runAtFixedRate(plugin, (o) -> task.accept(null), retired, initialDelayTicks, periodTicks));
return new TaskWrapper(entity.getScheduler().runAtFixedRate(plugin, (o) -> task.accept(null), retired, initialDelayTicks, periodTicks));
}
}
@@ -18,9 +18,6 @@
package io.github.retrooper.packetevents.util.folia;
import io.github.retrooper.packetevents.util.folia.task.BukkitTaskWrapper;
import io.github.retrooper.packetevents.util.folia.task.FoliaTaskWrapper;
import io.github.retrooper.packetevents.util.folia.task.TaskWrapper;
import org.bukkit.Bukkit;
import org.bukkit.plugin.Plugin;
import org.bukkit.scheduler.BukkitScheduler;
@@ -68,10 +65,10 @@ public class GlobalRegionScheduler {
*/
public TaskWrapper run(@NotNull Plugin plugin, @NotNull Consumer<Object> task) {
if (!FoliaScheduler.isFolia) {
return new BukkitTaskWrapper(bukkitScheduler.runTask(plugin, () -> task.accept(null)));
return new TaskWrapper(bukkitScheduler.runTask(plugin, () -> task.accept(null)));
}
return new FoliaTaskWrapper(globalRegionScheduler.run(plugin, (o) -> task.accept(null)));
return new TaskWrapper(globalRegionScheduler.run(plugin, (o) -> task.accept(null)));
}
/**
@@ -86,10 +83,10 @@ public class GlobalRegionScheduler {
if (delay < 1) delay = 1;
if (!FoliaScheduler.isFolia) {
return new BukkitTaskWrapper(bukkitScheduler.runTaskLater(plugin, () -> task.accept(null), delay));
return new TaskWrapper(bukkitScheduler.runTaskLater(plugin, () -> task.accept(null), delay));
}
return new FoliaTaskWrapper(globalRegionScheduler.runDelayed(plugin, (o) -> task.accept(null), delay));
return new TaskWrapper(globalRegionScheduler.runDelayed(plugin, (o) -> task.accept(null), delay));
}
/**
@@ -106,10 +103,10 @@ public class GlobalRegionScheduler {
if (periodTicks < 1) periodTicks = 1;
if (!FoliaScheduler.isFolia) {
return new BukkitTaskWrapper(bukkitScheduler.runTaskTimer(plugin, () -> task.accept(null), initialDelayTicks, periodTicks));
return new TaskWrapper(bukkitScheduler.runTaskTimer(plugin, () -> task.accept(null), initialDelayTicks, periodTicks));
}
return new FoliaTaskWrapper(globalRegionScheduler.runAtFixedRate(plugin, (o) -> task.accept(null), initialDelayTicks, periodTicks));
return new TaskWrapper(globalRegionScheduler.runAtFixedRate(plugin, (o) -> task.accept(null), initialDelayTicks, periodTicks));
}
/**
@@ -18,9 +18,6 @@
package io.github.retrooper.packetevents.util.folia;
import io.github.retrooper.packetevents.util.folia.task.BukkitTaskWrapper;
import io.github.retrooper.packetevents.util.folia.task.FoliaTaskWrapper;
import io.github.retrooper.packetevents.util.folia.task.TaskWrapper;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
@@ -92,10 +89,10 @@ public class RegionScheduler {
*/
public TaskWrapper run(@NotNull Plugin plugin, @NotNull World world, int chunkX, int chunkZ, @NotNull Consumer<Object> task) {
if (!FoliaScheduler.isFolia) {
return new BukkitTaskWrapper(Bukkit.getScheduler().runTask(plugin, () -> task.accept(null)));
return new TaskWrapper(Bukkit.getScheduler().runTask(plugin, () -> task.accept(null)));
}
return new FoliaTaskWrapper(regionScheduler.run(plugin, world, chunkX, chunkZ, (o) -> task.accept(null)));
return new TaskWrapper(regionScheduler.run(plugin, world, chunkX, chunkZ, (o) -> task.accept(null)));
}
/**
@@ -108,10 +105,10 @@ public class RegionScheduler {
*/
public TaskWrapper run(@NotNull Plugin plugin, @NotNull Location location, @NotNull Consumer<Object> task) {
if (!FoliaScheduler.isFolia) {
return new BukkitTaskWrapper(Bukkit.getScheduler().runTask(plugin, () -> task.accept(null)));
return new TaskWrapper(Bukkit.getScheduler().runTask(plugin, () -> task.accept(null)));
}
return new FoliaTaskWrapper(regionScheduler.run(plugin, location, (o) -> task.accept(null)));
return new TaskWrapper(regionScheduler.run(plugin, location, (o) -> task.accept(null)));
}
/**
@@ -129,10 +126,10 @@ public class RegionScheduler {
if (delayTicks < 1) delayTicks = 1;
if (!FoliaScheduler.isFolia) {
return new BukkitTaskWrapper(Bukkit.getScheduler().runTaskLater(plugin, () -> task.accept(null), delayTicks));
return new TaskWrapper(Bukkit.getScheduler().runTaskLater(plugin, () -> task.accept(null), delayTicks));
}
return new FoliaTaskWrapper(regionScheduler.runDelayed(plugin, world, chunkX, chunkZ, (o) -> task.accept(null), delayTicks));
return new TaskWrapper(regionScheduler.runDelayed(plugin, world, chunkX, chunkZ, (o) -> task.accept(null), delayTicks));
}
/**
@@ -148,10 +145,10 @@ public class RegionScheduler {
if (delayTicks < 1) delayTicks = 1;
if (!FoliaScheduler.isFolia) {
return new BukkitTaskWrapper(Bukkit.getScheduler().runTaskLater(plugin, () -> task.accept(null), delayTicks));
return new TaskWrapper(Bukkit.getScheduler().runTaskLater(plugin, () -> task.accept(null), delayTicks));
}
return new FoliaTaskWrapper(regionScheduler.runDelayed(plugin, location, (o) -> task.accept(null), delayTicks));
return new TaskWrapper(regionScheduler.runDelayed(plugin, location, (o) -> task.accept(null), delayTicks));
}
/**
@@ -171,10 +168,10 @@ public class RegionScheduler {
if (periodTicks < 1) periodTicks = 1;
if (!FoliaScheduler.isFolia) {
return new BukkitTaskWrapper(Bukkit.getScheduler().runTaskTimer(plugin, () -> task.accept(null), initialDelayTicks, periodTicks));
return new TaskWrapper(Bukkit.getScheduler().runTaskTimer(plugin, () -> task.accept(null), initialDelayTicks, periodTicks));
}
return new FoliaTaskWrapper(regionScheduler.runAtFixedRate(plugin, world, chunkX, chunkZ, (o) -> task.accept(null), initialDelayTicks, periodTicks));
return new TaskWrapper(regionScheduler.runAtFixedRate(plugin, world, chunkX, chunkZ, (o) -> task.accept(null), initialDelayTicks, periodTicks));
}
/**
@@ -192,9 +189,9 @@ public class RegionScheduler {
if (periodTicks < 1) periodTicks = 1;
if (!FoliaScheduler.isFolia) {
return new BukkitTaskWrapper(Bukkit.getScheduler().runTaskTimer(plugin, () -> task.accept(null), initialDelayTicks, periodTicks));
return new TaskWrapper(Bukkit.getScheduler().runTaskTimer(plugin, () -> task.accept(null), initialDelayTicks, periodTicks));
}
return new FoliaTaskWrapper(regionScheduler.runAtFixedRate(plugin, location, (o) -> task.accept(null), initialDelayTicks, periodTicks));
return new TaskWrapper(regionScheduler.runAtFixedRate(plugin, location, (o) -> task.accept(null), initialDelayTicks, periodTicks));
}
}
@@ -0,0 +1,82 @@
/*
* This file is part of packetevents - https://github.com/retrooper/packetevents
* Copyright (C) 2024 retrooper and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package io.github.retrooper.packetevents.util.folia;
import io.papermc.paper.threadedregions.scheduler.ScheduledTask;
import org.bukkit.plugin.Plugin;
import org.bukkit.scheduler.BukkitTask;
import org.jetbrains.annotations.NotNull;
/**
* Represents a wrapper around {@code BukkitTask} and Paper's {@code ScheduledTask}.
* This class provides a unified interface for interacting with both Bukkit's task scheduler
* and Paper's task scheduler.
*/
public class TaskWrapper {
private BukkitTask bukkitTask;
private ScheduledTask scheduledTask;
/**
* Constructs a new TaskWrapper around a BukkitTask.
*
* @param bukkitTask the BukkitTask to wrap
*/
public TaskWrapper(@NotNull BukkitTask bukkitTask) {
this.bukkitTask = bukkitTask;
}
/**
* Constructs a new TaskWrapper around Paper's ScheduledTask.
*
* @param scheduledTask the ScheduledTask to wrap
*/
public TaskWrapper(@NotNull ScheduledTask scheduledTask) {
this.scheduledTask = scheduledTask;
}
/**
* Retrieves the Plugin that owns this task.
*
* @return the owning {@link Plugin}
*/
public Plugin getOwner() {
return bukkitTask != null ? bukkitTask.getOwner() : scheduledTask.getOwningPlugin();
}
/**
* Checks if the task is canceled.
*
* @return true if the task is canceled, false otherwise
*/
public boolean isCancelled() {
return bukkitTask != null ? bukkitTask.isCancelled() : scheduledTask.isCancelled();
}
/**
* Cancels the task. If the task is running, it will be canceled.
*/
public void cancel() {
if (bukkitTask != null) {
bukkitTask.cancel();
} else {
scheduledTask.cancel();
}
}
}
@@ -1,48 +0,0 @@
/*
* This file is part of packetevents - https://github.com/retrooper/packetevents
* Copyright (C) 2025 retrooper and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package io.github.retrooper.packetevents.util.folia.task;
import org.bukkit.plugin.Plugin;
import org.bukkit.scheduler.BukkitTask;
/**
* An implementation of {@code TaskWrapper} for interacting with {@code BukkitTask}.
*/
public class BukkitTaskWrapper implements TaskWrapper {
private final BukkitTask task;
public BukkitTaskWrapper(BukkitTask task) {
this.task = task;
}
@Override
public Plugin getOwner() {
return task.getOwner();
}
@Override
public boolean isCancelled() {
return task.isCancelled();
}
@Override
public void cancel() {
task.cancel();
}
}
@@ -1,48 +0,0 @@
/*
* This file is part of packetevents - https://github.com/retrooper/packetevents
* Copyright (C) 2025 retrooper and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package io.github.retrooper.packetevents.util.folia.task;
import io.papermc.paper.threadedregions.scheduler.ScheduledTask;
import org.bukkit.plugin.Plugin;
/**
* An implementation of {@code TaskWrapper} for interacting with Paper's {@code ScheduledTask}.
*/
public class FoliaTaskWrapper implements TaskWrapper {
private final ScheduledTask scheduledTask;
public FoliaTaskWrapper(ScheduledTask scheduledTask) {
this.scheduledTask = scheduledTask;
}
@Override
public Plugin getOwner() {
return scheduledTask.getOwningPlugin();
}
@Override
public boolean isCancelled() {
return scheduledTask.isCancelled();
}
@Override
public void cancel() {
scheduledTask.cancel();
}
}
@@ -1,47 +0,0 @@
/*
* This file is part of packetevents - https://github.com/retrooper/packetevents
* Copyright (C) 2024 retrooper and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package io.github.retrooper.packetevents.util.folia.task;
import org.bukkit.plugin.Plugin;
/**
* A unified interface for interacting with tasks.
* This wrapper provides consistent methods to interact with tasks across multiple platforms, such as {@code BukkitTask} and Paper's {@code ScheduledTask}.
*/
public interface TaskWrapper {
/**
* Retrieves the plugin that owns this task.
*
* @return The {@code Plugin} instance owning this task.
*/
Plugin getOwner();
/**
* Checks if the task has been cancelled.
*
* @return {@code true} if the task is cancelled, {@code false} otherwise.
*/
boolean isCancelled();
/**
* Cancels the task.
*/
void cancel();
}