Skip to content

Commit

Permalink
Folia Support
Browse files Browse the repository at this point in the history
  • Loading branch information
Axionize committed Oct 11, 2024
1 parent 34be127 commit a1404bb
Show file tree
Hide file tree
Showing 11 changed files with 178 additions and 14 deletions.
10 changes: 10 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@
<id>opencollab</id>
<url>https://repo.opencollab.dev/maven-snapshots/</url>
</repository>
<repository> <!--folia-api-->
<id>papermc-repo</id>
<url>https://repo.papermc.io/repository/maven-public/</url>
</repository>
</repositories>

<dependencies>
Expand Down Expand Up @@ -129,5 +133,11 @@
<artifactId>bstats-bukkit</artifactId>
<version>3.0.2</version>
</dependency>
<dependency>
<groupId>dev.folia</groupId>
<artifactId>folia-api</artifactId>
<version>1.20.4-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
16 changes: 10 additions & 6 deletions src/main/java/me/caseload/knockbacksync/KnockbackSync.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@
import me.caseload.knockbacksync.command.MainCommand;
import me.caseload.knockbacksync.listener.*;
import me.caseload.knockbacksync.manager.ConfigManager;
import me.caseload.knockbacksync.stats.BuildTypePie;
import me.caseload.knockbacksync.stats.PlayerVersionsPie;
import me.caseload.knockbacksync.scheduler.BukkitSchedulerAdapter;
import me.caseload.knockbacksync.scheduler.FoliaSchedulerAdapter;
import me.caseload.knockbacksync.scheduler.SchedulerAdapter;
import me.caseload.knockbacksync.stats.StatsManager;
import org.bstats.bukkit.Metrics;
import org.bukkit.Bukkit;
import org.bukkit.event.Listener;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
Expand All @@ -23,7 +22,10 @@
public final class KnockbackSync extends JavaPlugin {

public static Logger LOGGER;
public static JavaPlugin INSTANCE;
public static KnockbackSync INSTANCE;
@Getter
private SchedulerAdapter scheduler;
private final boolean isFolia = io.github.retrooper.packetevents.util.folia.FoliaScheduler.isFolia();

@Getter
private final ConfigManager configManager = new ConfigManager();
Expand All @@ -41,6 +43,8 @@ public void onEnable() {
INSTANCE = this;
checkForUpdates();

scheduler = isFolia ? new FoliaSchedulerAdapter(this) : new BukkitSchedulerAdapter(this);

saveDefaultConfig();
configManager.loadConfig(false);

Expand Down Expand Up @@ -87,7 +91,7 @@ private void registerListeners(Listener... listeners) {
private void checkForUpdates() {
getLogger().info("Checking for updates...");

Bukkit.getScheduler().runTaskAsynchronously(this, () -> {
scheduler.runTaskAsynchronously(() -> {
try {
GitHub github = GitHub.connectAnonymously();
String latestVersion = github.getRepository("CASELOAD7000/knockback-sync")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import lombok.Setter;
import me.caseload.knockbacksync.KnockbackSync;
import me.caseload.knockbacksync.runnable.PingRunnable;
import me.caseload.knockbacksync.scheduler.AbstractTaskHandle;
import org.bukkit.scheduler.BukkitTask;

@Getter
Expand All @@ -26,7 +27,7 @@ public class ConfigManager {
private String playerIneligibleMessage;
private String reloadMessage;

private BukkitTask pingTask;
private AbstractTaskHandle pingTask;

public void loadConfig(boolean reloadConfig) {
KnockbackSync instance = KnockbackSync.getInstance();
Expand All @@ -45,7 +46,7 @@ public void loadConfig(boolean reloadConfig) {
runnableEnabled = newRunnableEnabled;

if (runnableEnabled)
pingTask = new PingRunnable().runTaskTimerAsynchronously(instance, 0L, runnableInterval);
pingTask = KnockbackSync.INSTANCE.getScheduler().runTaskTimerAsynchronously(new PingRunnable(), 0L, runnableInterval);

notifyUpdate = instance.getConfig().getBoolean("notify_updates", true);
runnableInterval = instance.getConfig().getLong("runnable.interval", 5L);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@
import com.github.retrooper.packetevents.protocol.player.ClientVersion;
import com.github.retrooper.packetevents.protocol.player.User;
import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerPing;
import io.github.retrooper.packetevents.util.folia.TaskWrapper;
import lombok.Getter;
import lombok.Setter;
import me.caseload.knockbacksync.KnockbackSync;
import me.caseload.knockbacksync.scheduler.AbstractTaskHandle;
import me.caseload.knockbacksync.util.MathUtil;
import org.bukkit.*;
import org.bukkit.attribute.Attribute;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import org.bukkit.scheduler.BukkitTask;
import org.bukkit.util.BoundingBox;
import org.bukkit.util.RayTraceResult;
import org.bukkit.util.Vector;
Expand Down Expand Up @@ -42,7 +43,7 @@ public class PlayerData {
private final Random random = new Random();

@Nullable
private BukkitTask combatTask;
private AbstractTaskHandle combatTask;

@Nullable @Setter
private Long ping, previousPing;
Expand Down Expand Up @@ -225,8 +226,8 @@ public void quitCombat(boolean cancelTask) {
}

@NotNull
private BukkitTask newCombatTask() {
return Bukkit.getScheduler().runTaskLaterAsynchronously(KnockbackSync.getInstance(),
private AbstractTaskHandle newCombatTask() {
return KnockbackSync.INSTANCE.getScheduler().runTaskLaterAsynchronously(
() -> quitCombat(false), KnockbackSync.getInstance().getConfigManager().getCombatTimer());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import java.util.UUID;

public class PingRunnable extends BukkitRunnable {
public class PingRunnable implements Runnable {

@Override
public void run() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package me.caseload.knockbacksync.scheduler;

import io.papermc.paper.threadedregions.scheduler.ScheduledTask;
import org.bukkit.plugin.Plugin;
import org.bukkit.scheduler.BukkitTask;
import org.jetbrains.annotations.NotNull;

public class AbstractTaskHandle {
private BukkitTask bukkitTask;
private ScheduledTask scheduledTask;

public AbstractTaskHandle(@NotNull BukkitTask bukkitTask) {
this.bukkitTask = bukkitTask;
}

public AbstractTaskHandle(@NotNull ScheduledTask scheduledTask) {
this.scheduledTask = scheduledTask;
}

public Plugin getOwner() {
return this.bukkitTask != null ? this.bukkitTask.getOwner() : this.scheduledTask.getOwningPlugin();
}

public boolean isCancelled() {
return this.bukkitTask != null ? this.bukkitTask.isCancelled() : this.scheduledTask.isCancelled();
}

public void cancel() {
if (this.bukkitTask != null) {
this.bukkitTask.cancel();
} else {
this.scheduledTask.cancel();
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package me.caseload.knockbacksync.scheduler;

import org.bukkit.Bukkit;
import org.bukkit.plugin.Plugin;
import org.bukkit.scheduler.BukkitScheduler;

public class BukkitSchedulerAdapter implements SchedulerAdapter {
private final Plugin plugin;
private final BukkitScheduler scheduler;

public BukkitSchedulerAdapter(Plugin plugin) {
this.plugin = plugin;
this.scheduler = Bukkit.getScheduler();
}

@Override
public AbstractTaskHandle runTask(Runnable task) {
return new AbstractTaskHandle(scheduler.runTask(plugin, task));
}

@Override
public AbstractTaskHandle runTaskAsynchronously(Runnable task) {
return new AbstractTaskHandle(scheduler.runTaskAsynchronously(plugin, task));
}

@Override
public AbstractTaskHandle runTaskLater(Runnable task, long delayTicks) {
return new AbstractTaskHandle(scheduler.runTaskLater(plugin, task, delayTicks));
}

@Override
public AbstractTaskHandle runTaskTimer(Runnable task, long delayTicks, long periodTicks) {
return new AbstractTaskHandle(scheduler.runTaskTimer(plugin, task, delayTicks, periodTicks));
}

@Override
public AbstractTaskHandle runTaskLaterAsynchronously(Runnable task, long delay) {
return new AbstractTaskHandle(scheduler.runTaskLaterAsynchronously(plugin, task, delay));
}

@Override
public AbstractTaskHandle runTaskTimerAsynchronously(Runnable task, long delay, long period) {
return new AbstractTaskHandle(scheduler.runTaskTimerAsynchronously(plugin, task, delay, period));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// FoliaSchedulerAdapter.java
package me.caseload.knockbacksync.scheduler;

import io.papermc.paper.threadedregions.scheduler.GlobalRegionScheduler;
import org.bukkit.Bukkit;
import org.bukkit.plugin.Plugin;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class FoliaSchedulerAdapter implements SchedulerAdapter {
private final Plugin plugin;
private GlobalRegionScheduler scheduler = null;

public FoliaSchedulerAdapter(Plugin plugin) {
this.plugin = plugin;
try {
// Attempt to find and call the `getGlobalRegionScheduler` method
Method getSchedulerMethod = Bukkit.getServer().getClass().getMethod("getGlobalRegionScheduler");
scheduler = (GlobalRegionScheduler) getSchedulerMethod.invoke(Bukkit.getServer());
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
plugin.getLogger().severe("Failed to access GlobalRegionScheduler: " + e.getMessage());
}
}

@Override
public AbstractTaskHandle runTask(Runnable task) {
// scheduler.execute(plugin, task);
return new AbstractTaskHandle(scheduler.run(plugin, scheduledTask -> task.run()));
}

@Override
public AbstractTaskHandle runTaskAsynchronously(Runnable task) {
return new AbstractTaskHandle(scheduler.run(plugin, scheduledTask -> task.run()));
}

@Override
public AbstractTaskHandle runTaskLater(Runnable task, long delayTicks) {
return new AbstractTaskHandle(scheduler.runDelayed(plugin, scheduledTask -> task.run(), delayTicks));
}

@Override
public AbstractTaskHandle runTaskTimer(Runnable task, long delayTicks, long periodTicks) {
return new AbstractTaskHandle(scheduler.runAtFixedRate(plugin, scheduledTask -> task.run(), delayTicks, periodTicks));
}

@Override
public AbstractTaskHandle runTaskLaterAsynchronously(Runnable task, long delay) {
return new AbstractTaskHandle(scheduler.runDelayed(plugin, scheduledTask -> task.run(), delay));
}

@Override
public AbstractTaskHandle runTaskTimerAsynchronously(Runnable task, long delay, long period) {
return new AbstractTaskHandle(scheduler.runAtFixedRate(plugin, scheduledTask -> task.run(), delay, period));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package me.caseload.knockbacksync.scheduler;

public interface SchedulerAdapter {
AbstractTaskHandle runTask(Runnable task);
AbstractTaskHandle runTaskAsynchronously(Runnable task);
AbstractTaskHandle runTaskLater(Runnable task, long delayTicks);
AbstractTaskHandle runTaskTimer(Runnable task, long delayTicks, long periodTicks);
AbstractTaskHandle runTaskLaterAsynchronously(Runnable task, long delay);
AbstractTaskHandle runTaskTimerAsynchronously(Runnable task, long delay, long period);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class StatsManager {
public static Metrics metrics;

public static void init() {
Bukkit.getScheduler().runTaskAsynchronously(KnockbackSync.getInstance(), () -> {
KnockbackSync.INSTANCE.getScheduler().runTaskAsynchronously(() -> {
BuildTypePie.determineBuildType(); // Function to calculate hash
metrics = new Metrics(KnockbackSync.INSTANCE, 23568);
metrics.addCustomChart(new PlayerVersionsPie());
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ main: me.caseload.knockbacksync.KnockbackSync
api-version: '1.18'
authors: [ Caseload ]
description: Synchronizes player knockback for smoother gameplay.
folia-supported: true
permissions:
knockbacksync.update:
default: op
Expand Down

0 comments on commit a1404bb

Please sign in to comment.