-
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add adapters for Command and CommandSender
- Loading branch information
1 parent
26ff36b
commit 581bc6a
Showing
6 changed files
with
109 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// Copyright (c) 2024, The Endstone Project. (https://endstone.dev) All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#pragma once | ||
|
||
#include "bedrock/command/command.h" | ||
#include "endstone/command/command_sender.h" | ||
#include "endstone/detail/server.h" | ||
#include "endstone/detail/singleton.h" | ||
|
||
namespace endstone::detail { | ||
|
||
class CommandSenderAdapter : public CommandSender { | ||
public: | ||
CommandSenderAdapter(EndstoneServer &server, const CommandOrigin &origin, CommandOutput &output); | ||
void sendMessage(const std::string &message) const override; | ||
[[nodiscard]] Server &getServer() const override; | ||
[[nodiscard]] std::string getName() const override; | ||
|
||
private: | ||
EndstoneServer &server_; | ||
const CommandOrigin &origin_; | ||
CommandOutput &output_; | ||
}; | ||
|
||
class CommandAdapter : public ::Command { | ||
void execute(const CommandOrigin &origin, CommandOutput &output) const override; | ||
}; | ||
|
||
} // namespace endstone::detail |
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,57 @@ | ||
// Copyright (c) 2024, The Endstone Project. (https://endstone.dev) All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#include "endstone/detail/command/command_adapter.h" | ||
|
||
namespace endstone::detail { | ||
CommandSenderAdapter::CommandSenderAdapter(EndstoneServer &server, const CommandOrigin &origin, CommandOutput &output) | ||
: server_(server), origin_(origin), output_(output) | ||
{ | ||
} | ||
|
||
void CommandSenderAdapter::sendMessage(const std::string &message) const | ||
{ | ||
// TODO: addMessage to output | ||
printf("%s\n", message.c_str()); | ||
printf("Sender name: %s\n", getName().c_str()); | ||
} | ||
|
||
Server &CommandSenderAdapter::getServer() const | ||
{ | ||
return server_; | ||
} | ||
|
||
std::string CommandSenderAdapter::getName() const | ||
{ | ||
return origin_.getName(); | ||
} | ||
|
||
void CommandAdapter::execute(const CommandOrigin &origin, CommandOutput &output) const | ||
{ | ||
auto &server = Singleton<EndstoneServer>::getInstance(); | ||
auto &command_map = server.getCommandMap(); | ||
auto *command = command_map.getCommand(getCommandName()); | ||
if (command) { | ||
auto sender = CommandSenderAdapter(server, origin, output); | ||
bool success = command->execute(sender, {}); | ||
if (!success) { | ||
// TODO: set output status code | ||
} | ||
} | ||
else { | ||
// TODO: shouldn't happen, what to do now? be silent or print error message? | ||
} | ||
} | ||
|
||
} // namespace endstone::detail |
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