Skip to content

Create your own plugin

Adammantium edited this page Aug 29, 2023 · 9 revisions

Works for Server Version 0.8.1

Project requirements

Project setup

  • Output type: Class Library
  • .NET Framework 4.7.2

Project References:

Available Base Types

Plugin Starter Class (required):

The AMP_Plugin Class will handle all Events coming from the server, if you want to use a event just create an override for the Event you want to use. A Basic Class will look like this:

public class FileLogger : AMP_Plugin {
    public override string NAME    => "FileLogger";
    public override string AUTHOR  => "Adammantium";
    public override string VERSION => "1.0.0";

    public override void OnStart() {
        Log.Info("Im logging stuff to the console");
    }
}

Available AMP_Plugin Overrides

These functions get called once your plugin gets loaded or unloaded.

// Start and Stop of Plugin
OnStart()
OnStop()

Command Handler

Adds server side console commands to your plugin. Just create a class with CommandHandler as a Base Type and the Plugin-Loader will automatically handle it.

internal class MyCoolCommand : CommandHandler {

    public override string[] ALIASES => new string[] { "cookies", "meep" }; // all the words that can trigger the command
    public override string   HELP    => "I like cookies."; // Hint what the command does

    public override string Process(string[] args) {
        // Your cool code here
        return "output";
    }
}

Plugin Config

If you want to make a config file available for your plugin. Its very easy to do, just create a PluginConfig class and the server will handle the rest.

internal class CookieConfig : PluginConfig {
    public string whatILike = "Cookies";
    public int howManyIWant = 100;
}

This will result in a json file inside the mods folder. It will have the name of your mod and look like this:

{
  "whatILike": "Cookies",
  "howManyIWant": 100
}

The file will automatically be generated when your plugin starts the first time, and will receive your assigned values as the default ones. To access your config from the mod, you can use the function GetConfig that AMP_Plugin contains. So just do this in your OnStart Method:

private CookieConfig config;
public override void OnStart() {
    config = (CookieConfig) GetConfig();
}

If you made changes to your config in code, for example if you track scores or something, you can just call the save method. The save function requires your AMP_Plugin class as a parameter, if you are doing it in the same class as your OnStart, you can just call it like this: (Otherwise just pass a reference to your plugin)

PluginConfig.Save(this);

Event Handling

AMP_Plugin Events:

ServerEvents

To get access to all different Events you can use the Events defined in ServerEvents.
These are the Events:

Player Events

Event Parameters Description
onPlayerJoin ClientData player A player connects to the server
onPlayerQuit ClientData player A Player disconnects from the server
onPlayerKilled ClientData killed, ClientData killer A Player got killed
onPlayerDamaged ClientData damaged, float damage, ClientData damager A Player got damaged

Item Events

Event Parameters Description
onItemSpawned ItemNetworkData item, ClientData clientSpawned A Item gets spawned by a player
onItemDespawned ItemNetworkData item, ClientData clientDespawned A Item gets despawned by a player
onItemOwnerChanged ItemNetworkData item, ClientData oldOwner, ClientData newOwner A Item changed authority to another player

Creature Events

Event Parameters Description
onCreatureSpawned CreatureNetworkData creature, ClientData spawner A Creature gets spawned by a player
onCreatureDespawned CreatureNetworkData creature, ClientData spawner A Creature gets despawned by a player
onCreatureOwnerChanged CreatureNetworkData creature, ClientData oldOwner, ClientData newOwner A Creature changed authority to another player
onCreatureKilled CreatureNetworkData creature, ClientData killer A Creature gets killed by a Player
onCreatureDamaged CreatureNetworkData creature, float damage, ClientData damager A Creature gets damaged by a Player

How to use them

To use them you just need to subscribe to them. You can do this in the OnStart function of your Plugin.

ServerEvents.onPlayerJoin += OnPlayerJoin;

After that just create a OnPlayerJoin function like this and you are good to go:

public void OnPlayerQuit(ClientData client) {
    // Do something
}