Skip to content

Commit

Permalink
feat: add adapters for Command and CommandSender
Browse files Browse the repository at this point in the history
  • Loading branch information
wu-vincent committed Mar 3, 2024
1 parent 26ff36b commit 581bc6a
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 13 deletions.
2 changes: 2 additions & 0 deletions include/bedrock/command/command_origin.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class CommandOrigin {
public:
CommandOrigin() = default;
virtual ~CommandOrigin() = default;
[[nodiscard]] virtual const std::string &getRequestId() const = 0;
[[nodiscard]] virtual std::string getName() const = 0;

private:
mce::UUID uuid_;
Expand Down
2 changes: 1 addition & 1 deletion include/bedrock/command/minecraft_commands.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

#pragma once

#include "bedrock/command/command_output_sender.h"
#include "bedrock/command/command_output.h"
#include "bedrock/command/command_registry.h"

class MinecraftCommands {
Expand Down
4 changes: 3 additions & 1 deletion include/endstone/command/command_sender.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@

#include <fmt/format.h>

class Server;
namespace endstone {

class Server;
class CommandSender {
public:
CommandSender() = default;
Expand Down Expand Up @@ -51,3 +52,4 @@ class CommandSender {
*/
[[nodiscard]] virtual std::string getName() const = 0;
};
} // namespace endstone
41 changes: 41 additions & 0 deletions include/endstone/detail/command/command_adapter.h
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
57 changes: 57 additions & 0 deletions src/endstone_core/command/command_adapter.cpp
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
16 changes: 5 additions & 11 deletions src/endstone_core/command/command_map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include "bedrock/command/command.h"
#include "bedrock/command/command_registry.h"
#include "endstone/detail/command/command_adapter.h"
#include "endstone/detail/command/defaults/version_command.h"
#include "endstone/detail/server.h"

Expand Down Expand Up @@ -55,16 +56,7 @@ void EndstoneCommandMap::setDefaultCommands()
registerCommand(std::make_unique<VersionCommand>());
}

} // namespace endstone::detail

class CommandWrapper : public Command {
void execute(const struct CommandOrigin &origin, struct CommandOutput &output) const override
{
printf("TODO...\n");
}
};

bool endstone::detail::EndstoneCommandMap::registerCommand(std::shared_ptr<endstone::Command> command)
bool EndstoneCommandMap::registerCommand(std::shared_ptr<Command> command)
{
std::lock_guard lock(mutex_);

Expand All @@ -80,6 +72,7 @@ bool endstone::detail::EndstoneCommandMap::registerCommand(std::shared_ptr<endst
}

auto &registry = server_.getMinecraftCommands().getRegistry();
// TODO: configure the permission level and flags
registry.registerCommand(name, command->getDescription().c_str(), CommandPermissionLevel::Any, {128}, {0});
known_commands_.emplace(name, command);

Expand All @@ -93,9 +86,10 @@ bool endstone::detail::EndstoneCommandMap::registerCommand(std::shared_ptr<endst
}

// TODO: register the overloads from usage
registry.registerOverload<CommandWrapper>(name.c_str(), {1, INT_MAX});
registry.registerOverload<CommandAdapter>(name.c_str(), {1, INT_MAX});

command->setAliases(registered_alias);
command->registerTo(*this);
return true;
}
} // namespace endstone::detail

0 comments on commit 581bc6a

Please sign in to comment.