Merge branch 'master' into 1.17

This commit is contained in:
gnembon committed 2020-12-07 22:13:45 -04:00
commit 947474e9e9
9 files changed
+101 -16

No files matched your search

@@ -11,6 +11,7 @@ import carpet.script.exception.CarpetExpressionException;
import carpet.script.exception.ExpressionException;
import carpet.script.exception.InternalExpressionException;
import carpet.script.exception.InvalidCallbackException;
import carpet.script.value.EntityValue;
import carpet.script.value.FunctionValue;
import carpet.script.value.MapValue;
import carpet.script.value.NumericValue;
@@ -38,6 +39,7 @@ import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
@@ -163,8 +165,9 @@ public class CarpetScriptHost extends ScriptHost
) throws CommandSyntaxException
{
String hostName = main.getName();
Function<ServerCommandSource, Boolean> configValidator = getCommandConfigPermissions();
LiteralArgumentBuilder<ServerCommandSource> command = literal(hostName).
requires((player) -> CarpetServer.scriptServer.modules.containsKey(hostName) && useValidator.apply(player));
requires((player) -> CarpetServer.scriptServer.modules.containsKey(hostName) && useValidator.apply(player) && configValidator.apply(player));
for (Pair<List<CommandToken>,FunctionArgument<Value>> commandData : entries)
{
command = this.addPathToCommand(command, commandData.getKey(), commandData.getValue());
@@ -172,6 +175,49 @@ public class CarpetScriptHost extends ScriptHost
return command;
}
public Function<ServerCommandSource, Boolean> getCommandConfigPermissions() throws CommandSyntaxException
{
Value confValue = appConfig.get(StringValue.of("command_permission"));
if (confValue == null) return s -> true;
if (confValue instanceof NumericValue)
{
int level = ((NumericValue) confValue).getInt();
if (level < 1 || level > 4) throw CommandArgument.error("Numeric permission level for custom commands should be between 1 and 4");
return s -> s.hasPermissionLevel(level);
}
if (!(confValue instanceof FunctionValue))
{
String perm = confValue.getString().toLowerCase(Locale.ROOT);
switch (perm)
{
case "ops": return s -> s.hasPermissionLevel(2);
case "server": return s -> !(s.getEntity() instanceof ServerPlayerEntity);
case "players": return s -> s.getEntity() instanceof ServerPlayerEntity;
case "all": return s -> true;
default: throw CommandArgument.error("Unknown command permission: "+perm);
}
}
FunctionValue fun = (FunctionValue) confValue;
if (fun.getNumParams() != 1) throw CommandArgument.error("Custom command permission function should expect 1 argument");
String hostName = getName();
return s -> {
try
{
CarpetScriptHost cHost = null;
cHost = CarpetServer.scriptServer.modules.get(hostName).retrieveOwnForExecution(s);
Value response = cHost.handleCommand(s, fun, Collections.singletonList(
(s.getEntity() instanceof ServerPlayerEntity)?new EntityValue(s.getEntity()):Value.NULL)
);
return response.getBoolean();
}
catch (CommandSyntaxException e)
{
Messenger.m(s, "rb Unable to run app command: "+e.getMessage());
return false;
}
};
}
@Override
protected ScriptHost duplicate()
{
@@ -221,7 +221,6 @@ public class CarpetScriptServer
public boolean addScriptHost(ServerCommandSource source, String name, Function<ServerCommandSource, Boolean> commandValidator,
boolean perPlayer, boolean autoload, boolean isRuleApp)
{
//TODO add per player modules to support player actions better on a server
if (commandValidator == null) commandValidator = p -> true;
long start = System.nanoTime();
name = name.toLowerCase(Locale.ROOT);
@@ -325,8 +324,19 @@ public class CarpetScriptServer
return true;
}
Function<ServerCommandSource, Boolean> configValidator;
try
{
configValidator = host.getCommandConfigPermissions();
}
catch (CommandSyntaxException e)
{
Messenger.m(source, "rb "+e.getMessage());
return false;
}
LiteralArgumentBuilder<ServerCommandSource> command = literal(hostName).
requires((player) -> modules.containsKey(hostName) && useValidator.apply(player)).
requires((player) -> modules.containsKey(hostName) && useValidator.apply(player) && configValidator.apply(player)).
executes( (c) ->
{
CarpetScriptHost targetHost = modules.get(hostName).retrieveOwnForExecution(c.getSource());
@@ -12,6 +12,7 @@ import carpet.script.value.Value;
import carpet.settings.ParsedRule;
import carpet.settings.SettingsManager;
import com.sun.management.OperatingSystemMXBean;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.util.WorldSavePath;
import net.minecraft.world.GameRules;
@@ -82,6 +83,7 @@ public class SystemInfo {
}
return whitelist;
});
put("server_dev_environment", c-> new NumericValue(FabricLoader.getInstance().isDevelopmentEnvironment()));
put("java_max_memory", c -> new NumericValue(Runtime.getRuntime().maxMemory()));
put("java_allocated_memory", c -> new NumericValue(Runtime.getRuntime().totalMemory()));
@@ -117,7 +119,7 @@ public class SystemInfo {
CarpetServer.extensions.forEach(e -> {
SettingsManager manager = e.customSettingsManager();
if (manager == null) return;
Collection<ParsedRule<?>> extensionRules = manager.getRules();
extensionRules.forEach(rule -> {
carpetRules.put(new StringValue(manager.getIdentifier()+":"+rule.name), new StringValue(rule.getAsString()));