From 36924b494287bdd2da1b074b676ac5f4161953a2 Mon Sep 17 00:00:00 2001 From: mastermc05 Date: Thu, 11 Aug 2022 11:36:27 +0300 Subject: [PATCH] Clear scary stacktraces --- .../v118_2/AsyncChunkProvider118_2.java | 21 ++++++++++++------- .../helper/v118_2/MapChunkCache118_2.java | 11 ++++++++-- .../helper/v119/AsyncChunkProvider119.java | 21 ++++++++++++------- .../bukkit/helper/v119/MapChunkCache119.java | 10 ++++++++- 4 files changed, 46 insertions(+), 17 deletions(-) diff --git a/bukkit-helper-118-2/src/main/java/org/dynmap/bukkit/helper/v118_2/AsyncChunkProvider118_2.java b/bukkit-helper-118-2/src/main/java/org/dynmap/bukkit/helper/v118_2/AsyncChunkProvider118_2.java index 947b4424..03e831f7 100644 --- a/bukkit-helper-118-2/src/main/java/org/dynmap/bukkit/helper/v118_2/AsyncChunkProvider118_2.java +++ b/bukkit-helper-118-2/src/main/java/org/dynmap/bukkit/helper/v118_2/AsyncChunkProvider118_2.java @@ -93,11 +93,12 @@ public class AsyncChunkProvider118_2 { } //prepare data synchronously CompletableFuture future = CompletableFuture.supplyAsync(() -> { + //Null will mean that we save with spigot methods, which may be risky on async + //Since we're not in main thread, it now refuses new tasks because of shutdown, the risk is lower + if (!Bukkit.isPrimaryThread()) return null; try { return getAsyncSaveData.invoke(null, world.getHandle(), c); } catch (IllegalAccessException | InvocationTargetException e) { - //Save as from main thread - if (((CraftServer) Bukkit.getServer()).getServer().hasStopped()) return null; throw new RuntimeException(e); } }, ((CraftServer) Bukkit.getServer()).getServer()); @@ -105,15 +106,21 @@ public class AsyncChunkProvider118_2 { if (++currChunks > MapManager.mapman.getMaxChunkLoadsPerTick()) { try { Thread.sleep(25); //hold the lock so other threads also won't stress main thread - } catch (InterruptedException e) { - throw new RuntimeException(e); - } + } catch (InterruptedException ignored) {} } //save data asynchronously return () -> { + Object o = null; try { - return (NBTTagCompound) save.invoke(null, world.getHandle(), c, future.get()); - } catch (ReflectiveOperationException | ExecutionException | InterruptedException e) { + o = future.get(); + return (NBTTagCompound) save.invoke(null, world.getHandle(), c, o); + } catch (InterruptedException e) { + return null; + } catch (InvocationTargetException e) { + //We tried to use simple spigot methods at shutdown and failed, hopes for reading from disk + if (o == null) return null; + throw new RuntimeException(e); + } catch (ReflectiveOperationException | ExecutionException e) { throw new RuntimeException(e); } }; diff --git a/bukkit-helper-118-2/src/main/java/org/dynmap/bukkit/helper/v118_2/MapChunkCache118_2.java b/bukkit-helper-118-2/src/main/java/org/dynmap/bukkit/helper/v118_2/MapChunkCache118_2.java index c96b6670..21b94ea3 100644 --- a/bukkit-helper-118-2/src/main/java/org/dynmap/bukkit/helper/v118_2/MapChunkCache118_2.java +++ b/bukkit-helper-118-2/src/main/java/org/dynmap/bukkit/helper/v118_2/MapChunkCache118_2.java @@ -22,7 +22,7 @@ import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.util.List; import java.util.concurrent.CompletableFuture; -import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.ExecutionException; import java.util.function.Supplier; /** @@ -61,7 +61,14 @@ public class MapChunkCache118_2 extends GenericMapChunkCache { try { CompletableFuture nbt = provider.getChunk(((CraftWorld) w).getHandle(), chunk.x, chunk.z); return () -> { - NBTTagCompound compound = nbt.join(); + NBTTagCompound compound; + try { + compound = nbt.get(); + } catch (InterruptedException e) { + return null; + } catch (ExecutionException e) { + throw new RuntimeException(e); + } return compound == null ? null : parseChunkFromNBT(new NBT.NBTCompound(compound)); }; } catch (InvocationTargetException | IllegalAccessException ignored) { diff --git a/bukkit-helper-119/src/main/java/org/dynmap/bukkit/helper/v119/AsyncChunkProvider119.java b/bukkit-helper-119/src/main/java/org/dynmap/bukkit/helper/v119/AsyncChunkProvider119.java index bbd907ed..eef534b0 100644 --- a/bukkit-helper-119/src/main/java/org/dynmap/bukkit/helper/v119/AsyncChunkProvider119.java +++ b/bukkit-helper-119/src/main/java/org/dynmap/bukkit/helper/v119/AsyncChunkProvider119.java @@ -92,11 +92,12 @@ public class AsyncChunkProvider119 { } //prepare data synchronously CompletableFuture future = CompletableFuture.supplyAsync(() -> { + //Null will mean that we save with spigot methods, which may be risky on async + //Since we're not in main thread, it now refuses new tasks because of shutdown, the risk is lower + if (!Bukkit.isPrimaryThread()) return null; try { return getAsyncSaveData.invoke(null, world.getHandle(), c); } catch (ReflectiveOperationException e) { - //Save as from main thread - if (((CraftServer) Bukkit.getServer()).getServer().hasStopped()) return null; throw new RuntimeException(e); } }, ((CraftServer) Bukkit.getServer()).getServer()); @@ -104,15 +105,21 @@ public class AsyncChunkProvider119 { if (++currChunks > MapManager.mapman.getMaxChunkLoadsPerTick()) { try { Thread.sleep(25); //hold the lock so other threads also won't stress main thread - } catch (InterruptedException e) { - throw new RuntimeException(e); - } + } catch (InterruptedException ignored) {} } //save data asynchronously return () -> { + Object o = null; try { - return (NBTTagCompound) save.invoke(null, world.getHandle(), c, future.get()); - } catch (ReflectiveOperationException | ExecutionException | InterruptedException e) { + o = future.get(); + return (NBTTagCompound) save.invoke(null, world.getHandle(), c, o); + } catch (InterruptedException e) { + return null; + } catch (InvocationTargetException e) { + //We tried to use simple spigot methods at shutdown and failed, hopes for reading from disk + if (o == null) return null; + throw new RuntimeException(e); + } catch (ReflectiveOperationException | ExecutionException e) { throw new RuntimeException(e); } }; diff --git a/bukkit-helper-119/src/main/java/org/dynmap/bukkit/helper/v119/MapChunkCache119.java b/bukkit-helper-119/src/main/java/org/dynmap/bukkit/helper/v119/MapChunkCache119.java index a94c0898..5ecbc277 100644 --- a/bukkit-helper-119/src/main/java/org/dynmap/bukkit/helper/v119/MapChunkCache119.java +++ b/bukkit-helper-119/src/main/java/org/dynmap/bukkit/helper/v119/MapChunkCache119.java @@ -19,6 +19,7 @@ import java.util.List; import java.util.NoSuchElementException; import java.util.concurrent.CancellationException; import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; import java.util.function.Supplier; /** @@ -58,7 +59,14 @@ public class MapChunkCache119 extends GenericMapChunkCache { try { CompletableFuture nbt = provider.getChunk(((CraftWorld) w).getHandle(), chunk.x, chunk.z); return () -> { - NBTTagCompound compound = nbt.join(); + NBTTagCompound compound; + try { + compound = nbt.get(); + } catch (InterruptedException e) { + return null; + } catch (ExecutionException e) { + throw new RuntimeException(e); + } return compound == null ? null : parseChunkFromNBT(new NBT.NBTCompound(compound)); }; } catch (InvocationTargetException | IllegalAccessException ignored) {