Provide script download suggestions async (#986)

This commit is contained in:
altrisi authored and GitHub committed 2021-07-11 07:43:11 -03:00
1 parent 7d4eeb464b
commit cca9d2ec7e
2 files changed
+32 -12

No files matched your search

@@ -2,7 +2,6 @@ package carpet.commands;
import carpet.CarpetServer; import carpet.CarpetServer;
import carpet.CarpetSettings; import carpet.CarpetSettings;
import carpet.script.CarpetScriptServer;
import carpet.script.utils.AppStoreManager; import carpet.script.utils.AppStoreManager;
import carpet.script.CarpetEventServer; import carpet.script.CarpetEventServer;
import carpet.script.CarpetExpression; import carpet.script.CarpetExpression;
@@ -22,7 +21,6 @@ import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.builder.LiteralArgumentBuilder; import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.context.CommandContext; import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException; import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
import com.mojang.brigadier.suggestion.Suggestions; import com.mojang.brigadier.suggestion.Suggestions;
import com.mojang.brigadier.suggestion.SuggestionsBuilder; import com.mojang.brigadier.suggestion.SuggestionsBuilder;
import net.minecraft.block.Block; import net.minecraft.block.Block;
@@ -117,15 +115,18 @@ public class ScriptCommand
CommandContext<ServerCommandSource> context, CommandContext<ServerCommandSource> context,
SuggestionsBuilder suggestionsBuilder SuggestionsBuilder suggestionsBuilder
) throws CommandSyntaxException { ) throws CommandSyntaxException {
try {
String previous = suggestionsBuilder.getRemaining(); return CompletableFuture.supplyAsync(()->{
AppStoreManager.suggestionsFromPath(previous).forEach(suggestionsBuilder::suggest); String previous = suggestionsBuilder.getRemaining();
return suggestionsBuilder.buildFuture(); try {
} AppStoreManager.suggestionsFromPath(previous).forEach(suggestionsBuilder::suggest);
catch (IOException e) }
{ catch (IOException e)
throw new SimpleCommandExceptionType(Messenger.c("rb "+e.getMessage())).create(); {
} CarpetSettings.LOG.warn("Exception when fetching app store structure", e);
}
return suggestionsBuilder.build();
});
} }
public static void register(CommandDispatcher<ServerCommandSource> dispatcher) public static void register(CommandDispatcher<ServerCommandSource> dispatcher)
@@ -38,6 +38,7 @@ import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.stream.Collectors; import java.util.stream.Collectors;
/** /**
@@ -81,6 +82,7 @@ public class AppStoreManager
public Map<String, StoreNode> children; public Map<String, StoreNode> children;
public boolean sealed; public boolean sealed;
public String value; public String value;
private CountDownLatch childrenProgress;
public static StoreNode folder(StoreNode parent, String name) public static StoreNode folder(StoreNode parent, String name)
{ {
StoreNode node = new StoreNode(parent, name); StoreNode node = new StoreNode(parent, name);
@@ -126,6 +128,20 @@ public class AppStoreManager
{ {
if (sealed) return; if (sealed) return;
if (scarpetRepoLink == null) throw new IOException("Accessing scarpet app repo is disabled"); 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 queryPath = scarpetRepoLink + getPath();
String response; String response;
@@ -135,7 +151,9 @@ public class AppStoreManager
} }
catch (IOException e) 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(); JsonArray files = new JsonParser().parse(response).getAsJsonArray();
for(JsonElement je : files) for(JsonElement je : files)
@@ -153,6 +171,7 @@ public class AppStoreManager
} }
} }
sealed = true; sealed = true;
childrenProgress.countDown();
} }
/** /**