mirror of
https://github.com/EssentialsX/Essentials.git
synced 2025-03-12 14:49:46 +01:00
Add helper methods for ensuring thread contexts
This commit is contained in:
parent
2d8cc827b9
commit
7216fddfbe
@ -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;
|
||||
}
|
||||
|
@ -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;
|
||||
|
@ -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();
|
||||
|
Loading…
Reference in New Issue
Block a user