Mod Examples: Difference between revisions
| Line 50: | Line 50: | ||
// Command handler - must implement CommandHandler delegate | // Command handler - must implement CommandHandler delegate | ||
void BleepHandler( Player player, Command cmd ){ | void BleepHandler( Player player, Command cmd ){ | ||
int numberOfBleeps; | int numberOfBleeps = 10; | ||
// try to parse next argument as a number | // if a param is given | ||
if( cmd.HasNext ) | |||
// try to parse next argument as a number | |||
if( !cmd.NextInt( out numberOfBleeps ) ){ | |||
// if something other than a number was given | |||
CdBleep.PrintUsage( player ); | |||
return; | |||
} | |||
} | } | ||
// check the range | // check the range | ||
if( numberOfBleeps < 1 || numberOfBleeps > 32 ){ | if( numberOfBleeps<1 || numberOfBleeps>32 ){ | ||
player.Message( "Specify between 1 and 32 bleeps." ); | player.Message("Specify between 1 and 32 bleeps."); | ||
return; | return; | ||
} | } | ||
| Line 70: | Line 74: | ||
// Registering your command with the server | // Registering your command with the server | ||
CommandManager.RegisterCustomCommand( CdBleep ); | CommandManager.RegisterCustomCommand(CdBleep); | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Revision as of 21:17, 23 October 2011
This page demonstrates examples of modifying and extending fCraft.
Calling custom code at regular intervals
The following example will call BleepTask method every 10 seconds.
// Interval at which your callback is called
TimeSpan bleepInterval = TimeSpan.FromSeconds( 10 );
// Adding your callback
Scheduler.NewTask( BleepTask ).RunForever( bleepInterval );
// Your callback. Implements SchedulerCallback delegate.
void BleepTask( SchedulerTask task ){
Chat.Say( Player.Console, "bleep" );
}
Besides RunForever(TimeSpan), SchedulerTask has many Run* methods for running a task once, several times, or forever - with different delays and at different intervals.
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
New commands can be added by calling CommandManager.RegisterCustomCommand. You need to provide a CommandDescriptor object. If there is any conflict between your command and existing ones, or if some aspect of your command's descriptor is unacceptible, a CommandRegistrationException will be thrown.
// 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 = BleepHandler
};
// Command handler - must implement CommandHandler delegate
void BleepHandler( Player player, Command cmd ){
int numberOfBleeps = 10;
// if a param is given
if( cmd.HasNext )
// try to parse next argument as a number
if( !cmd.NextInt( out numberOfBleeps ) ){
// if something other than a number was given
CdBleep.PrintUsage( player );
return;
}
}
// 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);