Skip to content

File command_sender_wrapper.h

File List > command > command_sender_wrapper.h

Go to the documentation of this file

// 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 <functional>
#include <string>
#include <unordered_set>
#include <utility>

#include "endstone/command/command_sender.h"
#include "endstone/util/pointers.h"

namespace endstone {

class CommandSenderWrapper final : public CommandSender {
public:
    using Callback = std::function<void(const Message &)>;
    explicit CommandSenderWrapper(const NotNull<CommandSender> &sender, Callback on_message = {},
                                  Callback on_error = {})
        : sender_(sender), on_message_(std::move(on_message)), on_error_(std::move(on_error)) {};

    void sendMessage(const Message &message) const override
    {
        if (on_message_) {
            on_message_(message);
        }
    }
    void sendErrorMessage(const Message &message) const override
    {
        if (on_error_) {
            on_error_(message);
        }
    }

    [[nodiscard]] PermissionLevel getPermissionLevel() const override { return sender_->getPermissionLevel(); }

    [[nodiscard]] bool isPermissionSet(std::string name) const override { return sender_->isPermissionSet(name); }

    [[nodiscard]] bool isPermissionSet(const NotNull<Permission> &perm) const override
    {
        return sender_->isPermissionSet(perm);
    }

    [[nodiscard]] bool hasPermission(std::string name) const override { return sender_->hasPermission(name); }

    [[nodiscard]] bool hasPermission(const NotNull<Permission> &perm) const override
    {
        return sender_->hasPermission(perm);
    }

    NotNull<PermissionAttachment> addAttachment(Plugin &plugin, const std::string &name, bool value) override
    {
        return sender_->addAttachment(plugin, name, value);
    }

    NotNull<PermissionAttachment> addAttachment(Plugin &plugin) override { return sender_->addAttachment(plugin); }

    bool removeAttachment(const NotNull<PermissionAttachment> &attachment) override
    {
        return sender_->removeAttachment(attachment);
    }

    void recalculatePermissions() override { sender_->recalculatePermissions(); }

    [[nodiscard]] std::unordered_set<NotNull<PermissionAttachmentInfo>> getEffectivePermissions() const override
    {
        return sender_->getEffectivePermissions();
    }

    [[nodiscard]] ClassInfo getClassInfo() const override { return typeid(CommandSenderWrapper); }
    [[nodiscard]] bool isInstanceOf(ClassInfo target) const override
    {
        return target == typeid(CommandSenderWrapper) || target == typeid(CommandSender) ||
               target == typeid(Permissible) || target == typeid(Object);
    }

    [[nodiscard]] const NotNull<CommandSender> &getWrapped() const { return sender_; }

    [[nodiscard]] Server &getServer() const override { return sender_->getServer(); }

    [[nodiscard]] std::string getName() const override { return sender_->getName(); }

private:
    NotNull<CommandSender> sender_;
    Callback on_message_;
    Callback on_error_;
};

}  // namespace endstone