From cca9d2ec7e02528b54015fe322fae1603ece2e64 Mon Sep 17 00:00:00 2001 From: altrisi Date: Sun, 11 Jul 2021 12:43:11 +0200 Subject: [PATCH] Provide script download suggestions async (#986) --- .../java/carpet/commands/ScriptCommand.java | 23 ++++++++++--------- .../carpet/script/utils/AppStoreManager.java | 21 ++++++++++++++++- 2 files changed, 32 insertions(+), 12 deletions(-) diff --git a/src/main/java/carpet/commands/ScriptCommand.java b/src/main/java/carpet/commands/ScriptCommand.java index 9e861fe0..8b1c6c0c 100644 --- a/src/main/java/carpet/commands/ScriptCommand.java +++ b/src/main/java/carpet/commands/ScriptCommand.java @@ -2,7 +2,6 @@ package carpet.commands; import carpet.CarpetServer; import carpet.CarpetSettings; -import carpet.script.CarpetScriptServer; import carpet.script.utils.AppStoreManager; import carpet.script.CarpetEventServer; import carpet.script.CarpetExpression; @@ -22,7 +21,6 @@ import com.mojang.brigadier.arguments.StringArgumentType; import com.mojang.brigadier.builder.LiteralArgumentBuilder; import com.mojang.brigadier.context.CommandContext; import com.mojang.brigadier.exceptions.CommandSyntaxException; -import com.mojang.brigadier.exceptions.SimpleCommandExceptionType; import com.mojang.brigadier.suggestion.Suggestions; import com.mojang.brigadier.suggestion.SuggestionsBuilder; import net.minecraft.block.Block; @@ -117,15 +115,18 @@ public class ScriptCommand CommandContext context, SuggestionsBuilder suggestionsBuilder ) throws CommandSyntaxException { - try { - String previous = suggestionsBuilder.getRemaining(); - AppStoreManager.suggestionsFromPath(previous).forEach(suggestionsBuilder::suggest); - return suggestionsBuilder.buildFuture(); - } - catch (IOException e) - { - throw new SimpleCommandExceptionType(Messenger.c("rb "+e.getMessage())).create(); - } + + return CompletableFuture.supplyAsync(()->{ + String previous = suggestionsBuilder.getRemaining(); + try { + AppStoreManager.suggestionsFromPath(previous).forEach(suggestionsBuilder::suggest); + } + catch (IOException e) + { + CarpetSettings.LOG.warn("Exception when fetching app store structure", e); + } + return suggestionsBuilder.build(); + }); } public static void register(CommandDispatcher dispatcher) diff --git a/src/main/java/carpet/script/utils/AppStoreManager.java b/src/main/java/carpet/script/utils/AppStoreManager.java index db825c8f..9c723b13 100644 --- a/src/main/java/carpet/script/utils/AppStoreManager.java +++ b/src/main/java/carpet/script/utils/AppStoreManager.java @@ -38,6 +38,7 @@ import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.concurrent.CountDownLatch; import java.util.stream.Collectors; /** @@ -81,6 +82,7 @@ public class AppStoreManager public Map children; public boolean sealed; public String value; + private CountDownLatch childrenProgress; public static StoreNode folder(StoreNode parent, String name) { StoreNode node = new StoreNode(parent, name); @@ -126,6 +128,20 @@ public class AppStoreManager { if (sealed) return; if (scarpetRepoLink == null) throw new IOException("Accessing scarpet app repo is disabled"); + try + { + if (childrenProgress != null) + { + childrenProgress.await(); + if (sealed) return; + else throw new IOException("Problems fetching suggestions. Check more details in previous exceptions."); + } + } + catch (InterruptedException e) + { + throw new IOException("Suggestion provider thread was interrupted, unexpected!", e); + } + childrenProgress = new CountDownLatch(1); String queryPath = scarpetRepoLink + getPath(); String response; @@ -135,7 +151,9 @@ public class AppStoreManager } catch (IOException e) { - throw new IOException("Problems fetching "+queryPath+": "+e); + childrenProgress.countDown(); + childrenProgress = null; // Reset to allow retrying + throw new IOException("Problems fetching " + queryPath, e); } JsonArray files = new JsonParser().parse(response).getAsJsonArray(); for(JsonElement je : files) @@ -153,6 +171,7 @@ public class AppStoreManager } } sealed = true; + childrenProgress.countDown(); } /**