Class BasePlugin
- Namespace
- CounterStrikeSharp.API.Core
- Assembly
- CounterStrikeSharp.API.dll
public abstract class BasePlugin : IPlugin, IDisposable
- Inheritance
-
objectBasePlugin
- Implements
Constructors
BasePlugin()
public BasePlugin()
Fields
CommandDefinitions
public readonly List<CommandDefinition> CommandDefinitions
Field Value
CommandListeners
public readonly Dictionary<Delegate, BasePlugin.CallbackSubscriber> CommandListeners
Field Value
EntityOutputHooks
public readonly Dictionary<Delegate, BasePlugin.CallbackSubscriber> EntityOutputHooks
Field Value
Handlers
public readonly Dictionary<Delegate, BasePlugin.CallbackSubscriber> Handlers
Field Value
Listeners
public readonly Dictionary<Delegate, BasePlugin.CallbackSubscriber> Listeners
Field Value
Timers
public readonly List<Timer> Timers
Field Value
Properties
CommandManager
public ICommandManager CommandManager { get; set; }
Property Value
Localizer
public IStringLocalizer Localizer { get; set; }
Property Value
Logger
public ILogger Logger { get; set; }
Property Value
ModuleAuthor
Author of the plugin as it will appear in the plugin list.
public virtual string ModuleAuthor { get; }
Property Value
ModuleDescription
Brief description of the plugin as it will appear in the plugin list.
public virtual string ModuleDescription { get; }
Property Value
ModuleDirectory
public string ModuleDirectory { get; }
Property Value
ModuleName
Name of the plugin as it will appear in the plugin list.
public abstract string ModuleName { get; }
Property Value
ModulePath
The path to the plugin's DLL file.
public string ModulePath { get; set; }
Property Value
ModuleVersion
Module version as it will appear in the plugin list.
public abstract string ModuleVersion { get; }
Property Value
Methods
AddCommand(string, string, CommandCallback)
Registers a new server command.
public void AddCommand(string name, string description, CommandInfo.CommandCallback handler)
Parameters
name
stringThe name of the command.
description
stringThe description of the command.
handler
CommandInfo.CommandCallbackThe 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
stringName of the command, e.g.
jointeam
handler
CommandInfo.CommandListenerCallbackCode to run when command is executed. Return Handled or higher to prevent command execution.
mode
HookModeWhether 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
floatInterval/Delay in seconds
callback
ActionCode to run when timer elapses
flags
TimerFlags?Controls if the timer is a one-off, repeat or stops on map change etc.
Returns
DeregisterEventHandler(string, Delegate, bool)
[Obsolete("Use the generic version of this method")]
public void DeregisterEventHandler(string name, Delegate handler, bool post)
Parameters
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
HookModeThe 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
stringClassname to hook, or
*
for wildcardoutputName
stringOutput name to hook, or
*
for wildcardhandler
EntityIO.EntityOutputHandlerHandler 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
CEntityInstanceEntity instance to hook
outputName
stringOutput name to hook, or
*
for wildcardhandler
EntityIO.EntityOutputHandlerHandler to call
HookUserMessage(int, UserMessageHandler, HookMode)
public void HookUserMessage(int messageId, UserMessage.UserMessageHandler handler, HookMode mode = HookMode.Pre)
Parameters
messageId
inthandler
UserMessage.UserMessageHandlermode
HookMode
InitializeConfig(object, Type)
public void InitializeConfig(object instance, Type pluginType)
Parameters
instance
objectpluginType
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
objectThe 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
objectThe 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
objectThe 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
HookModeThe 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
Typeinstance
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
stringThe name of the command.
handler
CommandInfo.CommandCallbackThe 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
stringName of the command, e.g.
jointeam
handler
CommandInfo.CommandListenerCallbackCode to run when command is executed. Return Handled or higher to prevent command execution.
mode
HookModeWhether 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
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
stringClassname to hook, or
*
for wildcardoutputName
stringOutput name to hook, or
*
for wildcardhandler
EntityIO.EntityOutputHandlerHandler 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
CEntityInstanceEntity instance to hook
outputName
stringOutput name to hook, or
*
for wildcardhandler
EntityIO.EntityOutputHandlerHandler to call
UnhookUserMessage(int, UserMessageHandler, HookMode)
public void UnhookUserMessage(int messageId, UserMessage.UserMessageHandler handler, HookMode mode = HookMode.Pre)
Parameters
messageId
inthandler
UserMessage.UserMessageHandlermode
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