From 24e7c0fab9b48c4c3d92fa11100827654f8e9f8f Mon Sep 17 00:00:00 2001 From: gnembon Date: Thu, 18 Mar 2021 18:06:21 -0300 Subject: [PATCH] command adding is due to the app refactor --- .../java/carpet/script/CarpetScriptHost.java | 105 ++++++++++++++ .../carpet/script/CarpetScriptServer.java | 129 +++--------------- 2 files changed, 126 insertions(+), 108 deletions(-) diff --git a/src/main/java/carpet/script/CarpetScriptHost.java b/src/main/java/carpet/script/CarpetScriptHost.java index c79477c0..2432024a 100644 --- a/src/main/java/carpet/script/CarpetScriptHost.java +++ b/src/main/java/carpet/script/CarpetScriptHost.java @@ -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 notifier) + { + if (appConfig.get(StringValue.of("commands")) != null) + { + try + { + LiteralArgumentBuilder 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 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 configValidator; + try + { + configValidator = getCommandConfigPermissions(); + } + catch (CommandSyntaxException e) + { + notifier.accept(Messenger.c("rb "+e.getMessage())); + return null; + } + String hostName = getName(); + LiteralArgumentBuilder 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 readCommands(Function useValidator) throws CommandSyntaxException { Value commands = appConfig.get(StringValue.of("commands")); diff --git a/src/main/java/carpet/script/CarpetScriptServer.java b/src/main/java/carpet/script/CarpetScriptServer.java index fc9eb5e2..8a93f8c2 100644 --- a/src/main/java/carpet/script/CarpetScriptServer.java +++ b/src/main/java/carpet/script/CarpetScriptServer.java @@ -272,35 +272,23 @@ public class CarpetScriptServer } //addEvents(source, name); String action = reload?"reloaded":"loaded"; - if (newHost.appConfig.get(StringValue.of("commands")) != null) - { - try - { - LiteralArgumentBuilder 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(); - } - 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 + Boolean isCommandAdded = newHost.addAppCommands(s -> { + if (!isRuleApp) Messenger.m(source, s); + }); + if (isCommandAdded == null) // error should be dispatched { removeScriptHost(source, name, false, false); - Messenger.m(source, "r Failed to build command system for "+name+" thus failed to load the app"); - return false; } + 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); + } + 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 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 configValidator; - try - { - configValidator = host.getCommandConfigPermissions(); - } - catch (CommandSyntaxException e) - { - Messenger.m(source, "rb "+e.getMessage()); - return false; - } - - LiteralArgumentBuilder 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 -> {})); + } }