From 46826fac906bcba970b08c54285646fa9b4a5eb4 Mon Sep 17 00:00:00 2001 From: YufiriaMazenta Date: Wed, 28 Feb 2024 12:36:28 +0800 Subject: [PATCH 1/3] Folia Support --- build.gradle.kts | 8 +- .../plugin/OldCombatMechanics/OCMMain.java | 4 +- .../module/ModuleChorusFruit.java | 3 +- .../module/ModuleGoldenApple.java | 3 +- .../module/ModuleOldArmourDurability.java | 3 +- .../module/ModulePlayerKnockback.java | 3 +- .../module/ModulePlayerRegen.java | 5 +- .../module/ModuleShieldDamageReduction.java | 3 +- .../module/ModuleSwordBlocking.java | 12 +- .../module/ModuleSwordSweep.java | 6 +- .../scheduler/BukkitScheduler.java | 88 ++++++++++ .../scheduler/FoliaScheduler.java | 98 +++++++++++ .../scheduler/IScheduler.java | 159 ++++++++++++++++++ .../scheduler/ProxyRunnable.java | 102 +++++++++++ .../scheduler/SchedulerManager.java | 22 +++ .../scheduler/task/BukkitTaskWrapper.java | 42 +++++ .../scheduler/task/FoliaTaskWrapper.java | 40 +++++ .../scheduler/task/ITaskWrapper.java | 43 +++++ .../OldCombatMechanics/tester/FakePlayer.java | 12 +- .../tester/InGameTester.java | 5 +- .../updater/ModuleUpdateChecker.java | 3 +- .../damage/AttackCooldownTracker.java | 3 +- .../damage/EntityDamageByEntityListener.java | 3 +- .../utilities/storage/PlayerStorage.java | 6 +- src/main/resources/plugin.yml | 2 +- 25 files changed, 648 insertions(+), 30 deletions(-) create mode 100644 src/main/java/kernitus/plugin/OldCombatMechanics/scheduler/BukkitScheduler.java create mode 100644 src/main/java/kernitus/plugin/OldCombatMechanics/scheduler/FoliaScheduler.java create mode 100644 src/main/java/kernitus/plugin/OldCombatMechanics/scheduler/IScheduler.java create mode 100644 src/main/java/kernitus/plugin/OldCombatMechanics/scheduler/ProxyRunnable.java create mode 100644 src/main/java/kernitus/plugin/OldCombatMechanics/scheduler/SchedulerManager.java create mode 100644 src/main/java/kernitus/plugin/OldCombatMechanics/scheduler/task/BukkitTaskWrapper.java create mode 100644 src/main/java/kernitus/plugin/OldCombatMechanics/scheduler/task/FoliaTaskWrapper.java create mode 100644 src/main/java/kernitus/plugin/OldCombatMechanics/scheduler/task/ITaskWrapper.java diff --git a/build.gradle.kts b/build.gradle.kts index 774a1a97..e1d801aa 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -37,6 +37,8 @@ repositories { // Spartan API maven("https://repo.crazycrew.us/api") maven("https://repo.minebench.de/") + // Folia API + maven("https://repo.papermc.io/repository/maven-public/") // Protocollib maven("https://repo.dmulloy2.net/repository/public/") } @@ -52,7 +54,9 @@ dependencies { // For BSON file serialisation implementation("org.mongodb:bson:4.11.0") // Spigot - compileOnly("org.spigotmc:spigot-api:1.20.3-R0.1-SNAPSHOT") +// compileOnly("org.spigotmc:spigot-api:1.20.3-R0.1-SNAPSHOT") + // Folia + compileOnly("dev.folia:folia-api:1.20.4-R0.1-SNAPSHOT") // ProtocolLib compileOnly("com.comphenix.protocol:ProtocolLib:5.1.0") @@ -72,7 +76,7 @@ description = "OldCombatMechanics" java { toolchain { // At least 17 required for MC 1.19 for ingametesting - languageVersion.set(JavaLanguageVersion.of(8)) + languageVersion.set(JavaLanguageVersion.of(17)) } } diff --git a/src/main/java/kernitus/plugin/OldCombatMechanics/OCMMain.java b/src/main/java/kernitus/plugin/OldCombatMechanics/OCMMain.java index 08db4592..6d66aab0 100644 --- a/src/main/java/kernitus/plugin/OldCombatMechanics/OCMMain.java +++ b/src/main/java/kernitus/plugin/OldCombatMechanics/OCMMain.java @@ -12,6 +12,7 @@ import kernitus.plugin.OldCombatMechanics.hooks.PlaceholderAPIHook; import kernitus.plugin.OldCombatMechanics.hooks.api.Hook; import kernitus.plugin.OldCombatMechanics.module.*; +import kernitus.plugin.OldCombatMechanics.scheduler.SchedulerManager; import kernitus.plugin.OldCombatMechanics.updater.ModuleUpdateChecker; import kernitus.plugin.OldCombatMechanics.utilities.Config; import kernitus.plugin.OldCombatMechanics.utilities.Messenger; @@ -58,6 +59,7 @@ public OCMMain() { public void onEnable() { INSTANCE = this; + SchedulerManager.INSTANCE.loadPlatform(); // Setting up config.yml CH.setupConfigIfNotPresent(); @@ -142,7 +144,7 @@ public void onEnable() { logger.info(pdfFile.getName() + " v" + pdfFile.getVersion() + " has been enabled"); if (Config.moduleEnabled("update-checker")) - Bukkit.getScheduler().runTaskLaterAsynchronously(this, + SchedulerManager.INSTANCE.getScheduler().runTaskLaterAsync(this, () -> new UpdateChecker(this).performUpdate(), 20L); metrics.addCustomChart(new SimplePie("auto_update_pie", diff --git a/src/main/java/kernitus/plugin/OldCombatMechanics/module/ModuleChorusFruit.java b/src/main/java/kernitus/plugin/OldCombatMechanics/module/ModuleChorusFruit.java index 5358da2d..57cae87b 100644 --- a/src/main/java/kernitus/plugin/OldCombatMechanics/module/ModuleChorusFruit.java +++ b/src/main/java/kernitus/plugin/OldCombatMechanics/module/ModuleChorusFruit.java @@ -6,6 +6,7 @@ package kernitus.plugin.OldCombatMechanics.module; import kernitus.plugin.OldCombatMechanics.OCMMain; +import kernitus.plugin.OldCombatMechanics.scheduler.SchedulerManager; import kernitus.plugin.OldCombatMechanics.utilities.MathsHelper; import org.bukkit.Bukkit; import org.bukkit.Location; @@ -45,7 +46,7 @@ public void onEat(PlayerItemConsumeEvent e) { // Run it on the next tick to reset things while not cancelling the chorus fruit eat event // This ensures the teleport event is fired and counts towards statistics - Bukkit.getScheduler().runTaskLater(plugin, () -> { + SchedulerManager.INSTANCE.getScheduler().runTaskLater(plugin, () -> { final int newFoodLevel = Math.min(hungerValue + previousFoodLevel, 20); final float newSaturation = Math.min((float) (saturationValue + previousSaturation), newFoodLevel); diff --git a/src/main/java/kernitus/plugin/OldCombatMechanics/module/ModuleGoldenApple.java b/src/main/java/kernitus/plugin/OldCombatMechanics/module/ModuleGoldenApple.java index 5b89c8c3..2531a3b0 100644 --- a/src/main/java/kernitus/plugin/OldCombatMechanics/module/ModuleGoldenApple.java +++ b/src/main/java/kernitus/plugin/OldCombatMechanics/module/ModuleGoldenApple.java @@ -7,6 +7,7 @@ import com.google.common.collect.ImmutableSet; import kernitus.plugin.OldCombatMechanics.OCMMain; +import kernitus.plugin.OldCombatMechanics.scheduler.SchedulerManager; import kernitus.plugin.OldCombatMechanics.utilities.Messenger; import org.bukkit.Bukkit; import org.bukkit.Material; @@ -178,7 +179,7 @@ public void onItemConsume(PlayerItemConsumeEvent e) { final Set defaultEffects = ENCHANTED_GOLDEN_APPLE.isSame(originalItem) ? nappleEffects : gappleEffects; - Bukkit.getScheduler().runTaskLater(plugin, () -> { + SchedulerManager.INSTANCE.getScheduler().runTaskLater(plugin, () -> { // Remove all potion effects the apple added player.getActivePotionEffects().stream() .map(PotionEffect::getType) diff --git a/src/main/java/kernitus/plugin/OldCombatMechanics/module/ModuleOldArmourDurability.java b/src/main/java/kernitus/plugin/OldCombatMechanics/module/ModuleOldArmourDurability.java index 063a60d4..83330059 100644 --- a/src/main/java/kernitus/plugin/OldCombatMechanics/module/ModuleOldArmourDurability.java +++ b/src/main/java/kernitus/plugin/OldCombatMechanics/module/ModuleOldArmourDurability.java @@ -6,6 +6,7 @@ package kernitus.plugin.OldCombatMechanics.module; import kernitus.plugin.OldCombatMechanics.OCMMain; +import kernitus.plugin.OldCombatMechanics.scheduler.SchedulerManager; import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.enchantments.Enchantment; @@ -80,7 +81,7 @@ public void onPlayerExplosionDamage(EntityDamageEvent e) { final List armour = Arrays.stream(player.getInventory().getArmorContents()).filter(Objects::nonNull).collect(Collectors.toList()); explosionDamaged.put(uuid, armour); - Bukkit.getScheduler().runTaskLater(plugin, () -> { + SchedulerManager.INSTANCE.getScheduler().runTaskLater(plugin, () -> { explosionDamaged.remove(uuid); debug("Removed from explosion set!", player); }, 1L); // This delay seems enough for the durability events to fire diff --git a/src/main/java/kernitus/plugin/OldCombatMechanics/module/ModulePlayerKnockback.java b/src/main/java/kernitus/plugin/OldCombatMechanics/module/ModulePlayerKnockback.java index d637bd0b..911a981d 100644 --- a/src/main/java/kernitus/plugin/OldCombatMechanics/module/ModulePlayerKnockback.java +++ b/src/main/java/kernitus/plugin/OldCombatMechanics/module/ModulePlayerKnockback.java @@ -6,6 +6,7 @@ package kernitus.plugin.OldCombatMechanics.module; import kernitus.plugin.OldCombatMechanics.OCMMain; +import kernitus.plugin.OldCombatMechanics.scheduler.SchedulerManager; import kernitus.plugin.OldCombatMechanics.utilities.reflection.Reflector; import org.bukkit.Bukkit; import org.bukkit.Location; @@ -166,6 +167,6 @@ public void onEntityDamageEntity(EntityDamageByEntityEvent event) { playerKnockbackHashMap.put(victimId, playerVelocity); // Sometimes PlayerVelocityEvent doesn't fire, remove data to not affect later events if that happens - Bukkit.getScheduler().runTaskLater(plugin, () -> playerKnockbackHashMap.remove(victimId), 1); + SchedulerManager.INSTANCE.getScheduler().runTaskLater(plugin, () -> playerKnockbackHashMap.remove(victimId), 1); } } \ No newline at end of file diff --git a/src/main/java/kernitus/plugin/OldCombatMechanics/module/ModulePlayerRegen.java b/src/main/java/kernitus/plugin/OldCombatMechanics/module/ModulePlayerRegen.java index 45e65e0f..df6aa10a 100644 --- a/src/main/java/kernitus/plugin/OldCombatMechanics/module/ModulePlayerRegen.java +++ b/src/main/java/kernitus/plugin/OldCombatMechanics/module/ModulePlayerRegen.java @@ -6,6 +6,7 @@ package kernitus.plugin.OldCombatMechanics.module; import kernitus.plugin.OldCombatMechanics.OCMMain; +import kernitus.plugin.OldCombatMechanics.scheduler.SchedulerManager; import kernitus.plugin.OldCombatMechanics.utilities.MathsHelper; import me.vagdedes.spartan.api.API; import me.vagdedes.spartan.system.Enums.HackType; @@ -65,7 +66,7 @@ public void onRegen(EntityRegainHealthEvent e) { // If we're skipping this heal, we must fix the exhaustion in the following tick if (hasLastHealTime && currentTime - lastHealTime <= module().getLong("interval")) { - Bukkit.getScheduler().runTaskLater(plugin, () -> p.setExhaustion(previousExhaustion), 1L); + SchedulerManager.INSTANCE.getScheduler().runTaskLater(plugin, () -> p.setExhaustion(previousExhaustion), 1L); return; } @@ -81,7 +82,7 @@ public void onRegen(EntityRegainHealthEvent e) { // Calculate new exhaustion value, must be between 0 and 4. If above, it will reduce the saturation in the following tick. final float exhaustionToApply = (float) module().getDouble("exhaustion"); - Bukkit.getScheduler().runTaskLater(plugin, () -> { + SchedulerManager.INSTANCE.getScheduler().runTaskLater(plugin, () -> { // We do this in the next tick because bukkit doesn't stop the exhaustion change when cancelling the event p.setExhaustion(previousExhaustion + exhaustionToApply); debug("Exh before: " + previousExhaustion + " Now: " + p.getExhaustion() + diff --git a/src/main/java/kernitus/plugin/OldCombatMechanics/module/ModuleShieldDamageReduction.java b/src/main/java/kernitus/plugin/OldCombatMechanics/module/ModuleShieldDamageReduction.java index c24938b4..fb0a44ff 100644 --- a/src/main/java/kernitus/plugin/OldCombatMechanics/module/ModuleShieldDamageReduction.java +++ b/src/main/java/kernitus/plugin/OldCombatMechanics/module/ModuleShieldDamageReduction.java @@ -6,6 +6,7 @@ package kernitus.plugin.OldCombatMechanics.module; import kernitus.plugin.OldCombatMechanics.OCMMain; +import kernitus.plugin.OldCombatMechanics.scheduler.SchedulerManager; import org.bukkit.Bukkit; import org.bukkit.entity.Entity; import org.bukkit.entity.Player; @@ -88,7 +89,7 @@ public void onHit(EntityDamageByEntityEvent e) { final List armour = Arrays.stream(player.getInventory().getArmorContents()).filter(Objects::nonNull).collect(Collectors.toList()); fullyBlocked.put(uuid, armour); - Bukkit.getScheduler().runTaskLater(plugin, () -> { + SchedulerManager.INSTANCE.getScheduler().runTaskLater(plugin, () -> { fullyBlocked.remove(uuid); debug("Removed from fully blocked set!", player); }, 1L); diff --git a/src/main/java/kernitus/plugin/OldCombatMechanics/module/ModuleSwordBlocking.java b/src/main/java/kernitus/plugin/OldCombatMechanics/module/ModuleSwordBlocking.java index b7d36ed8..3ed5f762 100644 --- a/src/main/java/kernitus/plugin/OldCombatMechanics/module/ModuleSwordBlocking.java +++ b/src/main/java/kernitus/plugin/OldCombatMechanics/module/ModuleSwordBlocking.java @@ -6,6 +6,8 @@ package kernitus.plugin.OldCombatMechanics.module; import kernitus.plugin.OldCombatMechanics.OCMMain; +import kernitus.plugin.OldCombatMechanics.scheduler.SchedulerManager; +import kernitus.plugin.OldCombatMechanics.scheduler.task.ITaskWrapper; import kernitus.plugin.OldCombatMechanics.utilities.reflection.Reflector; import org.bukkit.Bukkit; import org.bukkit.Material; @@ -30,7 +32,7 @@ public class ModuleSwordBlocking extends OCMModule { private static final ItemStack SHIELD = new ItemStack(Material.SHIELD); // Not using WeakHashMaps here, for extra reliability private final Map storedItems = new HashMap<>(); - private final Map> correspondingTasks = new HashMap<>(); + private final Map> correspondingTasks = new HashMap<>(); private int restoreDelay; public ModuleSwordBlocking(OCMMain plugin) { @@ -170,23 +172,23 @@ private void restore(Player p) { private void tryCancelTask(UUID id) { Optional.ofNullable(correspondingTasks.remove(id)) - .ifPresent(tasks -> tasks.forEach(BukkitTask::cancel)); + .ifPresent(tasks -> tasks.forEach(ITaskWrapper::cancel)); } private void scheduleRestore(Player p) { final UUID id = p.getUniqueId(); tryCancelTask(id); - final BukkitTask removeItem = Bukkit.getScheduler() + final ITaskWrapper removeItem = SchedulerManager.INSTANCE.getScheduler() .runTaskLater(plugin, () -> restore(p), restoreDelay); - final BukkitTask checkBlocking = Bukkit.getScheduler() + final ITaskWrapper checkBlocking = SchedulerManager.INSTANCE.getScheduler() .runTaskTimer(plugin, () -> { if (!isPlayerBlocking(p)) restore(p); }, 10L, 2L); - final List tasks = new ArrayList<>(2); + final List tasks = new ArrayList<>(2); tasks.add(removeItem); tasks.add(checkBlocking); correspondingTasks.put(p.getUniqueId(), tasks); diff --git a/src/main/java/kernitus/plugin/OldCombatMechanics/module/ModuleSwordSweep.java b/src/main/java/kernitus/plugin/OldCombatMechanics/module/ModuleSwordSweep.java index bf3bf347..654dc62e 100644 --- a/src/main/java/kernitus/plugin/OldCombatMechanics/module/ModuleSwordSweep.java +++ b/src/main/java/kernitus/plugin/OldCombatMechanics/module/ModuleSwordSweep.java @@ -6,6 +6,8 @@ package kernitus.plugin.OldCombatMechanics.module; import kernitus.plugin.OldCombatMechanics.OCMMain; +import kernitus.plugin.OldCombatMechanics.scheduler.SchedulerManager; +import kernitus.plugin.OldCombatMechanics.scheduler.task.ITaskWrapper; import kernitus.plugin.OldCombatMechanics.utilities.damage.NewWeaponDamage; import org.bukkit.Bukkit; import org.bukkit.Location; @@ -30,7 +32,7 @@ public class ModuleSwordSweep extends OCMModule { private final List sweepLocations = new ArrayList<>(); private EntityDamageEvent.DamageCause sweepDamageCause; - private BukkitTask task; + private ITaskWrapper task; public ModuleSwordSweep(OCMMain plugin) { super(plugin, "disable-sword-sweep"); @@ -52,7 +54,7 @@ public void reload() { if (task != null) task.cancel(); - task = Bukkit.getScheduler().runTaskTimer(plugin, sweepLocations::clear, 0, 1); + task = SchedulerManager.INSTANCE.getScheduler().runTaskTimer(plugin, sweepLocations::clear, 0, 1); } diff --git a/src/main/java/kernitus/plugin/OldCombatMechanics/scheduler/BukkitScheduler.java b/src/main/java/kernitus/plugin/OldCombatMechanics/scheduler/BukkitScheduler.java new file mode 100644 index 00000000..6d888df5 --- /dev/null +++ b/src/main/java/kernitus/plugin/OldCombatMechanics/scheduler/BukkitScheduler.java @@ -0,0 +1,88 @@ +package kernitus.plugin.OldCombatMechanics.scheduler; + +import kernitus.plugin.OldCombatMechanics.scheduler.task.BukkitTaskWrapper; +import kernitus.plugin.OldCombatMechanics.scheduler.task.ITaskWrapper; +import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.entity.Entity; +import org.bukkit.plugin.Plugin; +import org.jetbrains.annotations.NotNull; + +/** + * Bukkit平台的调度器 + */ +public enum BukkitScheduler implements IScheduler { + + INSTANCE; + + @Override + public ITaskWrapper runTask(@NotNull Plugin plugin, @NotNull Runnable task) { + return new BukkitTaskWrapper(Bukkit.getScheduler().runTask(plugin, task)); + } + + @Override + public ITaskWrapper runTaskAsync(@NotNull Plugin plugin, @NotNull Runnable task) { + return new BukkitTaskWrapper(Bukkit.getScheduler().runTaskAsynchronously(plugin, task)); + } + + @Override + public ITaskWrapper runTaskLater(@NotNull Plugin plugin, @NotNull Runnable task, long delayTicks) { + return new BukkitTaskWrapper(Bukkit.getScheduler().runTaskLater(plugin, task, delayTicks)); + } + + @Override + public ITaskWrapper runTaskLaterAsync(@NotNull Plugin plugin, @NotNull Runnable task, long delayTicks) { + return new BukkitTaskWrapper(Bukkit.getScheduler().runTaskLaterAsynchronously(plugin, task, delayTicks)); + } + + @Override + public ITaskWrapper runTaskTimer(@NotNull Plugin plugin, @NotNull Runnable task, long delayTicks, long periodTicks) { + return new BukkitTaskWrapper(Bukkit.getScheduler().runTaskTimer(plugin, task, delayTicks, periodTicks)); + } + + @Override + public ITaskWrapper runTaskTimerAsync(@NotNull Plugin plugin, @NotNull Runnable task, long delayTicks, long periodTicks) { + return new BukkitTaskWrapper(Bukkit.getScheduler().runTaskTimerAsynchronously(plugin, task, delayTicks, periodTicks)); + } + + @Override + public void cancelTask(int taskId) { + Bukkit.getScheduler().cancelTask(taskId); + } + + @Override + public ITaskWrapper runTaskOnEntity(Plugin plugin, Entity entity, Runnable task, Runnable retriedTask) { + return runTask(plugin, task); + } + + @Override + public ITaskWrapper runTaskOnEntityLater(Plugin plugin, Entity entity, Runnable task, Runnable retriedTask, long delayTicks) { + return runTaskLater(plugin, task, delayTicks); + } + + @Override + public ITaskWrapper runTaskOnEntityTimer(Plugin plugin, Entity entity, Runnable task, Runnable retriedTask, long delayTicks, long periodTicks) { + return runTaskTimer(plugin, task, delayTicks, periodTicks); + } + + @Override + public ITaskWrapper runTaskOnLocation(Plugin plugin, Location location, Runnable task) { + return runTask(plugin, task); + } + + @Override + public ITaskWrapper runTaskOnLocationLater(Plugin plugin, Location location, Runnable task, long delayTicks) { + return runTaskLater(plugin, task, delayTicks); + } + + @Override + public ITaskWrapper runTaskOnLocationTimer(Plugin plugin, Location location, Runnable task, long delayTicks, long periodTicks) { + return runTaskTimer(plugin, task, delayTicks, periodTicks); + } + + @Override + public void cancelTasks(@NotNull Plugin plugin) { + Bukkit.getScheduler().cancelTasks(plugin); + } + +} diff --git a/src/main/java/kernitus/plugin/OldCombatMechanics/scheduler/FoliaScheduler.java b/src/main/java/kernitus/plugin/OldCombatMechanics/scheduler/FoliaScheduler.java new file mode 100644 index 00000000..3b2fa096 --- /dev/null +++ b/src/main/java/kernitus/plugin/OldCombatMechanics/scheduler/FoliaScheduler.java @@ -0,0 +1,98 @@ +package kernitus.plugin.OldCombatMechanics.scheduler; + +import io.papermc.paper.threadedregions.scheduler.ScheduledTask; +import kernitus.plugin.OldCombatMechanics.scheduler.task.FoliaTaskWrapper; +import kernitus.plugin.OldCombatMechanics.scheduler.task.ITaskWrapper; +import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.entity.Entity; +import org.bukkit.plugin.Plugin; +import org.jetbrains.annotations.NotNull; + +import java.util.concurrent.TimeUnit; +import java.util.function.Consumer; + +/** + * Folia平台的调度器 + */ +public enum FoliaScheduler implements IScheduler { + + INSTANCE; + + @Override + public ITaskWrapper runTask(@NotNull Plugin plugin, @NotNull Runnable task) { + return new FoliaTaskWrapper(Bukkit.getGlobalRegionScheduler().run(plugin, runnableToConsumer(task))); + } + + @Override + public ITaskWrapper runTaskAsync(@NotNull Plugin plugin, @NotNull Runnable task) { + return new FoliaTaskWrapper(Bukkit.getAsyncScheduler().runNow(plugin, runnableToConsumer(task))); + } + + @Override + public ITaskWrapper runTaskLater(@NotNull Plugin plugin, @NotNull Runnable task, long delayTicks) { + return new FoliaTaskWrapper(Bukkit.getGlobalRegionScheduler().runDelayed(plugin, runnableToConsumer(task), delayTicks)); + } + + @Override + public ITaskWrapper runTaskLaterAsync(@NotNull Plugin plugin, @NotNull Runnable task, long delayTicks) { + return new FoliaTaskWrapper(Bukkit.getAsyncScheduler().runDelayed(plugin, runnableToConsumer(task), delayTicks * 50, TimeUnit.MILLISECONDS)); + } + + @Override + public ITaskWrapper runTaskTimer(@NotNull Plugin plugin, @NotNull Runnable task, long delayTicks, long periodTicks) { + return new FoliaTaskWrapper(Bukkit.getGlobalRegionScheduler().runAtFixedRate(plugin, runnableToConsumer(task), delayTicks, periodTicks)); + } + + @Override + public ITaskWrapper runTaskTimerAsync(@NotNull Plugin plugin, @NotNull Runnable task, long delayTicks, long periodTicks) { + return new FoliaTaskWrapper(Bukkit.getAsyncScheduler().runAtFixedRate(plugin, runnableToConsumer(task), delayTicks * 50, periodTicks * 50, TimeUnit.MILLISECONDS)); + } + + @Override + public void cancelTask(int taskId) { + throw new UnsupportedOperationException("Folia can not cancel task with task id"); + } + + @Override + public ITaskWrapper runTaskOnEntity(Plugin plugin, Entity entity, Runnable task, Runnable retriedTask) { + return new FoliaTaskWrapper(entity.getScheduler().run(plugin, runnableToConsumer(task), retriedTask)); + } + + + @Override + public ITaskWrapper runTaskOnEntityLater(Plugin plugin, Entity entity, Runnable task, Runnable retriedTask, long delayTicks) { + return new FoliaTaskWrapper(entity.getScheduler().runDelayed(plugin, runnableToConsumer(task), retriedTask, delayTicks)); + } + + @Override + public ITaskWrapper runTaskOnEntityTimer(Plugin plugin, Entity entity, Runnable task, Runnable retriedTask, long delayTicks, long periodTicks) { + return new FoliaTaskWrapper(entity.getScheduler().runAtFixedRate(plugin, runnableToConsumer(task), retriedTask, delayTicks, periodTicks)); + } + + @Override + public ITaskWrapper runTaskOnLocation(Plugin plugin, Location location, Runnable task) { + return new FoliaTaskWrapper(Bukkit.getRegionScheduler().run(plugin, location, runnableToConsumer(task))); + } + + @Override + public ITaskWrapper runTaskOnLocationLater(Plugin plugin, Location location, Runnable task, long delayTicks) { + return new FoliaTaskWrapper(Bukkit.getRegionScheduler().runDelayed(plugin, location, runnableToConsumer(task), delayTicks)); + } + + @Override + public ITaskWrapper runTaskOnLocationTimer(Plugin plugin, Location location, Runnable task, long delayTicks, long periodTicks) { + return new FoliaTaskWrapper(Bukkit.getRegionScheduler().runAtFixedRate(plugin, location, runnableToConsumer(task), delayTicks, periodTicks)); + } + + @Override + public void cancelTasks(@NotNull Plugin plugin) { + Bukkit.getGlobalRegionScheduler().cancelTasks(plugin); + Bukkit.getAsyncScheduler().cancelTasks(plugin); + } + + private Consumer runnableToConsumer(Runnable runnable) { + return (final ScheduledTask task) -> runnable.run(); + } + +} diff --git a/src/main/java/kernitus/plugin/OldCombatMechanics/scheduler/IScheduler.java b/src/main/java/kernitus/plugin/OldCombatMechanics/scheduler/IScheduler.java new file mode 100644 index 00000000..83e05b3b --- /dev/null +++ b/src/main/java/kernitus/plugin/OldCombatMechanics/scheduler/IScheduler.java @@ -0,0 +1,159 @@ +package kernitus.plugin.OldCombatMechanics.scheduler; + +import io.papermc.paper.threadedregions.scheduler.ScheduledTask; +import kernitus.plugin.OldCombatMechanics.scheduler.task.ITaskWrapper; +import org.bukkit.Location; +import org.bukkit.entity.Entity; +import org.bukkit.plugin.Plugin; +import org.bukkit.scheduler.BukkitTask; +import org.jetbrains.annotations.NotNull; + +/** + * 平台调度器接口 + */ +public interface IScheduler { + + /** + * 执行一个任务 + * + * @param plugin 调用的插件 + * @param task 需要执行的任务 + */ + ITaskWrapper runTask(@NotNull Plugin plugin, @NotNull Runnable task); + + /** + * 异步执行一个任务 + * + * @param plugin 调用的插件 + * @param task 需要执行的任务 + */ + ITaskWrapper runTaskAsync(@NotNull Plugin plugin, @NotNull Runnable task); + + /** + * 在延迟后执行一个任务 + * + * @param plugin 调用的插件 + * @param task 需要执行的任务 + * @param delayTicks 延迟的时间 + */ + ITaskWrapper runTaskLater(@NotNull Plugin plugin, @NotNull Runnable task, long delayTicks); + + /** + * 在延迟后异步执行一个任务 + * + * @param plugin 调用的插件 + * @param task 需要执行的任务 + * @param delayTicks 延迟的时间,单位为tick + */ + ITaskWrapper runTaskLaterAsync(@NotNull Plugin plugin, @NotNull Runnable task, long delayTicks); + + /** + * 在延迟后循环执行任务 + * + * @param plugin 调用的插件 + * @param task 需要执行的任务 + * @param delayTicks 延迟的时间,单位为tick + * @param periodTicks 循环执行的间隔时间,单位为tick + */ + ITaskWrapper runTaskTimer(@NotNull Plugin plugin, @NotNull Runnable task, long delayTicks, long periodTicks); + + /** + * 在延迟后循环执行任务 + * + * @param plugin 调用的插件 + * @param task 需要执行的任务 + * @param delayTicks 延迟的时间,单位为tick + * @param periodTicks 循环执行的间隔时间,单位为tick + */ + ITaskWrapper runTaskTimerAsync(@NotNull Plugin plugin, @NotNull Runnable task, long delayTicks, long periodTicks); + + /** + * 取消某个任务 + * + * @param task 需要取消的任务的包装 + */ + default void cancelTask(@NotNull ITaskWrapper task) { + if (task instanceof BukkitTask) { + ((BukkitTask) task).cancel(); + } else if (task instanceof ScheduledTask) { + ((ScheduledTask) task).cancel(); + } else { + throw new AssertionError("Illegal task class " + task); + } + } + + void cancelTask(int taskId); + + void cancelTasks(@NotNull Plugin plugin); + + /** + * 在指定实体的调度器上执行任务 + * 当平台为非Folia时,效果等同于runTask + * + * @param plugin 执行的插件 + * @param entity 执行载体 + * @param task 执行的任务 + * @param retriedTask 执行任务失败时, 重新尝试的任务 + */ + ITaskWrapper runTaskOnEntity(Plugin plugin, Entity entity, Runnable task, Runnable retriedTask); + + /** + * 在指定实体的调度器上延迟执行任务 + * 当平台为非Folia时,效果等同于runTaskLater + * + * @param plugin 执行的插件 + * @param entity 执行载体 + * @param task 执行的任务 + * @param retriedTask 执行任务失败时, 重新尝试的任务 + * @param delayTicks 延迟执行的时间 + */ + ITaskWrapper runTaskOnEntityLater(Plugin plugin, Entity entity, Runnable task, Runnable retriedTask, long delayTicks); + + /** + * 在指定实体的调度器上延迟一段时间后重复执行任务 + * 当平台为非Folia时,效果等同于runTaskTimer + * + * @param plugin 执行的插件 + * @param entity 执行载体 + * @param task 执行的任务 + * @param retriedTask 执行任务失败时, 重新尝试的任务 + * @param delayTicks 延迟执行的时间 + * @param periodTicks 重复执行的间隔 + */ + ITaskWrapper runTaskOnEntityTimer(Plugin plugin, Entity entity, Runnable task, Runnable retriedTask, long delayTicks, long periodTicks); + + /** + * 在指定坐标的调度器上执行任务 + * 当平台为非Folia时,效果等同于runTask + * + * @param plugin 执行的插件 + * @param location 执行载体 + * @param task 执行的任务 + */ + ITaskWrapper runTaskOnLocation(Plugin plugin, Location location, Runnable task); + + + /** + * 在指定实体的调度器上延迟执行任务 + * 当平台为非Folia时,效果等同于runTaskLater + * + * @param plugin 执行的插件 + * @param location 执行载体 + * @param task 执行的任务 + * @param delayTicks 延迟执行的时间 + */ + ITaskWrapper runTaskOnLocationLater(Plugin plugin, Location location, Runnable task, long delayTicks); + + /** + * 在指定实体的调度器上延迟一段时间后重复执行任务 + * 当平台为非Folia时,效果等同于runTaskTimer + * + * @param plugin 执行的插件 + * @param location 执行载体 + * @param task 执行的任务 + * @param delayTicks 延迟执行的时间 + * @param periodTicks 重复执行的间隔 + */ + ITaskWrapper runTaskOnLocationTimer(Plugin plugin, Location location, Runnable task, long delayTicks, long periodTicks); + +} diff --git a/src/main/java/kernitus/plugin/OldCombatMechanics/scheduler/ProxyRunnable.java b/src/main/java/kernitus/plugin/OldCombatMechanics/scheduler/ProxyRunnable.java new file mode 100644 index 00000000..8851b0c7 --- /dev/null +++ b/src/main/java/kernitus/plugin/OldCombatMechanics/scheduler/ProxyRunnable.java @@ -0,0 +1,102 @@ +package kernitus.plugin.OldCombatMechanics.scheduler; + +import kernitus.plugin.OldCombatMechanics.scheduler.task.ITaskWrapper; +import org.bukkit.Location; +import org.bukkit.entity.Entity; +import org.bukkit.plugin.Plugin; + +public abstract class ProxyRunnable implements Runnable { + + protected ITaskWrapper taskWrapper; + + @Override + public abstract void run(); + + public ITaskWrapper runTask(Plugin plugin) { + checkTaskNotNull(); + return setTaskWrapper(SchedulerManager.INSTANCE.getScheduler().runTask(plugin, this)); + } + + public ITaskWrapper runTaskLater(Plugin plugin, long delayTicks) { + checkTaskNotNull(); + return setTaskWrapper(SchedulerManager.INSTANCE.getScheduler().runTaskLater(plugin, this, delayTicks)); + } + + public ITaskWrapper runTaskTimer(Plugin plugin, long delayTicks, long periodTicks) { + checkTaskNotNull(); + return setTaskWrapper(SchedulerManager.INSTANCE.getScheduler().runTaskTimer(plugin, this, delayTicks, periodTicks)); + } + + public ITaskWrapper runTaskAsync(Plugin plugin) { + checkTaskNotNull(); + return setTaskWrapper(SchedulerManager.INSTANCE.getScheduler().runTaskAsync(plugin, this)); + } + + public ITaskWrapper runTaskLaterAsync(Plugin plugin, long delayTicks) { + checkTaskNotNull(); + return setTaskWrapper(SchedulerManager.INSTANCE.getScheduler().runTaskLaterAsync(plugin, this, delayTicks)); + } + + public ITaskWrapper runTaskTimerAsync(Plugin plugin, long delayTicks, long periodTicks) { + checkTaskNotNull(); + return setTaskWrapper(SchedulerManager.INSTANCE.getScheduler().runTaskTimerAsync(plugin, this, delayTicks, periodTicks)); + } + + public ITaskWrapper runTaskOnLocation(Plugin plugin, Location location) { + checkTaskNotNull(); + return setTaskWrapper(SchedulerManager.INSTANCE.getScheduler().runTaskOnLocation(plugin, location, this)); + } + + public ITaskWrapper runTaskOnLocationLater(Plugin plugin, Location location, long delayTicks) { + checkTaskNotNull(); + return setTaskWrapper(SchedulerManager.INSTANCE.getScheduler().runTaskOnLocationLater(plugin, location, this, delayTicks)); + } + + public ITaskWrapper runTaskOnLocationTimer(Plugin plugin, Location location, long delayTicks, long periodTicks) { + checkTaskNotNull(); + return setTaskWrapper(SchedulerManager.INSTANCE.getScheduler().runTaskOnLocationTimer(plugin, location, this, delayTicks, periodTicks)); + } + + public ITaskWrapper runTaskOnEntity(Plugin plugin, Entity entity) { + checkTaskNotNull(); + return setTaskWrapper(SchedulerManager.INSTANCE.getScheduler().runTaskOnEntity(plugin, entity, this, this)); + } + + public ITaskWrapper runTaskOnEntityLater(Plugin plugin, Entity entity, long delayTicks) { + checkTaskNotNull(); + return setTaskWrapper(SchedulerManager.INSTANCE.getScheduler().runTaskOnEntityLater(plugin, entity, this, this, delayTicks)); + } + + public ITaskWrapper runTaskOnEntityTimer(Plugin plugin, Entity entity, long delayTicks, long periodTicks) { + checkTaskNotNull(); + return setTaskWrapper(SchedulerManager.INSTANCE.getScheduler().runTaskOnEntityTimer(plugin, entity, this, this, delayTicks, periodTicks)); + } + + public void cancel() { + checkTaskNull(); + this.taskWrapper.cancel(); + } + + public boolean isCancelled() { + checkTaskNull(); + return this.taskWrapper.isCancelled(); + } + + protected ITaskWrapper setTaskWrapper(ITaskWrapper taskWrapper) { + this.taskWrapper = taskWrapper; + return this.taskWrapper; + } + + protected void checkTaskNotNull() { + if (this.taskWrapper != null) { + throw new IllegalArgumentException("Runnable is null"); + } + } + + protected void checkTaskNull() { + if (this.taskWrapper == null) { + throw new IllegalArgumentException("Task is null"); + } + } + +} diff --git a/src/main/java/kernitus/plugin/OldCombatMechanics/scheduler/SchedulerManager.java b/src/main/java/kernitus/plugin/OldCombatMechanics/scheduler/SchedulerManager.java new file mode 100644 index 00000000..845776a1 --- /dev/null +++ b/src/main/java/kernitus/plugin/OldCombatMechanics/scheduler/SchedulerManager.java @@ -0,0 +1,22 @@ +package kernitus.plugin.OldCombatMechanics.scheduler; + +public enum SchedulerManager { + + INSTANCE; + private IScheduler scheduler; + + public void loadPlatform() { + try { + Class pluginMetaClass = Class.forName("io.papermc.paper.plugin.configuration.PluginMeta"); + pluginMetaClass.getMethod("isFoliaSupported"); + scheduler = FoliaScheduler.INSTANCE; + } catch (ClassNotFoundException | NoSuchMethodException e) { + scheduler = BukkitScheduler.INSTANCE; + } + } + + public IScheduler getScheduler() { + return scheduler; + } + +} diff --git a/src/main/java/kernitus/plugin/OldCombatMechanics/scheduler/task/BukkitTaskWrapper.java b/src/main/java/kernitus/plugin/OldCombatMechanics/scheduler/task/BukkitTaskWrapper.java new file mode 100644 index 00000000..41aa27fa --- /dev/null +++ b/src/main/java/kernitus/plugin/OldCombatMechanics/scheduler/task/BukkitTaskWrapper.java @@ -0,0 +1,42 @@ +package kernitus.plugin.OldCombatMechanics.scheduler.task; + +import org.bukkit.plugin.Plugin; +import org.bukkit.scheduler.BukkitTask; +import org.jetbrains.annotations.NotNull; + +public class BukkitTaskWrapper implements ITaskWrapper { + + private final BukkitTask bukkitTask; + + public BukkitTaskWrapper(@NotNull BukkitTask bukkitTask) { + this.bukkitTask = bukkitTask; + } + + @Override + public void cancel() { + bukkitTask.cancel(); + } + + @Override + @NotNull + public Plugin owner() { + return bukkitTask.getOwner(); + } + + @Override + public Integer taskId() { + return bukkitTask.getTaskId(); + } + + @Override + public boolean isCancelled() { + return bukkitTask.isCancelled(); + } + + @Override + @NotNull + public BukkitTask platformTask() { + return bukkitTask; + } + +} diff --git a/src/main/java/kernitus/plugin/OldCombatMechanics/scheduler/task/FoliaTaskWrapper.java b/src/main/java/kernitus/plugin/OldCombatMechanics/scheduler/task/FoliaTaskWrapper.java new file mode 100644 index 00000000..d03701b4 --- /dev/null +++ b/src/main/java/kernitus/plugin/OldCombatMechanics/scheduler/task/FoliaTaskWrapper.java @@ -0,0 +1,40 @@ +package kernitus.plugin.OldCombatMechanics.scheduler.task; + +import io.papermc.paper.threadedregions.scheduler.ScheduledTask; +import org.bukkit.plugin.Plugin; +import org.jetbrains.annotations.NotNull; + +public class FoliaTaskWrapper implements ITaskWrapper { + + private final ScheduledTask scheduledTask; + + public FoliaTaskWrapper(ScheduledTask scheduledTask) { + this.scheduledTask = scheduledTask; + } + + @Override + public void cancel() { + scheduledTask.cancel(); + } + + @Override + public @NotNull Plugin owner() { + return scheduledTask.getOwningPlugin(); + } + + @Override + public Integer taskId() { + throw new UnsupportedOperationException("Folia task can not get task id"); + } + + @Override + public boolean isCancelled() { + return scheduledTask.isCancelled(); + } + + @Override + public @NotNull ScheduledTask platformTask() { + return scheduledTask; + } + +} diff --git a/src/main/java/kernitus/plugin/OldCombatMechanics/scheduler/task/ITaskWrapper.java b/src/main/java/kernitus/plugin/OldCombatMechanics/scheduler/task/ITaskWrapper.java new file mode 100644 index 00000000..e8d3baac --- /dev/null +++ b/src/main/java/kernitus/plugin/OldCombatMechanics/scheduler/task/ITaskWrapper.java @@ -0,0 +1,43 @@ +package kernitus.plugin.OldCombatMechanics.scheduler.task; + +import org.bukkit.plugin.Plugin; +import org.jetbrains.annotations.NotNull; + +public interface ITaskWrapper { + + /** + * 取消此任务 + */ + void cancel(); + + /** + * 获取此任务的所属插件 + * + * @return 此任务的所属插件 + */ + @NotNull + Plugin owner(); + + /** + * 获取此任务的Task Id,Folia平台不支持此方法 + * + * @return 此任务的Task id + */ + Integer taskId(); + + /** + * 获取此任务是否被取消 + * + * @return 此任务是否被取消 + */ + boolean isCancelled(); + + /** + * 返回对应平台的原始Task类型 + * + * @return 对应平台的原始Task类型 + */ + @NotNull + Object platformTask(); + +} diff --git a/src/main/java/kernitus/plugin/OldCombatMechanics/tester/FakePlayer.java b/src/main/java/kernitus/plugin/OldCombatMechanics/tester/FakePlayer.java index 115e918e..2349e1fa 100644 --- a/src/main/java/kernitus/plugin/OldCombatMechanics/tester/FakePlayer.java +++ b/src/main/java/kernitus/plugin/OldCombatMechanics/tester/FakePlayer.java @@ -11,6 +11,8 @@ import io.netty.channel.ChannelInboundHandlerAdapter; import io.netty.channel.embedded.EmbeddedChannel; import kernitus.plugin.OldCombatMechanics.OCMMain; +import kernitus.plugin.OldCombatMechanics.scheduler.SchedulerManager; +import kernitus.plugin.OldCombatMechanics.scheduler.task.ITaskWrapper; import kernitus.plugin.OldCombatMechanics.utilities.reflection.Reflector; import net.minecraft.network.Connection; import net.minecraft.network.protocol.Packet; @@ -69,12 +71,12 @@ public class FakePlayer { private final String name; private ServerPlayer entityPlayer; private Player bukkitPlayer; - private Integer tickTaskId; + private ITaskWrapper tickTask; public FakePlayer() { uuid = UUID.randomUUID(); name = uuid.toString().substring(0, 16); - tickTaskId = null; + tickTask = null; } public UUID getUuid() { @@ -150,12 +152,12 @@ public void spawn(Location location) { // Spawn the player for the client sendPacket(new ClientboundAddPlayerPacket(entityPlayer)); - tickTaskId = Bukkit.getScheduler().scheduleSyncRepeatingTask(OCMMain.getInstance(), entityPlayer::tick, 1, 1); + tickTask = SchedulerManager.INSTANCE.getScheduler().runTaskTimer(OCMMain.getInstance(), entityPlayer::tick, 1, 1); } public void removePlayer() { - if(tickTaskId != null) Bukkit.getScheduler().cancelTask(tickTaskId); - tickTaskId = null; + if(tickTask != null) SchedulerManager.INSTANCE.getScheduler().cancelTask(tickTask); + tickTask = null; final MinecraftServer mcServer = ((CraftServer) Bukkit.getServer()).getServer(); diff --git a/src/main/java/kernitus/plugin/OldCombatMechanics/tester/InGameTester.java b/src/main/java/kernitus/plugin/OldCombatMechanics/tester/InGameTester.java index abfabc8a..90d6709a 100644 --- a/src/main/java/kernitus/plugin/OldCombatMechanics/tester/InGameTester.java +++ b/src/main/java/kernitus/plugin/OldCombatMechanics/tester/InGameTester.java @@ -6,6 +6,7 @@ package kernitus.plugin.OldCombatMechanics.tester; import kernitus.plugin.OldCombatMechanics.OCMMain; +import kernitus.plugin.OldCombatMechanics.scheduler.SchedulerManager; import kernitus.plugin.OldCombatMechanics.utilities.Messenger; import kernitus.plugin.OldCombatMechanics.utilities.damage.DamageUtils; import kernitus.plugin.OldCombatMechanics.utilities.damage.DefenceUtils; @@ -304,7 +305,7 @@ public void onEvent(EntityDamageByEntityEvent e) { for (OCMTest test : testQueue) { attackDelay += test.attackDelay; - Bukkit.getScheduler().runTaskLater(ocm, () -> { + SchedulerManager.INSTANCE.getScheduler().runTaskLater(ocm, () -> { beforeEach(); test.preparations.run(); preparePlayer(test.weapon); @@ -314,7 +315,7 @@ public void onEvent(EntityDamageByEntityEvent e) { } - Bukkit.getScheduler().runTaskLater(ocm, () -> { + SchedulerManager.INSTANCE.getScheduler().runTaskLater(ocm, () -> { afterAll(testCount); EntityDamageByEntityEvent.getHandlerList().unregister(listener); }, attackDelay + 1); diff --git a/src/main/java/kernitus/plugin/OldCombatMechanics/updater/ModuleUpdateChecker.java b/src/main/java/kernitus/plugin/OldCombatMechanics/updater/ModuleUpdateChecker.java index 71f69a43..8a10598a 100644 --- a/src/main/java/kernitus/plugin/OldCombatMechanics/updater/ModuleUpdateChecker.java +++ b/src/main/java/kernitus/plugin/OldCombatMechanics/updater/ModuleUpdateChecker.java @@ -8,6 +8,7 @@ import kernitus.plugin.OldCombatMechanics.OCMMain; import kernitus.plugin.OldCombatMechanics.UpdateChecker; import kernitus.plugin.OldCombatMechanics.module.OCMModule; +import kernitus.plugin.OldCombatMechanics.scheduler.SchedulerManager; import org.bukkit.Bukkit; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; @@ -23,7 +24,7 @@ public ModuleUpdateChecker(OCMMain plugin) { public void onPlayerLogin(PlayerJoinEvent e) { final Player player = e.getPlayer(); if (player.hasPermission("OldCombatMechanics.notify")) - Bukkit.getScheduler().runTaskLaterAsynchronously(plugin, + SchedulerManager.INSTANCE.getScheduler().runTaskLaterAsync(plugin, () -> new UpdateChecker(plugin).performUpdate(), 20L); } } diff --git a/src/main/java/kernitus/plugin/OldCombatMechanics/utilities/damage/AttackCooldownTracker.java b/src/main/java/kernitus/plugin/OldCombatMechanics/utilities/damage/AttackCooldownTracker.java index 26ac748c..20e1e154 100644 --- a/src/main/java/kernitus/plugin/OldCombatMechanics/utilities/damage/AttackCooldownTracker.java +++ b/src/main/java/kernitus/plugin/OldCombatMechanics/utilities/damage/AttackCooldownTracker.java @@ -2,6 +2,7 @@ import kernitus.plugin.OldCombatMechanics.OCMMain; import kernitus.plugin.OldCombatMechanics.module.OCMModule; +import kernitus.plugin.OldCombatMechanics.scheduler.SchedulerManager; import kernitus.plugin.OldCombatMechanics.utilities.reflection.VersionCompatUtils; import org.bukkit.Bukkit; import org.bukkit.World; @@ -31,7 +32,7 @@ public AttackCooldownTracker(OCMMain plugin) { player -> lastCooldown.put(player.getUniqueId(), VersionCompatUtils.getAttackCooldown(player) )); - Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, cooldownTask, 0, 1L); + SchedulerManager.INSTANCE.getScheduler().runTaskTimer(plugin, cooldownTask, 0, 1L); } @EventHandler diff --git a/src/main/java/kernitus/plugin/OldCombatMechanics/utilities/damage/EntityDamageByEntityListener.java b/src/main/java/kernitus/plugin/OldCombatMechanics/utilities/damage/EntityDamageByEntityListener.java index a6b79d3a..a5ad19e5 100644 --- a/src/main/java/kernitus/plugin/OldCombatMechanics/utilities/damage/EntityDamageByEntityListener.java +++ b/src/main/java/kernitus/plugin/OldCombatMechanics/utilities/damage/EntityDamageByEntityListener.java @@ -7,6 +7,7 @@ import kernitus.plugin.OldCombatMechanics.OCMMain; import kernitus.plugin.OldCombatMechanics.module.OCMModule; +import kernitus.plugin.OldCombatMechanics.scheduler.SchedulerManager; import org.bukkit.Bukkit; import org.bukkit.entity.Entity; import org.bukkit.entity.HumanEntity; @@ -184,7 +185,7 @@ public void afterEntityDamage(EntityDamageEvent event) { if(event instanceof EntityDamageByEntityEvent) { if (lastDamages.containsKey(damagee.getUniqueId())) { // Set last damage to 0, so we can detect attacks even by weapons with a weaker attack value than what OCM would calculate - Bukkit.getScheduler().runTaskLater(plugin, () -> { + SchedulerManager.INSTANCE.getScheduler().runTaskLater(plugin, () -> { ((LivingEntity) damagee).setLastDamage(0); debug("Set last damage to 0", damagee); }, 1L); diff --git a/src/main/java/kernitus/plugin/OldCombatMechanics/utilities/storage/PlayerStorage.java b/src/main/java/kernitus/plugin/OldCombatMechanics/utilities/storage/PlayerStorage.java index 4c1a3edb..64fd1cb2 100644 --- a/src/main/java/kernitus/plugin/OldCombatMechanics/utilities/storage/PlayerStorage.java +++ b/src/main/java/kernitus/plugin/OldCombatMechanics/utilities/storage/PlayerStorage.java @@ -13,6 +13,8 @@ package kernitus.plugin.OldCombatMechanics.utilities.storage; import kernitus.plugin.OldCombatMechanics.OCMMain; +import kernitus.plugin.OldCombatMechanics.scheduler.SchedulerManager; +import kernitus.plugin.OldCombatMechanics.scheduler.task.ITaskWrapper; import org.bson.*; import org.bson.codecs.*; import org.bson.codecs.configuration.CodecRegistries; @@ -40,7 +42,7 @@ public class PlayerStorage { private static Path dataFilePath; private static DocumentCodec documentCodec; private static Document data; - private static final AtomicReference saveTask = new AtomicReference<>(); + private static final AtomicReference saveTask = new AtomicReference<>(); private static CodecRegistry codecRegistry; public static void initialise(OCMMain plugin) { @@ -76,7 +78,7 @@ private static Document loadData() { public static void scheduleSave() { // Schedule a task for later, if there isn't one already scheduled saveTask.compareAndSet(null, - Bukkit.getScheduler().runTaskLaterAsynchronously(plugin, () -> { + SchedulerManager.INSTANCE.getScheduler().runTaskLaterAsync(plugin, () -> { instantSave(); saveTask.set(null); }, 2400L) // Save after 2 minutes diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 27331ab2..1acd271c 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -10,7 +10,7 @@ website: https://github.com/kernitus/BukkitOldCombatMechanics load: POSTWORLD softdepend: [ProtocolLib, PlaceholderAPI, Spartan] api-version: 1.13 - +folia-supported: true commands: OldCombatMechanics: description: OldCombatMechanics's main command From 427432604bcb133b17df76c7f629df54dca5addc Mon Sep 17 00:00:00 2001 From: YufiriaMazenta Date: Tue, 12 Mar 2024 09:47:10 +0800 Subject: [PATCH 2/3] Translate comments --- build.gradle.kts | 3 +- .../scheduler/IScheduler.java | 135 +++++++++--------- .../scheduler/task/ITaskWrapper.java | 15 +- 3 files changed, 77 insertions(+), 76 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index e1d801aa..c55cb4cc 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -56,7 +56,7 @@ dependencies { // Spigot // compileOnly("org.spigotmc:spigot-api:1.20.3-R0.1-SNAPSHOT") // Folia - compileOnly("dev.folia:folia-api:1.20.4-R0.1-SNAPSHOT") + compileOnly("dev.folia:folia-api:1.19.4-R0.1-SNAPSHOT") // ProtocolLib compileOnly("com.comphenix.protocol:ProtocolLib:5.1.0") @@ -75,7 +75,6 @@ description = "OldCombatMechanics" java { toolchain { - // At least 17 required for MC 1.19 for ingametesting languageVersion.set(JavaLanguageVersion.of(17)) } } diff --git a/src/main/java/kernitus/plugin/OldCombatMechanics/scheduler/IScheduler.java b/src/main/java/kernitus/plugin/OldCombatMechanics/scheduler/IScheduler.java index 83e05b3b..3735cd7a 100644 --- a/src/main/java/kernitus/plugin/OldCombatMechanics/scheduler/IScheduler.java +++ b/src/main/java/kernitus/plugin/OldCombatMechanics/scheduler/IScheduler.java @@ -9,68 +9,68 @@ import org.jetbrains.annotations.NotNull; /** - * 平台调度器接口 + * Platform scheduler interface */ public interface IScheduler { /** - * 执行一个任务 + * Schedules this in server scheduler to run on next tick. * - * @param plugin 调用的插件 - * @param task 需要执行的任务 + * @param plugin the reference to the plugin scheduling task + * @param task the task to be run */ ITaskWrapper runTask(@NotNull Plugin plugin, @NotNull Runnable task); /** - * 异步执行一个任务 + * Schedules this in server scheduler to run asynchronously. * - * @param plugin 调用的插件 - * @param task 需要执行的任务 + * @param plugin the reference to the plugin scheduling task + * @param task the task to be run */ ITaskWrapper runTaskAsync(@NotNull Plugin plugin, @NotNull Runnable task); /** - * 在延迟后执行一个任务 + * Schedules this to run after the specified number of server ticks. * - * @param plugin 调用的插件 - * @param task 需要执行的任务 - * @param delayTicks 延迟的时间 + * @param plugin the reference to the plugin scheduling task + * @param task the task to be run + * @param delayTicks the ticks to wait before running the task */ ITaskWrapper runTaskLater(@NotNull Plugin plugin, @NotNull Runnable task, long delayTicks); /** - * 在延迟后异步执行一个任务 + * Schedules this to run asynchronously after the specified number of server ticks. * - * @param plugin 调用的插件 - * @param task 需要执行的任务 - * @param delayTicks 延迟的时间,单位为tick + * @param plugin the reference to the plugin scheduling task + * @param task the task to be run + * @param delayTicks the ticks to wait before running the task */ ITaskWrapper runTaskLaterAsync(@NotNull Plugin plugin, @NotNull Runnable task, long delayTicks); /** - * 在延迟后循环执行任务 + * Returns a task that will repeatedly run until cancelled, starting after the specified number of server ticks. * - * @param plugin 调用的插件 - * @param task 需要执行的任务 - * @param delayTicks 延迟的时间,单位为tick - * @param periodTicks 循环执行的间隔时间,单位为tick + * @param plugin the reference to the plugin scheduling task + * @param task the task to be run + * @param delayTicks the ticks to wait before running the task + * @param periodTicks the ticks to wait between runs */ ITaskWrapper runTaskTimer(@NotNull Plugin plugin, @NotNull Runnable task, long delayTicks, long periodTicks); /** - * 在延迟后循环执行任务 + * Returns a task that will repeatedly run asynchronously until cancelled, starting after the specified number of server ticks * - * @param plugin 调用的插件 - * @param task 需要执行的任务 - * @param delayTicks 延迟的时间,单位为tick - * @param periodTicks 循环执行的间隔时间,单位为tick + * @param plugin the reference to the plugin scheduling task + * @param task the task to be run + * @param delayTicks the ticks to wait before running the task + * @param periodTicks the ticks to wait between runs */ ITaskWrapper runTaskTimerAsync(@NotNull Plugin plugin, @NotNull Runnable task, long delayTicks, long periodTicks); /** - * 取消某个任务 + * Removes task from scheduler. * - * @param task 需要取消的任务的包装 + * @param task the task to be removed */ default void cancelTask(@NotNull ITaskWrapper task) { if (task instanceof BukkitTask) { @@ -87,72 +87,75 @@ default void cancelTask(@NotNull ITaskWrapper task) { void cancelTasks(@NotNull Plugin plugin); /** - * 在指定实体的调度器上执行任务 - * 当平台为非Folia时,效果等同于runTask + * Schedules a task to execute on the next tick. If the task failed to schedule because the scheduler is retired (entity removed), then returns null. Otherwise, either the task callback will be invoked after the specified delay, or the retired callback will be invoked if the scheduler is retired. Note that the retired callback is invoked in critical code, so it should not attempt to remove the entity, remove other entities, load chunks, load worlds, modify ticket levels, etc. + * It is guaranteed that the task and retired callback are invoked on the region which owns the entity. + * When the platform is not Folia, the effect is equivalent to runTask * - * @param plugin 执行的插件 - * @param entity 执行载体 - * @param task 执行的任务 - * @param retriedTask 执行任务失败时, 重新尝试的任务 + * @param plugin the plugin that owns the task + * @param entity the task's owning entity + * @param task the task to be run + * @param retriedTask retire callback to run if the entity is retired before the run callback can be invoked, may be null. */ ITaskWrapper runTaskOnEntity(Plugin plugin, Entity entity, Runnable task, Runnable retriedTask); /** - * 在指定实体的调度器上延迟执行任务 - * 当平台为非Folia时,效果等同于runTaskLater + * Schedules a task with the given delay. If the task failed to schedule because the scheduler is retired (entity removed), then returns null. Otherwise, either the task callback will be invoked after the specified delay, or the retired callback will be invoked if the scheduler is retired. Note that the retired callback is invoked in critical code, so it should not attempt to remove the entity, remove other entities, load chunks, load worlds, modify ticket levels, etc. + * It is guaranteed that the task and retired callback are invoked on the region which owns the entity. + * When the platform is not Folia, the effect is equivalent to runTaskLater * - * @param plugin 执行的插件 - * @param entity 执行载体 - * @param task 执行的任务 - * @param retriedTask 执行任务失败时, 重新尝试的任务 - * @param delayTicks 延迟执行的时间 + * @param plugin the plugin that owns the task + * @param entity the task's owning entity + * @param task the task to be run + * @param retriedTask retire callback to run if the entity is retired before the run callback can be invoked, may be null. + * @param delayTicks the ticks to wait before running the task */ ITaskWrapper runTaskOnEntityLater(Plugin plugin, Entity entity, Runnable task, Runnable retriedTask, long delayTicks); /** - * 在指定实体的调度器上延迟一段时间后重复执行任务 - * 当平台为非Folia时,效果等同于runTaskTimer + * Schedules a repeating task with the given delay and period. If the task failed to schedule because the scheduler is retired (entity removed), then returns null. Otherwise, either the task callback will be invoked after the specified delay, or the retired callback will be invoked if the scheduler is retired. Note that the retired callback is invoked in critical code, so it should not attempt to remove the entity, remove other entities, load chunks, load worlds, modify ticket levels, etc. + * It is guaranteed that the task and retired callback are invoked on the region which owns the entity. + * When the platform is not Folia, the effect is equivalent to runTaskTimer * - * @param plugin 执行的插件 - * @param entity 执行载体 - * @param task 执行的任务 - * @param retriedTask 执行任务失败时, 重新尝试的任务 - * @param delayTicks 延迟执行的时间 - * @param periodTicks 重复执行的间隔 + * @param plugin the plugin that owns the task + * @param entity the task's owning entity + * @param task the task to be run + * @param retriedTask retire callback to run if the entity is retired before the run callback can be invoked, may be null. + * @param delayTicks the ticks to wait before running the task + * @param periodTicks the ticks to wait between runs */ ITaskWrapper runTaskOnEntityTimer(Plugin plugin, Entity entity, Runnable task, Runnable retriedTask, long delayTicks, long periodTicks); /** - * 在指定坐标的调度器上执行任务 - * 当平台为非Folia时,效果等同于runTask + * Schedules a task to be executed on the region which owns the location on the next tick. + * When the platform is not Folia, the effect is equivalent to runTask * - * @param plugin 执行的插件 - * @param location 执行载体 - * @param task 执行的任务 + * @param plugin the plugin that owns the task + * @param location used to get the region which the task belongs + * @param task the task to be run */ ITaskWrapper runTaskOnLocation(Plugin plugin, Location location, Runnable task); /** - * 在指定实体的调度器上延迟执行任务 - * 当平台为非Folia时,效果等同于runTaskLater + * Schedules a task to be executed on the region which owns the location after the specified delay in ticks. + * When the platform is not Folia, the effect is equivalent to runTaskLater * - * @param plugin 执行的插件 - * @param location 执行载体 - * @param task 执行的任务 - * @param delayTicks 延迟执行的时间 + * @param plugin the plugin that owns the task + * @param location used to get the region which the task belongs + * @param task the task to be run + * @param delayTicks the ticks to wait before running the task */ ITaskWrapper runTaskOnLocationLater(Plugin plugin, Location location, Runnable task, long delayTicks); /** - * 在指定实体的调度器上延迟一段时间后重复执行任务 - * 当平台为非Folia时,效果等同于runTaskTimer + * Schedules a repeating task to be executed on the region which owns the location after the initial delay with the specified period. + * When the platform is not Folia, the effect is equivalent to runTaskTimer * - * @param plugin 执行的插件 - * @param location 执行载体 - * @param task 执行的任务 - * @param delayTicks 延迟执行的时间 - * @param periodTicks 重复执行的间隔 + * @param plugin the plugin that owns the task + * @param location used to get the region which the task belongs + * @param task the task to be run + * @param delayTicks the ticks to wait before running the task + * @param periodTicks the ticks to wait between runs */ ITaskWrapper runTaskOnLocationTimer(Plugin plugin, Location location, Runnable task, long delayTicks, long periodTicks); diff --git a/src/main/java/kernitus/plugin/OldCombatMechanics/scheduler/task/ITaskWrapper.java b/src/main/java/kernitus/plugin/OldCombatMechanics/scheduler/task/ITaskWrapper.java index e8d3baac..04e4b91a 100644 --- a/src/main/java/kernitus/plugin/OldCombatMechanics/scheduler/task/ITaskWrapper.java +++ b/src/main/java/kernitus/plugin/OldCombatMechanics/scheduler/task/ITaskWrapper.java @@ -11,31 +11,30 @@ public interface ITaskWrapper { void cancel(); /** - * 获取此任务的所属插件 + * returns the plugin that owns the task * - * @return 此任务的所属插件 + * @return the plugin that owns the task */ @NotNull Plugin owner(); /** - * 获取此任务的Task Id,Folia平台不支持此方法 + * Returns the Task ID of this task. The Folia platform does not support this method. * - * @return 此任务的Task id + * @return the task id of this task */ Integer taskId(); /** - * 获取此任务是否被取消 + * Returns whether this task has been canceled * - * @return 此任务是否被取消 */ boolean isCancelled(); /** - * 返回对应平台的原始Task类型 + * Returns the original Task object corresponding to the platform * - * @return 对应平台的原始Task类型 + * @return the original Task object corresponding to the platform */ @NotNull Object platformTask(); From 987639b640ebb07594df4e0089d2c6d459732665 Mon Sep 17 00:00:00 2001 From: YufiriaMazenta Date: Fri, 15 Mar 2024 10:12:03 +0800 Subject: [PATCH 3/3] Modified to build with java8 --- build.gradle.kts | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index c55cb4cc..510bb8b9 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -56,10 +56,17 @@ dependencies { // Spigot // compileOnly("org.spigotmc:spigot-api:1.20.3-R0.1-SNAPSHOT") // Folia - compileOnly("dev.folia:folia-api:1.19.4-R0.1-SNAPSHOT") + compileOnly("dev.folia:folia-api:1.19.4-R0.1-20230527.192927-40@jar") // ProtocolLib compileOnly("com.comphenix.protocol:ProtocolLib:5.1.0") - + // Guava + compileOnly("com.google.guava:guava:33.0.0-jre") + // Gson + compileOnly("com.google.code.gson:gson:2.10.1") + // Adventure + compileOnly("net.kyori:adventure-api:4.16.0") + // BungeeCord Chat + compileOnly("net.md-5:bungeecord-chat:1.16-R0.4") /* For ingametesting // Mojang mappings for NMS compileOnly("com.mojang:authlib:4.0.43") @@ -74,9 +81,8 @@ version = "2.0.2-beta" description = "OldCombatMechanics" java { - toolchain { - languageVersion.set(JavaLanguageVersion.of(17)) - } + sourceCompatibility = JavaVersion.VERSION_1_8 + targetCompatibility = JavaVersion.VERSION_1_8 } sourceSets {