Merge branch 'master' into 1.17

This commit is contained in:
gnembon committed 2020-11-23 02:40:16 -04:00
commit c8d3f01f78
6 files changed
+31 -27

No files matched your search

@@ -39,6 +39,7 @@ import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import static java.lang.Math.max;
@@ -152,12 +153,12 @@ public class CarpetScriptHost extends ScriptHost
}
public LiteralArgumentBuilder<ServerCommandSource> getNewCommandTree(
List<Pair<List<CommandToken>,FunctionArgument<Value>>> entries
List<Pair<List<CommandToken>,FunctionArgument<Value>>> entries, Function<ServerCommandSource, Boolean> useValidator
) throws CommandSyntaxException
{
String hostName = main.getName();
LiteralArgumentBuilder<ServerCommandSource> command = literal(hostName).
requires((player) -> CarpetServer.scriptServer.modules.containsKey(hostName));
requires((player) -> CarpetServer.scriptServer.modules.containsKey(hostName) && useValidator.apply(player));
for (Pair<List<CommandToken>,FunctionArgument<Value>> commandData : entries)
{
command = this.addPathToCommand(command, commandData.getKey(), commandData.getValue());
@@ -243,7 +244,7 @@ public class CarpetScriptHost extends ScriptHost
}
}
public LiteralArgumentBuilder<ServerCommandSource> readCommands() throws CommandSyntaxException
public LiteralArgumentBuilder<ServerCommandSource> readCommands(Function<ServerCommandSource, Boolean> useValidator) throws CommandSyntaxException
{
Value commands = appConfig.get(StringValue.of("commands"));
@@ -279,7 +280,7 @@ public class CarpetScriptHost extends ScriptHost
}
}
}
return this.getNewCommandTree(commandEntries);
return this.getNewCommandTree(commandEntries, useValidator);
}
@Override
@@ -30,6 +30,7 @@ import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
import static net.minecraft.server.command.CommandManager.argument;
@@ -53,7 +54,7 @@ public class CarpetScriptServer
/**
* Registers a Scarpet App to be always available to be loaded
* in the /script load list.
* @see BundledModule#fromPath(ClassLoader, String, String, boolean)
* @see BundledModule#fromPath(String, String, boolean)
*
* @param app The {@link BundledModule} of the app
*/
@@ -66,7 +67,7 @@ public class CarpetScriptServer
* Registers a Scarpet App to be used as a Rule App
* (to be controlled with the value of a Carpet rule).
* Libraries should be registered with {@link #registerBuiltInScript(BundledModule)} instead
* @see BundledModule#fromPath(ClassLoader, String, String, boolean)
* @see BundledModule#fromPath(String, String, boolean)
*
* @param app The {@link BundledModule} of the app.
*/
@@ -116,7 +117,7 @@ public class CarpetScriptServer
Messenger.m(server.getCommandSource(), "Auto-loading world scarpet apps");
for (String moduleName: listAvailableModules(false))
{
addScriptHost(server.getCommandSource(), moduleName, true, true, false);
addScriptHost(server.getCommandSource(), moduleName, true, true, null);
}
}
@@ -192,9 +193,10 @@ public class CarpetScriptServer
return modules.get(name);
}
public boolean addScriptHost(ServerCommandSource source, String name, boolean perPlayer, boolean autoload, boolean isRuleApp)
public boolean addScriptHost(ServerCommandSource source, String name, boolean perPlayer, boolean autoload, Function<ServerCommandSource, Boolean> commandValidator)
{
//TODO add per player modules to support player actions better on a server
boolean isRuleApp = commandValidator != null;
long start = System.nanoTime();
name = name.toLowerCase(Locale.ROOT);
boolean reload = false;
@@ -242,7 +244,7 @@ public class CarpetScriptServer
{
try
{
LiteralArgumentBuilder<ServerCommandSource> command = newHost.readCommands();
LiteralArgumentBuilder<ServerCommandSource> command = newHost.readCommands(commandValidator);
if (command != null)
{
if (!isRuleApp) Messenger.m(source, "gi "+name+" app "+action+" with /"+name+" command");
@@ -261,7 +263,7 @@ public class CarpetScriptServer
}
}
else if (!addLegacyCommand(source, name, reload, !isRuleApp)) // this needs to be moved to config reader, only supporting legacy command here
else if (!addLegacyCommand(source, name, reload, !isRuleApp, commandValidator)) // this needs to be moved to config reader, only supporting legacy command here
{
removeScriptHost(source, name, false, false);
Messenger.m(source, "r Failed to build command system for "+name+" thus failed to load the app");
@@ -272,7 +274,7 @@ public class CarpetScriptServer
return true;
}
private boolean addLegacyCommand(ServerCommandSource source, String hostName, boolean isReload, boolean notifySource)
private boolean addLegacyCommand(ServerCommandSource source, String hostName, boolean isReload, boolean notifySource, Function<ServerCommandSource, Boolean> useValidator)
{
CarpetScriptHost host = modules.get(hostName);
String loaded = isReload?"reloaded":"loaded";
@@ -293,7 +295,7 @@ public class CarpetScriptServer
}
LiteralArgumentBuilder<ServerCommandSource> command = literal(hostName).
requires((player) -> modules.containsKey(hostName)).
requires((player) -> modules.containsKey(hostName) && useValidator.apply(player)).
executes( (c) ->
{
Value response = modules.get(hostName).retrieveForExecution(c.getSource()).
@@ -439,6 +441,6 @@ public class CarpetScriptServer
apps.keySet().forEach(s -> removeScriptHost(server.getCommandSource(), s, false, false));
CarpetEventServer.Event.clearAllBuiltinEvents();
init();
apps.forEach((s, pp) -> addScriptHost(server.getCommandSource(), s, pp, false, false));
apps.forEach((s, pp) -> addScriptHost(server.getCommandSource(), s, pp, false, null));
}
}
@@ -30,7 +30,7 @@ public class BundledModule extends Module
/**
* Creates a new {@link BundledModule} with an app located at a specified place.
* @see #fromPathWithCustomName(ClassLoader, String, String, boolean)
* @see #fromPathWithCustomName(String, String, boolean)
*
* @param path A {@link String} being the path to the directory where the app is located.
* @param scriptName A {@link String} being the name of the script. The extension will be autocompleted
@@ -43,10 +43,10 @@ public class BundledModule extends Module
/**
* Creates a new {@link BundledModule} with an app located at the specified fullPath with a custom name.
* @see #fromPath(ClassLoader, String, String, boolean)
* @see #fromPath(String, String, boolean)
*
* @param fullPath A {@link String} being the full path to the app's code, including file and extension.
* @param scriptName A {@link String} being the custom name for the script.
* @param customName A {@link String} being the custom name for the script.
* @param isLibrary A {@link boolean} indicating whether or not the script is a library
* @return The created {@link BundledModule}
*/