Writing a plugin

From Coop wiki
Jump to navigation Jump to search

The following guide will explain how to write a plugin for the server and use the plugin API.

Requirements

  • Visual Studio 2019 (or dotnet in your path)
  • Experience with C#/.NET

Setup

  1. Create a new project in Visual Studio and choose "Class Library (.NET Standard)".
  2. Add the gtaserver.core.dll in the server folder as reference.
  3. Create a new class e.g. 'Example' and inherit the IPlugin (GTAServer.PluginAPI) interface.

The OnEnable method will be called as soon as the plugin is loaded, the plugin can return false here when for example something got wrong while starting. The GameServer instance is also exposed which contains most of the functions to access the server.

Commands

Commands can be executed by the server and the client, they are registed by creating a function with the Command attribute.

Two arguments are passed ctx and args, args contains all arguments passed after the command e.g. '/hello hello there' returns "hello" and "there". The CommandContext class contains a couple of properties about the context of the command execution e.g. the command sender, server instance, orginal message.

To register all commands in a class use gameServer.RegisterCommands<ExampleCommands>(); in your OnEnable where ExampleCommands is your class containing all your commands.

Hooks

A plugin can register several hooks to get notified when certain events happen, for this you can use the following classes ConnectionEvents, GameEvents and PacketEvents. Some require you to return a PluginResponse with the ability to modify the message passed on to other plugins and the server.