Skip to content

Commit ef065c0

Browse files
authored
Fix #14 - Check projectile death state (#24)
* Check projectile death state, fixes #14 * Move checks to utility method
1 parent 5f6120f commit ef065c0

2 files changed

Lines changed: 29 additions & 13 deletions

File tree

platform/legacy-spigot/src/main/java/com/tcoded/folialib/impl/LegacySpigotImplementation.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.bukkit.block.Block;
1313
import org.bukkit.entity.Entity;
1414
import org.bukkit.entity.Player;
15+
import org.bukkit.entity.Projectile;
1516
import org.bukkit.event.player.PlayerTeleportEvent;
1617
import org.bukkit.plugin.java.JavaPlugin;
1718
import org.bukkit.scheduler.BukkitScheduler;
@@ -280,7 +281,7 @@ public void runAtLocationTimer(Location location, @NotNull Consumer<WrappedTask>
280281
WrappedTask[] taskReference = new WrappedTask[1];
281282

282283
taskReference[0] = this.wrapTask(this.scheduler.runTask(plugin, () -> {
283-
if (entity.isValid()) {
284+
if (isValid(entity)) {
284285
consumer.accept(taskReference[0]);
285286
future.complete(EntityTaskResult.SUCCESS);
286287
} else {
@@ -299,7 +300,7 @@ public WrappedTask runAtEntityLater(Entity entity, @NotNull Runnable runnable, l
299300

300301
@Override
301302
public WrappedTask runAtEntityLater(Entity entity, @NotNull Runnable runnable, @Nullable Runnable fallback, long delay) {
302-
if (!entity.isValid()) {
303+
if (!isValid(entity)) {
303304
if (fallback != null) fallback.run();
304305
return null;
305306
}
@@ -315,7 +316,7 @@ public WrappedTask runAtEntityLater(Entity entity, @NotNull Runnable runnable, @
315316
public @NotNull CompletableFuture<Void> runAtEntityLater(Entity entity, @NotNull Consumer<WrappedTask> consumer, Runnable fallback, long delay) {
316317
CompletableFuture<Void> future = new CompletableFuture<>();
317318

318-
if (!entity.isValid()) {
319+
if (!isValid(entity)) {
319320
if (fallback != null) {
320321
fallback.run();
321322
future.complete(null);
@@ -348,7 +349,7 @@ public WrappedTask runAtEntityTimer(Entity entity, @NotNull Runnable runnable, l
348349

349350
@Override
350351
public WrappedTask runAtEntityTimer(Entity entity, @NotNull Runnable runnable, Runnable fallback, long delay, long period) {
351-
if (!entity.isValid()) {
352+
if (!isValid(entity)) {
352353
if (fallback != null) fallback.run();
353354
return null;
354355
}
@@ -362,7 +363,7 @@ public void runAtEntityTimer(Entity entity, @NotNull Consumer<WrappedTask> consu
362363

363364
@Override
364365
public void runAtEntityTimer(Entity entity, @NotNull Consumer<WrappedTask> consumer, Runnable fallback, long delay, long period) {
365-
if (!entity.isValid()) {
366+
if (!isValid(entity)) {
366367
if (fallback != null) fallback.run();
367368
}
368369

@@ -433,7 +434,7 @@ public CompletableFuture<Boolean> teleportAsync(Entity entity, Location location
433434
CompletableFuture<Boolean> future = new CompletableFuture<>();
434435

435436
this.runAtEntity(entity, (task) -> {
436-
if (entity.isValid() && ((entity instanceof Player) && ((Player) entity).isOnline())) {
437+
if (isValid(entity)) {
437438
entity.teleport(location);
438439
future.complete(true);
439440
} else {
@@ -480,4 +481,11 @@ private Player getPlayerFromMainThread(Supplier<Player> playerSupplier) {
480481
// Fallback to null
481482
return null;
482483
}
484+
485+
private boolean isValid(Entity entity) {
486+
if (entity.isValid()) {
487+
return !(entity instanceof Player) || ((Player) entity).isOnline();
488+
}
489+
return entity instanceof Projectile && !entity.isDead();
490+
}
483491
}

platform/spigot/src/main/java/com/tcoded/folialib/impl/SpigotImplementation.java

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.bukkit.block.Block;
1111
import org.bukkit.entity.Entity;
1212
import org.bukkit.entity.Player;
13+
import org.bukkit.entity.Projectile;
1314
import org.bukkit.event.player.PlayerTeleportEvent;
1415
import org.bukkit.plugin.java.JavaPlugin;
1516
import 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

Comments
 (0)