Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,8 @@ protected void nowAsync(final IUser teleportee, final ITarget target, final Tele
}

try {
runOnMain(() -> teleportee.getBase().eject()); //EntityDismountEvent requires a sync context.
} catch (final ExecutionException | InterruptedException e) {
ess.getSchedulerAdapter().runEntityTask(teleportee.getBase(), () -> teleportee.getBase().eject()); //EntityDismountEvent requires a sync context.
} catch (final Exception e) {
future.completeExceptionally(e);
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.UUID;
import java.util.concurrent.CompletableFuture;

import com.earth2me.essentials.utils.schedulers.SchedulerTask;
import org.bukkit.Location;
import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;

Expand Down Expand Up @@ -31,7 +32,7 @@ public class AsyncTimedTeleport implements Runnable {
private final boolean timer_canMove;
private final Trade timer_chargeFor;
private final TeleportCause timer_cause;
private int timer_task;
private SchedulerTask timer_task;
private double timer_health;

AsyncTimedTeleport(final IUser user, final IEssentials ess, final AsyncTeleport teleport, final long delay, final IUser teleportUser, final ITarget target, final Trade chargeFor, final TeleportCause cause, final boolean respawn) {
Expand All @@ -55,7 +56,7 @@ public class AsyncTimedTeleport implements Runnable {
this.timer_respawn = respawn;
this.timer_canMove = user.isAuthorized("essentials.teleport.timer.move");

timer_task = ess.runTaskTimerAsynchronously(this, 20, 20).getTaskId();
timer_task = ess.getSchedulerAdapter().runTaskTimerAsynchronously(this, 20*50, 20*50);

if (future != null) {
this.parentFuture = future;
Expand Down Expand Up @@ -142,16 +143,16 @@ public void run() {
}
}

ess.scheduleSyncDelayedTask(new DelayedTeleportTask());
ess.getSchedulerAdapter().runTask(new DelayedTeleportTask());
}

//If we need to cancelTimer a pending teleportPlayer call this method
void cancelTimer(final boolean notifyUser) {
if (timer_task == -1) {
if (timer_task == null) {
return;
}
try {
ess.getServer().getScheduler().cancelTask(timer_task);
timer_task.cancel();

final IUser teleportUser = ess.getUser(this.timer_teleportee);
if (teleportUser != null && teleportUser.getBase() != null) {
Expand All @@ -167,7 +168,7 @@ void cancelTimer(final boolean notifyUser) {
}
}
} finally {
timer_task = -1;
timer_task = null;
}
}
}
19 changes: 10 additions & 9 deletions Essentials/src/main/java/com/earth2me/essentials/Backup.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.earth2me.essentials;

import com.earth2me.essentials.utils.schedulers.SchedulerTask;
import net.ess3.api.IEssentials;
import org.bukkit.Server;
import org.bukkit.command.CommandSender;
Expand All @@ -18,15 +19,15 @@ public class Backup implements Runnable {
private transient final IEssentials ess;
private final AtomicBoolean pendingShutdown = new AtomicBoolean(false);
private transient boolean running = false;
private transient int taskId = -1;
private transient SchedulerTask taskId = null;
private transient boolean active = false;
private transient CompletableFuture<Object> taskLock = null;

public Backup(final IEssentials ess) {
this.ess = ess;
server = ess.getServer();
if (!ess.getOnlinePlayers().isEmpty() || ess.getSettings().isAlwaysRunBackup()) {
ess.runTaskAsynchronously(this::startTask);
ess.getSchedulerAdapter().runTaskAsynchronously(this::startTask);
}
}

Expand All @@ -36,10 +37,10 @@ public void onPlayerJoin() {

public synchronized void stopTask() {
running = false;
if (taskId != -1) {
server.getScheduler().cancelTask(taskId);
if (taskId != null) {
taskId.cancel();
}
taskId = -1;
taskId = null;
}

private synchronized void startTask() {
Expand All @@ -48,7 +49,7 @@ private synchronized void startTask() {
if (interval < 1200) {
return;
}
taskId = ess.scheduleSyncRepeatingTask(this, interval, interval);
taskId = ess.getSchedulerAdapter().runTaskTimer(this, interval, interval);
running = true;
}
}
Expand Down Expand Up @@ -84,13 +85,13 @@ public void run() {
server.dispatchCommand(cs, "save-all");
server.dispatchCommand(cs, "save-off");

ess.runTaskAsynchronously(() -> {
ess.getSchedulerAdapter().runTaskAsynchronously(() -> {
try {
final ProcessBuilder childBuilder = new ProcessBuilder(command.split(" "));
childBuilder.redirectErrorStream(true);
childBuilder.directory(ess.getDataFolder().getParentFile().getParentFile());
final Process child = childBuilder.start();
ess.runTaskAsynchronously(() -> {
ess.getSchedulerAdapter().runTaskAsynchronously(() -> {
try {
try (final BufferedReader reader = new BufferedReader(new InputStreamReader(child.getInputStream()))) {
String line;
Expand Down Expand Up @@ -123,7 +124,7 @@ public void run() {
}

if (!pendingShutdown.get()) {
ess.scheduleSyncDelayedTask(new BackupEnableSaveTask());
ess.getSchedulerAdapter().runTask(new BackupEnableSaveTask());
}
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public CompletableFuture<Void> calculateBalanceTopMapAsync() {
return cacheLock;
}
cacheLock = new CompletableFuture<>();
ess.runTaskAsynchronously(this::calculateBalanceTopMap);
ess.getSchedulerAdapter().runTaskAsynchronously(this::calculateBalanceTopMap);
return cacheLock;
}

Expand Down
20 changes: 18 additions & 2 deletions Essentials/src/main/java/com/earth2me/essentials/Essentials.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@
import com.earth2me.essentials.userstorage.ModernUserMap;
import com.earth2me.essentials.utils.FormatUtil;
import com.earth2me.essentials.utils.VersionUtil;
import com.earth2me.essentials.utils.schedulers.SchedulerAdapter;
import com.earth2me.essentials.utils.schedulers.adapter.FoliaScheduler;
import com.earth2me.essentials.utils.schedulers.adapter.SpigotScheduler;
import io.papermc.lib.PaperLib;
import net.ess3.api.Economy;
import com.earth2me.essentials.config.EssentialsConfiguration;
Expand Down Expand Up @@ -154,6 +157,7 @@ public class Essentials extends JavaPlugin implements net.ess3.api.IEssentials {
private static final Logger BUKKIT_LOGGER = Logger.getLogger("Essentials");
private static Logger LOGGER = null;
public static boolean TESTING = false;
private static SchedulerAdapter schedulerAdapter;
private final transient TNTExplodeListener tntListener = new TNTExplodeListener();
private final transient Set<String> vanishedPlayers = new LinkedHashSet<>();
private final transient Map<String, IEssentialsCommand> commandMap = new HashMap<>();
Expand Down Expand Up @@ -209,6 +213,13 @@ public void onEnable() {
if (BUKKIT_LOGGER != super.getLogger()) {
BUKKIT_LOGGER.setParent(super.getLogger());
}
if (schedulerAdapter == null) {
if (VersionUtil.isFoliaServer()) {
schedulerAdapter = new FoliaScheduler(this);
} else {
schedulerAdapter = new SpigotScheduler(this);
}
}
LOGGER = EssentialsLogger.getLoggerProvider(this);
EssentialsLogger.updatePluginLogger(this);

Expand Down Expand Up @@ -428,7 +439,7 @@ public void onEnable() {
alternativeCommandsHandler = new AlternativeCommandsHandler(this);

timer = new EssentialsTimer(this);
scheduleSyncRepeatingTask(timer, 1000, 50);
getSchedulerAdapter().runTaskTimer(timer, 1000, 50);

Economy.setEss(this);
execTimer.mark("RegHandler");
Expand All @@ -439,7 +450,7 @@ public void onEnable() {

if (!TESTING) {
updateChecker = new UpdateChecker(this);
runTaskAsynchronously(() -> {
getSchedulerAdapter().runTaskAsynchronously(() -> {
getLogger().log(Level.INFO, getAdventureFacet().miniToLegacy(tlLiteral("versionFetching")));
for (final ComponentHolder component : updateChecker.getVersionMessages(false, true, new CommandSource(this, Bukkit.getConsoleSender()))) {
getLogger().log(getSettings().isUpdateCheckEnabled() ? Level.WARNING : Level.INFO, getAdventureFacet().adventureToLegacy(component));
Expand Down Expand Up @@ -1255,6 +1266,11 @@ public int scheduleSyncRepeatingTask(final Runnable run, final long delay, final
return this.getScheduler().scheduleSyncRepeatingTask(this, run, delay, period);
}

@Override
public SchedulerAdapter getSchedulerAdapter() {
return schedulerAdapter;
}

@Override
public PermissionsHandler getPermissionsHandler() {
return permissionsHandler;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void onBlockPlace(final BlockPlaceEvent event) {

final User user = ess.getUser(event.getPlayer());
if (user.hasUnlimited(is) && user.getBase().getGameMode() == GameMode.SURVIVAL) {
ess.scheduleSyncDelayedTask(() -> {
ess.getSchedulerAdapter().runEntityTask(user.getBase(), () -> {
if (is != null && is.getType() != null && !MaterialUtil.isAir(is.getType())) {
final ItemStack cloneIs = is.clone();
cloneIs.setAmount(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public void run() {
}
}

ess.scheduleSyncDelayedTask(new PowerToolInteractTask());
ess.getSchedulerAdapter().runEntityTask(attacker.getBase(), new PowerToolInteractTask());

event.setCancelled(true);
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.earth2me.essentials.utils.LocationUtil;
import com.earth2me.essentials.utils.MaterialUtil;
import com.earth2me.essentials.utils.VersionUtil;
import com.earth2me.essentials.utils.schedulers.SchedulerTask;
import io.papermc.lib.PaperLib;
import io.papermc.paper.ban.BanListType;
import io.papermc.paper.event.connection.configuration.AsyncPlayerConnectionConfigureEvent;
Expand Down Expand Up @@ -94,7 +95,7 @@

public class EssentialsPlayerListener implements Listener {
private final transient IEssentials ess;
private final ConcurrentHashMap<UUID, Integer> pendingMotdTasks = new ConcurrentHashMap<>();
private final ConcurrentHashMap<UUID, SchedulerTask> pendingMotdTasks = new ConcurrentHashMap<>();

public EssentialsPlayerListener(final IEssentials parent) {
this.ess = parent;
Expand Down Expand Up @@ -289,9 +290,9 @@ public void onPlayerMove(final PlayerMoveEvent event) {
public void onPlayerQuit(final PlayerQuitEvent event) {
final User user = ess.getUser(event.getPlayer());

final Integer pendingId = pendingMotdTasks.remove(user.getUUID());
final SchedulerTask pendingId = pendingMotdTasks.remove(user.getUUID());
if (pendingId != null) {
ess.getScheduler().cancelTask(pendingId);
pendingId.cancel();
}

if (hideJoinQuitMessages() || ess.getSettings().allowSilentJoinQuit() && user.isAuthorized("essentials.silentquit")) {
Expand Down Expand Up @@ -408,7 +409,7 @@ private boolean hideJoinQuitMessages() {

private void legacyJoinFlow(final PlayerJoinEvent event) {
final String joinMessage = event.getJoinMessage();
ess.runTaskAsynchronously(() -> delayedJoin(event.getPlayer(), joinMessage));
ess.getSchedulerAdapter().runTaskAsynchronously(() -> delayedJoin(event.getPlayer(), joinMessage));

if (hideJoinQuitMessages() || ess.getSettings().allowSilentJoinQuit() || ess.getSettings().isCustomJoinMessage()) {
event.setJoinMessage(null);
Expand Down Expand Up @@ -473,13 +474,13 @@ private void joinFlow(final User user, final long currentTime, final String mess
joinMessageConsumer.accept(effectiveMessage);
}

ess.runTaskAsynchronously(() -> ess.getServer().getPluginManager().callEvent(new AsyncUserDataLoadEvent(user, effectiveMessage)));
ess.getSchedulerAdapter().runTaskAsynchronously(() -> ess.getServer().getPluginManager().callEvent(new AsyncUserDataLoadEvent(user, effectiveMessage)));

if (ess.getSettings().getMotdDelay() >= 0) {
final int motdDelay = ess.getSettings().getMotdDelay() / 50;
final Runnable motdTask = () -> motdFlow(user);
if (motdDelay > 0) {
pendingMotdTasks.put(user.getUUID(), ess.scheduleSyncDelayedTask(motdTask, motdDelay));
pendingMotdTasks.put(user.getUUID(), ess.getSchedulerAdapter().runTaskLater(motdTask, motdDelay));
} else {
motdTask.run();
}
Expand All @@ -496,7 +497,7 @@ private void joinFlow(final User user, final long currentTime, final String mess
}

if (user.isAuthorized("essentials.updatecheck")) {
ess.runTaskAsynchronously(() -> {
ess.getSchedulerAdapter().runTaskAsynchronously(() -> {
for (final ComponentHolder component : ess.getUpdateChecker().getVersionMessages(false, false, user.getSource())) {
user.sendComponent(component);
}
Expand Down Expand Up @@ -581,7 +582,7 @@ private void delayedJoin(final Player player, final String message) {
dUser.updateActivity(false, AfkStatusChangeEvent.Cause.JOIN);
dUser.stopTransaction();

ess.scheduleSyncDelayedTask(() -> {
ess.getSchedulerAdapter().runEntityTask(player, () -> {
final User user = ess.getUser(player);

if (!user.getBase().isOnline()) {
Expand Down Expand Up @@ -751,7 +752,7 @@ public void onPlayerBucketEmpty(final PlayerBucketEmptyEvent event) {
final User user = ess.getUser(event.getPlayer());
if (user.hasUnlimited(new ItemStack(event.getBucket()))) {
event.getItemStack().setType(event.getBucket());
ess.scheduleSyncDelayedTask(user.getBase()::updateInventory);
ess.getSchedulerAdapter().runEntityTask(user.getBase(), user.getBase()::updateInventory);
}
}

Expand Down Expand Up @@ -1012,7 +1013,7 @@ public void run() {
}
}

ess.scheduleSyncDelayedTask(new DelayedClickJumpTask());
ess.getSchedulerAdapter().runEntityTask(user.getBase(), new DelayedClickJumpTask());
} catch (final Exception ex) {
if (ess.getSettings().isDebug()) {
ess.getLogger().log(Level.WARNING, ex.getMessage(), ex);
Expand Down Expand Up @@ -1043,7 +1044,7 @@ public void run() {
}
}

ess.scheduleSyncDelayedTask(new PowerToolUseTask());
ess.getSchedulerAdapter().runEntityTask(user.getBase(), new PowerToolUseTask());

}
}
Expand Down Expand Up @@ -1105,7 +1106,7 @@ public void onInventoryClickEvent(final InventoryClickEvent event) {
}

if (refreshPlayer != null) {
ess.scheduleSyncDelayedTask(refreshPlayer::updateInventory, 1);
ess.getSchedulerAdapter().runEntityTaskLater(refreshPlayer, refreshPlayer::updateInventory, 1);
}
}

Expand Down Expand Up @@ -1168,7 +1169,7 @@ public void onInventoryCloseEvent(final InventoryCloseEvent event) {
}

if (refreshPlayer != null) {
ess.scheduleSyncDelayedTask(refreshPlayer::updateInventory, 1);
ess.getSchedulerAdapter().runEntityTaskLater(refreshPlayer, refreshPlayer::updateInventory, 1);
}
}

Expand All @@ -1192,7 +1193,7 @@ public void onPlayerGameModeChange(final PlayerGameModeChangeEvent event) {
final Player player = event.getPlayer();
if (player.isFlying() && player.getAllowFlight() && user.isAuthorized("essentials.fly")) {
// The gamemode change happens after the event, so we need to delay the flight enable
ess.scheduleSyncDelayedTask(() -> {
ess.getSchedulerAdapter().runEntityTaskLater(player, () -> {
player.setAllowFlight(true);
player.setFlying(true);
}, 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.earth2me.essentials.perm.PermissionsHandler;
import com.earth2me.essentials.updatecheck.UpdateChecker;
import com.earth2me.essentials.userstorage.IUserMap;
import com.earth2me.essentials.utils.schedulers.SchedulerAdapter;
import net.ess3.provider.Provider;
import net.essentialsx.api.v2.services.BalanceTop;
import net.essentialsx.api.v2.services.mail.MailService;
Expand Down Expand Up @@ -108,6 +109,8 @@ public interface IEssentials extends Plugin {

int scheduleSyncRepeatingTask(Runnable run, long delay, long period);

SchedulerAdapter getSchedulerAdapter();

PermissionsHandler getPermissionsHandler();

AlternativeCommandsHandler getAlternativeCommandsHandler();
Expand Down
8 changes: 5 additions & 3 deletions Essentials/src/main/java/com/earth2me/essentials/Kit.java
Original file line number Diff line number Diff line change
Expand Up @@ -286,9 +286,11 @@ public boolean expandItems(final User user, final List<String> items) throws Exc
t.pay(user, OverflowType.DROP);
}

for (final String cmd : commandQueue) {
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), cmd);
}
ess.getSchedulerAdapter().runTask(() -> {
for (final String cmd : commandQueue) {
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), cmd);
}
});

if (spew) {
user.sendTl("kitInvFull");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public CompletableFuture<Location> getRandomLocation(final Location center, fina

// Prompts caching random valid locations, up to a maximum number of attempts.
public void cacheRandomLocations(final String name) {
ess.getServer().getScheduler().scheduleSyncDelayedTask(ess, () -> {
ess.getSchedulerAdapter().runTask(() -> {
for (int i = 0; i < this.getFindAttempts(); ++i) {
calculateRandomLocation(getCenter(name), getMinRange(name), getMaxRange(name)).thenAccept(location -> {
if (isValidRandomLocation(location)) {
Expand Down
Loading