Commit ea7177f5 by Jonathan Thomas

Fixed permissions on commands, so only help command can be run by all users. All…

Fixed permissions on commands, so only help command can be run by all users. All other commands require op (level 4 permission). Also added more debug log output from commands, so it's logged on the server when a command is run.
parent 9eaae2c4
Pipeline #12004 passed with stage
in 19 seconds
......@@ -7,12 +7,15 @@ import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.text.Text;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* The {@code CreatureChatCommands} class registers custom commands to set new API key, model, and url.
* Permission level set to 4 (server owner), since this deals with API keys and potential costs.
*/
public class CreatureChatCommands {
public static final Logger LOGGER = LoggerFactory.getLogger("creaturechat");
public static void register() {
ServerLifecycleEvents.SERVER_STARTING.register(server -> {
......@@ -31,6 +34,7 @@ public class CreatureChatCommands {
private static LiteralArgumentBuilder<ServerCommandSource> registerSetCommand(String settingName, String settingDescription) {
return CommandManager.literal(settingName)
.requires(source -> source.hasPermissionLevel(4))
.then(CommandManager.literal("set")
.then(CommandManager.argument("value", StringArgumentType.string())
.then(CommandManager.literal("--config")
......@@ -58,20 +62,25 @@ public class CreatureChatCommands {
break;
}
configHandler.saveConfig(config, useServerConfig);
source.sendFeedback(() -> Text.literal(settingDescription + " set to: " + value + " in " + (useServerConfig ? "server" : "default") + " configuration."), false);
String playerName = source.getName();
Text feedbackMessage = Text.literal(settingDescription + " set to: " + value + " in " + (useServerConfig ? "server" : "default") + " configuration by " + playerName);
source.sendFeedback(() -> feedbackMessage, false);
LOGGER.info("Command executed: " + feedbackMessage.getLiteralString());
return 1;
}
private static LiteralArgumentBuilder<ServerCommandSource> registerHelpCommand() {
return CommandManager.literal("help")
.requires(source -> source.hasPermissionLevel(4)) // Restricts this command to high-level permissions
.executes(context -> {
String helpMessage = "Usage of CreatureChat Commands:\n"
+ "/creaturechat key set <key> - Sets the API key.\n"
+ "/creaturechat url set <url> - Sets the URL.\n"
+ "/creaturechat model set <model> - Sets the model.\n"
+ "\n"
+ "Optional: Append [--config default | server] to any command to specify configuration scope. If --config is not specified, 'default' is assumed'.";
+ "Optional: Append [--config default | server] to any command to specify configuration scope. If --config is not specified, 'default' is assumed'.\n"
+ "\n"
+ "Security: Level 4 permission required.";
context.getSource().sendFeedback(() -> Text.literal(helpMessage), false);
return 1;
});
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment