fCraft.World.IsValidName C# (CSharp) Method

IsValidName() public static method

Ensures that player name has the correct length (2-16 characters) and character set (alphanumeric chars and underscores allowed).
public static IsValidName ( [ name ) : bool
name [
return bool
        public static bool IsValidName( [NotNull] string name )
        {
            if ( name == null )
                throw new ArgumentNullException( "name" );
            if ( name.Length < 2 || name.Length > 16 )
                return false;
            // ReSharper disable LoopCanBeConvertedToQuery
            for ( int i = 0; i < name.Length; i++ ) {
                char ch = name[i];
                if ( ch != '-' ) {
                    if ( ch < '0' ||
                        ch > '9' && ch < 'A' ||
                        ch > 'Z' && ch < '_' ||
                        ch > '_' && ch < 'a' ||
                        ch > 'z' ) {
                        return false;
                    }
                }
            }
            // ReSharper restore LoopCanBeConvertedToQuery
            return true;
        }

Usage Example

Example #1
0
        private static void ZoneRenameHandler(Player player, Command cmd)
        {
            World playerWorld = player.World;

            if (playerWorld == null)
            {
                PlayerOpException.ThrowNoWorld(player);
            }

            // make sure that both parameters are given
            string oldName = cmd.Next();
            string newName = cmd.Next();

            if (oldName == null || newName == null)
            {
                CdZoneRename.PrintUsage(player);
                return;
            }

            // make sure that the new name is valid
            if (!World.IsValidName(newName))
            {
                player.Message("\"{0}\" is not a valid zone name", newName);
                return;
            }

            // find the old zone
            var  zones   = player.WorldMap.Zones;
            Zone oldZone = zones.Find(oldName);

            if (oldZone == null)
            {
                player.MessageNoZone(oldName);
                return;
            }

            // Check if a zone with "newName" name already exists
            Zone newZone = zones.FindExact(newName);

            if (newZone != null && newZone != oldZone)
            {
                player.Message("A zone with the name \"{0}\" already exists.", newName);
                return;
            }

            // check if any change is needed
            string fullOldName = oldZone.Name;

            if (fullOldName == newName)
            {
                player.Message("The zone is already named \"{0}\"", fullOldName);
                return;
            }

            // actually rename the zone
            zones.Rename(oldZone, newName);

            // announce the rename
            playerWorld.Players.Message("&SZone \"{0}\" was renamed to \"{1}&S\" by {2}",
                                        fullOldName, oldZone.ClassyName, player.ClassyName);
            Logger.Log(LogType.UserActivity,
                       "Player {0} renamed zone \"{1}\" to \"{2}\" on world {3}",
                       player.Name, fullOldName, newName, playerWorld.Name);
        }
All Usage Examples Of fCraft.World::IsValidName