From e7c0c9e9a1b996826bb01eabd4d370e6e2c4e4c7 Mon Sep 17 00:00:00 2001 From: Shynixn Date: Fri, 8 Apr 2022 13:11:24 +0200 Subject: [PATCH 1/2] #94 Changed asynchronous and synchronous thread dispatching. --- .../api/service/ProxyService.java | 26 ++- .../bukkit/service/ProxyServiceImpl.java | 30 ++++ .../core/entity/ProgressTokenImpl.java | 19 +-- .../entity/StructureLoaderAbstractImpl.java | 152 ++++++++---------- .../entity/StructureSaverAbstractImpl.java | 120 +++++++------- 5 files changed, 182 insertions(+), 165 deletions(-) diff --git a/structureblocklib-api/src/main/java/com/github/shynixn/structureblocklib/api/service/ProxyService.java b/structureblocklib-api/src/main/java/com/github/shynixn/structureblocklib/api/service/ProxyService.java index 38cfc13c..aa1163da 100644 --- a/structureblocklib-api/src/main/java/com/github/shynixn/structureblocklib/api/service/ProxyService.java +++ b/structureblocklib-api/src/main/java/com/github/shynixn/structureblocklib/api/service/ProxyService.java @@ -5,6 +5,8 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import java.util.concurrent.Executor; + /** * Proxies small framework specific actions. */ @@ -37,19 +39,31 @@ public interface ProxyService { @Nullable Position toPosition(@Nullable L location); /** - * Runs an async task. - * - * @param runnable Runnable. + * Deprecated. Use getAsyncExecutor().execute instead. */ + @Deprecated void runAsyncTask(@NotNull Runnable runnable); /** - * Runs a sync task. - * - * @param runnable Runnable. + * Deprecated. Use getSyncExecutor().execute instead. */ + @Deprecated void runSyncTask(@NotNull Runnable runnable); + /** + * Gets an execute to schedule tasks on the synchronous bukkit thread. + * + * @return {@link Executor}. + */ + Executor getSyncExecutor(); + + /** + * Gets an execute to schedule tasks on the asynchronous bukkit threadPool. + * + * @return {@link Executor}. + */ + Executor getAsyncExecutor(); + /** * Gets the running minecraft version. * diff --git a/structureblocklib-bukkit-core/src/main/java/com/github/shynixn/structureblocklib/bukkit/service/ProxyServiceImpl.java b/structureblocklib-bukkit-core/src/main/java/com/github/shynixn/structureblocklib/bukkit/service/ProxyServiceImpl.java index a9ab21b5..7bf172b4 100644 --- a/structureblocklib-bukkit-core/src/main/java/com/github/shynixn/structureblocklib/bukkit/service/ProxyServiceImpl.java +++ b/structureblocklib-bukkit-core/src/main/java/com/github/shynixn/structureblocklib/bukkit/service/ProxyServiceImpl.java @@ -11,6 +11,8 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import java.util.concurrent.Executor; + @SuppressWarnings("unchecked") public class ProxyServiceImpl implements ProxyService { private Plugin plugin; @@ -106,6 +108,34 @@ public void runSyncTask(@NotNull Runnable runnable) { plugin.getServer().getScheduler().runTask(plugin, runnable); } + /** + * Gets an execute to schedule tasks on the synchronous bukkit thread. + * + * @return {@link Executor}. + */ + @Override + public Executor getSyncExecutor() { + if (!plugin.isEnabled()) { + return Runnable::run; + } + + return command -> plugin.getServer().getScheduler().runTask(plugin, command); + } + + /** + * Gets an execute to schedule tasks on the asynchronous bukkit threadpool. + * + * @return {@link Executor}. + */ + @Override + public Executor getAsyncExecutor() { + if (!plugin.isEnabled()) { + return Runnable::run; + } + + return command -> plugin.getServer().getScheduler().runTaskAsynchronously(plugin, command); + } + /** * Gets the running minecraft version. * diff --git a/structureblocklib-core/src/main/java/com/github/shynixn/structureblocklib/core/entity/ProgressTokenImpl.java b/structureblocklib-core/src/main/java/com/github/shynixn/structureblocklib/core/entity/ProgressTokenImpl.java index 37f5f1e2..1437ec1b 100644 --- a/structureblocklib-core/src/main/java/com/github/shynixn/structureblocklib/core/entity/ProgressTokenImpl.java +++ b/structureblocklib-core/src/main/java/com/github/shynixn/structureblocklib/core/entity/ProgressTokenImpl.java @@ -14,25 +14,26 @@ */ public class ProgressTokenImpl implements ProgressToken { private final Set> progressConsumers = new HashSet<>(); - private final CompletionStage item; + + private CompletionStage item; private volatile boolean cancelled = false; /** - * Creates a new instance of {@link ProgressTokenImpl}. + * Calls the progress consumers with the progress update. * - * @param item Not null completionState. + * @param progress update. */ - public ProgressTokenImpl(CompletionStage item) { - this.item = item; + public void progress(double progress) { + progressConsumers.forEach(e -> e.accept(progress)); } /** - * Calls the progress consumers with the progress update. + * Sets the completion stage. * - * @param progress update. + * @param item {@link CompletionStage} */ - public void progress(double progress) { - progressConsumers.forEach(e -> e.accept(progress)); + public void setCompletionStage(CompletionStage item) { + this.item = item; } /** diff --git a/structureblocklib-core/src/main/java/com/github/shynixn/structureblocklib/core/entity/StructureLoaderAbstractImpl.java b/structureblocklib-core/src/main/java/com/github/shynixn/structureblocklib/core/entity/StructureLoaderAbstractImpl.java index 7799b925..8d066a9a 100644 --- a/structureblocklib-core/src/main/java/com/github/shynixn/structureblocklib/core/entity/StructureLoaderAbstractImpl.java +++ b/structureblocklib-core/src/main/java/com/github/shynixn/structureblocklib/core/entity/StructureLoaderAbstractImpl.java @@ -15,6 +15,7 @@ import java.nio.file.Path; import java.util.Base64; import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CompletionStage; public class StructureLoaderAbstractImpl implements StructureLoaderAbstract { private final ProxyService proxyService; @@ -210,36 +211,25 @@ public StructureLoaderAbstract seed(long seed) { */ @Override public @NotNull ProgressToken loadFromSaver(@NotNull StructureSaverAbstract source) { - CompletableFuture completableFuture = new CompletableFuture<>(); - ProgressTokenImpl rootToken = new ProgressTokenImpl<>(completableFuture); + ProgressTokenImpl progressToken = new ProgressTokenImpl<>(); + progressToken.progress(0.0); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); - ProgressToken innerToken = source.saveToOutputStream(outputStream); - rootToken.progress(0.0); - innerToken.getCompletionStage().thenAccept(e_ -> { - rootToken.progress(0.5); + CompletionStage completableFuture = source.saveToOutputStream(outputStream).getCompletionStage().thenComposeAsync(e_ -> { + progressToken.progress(0.5); ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray()); - ProgressToken finalToken = this.loadFromInputStream(inputStream); - finalToken.getCompletionStage().exceptionally(e -> { - completableFuture.completeExceptionally(e); - return null; - }); - finalToken.getCompletionStage().thenAccept(a_ -> { - try { - outputStream.close(); - inputStream.close(); - rootToken.progress(1.0); - completableFuture.complete(a_); - } catch (IOException ioException) { - completableFuture.completeExceptionally(ioException); - } - }); - }); - innerToken.getCompletionStage().exceptionally(e -> { - completableFuture.completeExceptionally(e); - return null; - }); - - return rootToken; + return loadFromInputStream(inputStream).getCompletionStage() + .thenAcceptAsync(e1_ -> { + progressToken.progress(1.0); + try { + inputStream.close(); + outputStream.close(); + } catch (IOException e) { + throw new RuntimeException(e); + } + }, proxyService.getSyncExecutor()); + }, proxyService.getSyncExecutor()); + progressToken.setCompletionStage(completableFuture); + return progressToken; } /** @@ -259,19 +249,24 @@ public StructureLoaderAbstract seed(long seed) { public @NotNull ProgressToken loadFromWorld(@NotNull String worldName, @NotNull String author, @NotNull String name) { Version version = proxyService.getServerVersion(); File file; - if (version.isVersionSameOrGreaterThan(Version.VERSION_1_13_R2)) { file = new File(worldName + File.separator + "generated" + File.separator + author + File.separator + "structures" + File.separator + name + ".nbt"); } else { file = new File(worldName + File.separator + "structures" + File.separator + name + ".nbt"); } - try { - Files.createDirectories(file.getParentFile().toPath()); - } catch (IOException e) { - throw new RuntimeException(e); - } - + ProgressTokenImpl progressToken = new ProgressTokenImpl<>(); + CompletionStage completionStage = CompletableFuture.completedFuture(null).thenComposeAsync(e_ -> { + try { + Files.createDirectories(file.getParentFile().toPath()); + } catch (IOException e) { + throw new RuntimeException(e); + } + ProgressToken childProgressToken = loadFromFile(file); + childProgressToken.onProgress(progressToken::progress); + return childProgressToken.getCompletionStage(); + }, proxyService.getAsyncExecutor()); + progressToken.setCompletionStage(completionStage); return loadFromFile(file); } @@ -288,27 +283,22 @@ public StructureLoaderAbstract seed(long seed) { @Override @NotNull public ProgressToken loadFromString(@NotNull String source) { - CompletableFuture completableFuture = new CompletableFuture<>(); - ProgressTokenImpl rootToken = new ProgressTokenImpl<>(completableFuture); - byte[] content = Base64.getDecoder().decode(source); - - ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(content); - ProgressToken innerToken = loadFromInputStream(byteArrayInputStream); - innerToken.onProgress(rootToken::progress); - innerToken.getCompletionStage().thenAccept(e -> { - try { - byteArrayInputStream.close(); - completableFuture.complete(e); - } catch (IOException ioException) { - completableFuture.completeExceptionally(ioException); - } - }); - innerToken.getCompletionStage().exceptionally(e -> { - completableFuture.completeExceptionally(e); - return null; - }); - - return rootToken; + ProgressTokenImpl progressToken = new ProgressTokenImpl<>(); + CompletableFuture completableFuture = CompletableFuture.completedFuture(null).thenComposeAsync(e_ -> { + byte[] content = Base64.getDecoder().decode(source); + ByteArrayInputStream inputStream = new ByteArrayInputStream(content); + ProgressToken childProgressToken = loadFromInputStream(inputStream); + childProgressToken.onProgress(progressToken::progress); + return childProgressToken.getCompletionStage().thenAcceptAsync(e1_ -> { + try { + inputStream.close(); + } catch (IOException e) { + throw new RuntimeException(e); + } + }, proxyService.getAsyncExecutor()); + }, proxyService.getAsyncExecutor()); + progressToken.setCompletionStage(completableFuture); + return progressToken; } /** @@ -340,32 +330,25 @@ public ProgressToken loadFromPath(@NotNull Path source) { @Override @NotNull public ProgressToken loadFromFile(@NotNull File source) { - CompletableFuture completableFuture = new CompletableFuture<>(); - ProgressTokenImpl rootToken = new ProgressTokenImpl<>(completableFuture); - - proxyService.runAsyncTask(() -> { + ProgressTokenImpl progressToken = new ProgressTokenImpl<>(); + CompletableFuture completableFuture = CompletableFuture.completedFuture(null).thenComposeAsync(e_ -> { try { FileInputStream inputStream = new FileInputStream(source); - ProgressToken progressToken = loadFromInputStream(inputStream); - progressToken.onProgress(rootToken::progress); - progressToken.getCompletionStage().thenAccept(c -> { + ProgressToken childProgressToken = loadFromInputStream(inputStream); + childProgressToken.onProgress(progressToken::progress); + return childProgressToken.getCompletionStage().thenAcceptAsync(e1_ -> { try { inputStream.close(); - completableFuture.complete(c); } catch (IOException e) { - completableFuture.completeExceptionally(e); + throw new RuntimeException(e); } - }); - progressToken.getCompletionStage().exceptionally(throwable -> { - completableFuture.completeExceptionally(throwable); - return null; - }); - } catch (IOException e) { - proxyService.runSyncTask(() -> completableFuture.completeExceptionally(e)); + }, proxyService.getAsyncExecutor()); + } catch (FileNotFoundException e) { + throw new RuntimeException(e); } - }); - - return rootToken; + }, proxyService.getAsyncExecutor()); + progressToken.setCompletionStage(completableFuture); + return progressToken; } /** @@ -381,8 +364,7 @@ public ProgressToken loadFromFile(@NotNull File source) { @Override @NotNull public ProgressToken loadFromInputStream(@NotNull InputStream source) { - CompletableFuture completableFuture = new CompletableFuture<>(); - ProgressTokenImpl progressToken = new ProgressTokenImpl<>(completableFuture); + ProgressTokenImpl progressToken = new ProgressTokenImpl<>(); StructurePlaceMetaImpl meta = new StructurePlaceMetaImpl(); meta.location = this.location; meta.includeEntities = this.includeEntities; @@ -392,23 +374,23 @@ public ProgressToken loadFromInputStream(@NotNull InputStream source) { meta.rotation = this.rotation; progressToken.progress(0.0); - proxyService.runAsyncTask(() -> { + CompletableFuture completableFuture = CompletableFuture.completedFuture(null).thenComposeAsync(e_ -> { try { Object definedStructure = serializationService.deSerialize(source); - proxyService.runSyncTask(() -> { - progressToken.progress(0.5); + return CompletableFuture.runAsync(() -> { try { + progressToken.progress(0.5); worldService.placeStructureToWorld(meta, definedStructure); - completableFuture.complete(null); progressToken.progress(1.0); } catch (Exception e) { - completableFuture.completeExceptionally(e); + throw new RuntimeException(e); } - }); + }, proxyService.getSyncExecutor()); } catch (IOException e) { - proxyService.runSyncTask(() -> completableFuture.completeExceptionally(e)); + throw new RuntimeException(e); } - }); + }, proxyService.getAsyncExecutor()); + progressToken.setCompletionStage(completableFuture); return progressToken; } diff --git a/structureblocklib-core/src/main/java/com/github/shynixn/structureblocklib/core/entity/StructureSaverAbstractImpl.java b/structureblocklib-core/src/main/java/com/github/shynixn/structureblocklib/core/entity/StructureSaverAbstractImpl.java index 707d9594..a5ea9163 100644 --- a/structureblocklib-core/src/main/java/com/github/shynixn/structureblocklib/core/entity/StructureSaverAbstractImpl.java +++ b/structureblocklib-core/src/main/java/com/github/shynixn/structureblocklib/core/entity/StructureSaverAbstractImpl.java @@ -17,6 +17,7 @@ import java.nio.file.Path; import java.util.Base64; import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CompletionStage; /** * Interface fluent API to save structures from the world into @@ -301,15 +302,22 @@ public StructureSaverAbstract structureVoidTypeName(@NotNull String name) file = new File(worldName + File.separator + "structures" + File.separator + name + ".nbt"); } - try { - Files.createDirectories(file.getParentFile().toPath()); - } catch (IOException e) { - throw new RuntimeException(e); - } - author(author); - return saveToFile(file); + ProgressTokenImpl progressToken = new ProgressTokenImpl<>(); + CompletableFuture completableFuture = CompletableFuture.completedFuture(null).thenComposeAsync(e1 -> { + try { + Files.createDirectories(file.getParentFile().toPath()); + } catch (IOException e) { + throw new RuntimeException(e); + } + + ProgressToken childToken = saveToFile(file); + childToken.onProgress(progressToken::progress); + return childToken.getCompletionStage(); + }, proxyService.getAsyncExecutor()); + progressToken.setCompletionStage(completableFuture); + return progressToken; } @@ -324,27 +332,22 @@ public StructureSaverAbstract structureVoidTypeName(@NotNull String name) */ @Override public @NotNull ProgressToken saveToString() { - CompletableFuture completableFuture = new CompletableFuture<>(); - ProgressTokenImpl rootToken = new ProgressTokenImpl<>(completableFuture); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); - + ProgressTokenImpl progressToken = new ProgressTokenImpl<>(); ProgressToken innerToken = saveToOutputStream(byteArrayOutputStream); - innerToken.onProgress(rootToken::progress); - innerToken.getCompletionStage().thenAccept(e -> { + innerToken.onProgress(progressToken::progress); + + CompletionStage completableFuture = innerToken.getCompletionStage().thenApplyAsync(e_ -> { + String data = Base64.getEncoder().encodeToString(byteArrayOutputStream.toByteArray()); try { - String data = Base64.getEncoder().encodeToString(byteArrayOutputStream.toByteArray()); byteArrayOutputStream.close(); - completableFuture.complete(data); - } catch (IOException ioException) { - completableFuture.completeExceptionally(ioException); + } catch (IOException e) { + throw new RuntimeException(e); } - }); - innerToken.getCompletionStage().exceptionally(e -> { - completableFuture.completeExceptionally(e); - return null; - }); - - return rootToken; + return data; + }, proxyService.getAsyncExecutor()); + progressToken.setCompletionStage(completableFuture); + return progressToken; } /** @@ -374,32 +377,26 @@ public StructureSaverAbstract structureVoidTypeName(@NotNull String name) */ @Override public @NotNull ProgressToken saveToFile(@NotNull File target) { - CompletableFuture completableFuture = new CompletableFuture<>(); - ProgressTokenImpl rootToken = new ProgressTokenImpl<>(completableFuture); + ProgressTokenImpl progressToken = new ProgressTokenImpl<>(); - proxyService.runAsyncTask(() -> { + CompletionStage completionStage = CompletableFuture.completedFuture(null).thenComposeAsync(e_ -> { try { FileOutputStream outputStream = new FileOutputStream(target); - ProgressToken progressToken = saveToOutputStream(outputStream); - progressToken.onProgress(rootToken::progress); - progressToken.getCompletionStage().thenAccept(c -> { + ProgressToken childProgressToken = saveToOutputStream(outputStream); + childProgressToken.onProgress(progressToken::progress); + return childProgressToken.getCompletionStage().thenAcceptAsync(e1_ -> { try { outputStream.close(); - completableFuture.complete(c); } catch (IOException e) { - completableFuture.completeExceptionally(e); + throw new RuntimeException(e); } - }); - progressToken.getCompletionStage().exceptionally(throwable -> { - completableFuture.completeExceptionally(throwable); - return null; - }); - } catch (IOException e) { - proxyService.runSyncTask(() -> completableFuture.completeExceptionally(e)); + }, proxyService.getAsyncExecutor()); + } catch (FileNotFoundException e) { + throw new RuntimeException(e); } - }); - - return rootToken; + }, proxyService.getAsyncExecutor()); + progressToken.setCompletionStage(completionStage); + return progressToken; } /** @@ -415,32 +412,25 @@ public StructureSaverAbstract structureVoidTypeName(@NotNull String name) @Override public @NotNull ProgressToken saveToOutputStream(@NotNull OutputStream target) { StructureReadMeta meta = validate(); - CompletableFuture completableFuture = new CompletableFuture<>(); - ProgressTokenImpl progressToken = new ProgressTokenImpl<>(completableFuture); + ProgressTokenImpl progressToken = new ProgressTokenImpl<>(); - proxyService.runSyncTask(() -> { - progressToken.progress(0.0); - Object definedStructure; + progressToken.progress(0.0); + CompletionStage completionStage = CompletableFuture.completedFuture(null).thenComposeAsync(e_ -> { try { - definedStructure = worldService.readStructureFromWorld(meta); + final Object definedStructure = worldService.readStructureFromWorld(meta); + return CompletableFuture.completedFuture(null).thenComposeAsync(e1_ -> { + try { + serializationService.serialize(definedStructure, target); + } catch (IOException e) { + throw new RuntimeException(e); + } + return CompletableFuture.runAsync(() -> progressToken.progress(1.0), proxyService.getSyncExecutor()); + }, proxyService.getAsyncExecutor()); } catch (Exception e) { - completableFuture.completeExceptionally(e); - return; + throw new RuntimeException(e); } - progressToken.progress(0.5); - Object finalDefinedStructure = definedStructure; - proxyService.runAsyncTask(() -> { - try { - serializationService.serialize(finalDefinedStructure, target); - proxyService.runSyncTask(() -> { - completableFuture.complete(null); - progressToken.progress(1.0); - }); - } catch (IOException e) { - proxyService.runSyncTask(() -> completableFuture.completeExceptionally(e)); - } - }); - }); + }, proxyService.getSyncExecutor()); + progressToken.setCompletionStage(completionStage); return progressToken; } @@ -458,7 +448,7 @@ private StructureReadMeta validate() { } StructureReadMeta structureReadMeta = createReadMeta(); - changeOffSetToPositivOffset(structureReadMeta.getLocation(), structureReadMeta.getOffset()); + changeOffSetToPositiveOffset(structureReadMeta.getLocation(), structureReadMeta.getOffset()); validateDirection("x", structureReadMeta.getLocation().getX(), structureReadMeta.getOffset().getX()); validateDirection("y", structureReadMeta.getLocation().getY(), structureReadMeta.getOffset().getY()); validateDirection("z", structureReadMeta.getLocation().getZ(), structureReadMeta.getOffset().getZ()); @@ -514,7 +504,7 @@ private void validateDirection(String direction, double source, double offset) { * @param source source. * @param offSet fofset. */ - private void changeOffSetToPositivOffset(Position source, Position offSet) { + private void changeOffSetToPositiveOffset(Position source, Position offSet) { if (offSet.getX() < 0) { source.setX(source.getX() + offSet.getX() + 1); offSet.setX(offSet.getX() * -1); From 99ae4b1200437ed8e07b2db06dc6a82121d36837 Mon Sep 17 00:00:00 2001 From: Shynixn Date: Fri, 8 Apr 2022 14:27:02 +0200 Subject: [PATCH 2/2] #94 Updated tests. --- .../test/java/helper/MockedProxyService.java | 21 ++++++++++++++ .../test/java/helper/MockedProxyService.java | 21 ++++++++++++++ .../test/java/helper/MockedProxyService.java | 21 ++++++++++++++ .../test/java/helper/MockedProxyService.java | 21 ++++++++++++++ .../test/java/helper/MockedProxyService.java | 21 ++++++++++++++ .../test/java/helper/MockedProxyService.java | 21 ++++++++++++++ .../test/java/helper/MockedProxyService.java | 21 ++++++++++++++ .../test/java/helper/MockedProxyService.java | 21 ++++++++++++++ .../test/java/helper/MockedProxyService.java | 21 ++++++++++++++ .../test/java/helper/MockedProxyService.java | 21 ++++++++++++++ .../test/java/helper/MockedProxyService.java | 21 ++++++++++++++ .../entity/StructureLoaderAbstractImpl.java | 28 +++++++++---------- .../entity/StructureSaverAbstractImpl.java | 3 +- 13 files changed, 247 insertions(+), 15 deletions(-) diff --git a/structureblocklib-bukkit-core/bukkit-nms-109R2/src/test/java/helper/MockedProxyService.java b/structureblocklib-bukkit-core/bukkit-nms-109R2/src/test/java/helper/MockedProxyService.java index fa09db3c..98afe462 100644 --- a/structureblocklib-bukkit-core/bukkit-nms-109R2/src/test/java/helper/MockedProxyService.java +++ b/structureblocklib-bukkit-core/bukkit-nms-109R2/src/test/java/helper/MockedProxyService.java @@ -13,6 +13,7 @@ import java.util.Queue; import java.util.concurrent.ConcurrentLinkedQueue; +import java.util.concurrent.Executor; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; @@ -94,6 +95,26 @@ public void runSyncTask(@NotNull Runnable runnable) { concurrentList.add(runnable); } + /** + * Gets an execute to schedule tasks on the synchronous bukkit thread. + * + * @return {@link Executor}. + */ + @Override + public Executor getSyncExecutor() { + return c -> concurrentList.add(c); + } + + /** + * Gets an execute to schedule tasks on the asynchronous bukkit threadPool. + * + * @return {@link Executor}. + */ + @Override + public Executor getAsyncExecutor() { + return executor; + } + /** * Gets the running minecraft version. * diff --git a/structureblocklib-bukkit-core/bukkit-nms-110R1/src/test/java/helper/MockedProxyService.java b/structureblocklib-bukkit-core/bukkit-nms-110R1/src/test/java/helper/MockedProxyService.java index fa09db3c..c5f9dcfd 100644 --- a/structureblocklib-bukkit-core/bukkit-nms-110R1/src/test/java/helper/MockedProxyService.java +++ b/structureblocklib-bukkit-core/bukkit-nms-110R1/src/test/java/helper/MockedProxyService.java @@ -13,6 +13,7 @@ import java.util.Queue; import java.util.concurrent.ConcurrentLinkedQueue; +import java.util.concurrent.Executor; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; @@ -94,6 +95,26 @@ public void runSyncTask(@NotNull Runnable runnable) { concurrentList.add(runnable); } + /** + * Gets an execute to schedule tasks on the synchronous bukkit thread. + * + * @return {@link Executor}. + */ + @Override + public Executor getSyncExecutor() { + return c -> concurrentList.add(c); + } + + /** + * Gets an execute to schedule tasks on the asynchronous bukkit threadPool. + * + * @return {@link Executor}. + */ + @Override + public Executor getAsyncExecutor() { + return executor; + } + /** * Gets the running minecraft version. * diff --git a/structureblocklib-bukkit-core/bukkit-nms-111R1/src/test/java/helper/MockedProxyService.java b/structureblocklib-bukkit-core/bukkit-nms-111R1/src/test/java/helper/MockedProxyService.java index fa09db3c..c5f9dcfd 100644 --- a/structureblocklib-bukkit-core/bukkit-nms-111R1/src/test/java/helper/MockedProxyService.java +++ b/structureblocklib-bukkit-core/bukkit-nms-111R1/src/test/java/helper/MockedProxyService.java @@ -13,6 +13,7 @@ import java.util.Queue; import java.util.concurrent.ConcurrentLinkedQueue; +import java.util.concurrent.Executor; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; @@ -94,6 +95,26 @@ public void runSyncTask(@NotNull Runnable runnable) { concurrentList.add(runnable); } + /** + * Gets an execute to schedule tasks on the synchronous bukkit thread. + * + * @return {@link Executor}. + */ + @Override + public Executor getSyncExecutor() { + return c -> concurrentList.add(c); + } + + /** + * Gets an execute to schedule tasks on the asynchronous bukkit threadPool. + * + * @return {@link Executor}. + */ + @Override + public Executor getAsyncExecutor() { + return executor; + } + /** * Gets the running minecraft version. * diff --git a/structureblocklib-bukkit-core/bukkit-nms-112R1/src/test/java/helper/MockedProxyService.java b/structureblocklib-bukkit-core/bukkit-nms-112R1/src/test/java/helper/MockedProxyService.java index fa09db3c..c5f9dcfd 100644 --- a/structureblocklib-bukkit-core/bukkit-nms-112R1/src/test/java/helper/MockedProxyService.java +++ b/structureblocklib-bukkit-core/bukkit-nms-112R1/src/test/java/helper/MockedProxyService.java @@ -13,6 +13,7 @@ import java.util.Queue; import java.util.concurrent.ConcurrentLinkedQueue; +import java.util.concurrent.Executor; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; @@ -94,6 +95,26 @@ public void runSyncTask(@NotNull Runnable runnable) { concurrentList.add(runnable); } + /** + * Gets an execute to schedule tasks on the synchronous bukkit thread. + * + * @return {@link Executor}. + */ + @Override + public Executor getSyncExecutor() { + return c -> concurrentList.add(c); + } + + /** + * Gets an execute to schedule tasks on the asynchronous bukkit threadPool. + * + * @return {@link Executor}. + */ + @Override + public Executor getAsyncExecutor() { + return executor; + } + /** * Gets the running minecraft version. * diff --git a/structureblocklib-bukkit-core/bukkit-nms-113R2/src/test/java/helper/MockedProxyService.java b/structureblocklib-bukkit-core/bukkit-nms-113R2/src/test/java/helper/MockedProxyService.java index fa09db3c..c5f9dcfd 100644 --- a/structureblocklib-bukkit-core/bukkit-nms-113R2/src/test/java/helper/MockedProxyService.java +++ b/structureblocklib-bukkit-core/bukkit-nms-113R2/src/test/java/helper/MockedProxyService.java @@ -13,6 +13,7 @@ import java.util.Queue; import java.util.concurrent.ConcurrentLinkedQueue; +import java.util.concurrent.Executor; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; @@ -94,6 +95,26 @@ public void runSyncTask(@NotNull Runnable runnable) { concurrentList.add(runnable); } + /** + * Gets an execute to schedule tasks on the synchronous bukkit thread. + * + * @return {@link Executor}. + */ + @Override + public Executor getSyncExecutor() { + return c -> concurrentList.add(c); + } + + /** + * Gets an execute to schedule tasks on the asynchronous bukkit threadPool. + * + * @return {@link Executor}. + */ + @Override + public Executor getAsyncExecutor() { + return executor; + } + /** * Gets the running minecraft version. * diff --git a/structureblocklib-bukkit-core/bukkit-nms-114R1/src/test/java/helper/MockedProxyService.java b/structureblocklib-bukkit-core/bukkit-nms-114R1/src/test/java/helper/MockedProxyService.java index fa09db3c..c5f9dcfd 100644 --- a/structureblocklib-bukkit-core/bukkit-nms-114R1/src/test/java/helper/MockedProxyService.java +++ b/structureblocklib-bukkit-core/bukkit-nms-114R1/src/test/java/helper/MockedProxyService.java @@ -13,6 +13,7 @@ import java.util.Queue; import java.util.concurrent.ConcurrentLinkedQueue; +import java.util.concurrent.Executor; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; @@ -94,6 +95,26 @@ public void runSyncTask(@NotNull Runnable runnable) { concurrentList.add(runnable); } + /** + * Gets an execute to schedule tasks on the synchronous bukkit thread. + * + * @return {@link Executor}. + */ + @Override + public Executor getSyncExecutor() { + return c -> concurrentList.add(c); + } + + /** + * Gets an execute to schedule tasks on the asynchronous bukkit threadPool. + * + * @return {@link Executor}. + */ + @Override + public Executor getAsyncExecutor() { + return executor; + } + /** * Gets the running minecraft version. * diff --git a/structureblocklib-bukkit-core/bukkit-nms-115R1/src/test/java/helper/MockedProxyService.java b/structureblocklib-bukkit-core/bukkit-nms-115R1/src/test/java/helper/MockedProxyService.java index fa09db3c..c5f9dcfd 100644 --- a/structureblocklib-bukkit-core/bukkit-nms-115R1/src/test/java/helper/MockedProxyService.java +++ b/structureblocklib-bukkit-core/bukkit-nms-115R1/src/test/java/helper/MockedProxyService.java @@ -13,6 +13,7 @@ import java.util.Queue; import java.util.concurrent.ConcurrentLinkedQueue; +import java.util.concurrent.Executor; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; @@ -94,6 +95,26 @@ public void runSyncTask(@NotNull Runnable runnable) { concurrentList.add(runnable); } + /** + * Gets an execute to schedule tasks on the synchronous bukkit thread. + * + * @return {@link Executor}. + */ + @Override + public Executor getSyncExecutor() { + return c -> concurrentList.add(c); + } + + /** + * Gets an execute to schedule tasks on the asynchronous bukkit threadPool. + * + * @return {@link Executor}. + */ + @Override + public Executor getAsyncExecutor() { + return executor; + } + /** * Gets the running minecraft version. * diff --git a/structureblocklib-bukkit-core/bukkit-nms-116R3/src/test/java/helper/MockedProxyService.java b/structureblocklib-bukkit-core/bukkit-nms-116R3/src/test/java/helper/MockedProxyService.java index fa09db3c..c5f9dcfd 100644 --- a/structureblocklib-bukkit-core/bukkit-nms-116R3/src/test/java/helper/MockedProxyService.java +++ b/structureblocklib-bukkit-core/bukkit-nms-116R3/src/test/java/helper/MockedProxyService.java @@ -13,6 +13,7 @@ import java.util.Queue; import java.util.concurrent.ConcurrentLinkedQueue; +import java.util.concurrent.Executor; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; @@ -94,6 +95,26 @@ public void runSyncTask(@NotNull Runnable runnable) { concurrentList.add(runnable); } + /** + * Gets an execute to schedule tasks on the synchronous bukkit thread. + * + * @return {@link Executor}. + */ + @Override + public Executor getSyncExecutor() { + return c -> concurrentList.add(c); + } + + /** + * Gets an execute to schedule tasks on the asynchronous bukkit threadPool. + * + * @return {@link Executor}. + */ + @Override + public Executor getAsyncExecutor() { + return executor; + } + /** * Gets the running minecraft version. * diff --git a/structureblocklib-bukkit-core/bukkit-nms-117R1/src/test/java/helper/MockedProxyService.java b/structureblocklib-bukkit-core/bukkit-nms-117R1/src/test/java/helper/MockedProxyService.java index fa09db3c..c5f9dcfd 100644 --- a/structureblocklib-bukkit-core/bukkit-nms-117R1/src/test/java/helper/MockedProxyService.java +++ b/structureblocklib-bukkit-core/bukkit-nms-117R1/src/test/java/helper/MockedProxyService.java @@ -13,6 +13,7 @@ import java.util.Queue; import java.util.concurrent.ConcurrentLinkedQueue; +import java.util.concurrent.Executor; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; @@ -94,6 +95,26 @@ public void runSyncTask(@NotNull Runnable runnable) { concurrentList.add(runnable); } + /** + * Gets an execute to schedule tasks on the synchronous bukkit thread. + * + * @return {@link Executor}. + */ + @Override + public Executor getSyncExecutor() { + return c -> concurrentList.add(c); + } + + /** + * Gets an execute to schedule tasks on the asynchronous bukkit threadPool. + * + * @return {@link Executor}. + */ + @Override + public Executor getAsyncExecutor() { + return executor; + } + /** * Gets the running minecraft version. * diff --git a/structureblocklib-bukkit-core/bukkit-nms-118R1/src/test/java/helper/MockedProxyService.java b/structureblocklib-bukkit-core/bukkit-nms-118R1/src/test/java/helper/MockedProxyService.java index fa09db3c..01858a82 100644 --- a/structureblocklib-bukkit-core/bukkit-nms-118R1/src/test/java/helper/MockedProxyService.java +++ b/structureblocklib-bukkit-core/bukkit-nms-118R1/src/test/java/helper/MockedProxyService.java @@ -13,6 +13,7 @@ import java.util.Queue; import java.util.concurrent.ConcurrentLinkedQueue; +import java.util.concurrent.Executor; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; @@ -74,6 +75,26 @@ public class MockedProxyService implements ProxyService { return position; } + /** + * Gets an execute to schedule tasks on the synchronous bukkit thread. + * + * @return {@link Executor}. + */ + @Override + public Executor getSyncExecutor() { + return c -> concurrentList.add(c); + } + + /** + * Gets an execute to schedule tasks on the asynchronous bukkit threadPool. + * + * @return {@link Executor}. + */ + @Override + public Executor getAsyncExecutor() { + return executor; + } + /** * Runs an async task. * diff --git a/structureblocklib-bukkit-core/bukkit-nms-118R2/src/test/java/helper/MockedProxyService.java b/structureblocklib-bukkit-core/bukkit-nms-118R2/src/test/java/helper/MockedProxyService.java index fa09db3c..c5f9dcfd 100644 --- a/structureblocklib-bukkit-core/bukkit-nms-118R2/src/test/java/helper/MockedProxyService.java +++ b/structureblocklib-bukkit-core/bukkit-nms-118R2/src/test/java/helper/MockedProxyService.java @@ -13,6 +13,7 @@ import java.util.Queue; import java.util.concurrent.ConcurrentLinkedQueue; +import java.util.concurrent.Executor; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; @@ -94,6 +95,26 @@ public void runSyncTask(@NotNull Runnable runnable) { concurrentList.add(runnable); } + /** + * Gets an execute to schedule tasks on the synchronous bukkit thread. + * + * @return {@link Executor}. + */ + @Override + public Executor getSyncExecutor() { + return c -> concurrentList.add(c); + } + + /** + * Gets an execute to schedule tasks on the asynchronous bukkit threadPool. + * + * @return {@link Executor}. + */ + @Override + public Executor getAsyncExecutor() { + return executor; + } + /** * Gets the running minecraft version. * diff --git a/structureblocklib-core/src/main/java/com/github/shynixn/structureblocklib/core/entity/StructureLoaderAbstractImpl.java b/structureblocklib-core/src/main/java/com/github/shynixn/structureblocklib/core/entity/StructureLoaderAbstractImpl.java index 8d066a9a..7cb86a4b 100644 --- a/structureblocklib-core/src/main/java/com/github/shynixn/structureblocklib/core/entity/StructureLoaderAbstractImpl.java +++ b/structureblocklib-core/src/main/java/com/github/shynixn/structureblocklib/core/entity/StructureLoaderAbstractImpl.java @@ -373,23 +373,23 @@ public ProgressToken loadFromInputStream(@NotNull InputStream source) { meta.mirror = this.mirror; meta.rotation = this.rotation; - progressToken.progress(0.0); - CompletableFuture completableFuture = CompletableFuture.completedFuture(null).thenComposeAsync(e_ -> { - try { - Object definedStructure = serializationService.deSerialize(source); - return CompletableFuture.runAsync(() -> { + CompletableFuture completableFuture = CompletableFuture.completedFuture(null) + .thenAcceptAsync(e_ -> progressToken.progress(0.0), proxyService.getSyncExecutor()).thenComposeAsync(e_ -> { try { - progressToken.progress(0.5); - worldService.placeStructureToWorld(meta, definedStructure); - progressToken.progress(1.0); - } catch (Exception e) { + Object definedStructure = serializationService.deSerialize(source); + return CompletableFuture.runAsync(() -> { + try { + progressToken.progress(0.5); + worldService.placeStructureToWorld(meta, definedStructure); + progressToken.progress(1.0); + } catch (Exception e) { + throw new RuntimeException(e); + } + }, proxyService.getSyncExecutor()); + } catch (IOException e) { throw new RuntimeException(e); } - }, proxyService.getSyncExecutor()); - } catch (IOException e) { - throw new RuntimeException(e); - } - }, proxyService.getAsyncExecutor()); + }, proxyService.getAsyncExecutor()); progressToken.setCompletionStage(completableFuture); return progressToken; diff --git a/structureblocklib-core/src/main/java/com/github/shynixn/structureblocklib/core/entity/StructureSaverAbstractImpl.java b/structureblocklib-core/src/main/java/com/github/shynixn/structureblocklib/core/entity/StructureSaverAbstractImpl.java index a5ea9163..495c503b 100644 --- a/structureblocklib-core/src/main/java/com/github/shynixn/structureblocklib/core/entity/StructureSaverAbstractImpl.java +++ b/structureblocklib-core/src/main/java/com/github/shynixn/structureblocklib/core/entity/StructureSaverAbstractImpl.java @@ -414,10 +414,11 @@ public StructureSaverAbstract structureVoidTypeName(@NotNull String name) StructureReadMeta meta = validate(); ProgressTokenImpl progressToken = new ProgressTokenImpl<>(); - progressToken.progress(0.0); CompletionStage completionStage = CompletableFuture.completedFuture(null).thenComposeAsync(e_ -> { try { + progressToken.progress(0.0); final Object definedStructure = worldService.readStructureFromWorld(meta); + progressToken.progress(0.5); return CompletableFuture.completedFuture(null).thenComposeAsync(e1_ -> { try { serializationService.serialize(definedStructure, target);