Table of Contents

Class BasePlugin

Namespace
CounterStrikeSharp.API.Core
Assembly
CounterStrikeSharp.API.dll
public abstract class BasePlugin : IPlugin, IDisposable
Inheritance
BasePlugin
Implements

Constructors

BasePlugin()

public BasePlugin()

Fields

CommandHandlers

public readonly Dictionary<Delegate, BasePlugin.CallbackSubscriber> CommandHandlers

Field Value

Dictionary<Delegate, BasePlugin.CallbackSubscriber>

CommandListeners

public readonly Dictionary<Delegate, BasePlugin.CallbackSubscriber> CommandListeners

Field Value

Dictionary<Delegate, BasePlugin.CallbackSubscriber>

EntityOutputHooks

public readonly Dictionary<Delegate, BasePlugin.CallbackSubscriber> EntityOutputHooks

Field Value

Dictionary<Delegate, BasePlugin.CallbackSubscriber>

Handlers

public readonly Dictionary<Delegate, BasePlugin.CallbackSubscriber> Handlers

Field Value

Dictionary<Delegate, BasePlugin.CallbackSubscriber>

Listeners

public readonly Dictionary<Delegate, BasePlugin.CallbackSubscriber> Listeners

Field Value

Dictionary<Delegate, BasePlugin.CallbackSubscriber>

Timers

public readonly List<Timer> Timers

Field Value

List<Timer>

Properties

CommandManager

public ICommandManager CommandManager { get; set; }

Property Value

ICommandManager

Localizer

public IStringLocalizer Localizer { get; set; }

Property Value

IStringLocalizer

Logger

public ILogger Logger { get; set; }

Property Value

ILogger

ModuleAuthor

Author of the plugin as it will appear in the plugin list.

public virtual string ModuleAuthor { get; }

Property Value

string

ModuleDescription

Brief description of the plugin as it will appear in the plugin list.

public virtual string ModuleDescription { get; }

Property Value

string

ModuleDirectory

public string ModuleDirectory { get; }

Property Value

string

ModuleName

Name of the plugin as it will appear in the plugin list.

public abstract string ModuleName { get; }

Property Value

string

ModulePath

The path to the plugin's DLL file.

public string ModulePath { get; set; }

Property Value

string

ModuleVersion

Module version as it will appear in the plugin list.

public abstract string ModuleVersion { get; }

Property Value

string

Methods

AddCommand(string, string, CommandCallback)

Registers a new server command.

public void AddCommand(string name, string description, CommandInfo.CommandCallback handler)

Parameters

name string

The name of the command.

description string

The description of the command.

handler CommandInfo.CommandCallback

The callback function to be invoked when the command is executed.

AddCommandListener(string?, CommandListenerCallback, HookMode)

Adds a command listener which will be called before or after the command is executed on the server by a player.

public void AddCommandListener(string? name, CommandInfo.CommandListenerCallback handler, HookMode mode = HookMode.Pre)

Parameters

name string

Name of the command, e.g. jointeam

handler CommandInfo.CommandListenerCallback

Code to run when command is executed. Return Handled or higher to prevent command execution.

mode HookMode

Whether to hook before or after the command is executed.

AddTimer(float, Action, TimerFlags?)

Adds a timer that will call the given callback after the specified amount of seconds. By default will only run once unless the REPEAT flag is set.

public Timer AddTimer(float interval, Action callback, TimerFlags? flags = null)

Parameters

interval float

Interval/Delay in seconds

callback Action

Code to run when timer elapses

flags TimerFlags?

Controls if the timer is a one-off, repeat or stops on map change etc.

Returns

Timer

An instance of the Timer

DeregisterEventHandler(string, Delegate, bool)

[Obsolete("Use the generic version of this method")]
public void DeregisterEventHandler(string name, Delegate handler, bool post)

Parameters

name string
handler Delegate
post bool

DeregisterEventHandler<T>(GameEventHandler<T>, HookMode)

De-registers a game event handler.

public void DeregisterEventHandler<T>(BasePlugin.GameEventHandler<T> handler, HookMode hookMode = HookMode.Post) where T : GameEvent

Parameters

handler BasePlugin.GameEventHandler<T>

The event handler to register.

hookMode HookMode

The mode in which the event handler is hooked. Default is HookMode.Post.

Type Parameters

T

The type of the game event.

Dispose()

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.

public void Dispose()

Dispose(bool)

protected virtual void Dispose(bool disposing)

Parameters

disposing bool

HookEntityOutput(string, string, EntityOutputHandler, HookMode)

Hooks an entity output.

public void HookEntityOutput(string classname, string outputName, EntityIO.EntityOutputHandler handler, HookMode mode = HookMode.Pre)

Parameters

classname string

Classname to hook, or * for wildcard

outputName string

Output name to hook, or * for wildcard

handler EntityIO.EntityOutputHandler

Handler to call

mode HookMode

HookSingleEntityOutput(CEntityInstance, string, EntityOutputHandler)

Hooks an entity output for a single entity instance.

public void HookSingleEntityOutput(CEntityInstance entityInstance, string outputName, EntityIO.EntityOutputHandler handler)

Parameters

entityInstance CEntityInstance

Entity instance to hook

outputName string

Output name to hook, or * for wildcard

handler EntityIO.EntityOutputHandler

Handler to call

HookUserMessage(int, UserMessageHandler, HookMode)

public void HookUserMessage(int messageId, UserMessage.UserMessageHandler handler, HookMode mode = HookMode.Pre)

