fCraft.Chat.SendGlobal C# (CSharp) Method

SendGlobal() public static method

Sends a global (white) chat.
public static SendGlobal ( [ player, [ rawMessage ) : bool
player [ Player writing the message.
rawMessage [ Message text.
return bool
        public static bool SendGlobal( [NotNull] Player player, [NotNull] string rawMessage ) {
            if ( player == null )
                throw new ArgumentNullException( "player" );
            if ( rawMessage == null )
                throw new ArgumentNullException( "rawMessage" );
            string OriginalMessage = rawMessage;
            if ( Server.Moderation && !Server.VoicedPlayers.Contains( player ) && player.World != null ) {
                player.Message( "&WError: Server Moderation is activated. Message failed to send" );
                return false;
            }
            rawMessage = rawMessage.Replace( "$name", "Hello my name is " + player.ClassyName );
            rawMessage = rawMessage.Replace( "$kicks", "I have kicked " + player.Info.TimesKickedOthers.ToString() + " players." );
            rawMessage = rawMessage.Replace( "$bans", "I have banned " + player.Info.TimesBannedOthers.ToString() + " players." );
            rawMessage = rawMessage.Replace( "$awesome", "It is my professional opinion, that " + ConfigKey.ServerName.GetString() + " is the best server on Minecraft" );
            rawMessage = rawMessage.Replace( "$server", ConfigKey.ServerName.GetString() );
            rawMessage = rawMessage.Replace( "$motd", ConfigKey.MOTD.GetString() );
            rawMessage = rawMessage.Replace( "$date", DateTime.UtcNow.ToShortDateString() );
            rawMessage = rawMessage.Replace( "$time", DateTime.UtcNow.ToString() );

            if ( !player.Can( Permission.ChatWithCaps ) ) {
                int caps = 0;
                for ( int i = 0; i < rawMessage.Length; i++ ) {
                    if ( Char.IsUpper( rawMessage[i] ) ) {
                        caps++;
                        if ( caps > ConfigKey.MaxCaps.GetInt() ) {
                            rawMessage = rawMessage.ToLower();
                            player.Message( "Your message was changed to lowercase as it exceeded the maximum amount of capital letters." );
                        }
                    }
                }
            }

            if ( !player.Can( Permission.Swear ) ) {
                if ( !File.Exists( "SwearWords.txt" ) ) {
                    StringBuilder sb = new StringBuilder();
                    sb.AppendLine( "#This txt file should be filled with bad words that you want to be filtered out" );
                    sb.AppendLine( "#I have included some examples, excuse my language :P" );
                    sb.AppendLine( "fuck" );
                    sb.AppendLine( "fucking" );
                    sb.AppendLine( "fucked" );
                    sb.AppendLine( "dick" );
                    sb.AppendLine( "bitch" );
                    sb.AppendLine( "shit" );
                    sb.AppendLine( "shitting" );
                    sb.AppendLine( "shithead" );
                    sb.AppendLine( "cunt" );
                    sb.AppendLine( "nigger" );
                    sb.AppendLine( "wanker" );
                    sb.AppendLine( "wank" );
                    sb.AppendLine( "wanking" );
                    sb.AppendLine( "piss" );
                    File.WriteAllText( "SwearWords.txt", sb.ToString() );
                }
                string CensoredText = Color.ReplacePercentCodes( ConfigKey.SwearName.GetString() ) + Color.White;
                if ( ConfigKey.SwearName.GetString() == null ) {
                    CensoredText = "&CBlock&F";
                }

                const string PatternTemplate = @"\b({0})(s?)\b";
                const RegexOptions Options = RegexOptions.IgnoreCase;

                if ( Swears.Count == 0 ) {
                    Swears.AddRange( File.ReadAllLines( "SwearWords.txt" ).
                        Where( line => line.StartsWith( "#" ) == false || line.Trim().Equals( String.Empty ) ) );
                }

                if ( badWordMatchers == null ) {
                    badWordMatchers = Swears.
                        Select( x => new Regex( string.Format( PatternTemplate, x ), Options ) );
                }

                string output = badWordMatchers.
                   Aggregate( rawMessage, ( current, matcher ) => matcher.Replace( current, CensoredText ) );
                rawMessage = output;
            }

            var recepientList = Server.Players.NotIgnoring( player );

            string formattedMessage = String.Format( "{0}&F: {1}",
                                                     player.ClassyName,
                                                     rawMessage );

            var e = new ChatSendingEventArgs( player,
                                              rawMessage,
                                              formattedMessage,
                                              ChatMessageType.Global,
                                              recepientList );

            if ( !SendInternal( e ) )
                return false;

            Logger.Log( LogType.GlobalChat,
                        "{0}: {1}", player.Name, OriginalMessage );
            return true;
        }

Usage Example

Example #1
0
        void HandleChatMessage([NotNull] string rawMessage)
        {
            if (rawMessage == null)
            {
                throw new ArgumentNullException("rawMessage");
            }
            if (!Can(Permission.Chat))
            {
                return;
            }

            if (Info.IsMuted)
            {
                MessageMuted();
                return;
            }

            if (DetectChatSpam())
            {
                return;
            }

            // Escaped slash removed AFTER logging, to avoid confusion with real commands
            if (rawMessage.StartsWith("//"))
            {
                rawMessage = rawMessage.Substring(1);
            }

            if (rawMessage.EndsWith("//"))
            {
                rawMessage = rawMessage.Substring(0, rawMessage.Length - 1);
            }

            Chat.SendGlobal(this, rawMessage);
        }