command adding is due to the app refactor

This commit is contained in:
gnembon committed 2021-03-18 18:06:21 -03:00
1 parent 087312abfa
commit 24e7c0fab9
2 files changed
+124 -106

No files matched your search

@@ -20,6 +20,7 @@ import carpet.script.value.Value;
import carpet.utils.Messenger;
import com.google.gson.JsonElement;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.builder.ArgumentBuilder;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.context.CommandContext;
@@ -32,6 +33,7 @@ import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.text.LiteralText;
import net.minecraft.text.Text;
import net.minecraft.util.math.BlockPos;
import org.apache.commons.lang3.tuple.Pair;
@@ -43,11 +45,13 @@ import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static java.lang.Math.max;
import static net.minecraft.server.command.CommandManager.argument;
import static net.minecraft.server.command.CommandManager.literal;
public class CarpetScriptHost extends ScriptHost
@@ -311,6 +315,107 @@ public class CarpetScriptHost extends ScriptHost
}
}
public Boolean addAppCommands(Consumer<Text> notifier)
{
if (appConfig.get(StringValue.of("commands")) != null)
{
try
{
LiteralArgumentBuilder<ServerCommandSource> command = readCommands(commandValidator);
if (command != null)
{
scriptServer.server.getCommandManager().getDispatcher().register(command);
return true;
}
else {
return false;
}
}
catch (CommandSyntaxException cse)
{
// failed
notifier.accept(Messenger.c("r Failed to build command system for "+getName()+" thus failed to load the app ", cse.getRawMessage()));
return null;
}
}
return addLegacyCommand(notifier);
}
private Boolean addLegacyCommand(Consumer<Text> notifier)
{
if (main == null) return false;
if (getFunction("__command") == null) return false;
if (scriptServer.isValidCommandRoot(getName()))
{
notifier.accept(Messenger.c("gi Tried to mask vanilla command."));
return null;
}
Function<ServerCommandSource, Boolean> configValidator;
try
{
configValidator = getCommandConfigPermissions();
}
catch (CommandSyntaxException e)
{
notifier.accept(Messenger.c("rb "+e.getMessage()));
return null;
}
String hostName = getName();
LiteralArgumentBuilder<ServerCommandSource> command = literal(hostName).
requires((player) -> scriptServer.modules.containsKey(hostName) && commandValidator.apply(player) && configValidator.apply(player)).
executes( (c) ->
{
CarpetScriptHost targetHost = scriptServer.modules.get(hostName).retrieveOwnForExecution(c.getSource());
Value response = targetHost.handleCommandLegacy(c.getSource(),"__command", null, "");
if (!response.isNull()) Messenger.m(c.getSource(), "gi "+response.getString());
return (int)response.readInteger();
});
for (String function : globaFunctionNames(main, s -> !s.startsWith("_")).sorted().collect(Collectors.toList()))
{
if (appConfig.getOrDefault(StringValue.of("legacy_command_type_support"), Value.FALSE).getBoolean())
{
try
{
FunctionValue functionValue = getFunction(function);
command = addPathToCommand(
command,
CommandToken.parseSpec(CommandToken.specFromSignature(functionValue), this),
FunctionArgument.fromCommandSpec(this, functionValue)
);
}
catch (CommandSyntaxException e)
{
return false;
}
}
else
{
command = command.
then(literal(function).
requires((player) -> scriptServer.modules.containsKey(hostName) && scriptServer.modules.get(hostName).getFunction(function) != null).
executes((c) -> {
CarpetScriptHost targetHost = scriptServer.modules.get(hostName).retrieveOwnForExecution(c.getSource());
Value response = targetHost.handleCommandLegacy(c.getSource(),function, null, "");
if (!response.isNull()) Messenger.m(c.getSource(), "gi " + response.getString());
return (int) response.readInteger();
}).
then(argument("args...", StringArgumentType.greedyString()).
executes( (c) -> {
CarpetScriptHost targetHost = scriptServer.modules.get(hostName).retrieveOwnForExecution(c.getSource());
Value response = targetHost.handleCommandLegacy(c.getSource(),function, null, StringArgumentType.getString(c, "args..."));
if (!response.isNull()) Messenger.m(c.getSource(), "gi "+response.getString());
return (int)response.readInteger();
})));
}
}
scriptServer.server.getCommandManager().getDispatcher().register(command);
return true;
}
public LiteralArgumentBuilder<ServerCommandSource> readCommands(Function<ServerCommandSource, Boolean> useValidator) throws CommandSyntaxException
{
Value commands = appConfig.get(StringValue.of("commands"));
@@ -272,35 +272,23 @@ public class CarpetScriptServer
}
//addEvents(source, name);
String action = reload?"reloaded":"loaded";
if (newHost.appConfig.get(StringValue.of("commands")) != null)
Boolean isCommandAdded = newHost.addAppCommands(s -> {
if (!isRuleApp) Messenger.m(source, s);
});
if (isCommandAdded == null) // error should be dispatched
{
try
{
LiteralArgumentBuilder<ServerCommandSource> command = newHost.readCommands(commandValidator);
if (command != null)
{
if (!isRuleApp) Messenger.m(source, "gi "+name+" app "+action+" with /"+name+" command");
server.getCommandManager().getDispatcher().register(command);
CarpetServer.settingsManager.notifyPlayersCommandsChanged();
removeScriptHost(source, name, false, false);
}
else {
else if (isCommandAdded)
{
CarpetServer.settingsManager.notifyPlayersCommandsChanged();
if (!isRuleApp) Messenger.m(source, "gi "+name+" app "+action+" with /"+name+" command");
}
else
{
if (!isRuleApp) Messenger.m(source, "gi "+name+" app "+action);
}
}
catch (CommandSyntaxException cse)
{
removeScriptHost(source, name, false, false);
Messenger.m(source, "r Failed to build command system for "+name+" thus failed to load the app ", cse.getRawMessage());
return false;
}
}
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");
return false;
}
if (newHost.isPerUser())
{
// that will provide player hosts right at the startup
@@ -317,89 +305,9 @@ public class CarpetScriptServer
return true;
}
private boolean addLegacyCommand(ServerCommandSource source, String hostName, boolean isReload, boolean notifySource, Function<ServerCommandSource, Boolean> useValidator)
public boolean isValidCommandRoot(String appName)
{
CarpetScriptHost host = modules.get(hostName);
String loaded = isReload?"reloaded":"loaded";
if (host == null)
{
return true;
}
if (host.getFunction("__command") == null)
{
if (notifySource) Messenger.m(source, "gi "+hostName+" app "+loaded+".");
return true;
}
if (holyMoly.contains(hostName))
{
Messenger.m(source, "gi "+hostName+" app "+loaded+" with no command.");
Messenger.m(source, "gi Tried to mask vanilla command.");
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) && configValidator.apply(player)).
executes( (c) ->
{
CarpetScriptHost targetHost = modules.get(hostName).retrieveOwnForExecution(c.getSource());
Value response = targetHost.handleCommandLegacy(c.getSource(),"__command", null, "");
if (!response.isNull()) Messenger.m(c.getSource(), "gi "+response.getString());
return (int)response.readInteger();
});
for (String function : host.globaFunctionNames(host.main, s -> !s.startsWith("_")).sorted().collect(Collectors.toList()))
{
if (host.appConfig.getOrDefault(StringValue.of("legacy_command_type_support"), Value.FALSE).getBoolean())
{
try
{
FunctionValue functionValue = host.getFunction(function);
command = host.addPathToCommand(
command,
CommandToken.parseSpec(CommandToken.specFromSignature(functionValue), host),
FunctionArgument.fromCommandSpec(host, functionValue)
);
}
catch (CommandSyntaxException e)
{
return false;
}
}
else
{
command = command.
then(literal(function).
requires((player) -> modules.containsKey(hostName) && modules.get(hostName).getFunction(function) != null).
executes((c) -> {
CarpetScriptHost targetHost = modules.get(hostName).retrieveOwnForExecution(c.getSource());
Value response = targetHost.handleCommandLegacy(c.getSource(),function, null, "");
if (!response.isNull()) Messenger.m(c.getSource(), "gi " + response.getString());
return (int) response.readInteger();
}).
then(argument("args...", StringArgumentType.greedyString()).
executes( (c) -> {
CarpetScriptHost targetHost = modules.get(hostName).retrieveOwnForExecution(c.getSource());
Value response = targetHost.handleCommandLegacy(c.getSource(),function, null, StringArgumentType.getString(c, "args..."));
if (!response.isNull()) Messenger.m(c.getSource(), "gi "+response.getString());
return (int)response.readInteger();
})));
}
}
if (notifySource) Messenger.m(source, "gi "+hostName+" app "+loaded+" with /"+hostName+" command");
server.getCommandManager().getDispatcher().register(command);
CarpetServer.settingsManager.notifyPlayersCommandsChanged();
return true;
return !holyMoly.contains(appName);
}
@@ -558,4 +466,9 @@ public class CarpetScriptServer
init();
apps.forEach((s, data) -> addScriptHost(server.getCommandSource(), s,data.commandValidator, data.perUser,false, data.isRuleApp));
}
public void reAddCommands()
{
modules.values().forEach(host -> host.addAppCommands(s -> {}));
}
}