Create your first plugin¶
Prerequisites¶
To develop your first plugin with the Python API, you will have to install the following prerequisites:
To develop your first plugin with the C++ API, you will have to install the following prerequisites:
- JetBrains CLion
- CMake (>= 3.15)
- C++ Compiler
- Windows: Visual Studio 2017 or newer
- Linux: LLVM Toolchain 5 or newer with Clang and libc++
Create a new project¶
When you start up PyCharm, you will see this welcome screen:
Click on New Project
Let's say you want to name your plugin MyPlugin. In Endstone plugin development, we follow the following naming convention:
- Use
lower-case-with-dash
for project name - Prefix your project name with
endstone-
Hence, you should put endstone-my-plugin
in the Name field.
Then, in the Interpreter type, select Custom environment. Select Select existing, and set the path to
where you previously installed endstone
as stated in the prerequisites.
Finally, click on Create. The PyCharm workspace will pop up and you will see this.
Tip
Endstone server requires its plugins to be installed in the same Python environment. A virtual environment is strongly recommended.
Check your dependencies¶
In the last step, you selected the existing interpreter where endstone
package is installed. For now, that's the
only dependency we need for a simple plugin. To check its installation, click on the icon on
the side bar to open the terminal and type:
You show see something like this:
Name: endstone
Version: 0.4.2
Summary: Endstone offers a plugin API for Bedrock Dedicated Servers, supporting both Python and C++.
Home-page:
Author:
Author-email: Vincent Wu <magicdroidx@gmail.com>
License: Apache License
Create pyproject.toml
¶
Modern Python packages can contain a pyproject.toml
file, first introduced in PEP 518. This file contains build
system requirements and information, which are used by pip to build the package.
Now, right click on the project folder and select New > File to create a pyproject.toml
.
Copy the following content and paste into the file.
pyproject.toml | |
---|---|
Notice
The name field should always be the project name. It must start with endstone-
which is enforced by the
plugin loader. The name should also use lower-case-with-dash
style.
When you start up CLion, you will see this welcome screen:
Click on New Project
In the side bar, select C++ Library. Select C++ 17 for Language standard. Select shared for Library type. Click on Create. The CLion workspace will pop up and you will see this.
File structure¶
From the project view in the side bar, you will notice that CLion created a few files for us.
.clang-format
: The configuration file for ClangFormatCMakeLists.txt
: The manifest file for CMake build systemlibrary.cpp
: Source filelibrary.h
: Header file
Delete library.cpp
and library.h
as we don't need them. You can keep the .clang-format
and
CMakeLists.txt
.
Update CMakeLists.txt
¶
Now, open the CMakeLists.txt
in the side bar and delete all the existing content.
Then, copy and paste the following into your CMakeLists.txt
.
CMakeLists.txt | |
---|---|
- This will use the latest development version of Endstone. Consider use a release tag (e.g.
v0.4.0
) instead ofmain
.
Create the main plugin class¶
Now, right click on the project folder and select New > Directory to create a src
directory.
Right click on the src
directory you just created and select Mark Directory as > Sources Root. You will notice
the colour of the icon changes to blue.
Right click again on the src
directory and select New > Python Package to create a package for our plugin.
Since my project name is endstone-my-plugin
, I will name the package endstone_my_plugin
.
You should have something similar to this:
Tip
For Python packages, it is a common practice to use lower-case-with-dash
for project name and
lower_case_with_underscore
for the package name. See PEP 8 for the style guide for Python.
Right click on the package you just created and select New > Python File to create a my_plugin.py
. Create a
class named MyPlugin
which extends the Plugin
class from endstone.plugin
.
src/endstone_my_plugin/my_plugin.py | |
---|---|
Then, open the __init__.py
under the same folder and import the MyPlugin
class from the Python file and add it
to the __all__
variable.
Now, create two files: src/my_plugin.cpp
and include/my_plugin.h
.
Open CMakeLists.txt
and add a new target.
You should have something similar to this:
Open include/my_plugin.h
and add a new class MyPlugin
which extends the endstone::Plugin
class.
include/my_plugin.h | |
---|---|
Then, in src/my_plugin.cpp
, include the header file.
src/my_plugin.cpp | |
---|---|
Add methods¶
Now we want to override a few methods from the base class:
on_load
: this will be called when the plugin is loaded by the serveron_enable
: this will be called when the plugin is enabledon_disable
: this will be called when the plugin is disabled (e.g. during server shutdown)
You can use the logger to log a message when the plugin is loaded, enabled and disabled like below:
src/endstone_my_plugin/my_plugin.py | |
---|---|
Now we want to override a few methods from the base class:
onLoad
: this will be called when the plugin is loaded by the serveronEnable
: this will be called when the plugin is enabledonDisable
: this will be called when the plugin is disabled (e.g. during server shutdown)
You can use the logger to log a message when the plugin is loaded, enabled and disabled like below:
include/my_plugin.h | |
---|---|
Configure plugin metadata¶
Now, the plugin is almost finished. Let's tell the server about our compatible API version.
src/endstone_my_plugin/my_plugin.py | |
---|---|
Lastly, to have the plugin discoverable by the server, you must specify an entry point in pyproject.toml
.
pyproject.toml | |
---|---|
Notice
For the entry point, the name must be the name of your project without the endstone-
prefix. For example,
our project name is endstone-my-plugin
so the entry point's name should be my-plugin
. The value is simply
{module}:{class}
.
Now, the plugin is almost finished. Let's tell the server about our name, version, main class and the description.
src/my_plugin.cpp | |
---|---|
- This is the plugin name!
- This is the plugin version!
- This is the main class of the plugin!
Notice
For plugin name, it must contains only lowercase letters, numbers and underscores.