Page 1 of 1

My first command :D

Posted: January 3rd, 2012, 7:03 pm
by BobKare
Well, just started modding fCraft, and I'm slowly learning C#!
This is my first command, lol:

Code: Select all

  
        static readonly CommandDescriptor CdBob = new CommandDescriptor
        {
            Name = "Bob",
            Category = CommandCategory.Chat,
            Aliases = new[] { "BobLol" },
            IsConsoleSafe = true,
            NotRepeatable = false,
            DisableLogging = true,
            Permissions = new[] { Permission.Chat },
            Usage = "/Bob",
            Help = "Shows something awesome!",
            Handler = BobHandler
        };

        static void BobHandler( Player player, Command cmd) {

            player.MessageNow("Bob is AWESOME");
        }
It's working^^

Re: My first command :D

Posted: January 3rd, 2012, 7:25 pm
by Hellenion
I laughed.

You should have made "Hello World"... either way, good job on picking up C#!

Re: My first command :D

Posted: January 3rd, 2012, 7:26 pm
by fragmer
I recommend using player.Message() instead of .MessageNow(), unless you know exactly what you're doing ;)

Re: My first command :D

Posted: January 3rd, 2012, 7:27 pm
by Hellenion
Well of course he knows what he's doing, he's a programmer!

Re: My first command :D

Posted: January 3rd, 2012, 7:45 pm
by BobKare
Hellenion wrote:Well of course he knows what he's doing, he's a programmer!
lol
fragmer wrote:I recommend using player.Message() instead of .MessageNow(), unless you know exactly what you're doing ;)
KK. What's the difference?

Re: My first command :D

Posted: January 3rd, 2012, 10:37 pm
by fragmer
MessageNow sends message directly to the player at once. It uses SendNow:

Code: Select all

/// <summary> Send packet to player (not thread safe, sync, immediate).
/// Should NEVER be used from any thread other than this session's ioThread.
/// Not thread-safe (for performance reason). </summary>
public void SendNow( Packet packet ) {
    if( Thread.CurrentThread != ioThread ) {
        throw new InvalidOperationException( "SendNow may only be called from player's own thread." );
    }
    writer.Write( packet.Data );
    BytesSent += packet.Data.Length;
}
Message puts the message into player's outgoing packet queue. It uses Send:

Code: Select all

/// <summary> Send packet (thread-safe, async, priority queue).
/// This is used for most packets (movement, chat, etc). </summary>
public void Send( Packet packet ) {
    if( canQueue ) priorityOutputQueue.Enqueue( packet );
}

Re: My first command :D

Posted: January 3rd, 2012, 10:57 pm
by Hellenion
The public access modifier is a bug.

Re: My first command :D

Posted: January 4th, 2012, 1:52 pm
by BobKare
I see, changed.

Thank you fragmer :)