Add helper methods for ensuring thread contexts

This commit is contained in:
Josh Roy 2023-03-30 13:40:13 -04:00
parent 2d8cc827b9
commit 7216fddfbe
No known key found for this signature in database
GPG Key ID: 64C8142336ED1F69
3 changed files with 67 additions and 16 deletions

View File

@ -13,7 +13,6 @@ import net.ess3.api.events.teleport.PreTeleportEvent;
import net.ess3.api.events.teleport.TeleportWarmupEvent;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerRespawnEvent;
import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;
@ -142,19 +141,6 @@ public class AsyncTeleport implements IAsyncTeleport {
paperFuture.exceptionally(future::completeExceptionally);
}
private void runOnEntity(final Entity entity, final Runnable runnable) throws ExecutionException, InterruptedException {
if (ess.isEntityThread(entity)) {
runnable.run();
return;
}
final CompletableFuture<Object> taskLock = new CompletableFuture<>();
ess.scheduleEntityDelayedTask(entity, () -> {
runnable.run();
taskLock.complete(new Object());
});
taskLock.get();
}
protected void nowAsync(final IUser teleportee, final ITarget target, final TeleportCause cause, final CompletableFuture<Boolean> future) {
cancel(false);
@ -172,8 +158,8 @@ public class AsyncTeleport implements IAsyncTeleport {
}
try {
runOnEntity(teleportee.getBase(), () -> teleportee.getBase().eject()); //EntityDismountEvent requires a sync context.
} catch (final ExecutionException | InterruptedException e) {
ess.ensureEntity(teleportee.getBase(), () -> teleportee.getBase().eject()); //EntityDismountEvent requires a sync context.
} catch (final RuntimeException e) {
future.completeExceptionally(e);
return;
}

View File

@ -138,6 +138,8 @@ import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.function.Predicate;
import java.util.logging.Level;
import java.util.logging.Logger;
@ -1244,6 +1246,63 @@ public class Essentials extends JavaPlugin implements net.ess3.api.IEssentials {
return schedulingProvider.isGlobalThread();
}
@Override
public void ensureEntity(Entity entity, Runnable runnable) {
if (isEntityThread(entity)) {
runnable.run();
return;
}
final CompletableFuture<Object> taskLock = new CompletableFuture<>();
scheduleEntityDelayedTask(entity, () -> {
runnable.run();
taskLock.complete(new Object());
});
try {
taskLock.get();
} catch (InterruptedException | ExecutionException e) {
throw new RuntimeException(e);
}
}
@Override
public void ensureRegion(Location location, Runnable runnable) {
if (isRegionThread(location)) {
runnable.run();
return;
}
final CompletableFuture<Object> taskLock = new CompletableFuture<>();
scheduleLocationDelayedTask(location, () -> {
runnable.run();
taskLock.complete(new Object());
});
try {
taskLock.get();
} catch (InterruptedException | ExecutionException e) {
throw new RuntimeException(e);
}
}
@Override
public void ensureGlobal(Runnable runnable) {
if (isGlobalThread()) {
runnable.run();
return;
}
final CompletableFuture<Object> taskLock = new CompletableFuture<>();
scheduleGlobalDelayedTask(() -> {
runnable.run();
taskLock.complete(new Object());
});
try {
taskLock.get();
} catch (InterruptedException | ExecutionException e) {
throw new RuntimeException(e);
}
}
@Override
public PermissionsHandler getPermissionsHandler() {
return permissionsHandler;

View File

@ -131,6 +131,12 @@ public interface IEssentials extends Plugin {
boolean isGlobalThread();
void ensureEntity(Entity entity, Runnable runnable);
void ensureRegion(Location location, Runnable runnable);
void ensureGlobal(Runnable runnable);
PermissionsHandler getPermissionsHandler();
AlternativeCommandsHandler getAlternativeCommandsHandler();