Mod Examples: Difference between revisions
Appearance
No edit summary |
No edit summary |
||
| Line 1: | Line 1: | ||
This page demonstrates how to use fCraft's event system to add functionality. | This page demonstrates how to use fCraft's event system to add functionality. | ||
==Reacting to server shutdown== | |||
There are two events associated with shutdown: Server.ShutdownBegan and Server.ShutdownEnded. Both supply a ShutdownEventArgs object that provides information about the shutdown parameters. | |||
<syntaxhighlight lang="csharp"> | |||
// Subscribing to the event | |||
Server.ShutdownBegan += OnShutdown; | |||
// Event handler | |||
void OnShutdown( object sender, ShutdownEventArgs e ){ | |||
Console.Write( "The end is near! Specifically, it's {0} seconds away.", | |||
e.Delay ); | |||
if( e.Restart ){ | |||
Console.Write( "But we will be back shortly." ); | |||
} | |||
} | |||
</syntaxhighlight> | |||
==Adding a new command== | ==Adding a new command== | ||
Revision as of 02:57, 23 June 2011
This page demonstrates how to use fCraft's event system to add functionality.
Reacting to server shutdown
There are two events associated with shutdown: Server.ShutdownBegan and Server.ShutdownEnded. Both supply a ShutdownEventArgs object that provides information about the shutdown parameters.
// Subscribing to the event
Server.ShutdownBegan += OnShutdown;
// Event handler
void OnShutdown( object sender, ShutdownEventArgs e ){
Console.Write( "The end is near! Specifically, it's {0} seconds away.",
e.Delay );
if( e.Restart ){
Console.Write( "But we will be back shortly." );
}
}
Adding a new command
// Command descriptor
CommandDescriptor CdBleep = new CommandDescriptor {
Name = "bleep",
Aliases = new[] { "bloop" },
Category = CommandCategory.Chat,
Permissions = new[] { Permission.Chat },
Help = "Prints a number of bleeps in chat.",
Usage = "/bleep [Times]",
Handler = Bleep
};
// Command handler - must implement CommandHandler delegate
void Bleep( Player player, Command cmd ){
int numberOfBleeps;
// try to parse next argument as a number
if( !cmd.NextInt( out numberOfBleeps ) ){
// fall back to 10 if no number was given
numberOfBleeps = 10;
}
// check the range
if( numberOfBleeps < 1 || numberOfBleeps > 32 ){
player.Message( "Specify between 1 and 32 bleeps." );
return;
}
for( int i=0; i<numberOfBleeps; i++ ){
Chat.SendGlobal( player, "bleep" );
}
}
// Registering your command with the server
CommandManager.RegisterCustomCommand( CdBleep );