CLion编写我的世界基岩版插件(LiteLoaderBDS)-2
如果你没有看过上一篇博客,请点击这里查看
添加链接库
将headers和lib文件夹复制到项目目录中
在CMakeLists.txt中添加对头文件和库文件的引用
CMakeLists.txt1 2 3 4 5 6 7 8 9 10 11 12 13 14
| 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中添加头文件
library.h1 2 3 4 5 6 7
| #ifndef PLUGINS_LIBRARY_H #define PLUGINS_LIBRARY_H #include "headers/api/basicEvent.h" #include "headers/mc/Player.h" #endif
|
添加事件监听
修改library.cpp
library.cpp1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| #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; }
|
构建该项目
如果在构建途中报了以下错误
只需要进入mass.h,注释掉这一行即可解决
将生成的dll文件复制到服务器plugins文件夹内,运行服务器
如果你是在自己的电脑上运行客户端,可能会受到UWP的回路限制,无法连接上自己的服务器,此时需要管理员身份运行cmd,并执行以下指令
cmd1
| CheckNetIsolation.exe LoopbackExempt –a –p=S-1-15-2-1958404141-86561845-1752920682-3514627264-368642714-62675701-733520436
|
进入自己的服务器,可以看到控制台上已经打印出信息