Parameters

messageId int
handler UserMessage.UserMessageHandler
mode HookMode

InitializeConfig(object, Type)

public void InitializeConfig(object instance, Type pluginType)

Parameters

instance object
pluginType Type

Load(bool)

This method is called by CounterStrikeSharp on plugin load and should be treated as plugin constructor. Called with true on a hot reload (DLL file replaced in plugins folder)

public virtual void Load(bool hotReload)

Parameters

hotReload bool

OnAllPluginsLoaded(bool)

Will be called by CounterStrikeSharp after all plugins have been loaded. This will also be called for convenience after a reload or a late l oad, so that you don't have to handle re-wiring everything.

public virtual void OnAllPluginsLoaded(bool hotReload)

Parameters

hotReload bool

RegisterAllAttributes(object)

Registers all attribute handlers on the given instance. Can be used to register event handlers, console commands, entity outputs etc. from classes that are not derived from BasePlugin.

public void RegisterAllAttributes(object instance)

Parameters

instance object

RegisterAttributeHandlers(object)

Registers all game event handlers that are decorated with the GameEventHandlerAttribute attribute.

public void RegisterAttributeHandlers(object instance)

Parameters

instance object

The instance of the object where the event handlers are defined.

RegisterConsoleCommandAttributeHandlers(object)

Registers all console command handlers that are decorated with the ConsoleCommandAttribute attribute.

public void RegisterConsoleCommandAttributeHandlers(object instance)

Parameters

instance object

The instance of the object where the console command handlers are defined.

RegisterEntityOutputAttributeHandlers(object)

Registers all entity output handlers that are decorated with the EntityOutputHookAttribute attribute.

public void RegisterEntityOutputAttributeHandlers(object instance)

Parameters

instance object

The instance of the object where entity output hook handlers are defined.

RegisterEventHandler<T>(GameEventHandler<T>, HookMode)

Registers a game event handler.

public void RegisterEventHandler<T>(BasePlugin.GameEventHandler<T> handler, HookMode hookMode = HookMode.Post) where T : GameEvent

Parameters

handler BasePlugin.GameEventHandler<T>

The event handler to register.

hookMode HookMode

The mode in which the event handler is hooked. Default is HookMode.Post.

Type Parameters

T

The type of the game event.

RegisterFakeConVars(object)

Used to bind a fake ConVar to a plugin command. Only required for ConVars that are not public properties of the plugin class.

public void RegisterFakeConVars(object instance)

Parameters

instance object

RegisterFakeConVars(Type, object)

public void RegisterFakeConVars(Type type, object instance = null)

Parameters

type Type
instance object

RegisterListener<T>(T)

Registers a global listener, e.g. Listeners.OnTick, Listeners.OnClientConnect.

public void RegisterListener<T>(T handler) where T : Delegate

Parameters

handler T

Type Parameters

T

Listener delegate type

Examples

RegisterListener<Listeners.OnTick>(OnTick);

Exceptions

ArgumentException

Invalid listener T provided

RemoveCommand(string, CommandCallback)

Removes a server command.

public void RemoveCommand(string name, CommandInfo.CommandCallback handler)

Parameters

name string

The name of the command.

handler CommandInfo.CommandCallback

The callback function to be invoked when the command is executed.

RemoveCommandListener(string, CommandListenerCallback, HookMode)

Remove a command listener.

public void RemoveCommandListener(string name, CommandInfo.CommandListenerCallback handler, HookMode mode)

Parameters

name string

Name of the command, e.g. jointeam

handler CommandInfo.CommandListenerCallback

Code to run when command is executed. Return Handled or higher to prevent command execution.

mode HookMode

Whether to hook before or after the command is executed.

RemoveListener(string, Delegate)

Removes a global listener.

[Obsolete("Use the generic version of this method")]
public void RemoveListener(string name, Delegate handler)

Parameters

name string
handler Delegate

RemoveListener<T>(T)

Removes a global listener.

public void RemoveListener<T>(T handler) where T : Delegate

Parameters

handler T

Type Parameters

T

Exceptions

ArgumentException

Invalid listener T provided

UnhookEntityOutput(string, string, EntityOutputHandler, HookMode)

Unhooks an entity output.

public void UnhookEntityOutput(string classname, string outputName, EntityIO.EntityOutputHandler handler, HookMode mode = HookMode.Pre)

Parameters

classname string

Classname to hook, or * for wildcard

outputName string

Output name to hook, or * for wildcard

handler EntityIO.EntityOutputHandler

Handler to call

mode HookMode

UnhookSingleEntityOutput(CEntityInstance, string, EntityOutputHandler)

Unhooks an entity output for a single entity instance.

public void UnhookSingleEntityOutput(CEntityInstance entityInstance, string outputName, EntityIO.EntityOutputHandler handler)

Parameters

entityInstance CEntityInstance

Entity instance to hook

outputName string

Output name to hook, or * for wildcard

handler EntityIO.EntityOutputHandler

Handler to call

UnhookUserMessage(int, UserMessageHandler, HookMode)

public void UnhookUserMessage(int messageId, UserMessage.UserMessageHandler handler, HookMode mode = HookMode.Pre)

Parameters

messageId int
handler UserMessage.UserMessageHandler
mode HookMode

Unload(bool)

Will be called by CounterStrikeSharp on plugin unload. In this method the plugin should cleanup any extra resources. Event handlers, listeners etc. will automatically be deregistered.

public virtual void Unload(bool hotReload)

Parameters

hotReload bool