-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improvement: migrated to different command framework
- Loading branch information
Showing
18 changed files
with
264 additions
and
186 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 65 additions & 34 deletions
99
plugin/core/src/main/java/core/configs/server/CommandsConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,82 @@ | ||
package core.configs.server; | ||
|
||
import com.mojang.brigadier.CommandDispatcher; | ||
import com.mojang.brigadier.exceptions.CommandSyntaxException; | ||
import io.github.empee.colonel.BrigadierCommand; | ||
import io.github.empee.colonel.BrigadierManager; | ||
import com.github.empee.commands.CommandContext; | ||
import com.github.empee.commands.CommandManager; | ||
import com.github.empee.commands.CommandNode; | ||
import com.github.empee.commands.exceptions.ArgumentException; | ||
import com.github.empee.commands.exceptions.CommandException; | ||
import com.github.empee.commands.spigot.BukkitInjector; | ||
import core.configs.client.resources.MessagesConfig; | ||
import core.controllers.commands.ICommand; | ||
import io.github.empee.lightwire.annotations.LightWired; | ||
import lombok.SneakyThrows; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.bukkit.Bukkit; | ||
import org.bukkit.command.CommandSender; | ||
import org.bukkit.plugin.java.JavaPlugin; | ||
import core.configs.client.resources.MessagesConfig; | ||
import core.exceptions.PluginException; | ||
import utils.Messenger; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.logging.Logger; | ||
|
||
@LightWired | ||
public class CommandsConfig extends BrigadierManager<CommandSender> { | ||
private final MessagesConfig messages; | ||
|
||
public CommandsConfig( | ||
JavaPlugin plugin, MessagesConfig messages, | ||
List<BrigadierCommand<CommandSender>> commands | ||
) { | ||
super(plugin, new CommandDispatcher<>()); | ||
|
||
this.messages = messages; | ||
commands.forEach(c -> { | ||
var registeredLabel = register(c.get()); | ||
Messenger.log("The command '{}' has been registered", registeredLabel); | ||
}); | ||
public class CommandsConfig { | ||
|
||
CommandManager<CommandSender> commandManager = new CommandManager<>(CommandSender.class); | ||
|
||
private final JavaPlugin plugin; | ||
private final MessagesConfig messagesConfig; | ||
private final List<ICommand> commands; | ||
|
||
public CommandsConfig(JavaPlugin plugin, MessagesConfig messagesConfig, List<ICommand> commands) { | ||
this.messagesConfig = messagesConfig; | ||
this.plugin = plugin; | ||
this.commands = commands; | ||
|
||
registerExceptionHandler(); | ||
registerCommands(); | ||
} | ||
|
||
@Override | ||
@SneakyThrows | ||
protected void handleException(CommandSender source, Exception e) { | ||
if (e instanceof PluginException) { | ||
Messenger.log(source, messages.get(((PluginException) e).getId(), ((PluginException) e).getArguments())); | ||
} else if (e instanceof CommandSyntaxException) { | ||
Messenger.log(source, e.getMessage()); | ||
} else { | ||
Messenger.log(source, messages.get("errors.internal_error")); | ||
Messenger.log("Error while executing command", e); | ||
private void registerCommands() { | ||
BukkitInjector injector = new BukkitInjector(plugin, commandManager); | ||
|
||
for (ICommand command : commands) { | ||
CommandNode<? extends CommandSender> cmd = command.get(); | ||
|
||
commandManager.register(cmd); | ||
injector.inject(command.get()); | ||
} | ||
} | ||
|
||
private void registerExceptionHandler() { | ||
commandManager.setExceptionHandler(this::handleCommandException); | ||
} | ||
|
||
public void handleCommandException(CommandContext<CommandSender> context, CommandException exception) { | ||
if (exception.getType() == CommandException.Type.ARG_PARSING_ERROR) { | ||
handleCommandArgumentException(context, (ArgumentException) exception); | ||
return; | ||
} | ||
|
||
if (exception.getType() == CommandException.Type.EXECUTION_ERROR) { | ||
Messenger.error("Error while executing command '/{}'", exception.getCause(), context.getReader().getText()); | ||
} | ||
|
||
Messenger.log(context.getSource(), messagesConfig.get("commands.errors." + exception.getType())); | ||
} | ||
|
||
@Override | ||
protected CommandSender getSource(CommandSender source) { | ||
return source; | ||
private void handleCommandArgumentException(CommandContext<CommandSender> context, ArgumentException exception) { | ||
String message; | ||
|
||
if (exception.getCauseID() == null) { | ||
message = messagesConfig.get("commands.errors.arg_parsing_error.unk"); | ||
Messenger.error("Error while parsing argument '{}' of command '/{}'", exception.getCause(), exception.getInput(), context.getReader().getText()); | ||
} else { | ||
var placeholders = Map.of("input", (Object) exception.getInput()); | ||
message = messagesConfig.get("commands.errors.arg_parsing_error." + exception.getCauseID(), placeholders); | ||
} | ||
|
||
Messenger.log(context.getSource(), message); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
plugin/core/src/main/java/core/controllers/commands/ICommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package core.controllers.commands; | ||
|
||
import com.github.empee.commands.CommandNode; | ||
import org.bukkit.command.CommandSender; | ||
|
||
public interface ICommand { | ||
|
||
CommandNode<? extends CommandSender> get(); | ||
|
||
} |
Oops, something went wrong.