
/*
 * Copyright 2011-2022 Cuberite Contributors
 *
 * 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 "ChatColor.h"
#include "CommandOutput.h"
#include "Root.h"
#include "Entities/Player.h"

/* 
 *
 * Below here is the Plugin managers commands, ripped out and integrated directly into cuberite.
 *
 */

void CommandSendMessage(cPlayer* a_Player, cCommandOutputCallback* a_Output, const AString& a_Message) {
    if (a_Player) {
        a_Player->SendMessageInfo(a_Message);
    } else {
        assert(a_Output);
        a_Output->Out(a_Message);
    }
}

void CommandSendMessageSuccess(cPlayer* a_Player, cCommandOutputCallback* a_Output, const AString& a_Message) {
    if (a_Player) {
        a_Player->SendMessageSuccess(a_Message);
    } else {
        assert(a_Output);
        a_Output->Out(a_Message);
    }
}

void CommandSendMessageFailure(cPlayer* a_Player, cCommandOutputCallback* a_Output, const AString& a_Message) {
    if (a_Player) {
        a_Player->SendMessageFailure(a_Message);
    } else {
        assert(a_Output);
        a_Output->Out(a_Message);
    }
}





cWorld* CommandGetWorld(const AString& world_name, cPlayer* a_Player) {
    if (world_name.empty())
        return (a_Player) ? a_Player->GetWorld() : cRoot::Get()->GetDefaultWorld();
    return cRoot::Get()->GetWorld(world_name);
}




int RelativeCommandCoord(const AString& split, int coord) {
    if (!split.empty()) {
        if (split[0] == '~') {
            int relative = 0;
            try {
                relative = std::stoi(split.substr(1));
            } catch (...) { relative = 0; }
            if (coord) {
                if (relative) {
                    return coord + relative;
                }
                return coord;
            }
            return relative;
        }
        try { return std::stoi(split); } catch (...) {}
    }
    throw std::runtime_error("Split is empty!");
}
