Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Folia Support #720

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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/")
}
Expand All @@ -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")

Expand All @@ -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))
kernitus marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -58,6 +59,7 @@ public OCMMain() {
public void onEnable() {
INSTANCE = this;

SchedulerManager.INSTANCE.loadPlatform();
// Setting up config.yml
CH.setupConfigIfNotPresent();

Expand Down Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -178,7 +179,7 @@ public void onItemConsume(PlayerItemConsumeEvent e) {
final Set<PotionEffectType> 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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -80,7 +81,7 @@ public void onPlayerExplosionDamage(EntityDamageEvent e) {
final List<ItemStack> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand All @@ -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() +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -88,7 +89,7 @@ public void onHit(EntityDamageByEntityEvent e) {
final List<ItemStack> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<UUID, ItemStack> storedItems = new HashMap<>();
private final Map<UUID, Collection<BukkitTask>> correspondingTasks = new HashMap<>();
private final Map<UUID, Collection<ITaskWrapper>> correspondingTasks = new HashMap<>();
private int restoreDelay;

public ModuleSwordBlocking(OCMMain plugin) {
Expand Down Expand Up @@ -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<BukkitTask> tasks = new ArrayList<>(2);
final List<ITaskWrapper> tasks = new ArrayList<>(2);
tasks.add(removeItem);
tasks.add(checkBlocking);
correspondingTasks.put(p.getUniqueId(), tasks);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -30,7 +32,7 @@ public class ModuleSwordSweep extends OCMModule {

private final List<Location> sweepLocations = new ArrayList<>();
private EntityDamageEvent.DamageCause sweepDamageCause;
private BukkitTask task;
private ITaskWrapper task;

public ModuleSwordSweep(OCMMain plugin) {
super(plugin, "disable-sword-sweep");
Expand All @@ -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);
}


Expand Down
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is a whole separate class needed that just wraps other methods? Does folia not provide a better way of doing this?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Folia does not provide a unified API. The method it provides to determine whether it is the folia only exists on the paper above 1.19.4, so there is currently no better way. The purpose of my separate encapsulation is for versatility.

Original file line number Diff line number Diff line change
@@ -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平台的调度器
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you translate this to english please

*/
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);
}

}
Loading