-
Notifications
You must be signed in to change notification settings - Fork 13
Making mods #1: Basics & setting up the SDK
First you should obtain the ModLoader SDK, You should go to the GitHub downloads page: https://github.com/minecraft-linux/server-modloader/releases, download mod_sdk.zip and extract it somewhere, making sure to write down the path, as you'll need it.
CoreMod provides us with a simple callback called when the server has been started: void modloader_on_server_start(ServerInstance* serverInstance)
.
The ModLoader also provides us with several logging functions, check out the log.h header for the list: https://github.com/minecraft-linux/server-modloader/blob/master/include/modloader/log.h. The easiest way to log something from a C mod is to use the modloader_log*() function family - the letter following the function name determines the log level.
The mod loader provides both a C and a C++ API. The C++ API is the recommended way to get started.
While you'll most likely want to write mods in C++, here's an example in C (save it as main.c
):
#include <modloader/log.h>
void modloader_on_server_start(void* serverInstance) {
modloader_logv("TestMod", "Server has been started!");
}
You can compile it using gcc main.c -I ${MODLOADER_SDK}/include/ -L ${MODLOADER_SDK}/lib -lserver_modloader -shared -o libTestMod.so
. Make sure to define MODLOADER_SDK
before running this command.
You should then copy libTestMod.so to the server mods/ directory.
The following example uses the C++ logging API instead of the C one (save it as main.cpp
):
#include <modloader/log.h>
using namespace modloader;
extern "C" void modloader_on_server_start(void* serverInstance) {
Log::verbose("TestMod", "Server has been started!");
}
You can compile it using g++ main.cpp -std=c++11 -I ${MODLOADER_SDK}/include/ -L ${MODLOADER_SDK}/lib/ -lserver_modloader -shared -fPIC -o libTestMod.so
. Make sure to define MODLOADER_SDK
before running this command.
You should then copy libTestMod.so to the server mods/ directory.
Please note that it is heavily recommeded to use CMake instead of plainly compiling the mod using the command line g++
executable.
In order to use the CMake build system please create a CMakeLists.txt file (in the same directory as the mod sources) with the following contents:
cmake_minimum_required(VERSION 3.0)
project(_YOUR_MOD_NAME_)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
find_package(ModLoader REQUIRED)
add_library(_YOUR_MOD_NAME_ SHARED main.cpp)
target_link_libraries(_YOUR_MOD_NAME_ PUBLIC ModLoader)
Replace all occurrences of _YOUR_MOD_NAME_
with the name of your mod, eg. ExampleMod
Then, to compile a mod using the CMake build system you can do the following:
export CMAKE_PREFIX_PATH=/Path/To/Mod/Sdk
mkdir -p build && cd build
cmake ..
make