Skip to content

Commit

Permalink
feat: improve 'Command' and 'CommandMap' classes with registration me…
Browse files Browse the repository at this point in the history
…thods
  • Loading branch information
wu-vincent committed Mar 1, 2024
1 parent 99bf036 commit bff79fb
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
55 changes: 55 additions & 0 deletions include/endstone/command/command.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,60 @@ class Command {
{
return {};
}

/**
* 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
*/
bool registerTo(CommandMap &command_map) noexcept
{
if (allowChangesFrom(command_map)) {
command_map_ = &command_map;
return true;
}

return false;
}

/**
* Unregisters this command from the passed CommandMap applying any
* outstanding changes
*
* @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
*/
bool unregisterFrom(CommandMap &command_map) noexcept
{
if (allowChangesFrom(command_map)) {
command_map_ = nullptr;
return true;
}

return false;
}

/**
* Returns the current registered state of this command
*
* @return true if this command is currently registered false otherwise
*/
[[nodiscard]] bool isRegistered() const noexcept
{
return command_map_ != nullptr;
}

private:
bool allowChangesFrom(CommandMap &command_map) noexcept
{
return (!isRegistered() || command_map_ == &command_map);
}

CommandMap *command_map_ = nullptr;
};
} // namespace endstone
4 changes: 4 additions & 0 deletions include/endstone/command/command_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ namespace endstone {
class CommandMap {
public:
virtual ~CommandMap() = default;
CommandMap(const CommandMap&) = delete;
CommandMap& operator=(const CommandMap&) = delete;
CommandMap(CommandMap&&) = default;
CommandMap& operator=(CommandMap&&) = default;

/**
* Registers a command. Returns true on success; false if name is already
Expand Down

0 comments on commit bff79fb

Please sign in to comment.