Skip to content

Commit

Permalink
refactor: command registration and execution logic
Browse files Browse the repository at this point in the history
  • Loading branch information
wu-vincent committed Mar 2, 2024
1 parent a6e7f7e commit 9433096
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 56 deletions.
61 changes: 34 additions & 27 deletions include/endstone/command/command.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <map>
#include <optional>
#include <string>
#include <utility>
#include <vector>

#include "endstone/command/command_map.h"
Expand All @@ -25,10 +26,16 @@

namespace endstone {
/**
* Represents a registered Command, which executes various tasks upon user input
* Represents a Command, which executes various tasks upon user input
*/
class Command {
public:
explicit Command(std::string name, std::string description = "", std::vector<std::string> usages = {"/command"},
std::vector<std::string> aliases = {}) noexcept
: name_(std::move(name)), description_(std::move(description)), usages_(std::move(usages)),
aliases_(std::move(aliases))
{
}
virtual ~Command() = default;

/**
Expand All @@ -45,47 +52,46 @@ class Command {
*
* @return Name of this command
*/
[[nodiscard]] virtual std::string getName() const = 0;
[[nodiscard]] std::string getName() const
{
return name_;
}

/**
* Gets a brief description of this command
*
* @return Description of this command
*/
[[nodiscard]] virtual std::string getDescription() const
[[nodiscard]] std::string getDescription() const
{
return "";
return description_;
}

/**
* Returns a list of aliases of this command
*
* @return List of aliases
*/
[[nodiscard]] virtual std::vector<std::string> getAliases() const
[[nodiscard]] std::vector<std::string> getAliases() const
{
return {};
return aliases_;
}

class Argument {
public:
template <typename T>
explicit Argument(std::string name) : type_(type_id<T>()), name_(std::move(name))
{
}

private:
std::string type_;
std::string name_;
};
/**
* Returns a list of usages of this command
*
* @return List of usages
*/
[[nodiscard]] std::vector<std::string> getUsages() const
{
return usages_;
}

/**
* Registers this command to a CommandMap.
* Once called it only allows changes the registered CommandMap
*
* @param command_map the CommandMap to register this command to
* @return true if the registration was successful (the current registered
* CommandMap was the passed CommandMap or null) false otherwise
* @param command_map the CommandMap to register to
* @return true if the registration was successful, false otherwise
*/
bool registerTo(CommandMap &command_map) noexcept
{
Expand All @@ -98,13 +104,10 @@ class Command {
}

/**
* Unregisters this command from the passed CommandMap applying any
* outstanding changes
* Unregisters this command from a CommandMap
*
* @param command_map the CommandMap to unregister
* @return true if the unregistration was successful (the current
* registered CommandMap was the passed CommandMap or null) false
* otherwise
* @param command_map the CommandMap to unregister from
* @return true if the unregistration was successful, false otherwise
*/
bool unregisterFrom(CommandMap &command_map) noexcept
{
Expand Down Expand Up @@ -132,6 +135,10 @@ class Command {
return (!isRegistered() || command_map_ == &command_map);
}

std::string name_;
std::string description_;
std::vector<std::string> aliases_;
std::vector<std::string> usages_;
CommandMap *command_map_ = nullptr;
};
} // namespace endstone
11 changes: 3 additions & 8 deletions include/endstone/command/command_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,12 @@ class CommandMap {
CommandMap& operator=(CommandMap&&) = default;

/**
* Registers a command. Returns true on success; false if name is already
* taken and fallback had to be used.
* Registers a command.
*
* @param label the label of the command, without the '/'-prefix.
* @param fallback_prefix a prefix which is prepended to the command with a ':' one or more times to make the
* command unique
* @param command the command to register
* @return true if command was registered with the passed in label, false otherwise, which indicates the
* fallbackPrefix was used one or more times
* @return true on success, false if a command with the same name is already registered
*/
virtual bool registerCommand(std::string label, std::string fallback_prefix, std::shared_ptr<Command> command) = 0;
virtual bool registerCommand(std::shared_ptr<Command> command) = 0;

// TODO: dispatch functions

Expand Down
19 changes: 0 additions & 19 deletions include/endstone/command/command_usage.h

This file was deleted.

3 changes: 1 addition & 2 deletions include/endstone/command/plugin_command.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
namespace endstone {
class PluginCommand : public Command {
public:
PluginCommand(CommandMap &command_map, Plugin &owner) : command_map_(command_map), owner_(owner) {}
PluginCommand(const Command &command, Plugin &owner) : Command(command), owner_(owner) {}

bool execute(CommandSender &sender, const std::map<std::string, std::string> &args) const override
{
Expand Down Expand Up @@ -82,6 +82,5 @@ class PluginCommand : public Command {
private:
Plugin &owner_;
std::shared_ptr<CommandExecutor> executor_;
CommandMap &command_map_;
};
} // namespace endstone

0 comments on commit 9433096

Please sign in to comment.