Skip to content

Commit

Permalink
feat: allow passing down command sender if possible
Browse files Browse the repository at this point in the history
  • Loading branch information
wu-vincent committed Oct 5, 2024
1 parent b4d9f0d commit 241afde
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 7 deletions.
3 changes: 3 additions & 0 deletions include/bedrock/server/commands/command_output.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ class CommandOutput {
CommandOutputMessageType);

CommandOutputType type_; // +0
public: //
bool has_command_sender{false}; // +4 used by Endstone
private: //
std::unique_ptr<CommandPropertyBag> data_; // +8
std::vector<CommandOutputMessage> messages_; // +16
int success_count_{0}; // +40
Expand Down
33 changes: 33 additions & 0 deletions include/endstone/detail/command/command_output_with_sender.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// 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/server/commands/command_output.h"

namespace endstone::detail {

class CommandOutputWithSender : public ::CommandOutput {
public:
CommandOutputWithSender(CommandOutputType type, CommandSender &sender) : CommandOutput(type), sender_(sender)
{
has_command_sender = true;
}

private:
friend class MinecraftCommandAdapter;
CommandSender &sender_;
};

} // namespace endstone::detail
6 changes: 3 additions & 3 deletions src/endstone_core/command/command_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <boost/algorithm/string.hpp>

#include "bedrock/server/commands/command_origin_loader.h"
#include "endstone/detail/command/command_output_with_sender.h"
#include "endstone/detail/level/level.h"

namespace endstone::detail {
Expand Down Expand Up @@ -50,9 +51,8 @@ bool CommandWrapper::execute(CommandSender &sender, const std::vector<std::strin
return false;
}

// run the command
// TODO: we should pass down the sender as well
CommandOutput output{CommandOutputType::AllOutput};
// run the command and pass down the sender
CommandOutputWithSender output{CommandOutputType::AllOutput, sender};
command->run(*command_origin, output);

// redirect outputs to sender
Expand Down
18 changes: 14 additions & 4 deletions src/endstone_core/command/minecraft_command_adapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,29 @@
#include <entt/entt.hpp>

#include "endstone/detail/command/command_origin_wrapper.h"
#include "endstone/detail/command/command_output_with_sender.h"

namespace endstone::detail {

void MinecraftCommandAdapter::execute(const CommandOrigin &origin, CommandOutput &output) const
{
auto &server = entt::locator<EndstoneServer>::value();
auto &command_map = server.getCommandMap();
auto command_name = getCommandName();
auto *command = static_cast<CommandWrapper *>(command_map.getCommand(command_name));
const auto &server = entt::locator<EndstoneServer>::value();
const auto &command_map = server.getCommandMap();
const auto command_name = getCommandName();
const auto *command = static_cast<CommandWrapper *>(command_map.getCommand(command_name));
if (!command) {
throw std::runtime_error("Command not found");
}

// We already have a sender passed down from CommandWrapper::execute
if (output.has_command_sender) {
if (command->unwrap().execute(static_cast<CommandOutputWithSender &>(output).sender_, args_)) {
output.success();
}
return;
}

// We don't have a sender yet - this is called from the original dispatching route
auto sender = origin.getEndstoneSender();
if (!sender) {
// Fallback to command origin via a wrapper for unsupported types
Expand Down

0 comments on commit 241afde

Please sign in to comment.