![]() |
FourKit 1469f90c
The LCE C# Server Plugin API
|
This will go over how to create your first plugin.
If you havent already, be sure to set up your development environment first:
Setting up your Development Environment
Plugins must have a class that extends ServerPlugin.
onEnable() is ran when the server starts. This is where you add listeners and anything else you need to do on startup.
onDisable() runs when the server stops. You can do stuff like cleaning up here.
Listeners are vital for events to be intercepted by your plugin. This will go over the usage and how to get started.
Listeners must implement the Listener interface. Your listener class should look like this:
To register a listener, you need to add it to FourKit, a common place to do this is in onEnable() in your plugin.
Now that we've registered the listener, we need to make it actually listen to events!
To listen to any given event in your listener class, you MUST create a method with the EventHandler attribute attached and the event specified by the type in the methods argument. The method can be named whatever you wish. Example:
This method will fire whenever a player joins the server. We can make it broadcast a greeting to the whole server:
You may modify what happens with most events and also obtain information about the given event. These functions are stored in the Event object in your method. Let's modify the message that is broadcasted when a player joins the server:
You can browse through the Event namespace to see all events that you can use.
The EventHandler attribute accepts a couple parameters.
Priority - indicates the priority. There are six different priorities, in order of execution:
These constants refer to the EventPriority enum.
Note: The Monitor priority should only be used for reading only. This priority is useful for logging plugins to see the results of an event and modifying values may interfere with those types of plugins.
IgnoreCancelled - A boolean which indicates whether or not your listener should fire if the event has been cancelled before it is the listener's turn to handle the event. False by default.
Example:
A big thing you will probably want to do is learn how to create commands.
They are not like bukkit, you dont fill out a yml file.
Lets start by creating our actual command handler. You must have a class that extends the CommandExecutor class.
sender is the actual command sender. This can be either a Player or a ConsoleCommandSender
command is the actual command.
label is the command name they used to execute.
args is the command arguments passed.
You might notice that the onCommand func returns a boolean. This indicates if the command executed successfully.
Now, lets actually register this command. To register the command, you have to use FourKit.getCommand("command").setExecutor()
Now we can run the command by running /cool in chat or typing cool in console!
getCommand() returns a PluginCommand class. You can see all the functions you can use from here!
Now, when we run help in console, we should see this:
We can even add a description and define usage to the command!
Now it shows this:
Now that we can do all this, we can check who is running the command. Best way to do this is check if the sender is an instance of Player or ConsoleCommandSender.
When console runs this command, they will see "Whats good console" in console. When a Player runs this command, they will see "Do it work?" in chat.
From there on, you can do whatever you want in the command. You can modify the player, such as teleport them somewhere. You can do whatever you want!
Say you want to make a plugin that links a Discord bot to your plugin. This is possible! You can use something like Discord.NET for that.
When a plugin needs dependencies, you also need to bring over the DLL's for the dependencies.
You can put them into a folder under the plugins folder next to the main plugin dll.
Example folder structure:
When a plugin folder is made, make sure the main dll matches the name of the folder, or else it will skip it.
You can also avoid this by using Fody Costura and bundle the dependencies into your DLL.
Fody Costura isnt very well documented, but heres the general usage guide that has worked for users:
Install Fody Costura
This can be through NuGet, or through anything you wish to use for getting dependencies.
Create a FodyWeavers.xml in your project root:
This will exclude bundling fourkit in the DLL too.
Make csproj copy dependency DLL files over to build dir
You can do this by adding <CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies> to the property group.
Build
After you've done all this, it should build and put all dependencies into one DLL in your output folder.