CLion编写我的世界基岩版插件(LiteLoaderBDS)-2

发布于 2021-11-08  37 次阅读


如果你没有看过上一篇博客,请点击这里查看

添加链接库

将headers和lib文件夹复制到项目目录中
DearXuan
在CMakeLists.txt中添加对头文件和库文件的引用

cmake_minimum_required(VERSION 3.20)
project(plugins)

set(CMAKE_CXX_STANDARD 17)

add_library(plugins SHARED library.cpp)

include_directories(${PROJECT_SOURCE_DIR}/headers)

link_directories(${PROJECT_SOURCE_DIR}/lib)

target_link_libraries(plugins
        ${PROJECT_SOURCE_DIR}/lib/Chakra.lib
        ${PROJECT_SOURCE_DIR}/lib/LiteLoader.lib)

在library.h中添加头文件

#ifndef PLUGINS_LIBRARY_H
#define PLUGINS_LIBRARY_H

#include "headers/api/basicEvent.h"
#include "headers/mc/Player.h"

#endif //PLUGINS_LIBRARY_H

添加事件监听

修改library.cpp

#include "library.h"

#include <iostream>
#include <windows.h>
#include <string>

BOOL APIENTRY DllMain(HMODULE hModule,
                      DWORD ul_reason_for_call,
                      LPVOID lpReserved
) {
    switch (ul_reason_for_call) {
        case DLL_PROCESS_ATTACH:
        case DLL_THREAD_ATTACH:
        case DLL_THREAD_DETACH:
        case DLL_PROCESS_DETACH:
            break;
    }
    return TRUE;
}

void entry();

extern "C" {
_declspec(dllexport)
void onPostInit() {
    std::ios::sync_with_stdio(false);
    entry();
}
}

void PlayerJoin(JoinEV ev);

void entry() {
    std::cout << "\nDearXuan's Plugins is runing!'\n" << std::endl;
    Event::addEventListener(PlayerJoin);
}

void PlayerJoin(JoinEV ev){
    //在玩家加入服务器后打印信息
    string name = ev.Player->getNameTag();
    std::cout << name << " joined the server." << std::endl;
}

构建该项目

如果在构建途中报了以下错误
DearXuan
只需要进入mass.h,注释掉这一行即可解决
DearXuan

将生成的dll文件复制到服务器plugins文件夹内,运行服务器

如果你是在自己的电脑上运行客户端,可能会受到UWP的回路限制,无法连接上自己的服务器,此时需要管理员身份运行cmd,并执行以下指令

CheckNetIsolation.exe LoopbackExempt –a –p=S-1-15-2-1958404141-86561845-1752920682-3514627264-368642714-62675701-733520436

进入自己的服务器,可以看到控制台上已经打印出信息
DearXuan