Scarpet rules can provide multinary boolean permission level, fixes #572
This commit is contained in:
1 parent
3aa646f563
commit
a297fd1441
5 files changed
+30
-25
No files matched your search
@@ -244,13 +244,13 @@ public class ScriptCommand
|
||||
suggests( (cc, bb) -> suggestMatching(CarpetServer.scriptServer.listAvailableModules(true),bb)).
|
||||
executes((cc) ->
|
||||
{
|
||||
boolean success = CarpetServer.scriptServer.addScriptHost(cc.getSource(), StringArgumentType.getString(cc, "app"), true, false, false);
|
||||
boolean success = CarpetServer.scriptServer.addScriptHost(cc.getSource(), StringArgumentType.getString(cc, "app"), true, false, null);
|
||||
return success?1:0;
|
||||
}).
|
||||
then(literal("global").
|
||||
executes((cc) ->
|
||||
{
|
||||
boolean success = CarpetServer.scriptServer.addScriptHost(cc.getSource(), StringArgumentType.getString(cc, "app"), false, false, false);
|
||||
boolean success = CarpetServer.scriptServer.addScriptHost(cc.getSource(), StringArgumentType.getString(cc, "app"), false, false, null);
|
||||
return success?1:0;
|
||||
}
|
||||
)
|
||||
|
||||
@@ -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}
|
||||
*/
|
||||
|
||||
@@ -180,17 +180,17 @@ public class SettingsManager
|
||||
observers.forEach(observer -> observer.accept(source, rule, userTypedValue));
|
||||
staticObservers.forEach(observer -> observer.accept(source, rule, userTypedValue));
|
||||
ServerNetworkHandler.updateRuleWithConnectedClients(rule);
|
||||
switchScarpetRule(source, rule);
|
||||
switchScarpetRuleIfNeeded(source, rule);
|
||||
if (CARPET_RULE_CHANGES.isNeeded()) CARPET_RULE_CHANGES.onCarpetRuleChanges(rule, source);
|
||||
}
|
||||
|
||||
void switchScarpetRule(ServerCommandSource source, ParsedRule<?> rule)
|
||||
private void switchScarpetRuleIfNeeded(ServerCommandSource source, ParsedRule<?> rule)
|
||||
{
|
||||
if (!rule.scarpetApp.isEmpty())
|
||||
{
|
||||
if (rule.getBoolValue() || (rule.type == String.class && !rule.get().equals("false")))
|
||||
{
|
||||
CarpetServer.scriptServer.addScriptHost(source, rule.scarpetApp, false, false, true);
|
||||
CarpetServer.scriptServer.addScriptHost(source, rule.scarpetApp, false, false, s -> canUseCommand(s, rule.get()));
|
||||
} else {
|
||||
CarpetServer.scriptServer.removeScriptHost(source, rule.scarpetApp, false, true);
|
||||
}
|
||||
@@ -205,7 +205,7 @@ public class SettingsManager
|
||||
for (ParsedRule<?> rule : rules.values())
|
||||
{
|
||||
if (!rule.scarpetApp.isEmpty()) {
|
||||
switchScarpetRule(server.getCommandSource(), rule);
|
||||
switchScarpetRuleIfNeeded(server.getCommandSource(), rule);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -332,9 +332,11 @@ public class SettingsManager
|
||||
* {@link boolean} value or "ops".
|
||||
* @return Whether or not the {@link ServerCommandSource} meets the required level
|
||||
*/
|
||||
public static boolean canUseCommand(ServerCommandSource source, String commandLevel)
|
||||
public static boolean canUseCommand(ServerCommandSource source, Object commandLevel)
|
||||
{
|
||||
switch (commandLevel)
|
||||
if (commandLevel instanceof Boolean) return (Boolean) commandLevel;
|
||||
String commandLevelString = commandLevel.toString();
|
||||
switch (commandLevelString)
|
||||
{
|
||||
case "true": return true;
|
||||
case "false": return false;
|
||||
@@ -344,7 +346,7 @@ public class SettingsManager
|
||||
case "2":
|
||||
case "3":
|
||||
case "4":
|
||||
return source.hasPermissionLevel(Integer.parseInt(commandLevel));
|
||||
return source.hasPermissionLevel(Integer.parseInt(commandLevelString));
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user