Skip to content

Commit

Permalink
Add Folia support via reflections to support older versions.
Browse files Browse the repository at this point in the history
  • Loading branch information
HarvelsX committed Jan 31, 2024
1 parent e6ac39a commit fd1b05d
Show file tree
Hide file tree
Showing 10 changed files with 316 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
import eu.decentsoftware.holograms.api.nms.NMS;
import eu.decentsoftware.holograms.api.nms.PacketListener;
import eu.decentsoftware.holograms.api.listeners.PlayerListener;
import eu.decentsoftware.holograms.api.utils.scheduler.SchedulerAdapter;
import eu.decentsoftware.holograms.api.utils.scheduler.adapters.BukkitSchedulerAdapter;
import eu.decentsoftware.holograms.api.utils.scheduler.adapters.FoliaSchedulerAdapter;
import eu.decentsoftware.holograms.api.utils.BungeeUtils;
import eu.decentsoftware.holograms.api.utils.Common;
import eu.decentsoftware.holograms.api.utils.DExecutor;
Expand Down Expand Up @@ -43,6 +46,7 @@
public final class DecentHolograms {

private final JavaPlugin plugin;
private final SchedulerAdapter scheduler;
private HologramManager hologramManager;
private CommandManager commandManager;
private FeatureManager featureManager;
Expand All @@ -57,6 +61,7 @@ public final class DecentHolograms {

DecentHolograms(@NonNull JavaPlugin plugin) {
this.plugin = plugin;
this.scheduler = FoliaSchedulerAdapter.isSupported() ? new FoliaSchedulerAdapter(plugin) : new BukkitSchedulerAdapter(plugin);
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public boolean execute(Player player, String... args) {
Validate.notNull(player);

String string = String.join(" ", args);
Bukkit.getScheduler().runTask(DECENT_HOLOGRAMS.getPlugin(), () -> {
DECENT_HOLOGRAMS.getScheduler().executeAtEntity(player, () -> {
//
player.chat(PAPI.setPlaceholders(player, string.replace("{player}", player.getName())));
});
Expand All @@ -80,7 +80,7 @@ public boolean execute(Player player, String... args) {
Validate.notNull(player);

String string = String.join(" ", args);
Bukkit.getScheduler().runTask(DECENT_HOLOGRAMS.getPlugin(), () -> {
DECENT_HOLOGRAMS.getScheduler().executeAtEntity(player, () -> {
//
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), PAPI.setPlaceholders(player, string.replace("{player}", player.getName())));
});
Expand Down Expand Up @@ -113,7 +113,7 @@ public boolean execute(Player player, String... args) {
if (location == null) {
return false;
}
Bukkit.getScheduler().runTask(DECENT_HOLOGRAMS.getPlugin(), () -> player.teleport(location));
DECENT_HOLOGRAMS.getScheduler().executeAtEntity(player, () -> player.teleport(location));
return true;
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,7 @@ public boolean show(@NonNull Player player, int pageIndex) {
} else {
// We need to run the task later on older versions as, if we don't, it causes issues with some holograms *randomly* becoming invisible.
// I *think* this is from despawning and spawning the entities (with the same ID) in the same tick.
S.sync(() -> showPageTo(player, page, pageIndex), 0L);
S.sync(player, () -> showPageTo(player, page, pageIndex), 0L);
}
return true;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package eu.decentsoftware.holograms.api.utils;

import eu.decentsoftware.holograms.api.DecentHologramsAPI;
import org.apache.commons.lang.Validate;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;

import java.io.IOException;
Expand All @@ -23,7 +23,7 @@ public UpdateChecker(JavaPlugin plugin, int resourceId) {
}

public void getVersion(Consumer<String> consumer) {
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
DecentHologramsAPI.get().getScheduler().runAsync(() -> {
try (InputStream inputStream = new URL("https://api.spigotmc.org/legacy/update.php?resource=" + resourceId).openStream();
Scanner scanner = new Scanner(inputStream)) {
if (scanner.hasNext() && consumer != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,52 +3,39 @@
import eu.decentsoftware.holograms.api.DecentHolograms;
import eu.decentsoftware.holograms.api.DecentHologramsAPI;
import eu.decentsoftware.holograms.api.utils.DExecutor;
import org.bukkit.Bukkit;
import org.bukkit.entity.Entity;
import org.bukkit.plugin.IllegalPluginAccessException;
import org.bukkit.scheduler.BukkitTask;

import java.util.concurrent.TimeUnit;

public class S {

private static final DecentHolograms DECENT_HOLOGRAMS = DecentHologramsAPI.get();

public static void stopTask(int id) {
Bukkit.getScheduler().cancelTask(id);
}

public static void sync(Runnable runnable) {
Bukkit.getScheduler().runTask(DECENT_HOLOGRAMS.getPlugin(), runnable);
}

public static BukkitTask sync(Runnable runnable, long delay) {
return Bukkit.getScheduler().runTaskLater(DECENT_HOLOGRAMS.getPlugin(), runnable, delay);
public static void sync(Entity entity, Runnable runnable, long delay) {
DECENT_HOLOGRAMS.getScheduler().runAtEntityDelayed(entity, runnable, delay);
}

public static BukkitTask syncTask(Runnable runnable, long interval) {
return Bukkit.getScheduler().runTaskTimer(DECENT_HOLOGRAMS.getPlugin(), runnable, 0, interval);
}

public static void async(Runnable runnable) {
try {
Bukkit.getScheduler().runTaskAsynchronously(DECENT_HOLOGRAMS.getPlugin(), runnable);
DECENT_HOLOGRAMS.getScheduler().runAsync(runnable);
} catch (IllegalPluginAccessException e) {
DExecutor.execute(runnable);
}
}

public static void async(Runnable runnable, long delay) {
try {
Bukkit.getScheduler().runTaskLaterAsynchronously(DECENT_HOLOGRAMS.getPlugin(), runnable, delay);
DECENT_HOLOGRAMS.getScheduler().runAsyncDelayed(runnable, delay * 50, TimeUnit.MILLISECONDS);
} catch (IllegalPluginAccessException e) {
DExecutor.execute(runnable);
}
}

public static BukkitTask asyncTask(Runnable runnable, long interval) {
return Bukkit.getScheduler().runTaskTimerAsynchronously(DECENT_HOLOGRAMS.getPlugin(), runnable, 0, interval);
}

public static BukkitTask asyncTask(Runnable runnable, long interval, long delay) {
return Bukkit.getScheduler().runTaskTimerAsynchronously(DECENT_HOLOGRAMS.getPlugin(), runnable, delay, interval);
return DECENT_HOLOGRAMS.getScheduler().runAsyncRate(runnable, 0, interval * 50, TimeUnit.MILLISECONDS);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package eu.decentsoftware.holograms.api.utils.scheduler;

import org.bukkit.entity.Entity;
import org.bukkit.scheduler.BukkitTask;

import java.util.concurrent.TimeUnit;

public interface SchedulerAdapter {

/**
* Schedules the specified task to be executed asynchronously immediately.
*
* @param runnable The task to execute.
*/
void runAsync(Runnable runnable);

/**
* Schedules the specified task to be executed asynchronously after the time delay has passed.
*
* @param runnable The task to execute.
* @param delay The time delay to pass before the task should be executed.
* @param unit The time unit for the initial delay and period.
*/
void runAsyncDelayed(Runnable runnable, long delay, TimeUnit unit);

/**
* Schedules the specified task to be executed asynchronously after the delay has passed,
* and then periodically executed with the specified period.
*
* @param runnable The task to execute.
* @param delay The time delay to pass before the task should be executed.
* @param period The time between task executions after the first execution of the task.
* @param unit The time unit for the initial delay and period.
* @return The BukkitTask that represents the scheduled task.
*/
BukkitTask runAsyncRate(Runnable runnable, long delay, long period, TimeUnit unit);

/**
* Schedules a task. If the task failed to schedule because the scheduler is retired (entity removed),
* then returns {@code false}. Otherwise, either the run 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.
*
* <p>
* It is guaranteed that the task and retired callback are invoked on the region which owns the entity.
* </p>
*
* @param entity The entity relative to which the scheduler is obtained.
* @param runnable The task to execute.
*/
void executeAtEntity(Entity entity, Runnable runnable);

/**
* Schedules a task with the given delay. If the task failed to schedule because the scheduler is retired (entity removed),
* then returns {@code false}. Otherwise, either the run 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.
*
* <p>
* It is guaranteed that the task and retired callback are invoked on the region which owns the entity.
* </p>
*
* @param entity The entity relative to which the scheduler is obtained.
* @param runnable The task to execute.
* @param delay The time delay to pass before the task should be executed, in ticks.
*/
void runAtEntityDelayed(Entity entity, Runnable runnable, long delay);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package eu.decentsoftware.holograms.api.utils.scheduler.adapters;

import eu.decentsoftware.holograms.api.utils.scheduler.SchedulerAdapter;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.bukkit.Bukkit;
import org.bukkit.entity.Entity;
import org.bukkit.plugin.Plugin;
import org.bukkit.scheduler.BukkitTask;

import java.util.concurrent.TimeUnit;

@RequiredArgsConstructor
public class BukkitSchedulerAdapter implements SchedulerAdapter {

private @NonNull Plugin plugin;

@Override
public void runAsync(Runnable runnable) {
Bukkit.getScheduler().runTaskAsynchronously(plugin, runnable);
}

@Override
public void runAsyncDelayed(Runnable runnable, long delay, TimeUnit unit) {
Bukkit.getScheduler().runTaskLaterAsynchronously(plugin, runnable, unit.toMillis(delay) / 50);
}

@Override
public BukkitTask runAsyncRate(Runnable runnable, long delay, long period, TimeUnit unit) {
return Bukkit.getScheduler().runTaskTimerAsynchronously(plugin, runnable, unit.toMillis(delay) / 50, unit.toMillis(period) / 50);
}

@Override
public void executeAtEntity(Entity entity, Runnable runnable) {
Bukkit.getScheduler().runTask(plugin, runnable);
}

@Override
public void runAtEntityDelayed(Entity entity, Runnable runnable, long delay) {
Bukkit.getScheduler().runTaskLater(plugin, runnable, delay);
}
}
Loading

0 comments on commit fd1b05d

Please sign in to comment.