1010import org .bukkit .block .Block ;
1111import org .bukkit .entity .Entity ;
1212import org .bukkit .entity .Player ;
13+ import org .bukkit .entity .Projectile ;
1314import org .bukkit .event .player .PlayerTeleportEvent ;
1415import org .bukkit .plugin .java .JavaPlugin ;
1516import org .bukkit .scheduler .BukkitScheduler ;
@@ -252,7 +253,7 @@ public void runAtLocationTimer(Location location, @NotNull Consumer<WrappedTask>
252253 CompletableFuture <EntityTaskResult > future = new CompletableFuture <>();
253254
254255 this .runNextTick (task -> {
255- if (entity . isValid ()) {
256+ if (isValid (entity )) {
256257 consumer .accept (task );
257258 future .complete (EntityTaskResult .SUCCESS );
258259 } else {
@@ -271,7 +272,7 @@ public WrappedTask runAtEntityLater(Entity entity, @NotNull Runnable runnable, l
271272
272273 @ Override
273274 public WrappedTask runAtEntityLater (Entity entity , @ NotNull Runnable runnable , Runnable fallback , long delay ) {
274- if (!entity . isValid ()) {
275+ if (!isValid (entity )) {
275276 if (fallback != null ) fallback .run ();
276277 return null ;
277278 }
@@ -288,7 +289,7 @@ public WrappedTask runAtEntityLater(Entity entity, @NotNull Runnable runnable, R
288289 public @ NotNull CompletableFuture <Void > runAtEntityLater (Entity entity , @ NotNull Consumer <WrappedTask > consumer , @ Nullable Runnable fallback , long delay ) {
289290 CompletableFuture <Void > future = new CompletableFuture <>();
290291
291- if (!entity . isValid ()) {
292+ if (!isValid (entity )) {
292293 if (fallback != null ) {
293294 fallback .run ();
294295 future .complete (null );
@@ -321,7 +322,7 @@ public WrappedTask runAtEntityTimer(Entity entity, @NotNull Runnable runnable, l
321322
322323 @ Override
323324 public WrappedTask runAtEntityTimer (Entity entity , @ NotNull Runnable runnable , @ Nullable Runnable fallback , long delay , long period ) {
324- if (!entity . isValid ()) {
325+ if (!isValid (entity )) {
325326 if (fallback != null ) fallback .run ();
326327 return null ;
327328 }
@@ -337,14 +338,14 @@ public void runAtEntityTimer(Entity entity, @NotNull Consumer<WrappedTask> consu
337338 @ Override
338339 public void runAtEntityTimer (Entity entity , @ NotNull Consumer <WrappedTask > consumer , Runnable fallback , long delay , long period ) {
339340 // Sanity check before running once
340- if (!entity . isValid ()) {
341+ if (!isValid (entity )) {
341342 if (fallback != null ) fallback .run ();
342343 return ;
343344 }
344345
345346 this .runTimer (task -> {
346347 // Perform sanity check before running each time
347- if (!entity . isValid ()) {
348+ if (!isValid (entity )) {
348349 if (fallback != null ) fallback .run ();
349350 return ;
350351 }
@@ -436,7 +437,7 @@ public CompletableFuture<Boolean> teleportAsync(Entity entity, Location location
436437 CompletableFuture <Boolean > future = new CompletableFuture <>();
437438
438439 this .runAtEntity (entity , (task ) -> {
439- if (entity . isValid () && (( entity instanceof Player ) && (( Player ) entity ). isOnline () )) {
440+ if (isValid (entity )) {
440441 entity .teleport (location );
441442 future .complete (true );
442443 } else {
@@ -456,4 +457,11 @@ public WrappedTask wrapTask(Object nativeTask) {
456457
457458 return new WrappedBukkitTask ((BukkitTask ) nativeTask );
458459 }
460+
461+ private boolean isValid (Entity entity ) {
462+ if (entity .isValid ()) {
463+ return !(entity instanceof Player ) || ((Player ) entity ).isOnline ();
464+ }
465+ return entity instanceof Projectile && !entity .isDead ();
466+ }
459467}
0 commit comments