}
сделай билдер для этого класса который позволит легко создавать команды
Certainly! Below is a SimpleCommandBuilder class designed to work with your SimpleCommand system. This builder will allow you to easily create and register commands by chaining methods in a fluent interface.
SimpleCommandBuilder.java
java
package ru.rainchat.command.components;
import ru.rainchat.command.services.MessageService;
import ru.rainchat.command.services.TabCompleterService;
import ru.rainchat.command.services.TypeParserService;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import java.lang.reflect.Method;
public class SimpleCommandBuilder {
private final SimpleCommand simpleCommand;
public SimpleCommandBuilder(TypeParserService typeParserService, TabCompleterService tabCompleterService, MessageService messageService) {
this.simpleCommand = new SimpleCommand(typeParserService, tabCompleterService, messageService);
}
public CommandBuilder command(String commandName) {
return new CommandBuilder(commandName, this);
}
public SimpleCommand build() {
return simpleCommand;
}
public class CommandBuilder {
private final String commandName;
private Object instance;
private Method method;
private String permission;
private String[] completions;
private final SimpleCommandBuilder builder;
public CommandBuilder(String commandName, SimpleCommandBuilder builder) {
this.commandName = commandName;
this.builder = builder;
}
public CommandBuilder withInstance(Object instance) {
this.instance = instance;
return this;
}
public CommandBuilder withMethod(Method method) {
this.method = method;
return this;
}
public CommandBuilder withPermission(String permission) {
this.permission = permission;
return this;
}
public CommandBuilder withCompletions(String... completions) {
this.completions = completions;
return this;
}
public SimpleCommandBuilder register() {
if (instance == null || method == null) {
throw new IllegalStateException("Instance and method must be specified");
}
// Add the command to SimpleCommand
builder.simpleCommand.addCommand(instance, method, commandName, permission, completions);
return builder;
}
}
}
Modifications to SimpleCommand.java
To support adding permissions and completions directly, you can modify the SimpleCommand class by adding a new addCommand method that accepts permissions and completions as parameters:
Here's how you can use the SimpleCommandBuilder to create and register commands:
java
// Initialize your services
TypeParserService typeParserService = new TypeParserService();
TabCompleterService tabCompleterService = new TabCompleterService();
MessageService messageService = new MessageService();
// Create an instance of SimpleCommandBuilder
SimpleCommandBuilder builder = new SimpleCommandBuilder(typeParserService, tabCompleterService, messageService);
// Your command handler instance
MyCommandHandler commandHandler = new MyCommandHandler();
// Register a command
builder.command("mycommand subcommand")
.withInstance(commandHandler)
.withMethod(MyCommandHandler.class.getMethod("handleMyCommand", CommandSender.class, String.class))
.withPermission("myplugin.permission")
.withCompletions("player", "world")
.register();
// Build the SimpleCommand
SimpleCommand simpleCommand = builder.build();
// Register 'simpleCommand' as your command executor and tab completer
// This is typically done in your plugin's main class, e.g., in onEnable()
getCommand("mycommand").setExecutor(simpleCommand);
getCommand("mycommand").setTabCompleter(simpleCommand);
Your Command Handler Class
Ensure your command handler methods match the expected signatures. For example:
java
public class MyCommandHandler {
public void handleMyCommand(CommandSender sender, String arg) {
// Command logic here
sender.sendMessage("You executed the command with argument: " + arg);
}
// Additional command methods can be added here
}
Notes
Method Signatures: The command methods should have parameters that match what your SimpleCommand class expects. Common parameters include CommandSender, String[], or specific argument types.
Reflection: When using Class.getMethod(), ensure you provide the correct parameter types. Handle NoSuchMethodException and SecurityException where necessary.
Fluent Interface: The builder pattern provides a fluent interface, allowing you to chain method calls for clarity and brevity.
Extensibility: You can extend the CommandBuilder to include more configuration options as needed, such as aliases or descriptions.
Conclusion
The SimpleCommandBuilder class simplifies the process of creating and registering commands with your SimpleCommand system. By using this builder, you can easily manage command registrations in a clean and maintainable way.
Feel free to adjust the builder to suit your specific needs or to add additional features.
This builder should help you streamline the command creation process in your project. Let me know if you have any questions or need further assistance!