Adding /script download (#826)

This reverts commit e98adb77a52d1e274ec320e0cafab34b24c1c738.

* First attempt at creating download system...

* Last try, I had uncommitted changes which I forgot to add.

If it fails now i swear imma delete my fork of fabric carpet.

* OK, this is the last chance. I have checked and double checked, this should not fail.

Todo make them more detailed, Im not the best person to do that tbh...

* Towards a suggestion builder

(Also committing stuff in ScriptDownloader incase it crashes)

* Finished first theoretically functional draft of suggestion builder

* improving suggestion building with actual github api

* Collapsing identical error messages

* Got suggestions to work!

Now gotta get the actual file to save to disk properly

* replacing whitespace with url whitespace to make it read form 'creativ building tools' etc

* Successfully downloaded script into folder location!

* only clients can access global scripts folder, so don't want to cause error message

* Compacting code and updating docs

* Fixed getting folders from file names
This commit is contained in:
Ghoulboy authored and GitHub committed 2021-05-19 17:30:39 -03:00
1 parent f56b22be6a
commit 5f16ffd933
3 files changed
+268 -9

No files matched your search

@@ -2,6 +2,7 @@ package carpet.commands;
import carpet.CarpetServer;
import carpet.CarpetSettings;
import carpet.helpers.ScriptDownloader;
import carpet.script.CarpetEventServer;
import carpet.script.CarpetExpression;
import carpet.script.CarpetScriptHost;
@@ -31,18 +32,13 @@ import net.minecraft.command.argument.BlockStateArgumentType;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.util.Clearable;
import net.minecraft.util.Pair;
import net.minecraft.util.math.BlockPos;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.math.BlockBox;
import org.apache.commons.lang3.StringUtils;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import java.util.TreeSet;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.function.Predicate;
import java.util.function.Supplier;
@@ -61,6 +57,7 @@ public class ScriptCommand
Set<String> allFunctions = (new CarpetExpression(null, "null", null, null)).getExpr().getFunctionNames();
scarpetFunctions = new TreeSet<>(Expression.none.getFunctionNames());
APIFunctions = allFunctions.stream().filter(s -> !scarpetFunctions.contains(s)).collect(Collectors.toCollection(TreeSet::new));
ScriptDownloader.updateLocalRepoStructure();
}
public static List<String> suggestFunctions(ScriptHost host, String previous, String prefix)
@@ -105,6 +102,23 @@ public class ScriptCommand
return suggestionsBuilder.buildFuture();
}
/**
* A method to suggest the available scarpet scripts based off of the current player input and {@link ScriptDownloader#localScarpetRepoStructure}
* variable.
*/
private static CompletableFuture<Suggestions> suggestDownloadableApps(
CommandContext<ServerCommandSource> context,
SuggestionsBuilder suggestionsBuilder
) {
String previous = suggestionsBuilder.getRemaining();
Pair<String, Set<String>> suggestions = ScriptDownloader.fileNamesFromPath(previous);
String currentValidPath = suggestions.getLeft();
suggestions.getRight().forEach(s -> suggestionsBuilder.suggest(currentValidPath + s));
return suggestionsBuilder.buildFuture();
}
public static void register(CommandDispatcher<ServerCommandSource> dispatcher)
{
LiteralArgumentBuilder<ServerCommandSource> b = literal("globals").
@@ -128,7 +142,7 @@ public class ScriptCommand
null,
null,
""
)).
)) .
then(argument("arguments", StringArgumentType.greedyString()).
executes( (cc) -> invoke(
cc,
@@ -302,10 +316,13 @@ public class ScriptCommand
StringArgumentType.getString(cc, "call")
)?1:0))));
LiteralArgumentBuilder<ServerCommandSource> d = literal("download").requires((player) -> SettingsManager.canUseCommand(player, CarpetSettings.commandScriptACE)).
then(literal("global").then(argument("path", StringArgumentType.greedyString()).suggests(ScriptCommand::suggestDownloadableApps).executes(cc->ScriptDownloader.downloadScript(cc, StringArgumentType.getString(cc,"path"), true)))).
then(literal("local").then(argument("path", StringArgumentType.greedyString()).suggests(ScriptCommand::suggestDownloadableApps).executes(cc->ScriptDownloader.downloadScript(cc, StringArgumentType.getString(cc,"path"), false))));
dispatcher.register(literal("script").
requires((player) -> SettingsManager.canUseCommand(player, CarpetSettings.commandScript)).
then(b).then(u).then(o).then(l).then(s).then(c).then(h).then(i).then(e).then(t).then(a).then(f).then(q));
then(b).then(u).then(o).then(l).then(s).then(c).then(h).then(i).then(e).then(t).then(a).then(f).then(q).then(d));
dispatcher.register(literal("script").
requires((player) -> SettingsManager.canUseCommand(player, CarpetSettings.commandScript)).
then(literal("in").