void TP(Player player, Command cmd) { if (player.Can(Permissions.Teleport)) { string name = cmd.Next(); if (name == null) { player.Send(PacketWriter.MakeTeleport(255, world.map.spawn)); } else { Player target = world.FindPlayer(name); if (target != null) { Position pos = target.pos; pos.x += 1; pos.y += 1; pos.h += 1; player.Send(PacketWriter.MakeTeleport(255, pos)); } else if (cmd.Next() == null) { world.NoPlayerMessage(player, name); } else { cmd.Rewind(); int x, y, h; if (cmd.NextInt(out x) && cmd.NextInt(out y) && cmd.NextInt(out h)) { if (x < 0 || x > world.map.widthX || y < 0 || y > world.map.widthY || y < 0 || y > world.map.height) { player.Message("Specified coordinates are outside the map!"); } else { player.pos.Set(x * 32 + 16, y * 32 + 16, h * 32 + 16, player.pos.r, player.pos.l); player.Send(PacketWriter.MakeTeleport(255, player.pos)); } } else { player.Message("See " + Color.Help + "/help tp" + Color.Sys + " for information on using /tp"); } } } } else { world.NoAccessMessage(player); } }
public void AskForConfirmation(Command cmd, string message, params object[] args) { if (cmd == null) { throw new ArgumentNullException("cmd"); } if (message == null) { throw new ArgumentNullException("message"); } CommandToConfirm = cmd; CommandToConfirmDate = DateTime.UtcNow; Message("{0} Type &H/ok&S to continue.", String.Format(message, args)); CommandToConfirm.Rewind(); }
public static void RealmCreate(Player player, Command cmd, string themeName, string templateName) { MapGenTemplate template; MapGenTheme theme; int wx, wy, height = 128; if (!(cmd.NextInt(out wx) && cmd.NextInt(out wy) && cmd.NextInt(out height))) { if (player.World != null) { wx = 128; wy = 128; height = 128; } else { player.Message("When used from console, /gen requires map dimensions."); return; } cmd.Rewind(); cmd.Next(); cmd.Next(); } if (!Map.IsValidDimension(wx)) { player.Message("Cannot make map with width {0}: dimensions must be multiples of 16.", wx); return; } else if (!Map.IsValidDimension(wy)) { player.Message("Cannot make map with length {0}: dimensions must be multiples of 16.", wy); return; } else if (!Map.IsValidDimension(height)) { player.Message("Cannot make map with height {0}: dimensions must be multiples of 16.", height); return; } string fileName = player.Name; string fullFileName = null; if (fileName == null) { if (player.World == null) { player.Message("When used from console, /gen requires FileName."); return; } if (!cmd.IsConfirmed) { player.Confirm(cmd, "Replace this realm's map with a generated one?"); return; } } else { fileName = fileName.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar); if (!fileName.EndsWith(".fcm", StringComparison.OrdinalIgnoreCase)) { fileName += ".fcm"; } fullFileName = Path.Combine(Paths.MapPath, fileName); if (!Paths.IsValidPath(fullFileName)) { player.Message("Invalid filename."); return; } if (!Paths.Contains(Paths.MapPath, fullFileName)) { player.MessageUnsafePath(); return; } string dirName = fullFileName.Substring(0, fullFileName.LastIndexOf(Path.DirectorySeparatorChar)); if (!Directory.Exists(dirName)) { Directory.CreateDirectory(dirName); } if (!cmd.IsConfirmed && File.Exists(fullFileName)) { player.Confirm(cmd, "The mapfile \"{0}\" already exists. Overwrite?", fileName); return; } } bool noTrees; if (themeName.Equals("grass", StringComparison.OrdinalIgnoreCase)) { theme = MapGenTheme.Forest; noTrees = true; } else { try { theme = (MapGenTheme)Enum.Parse(typeof(MapGenTheme), themeName, true); noTrees = (theme != MapGenTheme.Forest); } catch (Exception) { player.MessageNow("Unrecognized theme \"{0}\". Available themes are: Grass, {1}", themeName, String.Join(", ", Enum.GetNames(typeof(MapGenTheme)))); return; } } try { template = (MapGenTemplate)Enum.Parse(typeof(MapGenTemplate), templateName, true); } catch (Exception) { player.Message("Unrecognized template \"{0}\". Available templates are: {1}", templateName, String.Join(", ", Enum.GetNames(typeof(MapGenTemplate)))); return; } if (!Enum.IsDefined(typeof(MapGenTheme), theme) || !Enum.IsDefined(typeof(MapGenTemplate), template)) { return; } MapGeneratorArgs args = MapGenerator.MakeTemplate(template); args.MapWidth = wx; args.MapLength = wy; args.MapHeight = height; args.MaxHeight = (int)(args.MaxHeight / 80d * height); args.MaxDepth = (int)(args.MaxDepth / 80d * height); args.Theme = theme; args.AddTrees = !noTrees; Map map; try { if (theme == MapGenTheme.Forest && noTrees) { player.MessageNow("Generating Grass {0}...", template); } else { player.MessageNow("Generating {0} {1}...", theme, template); } if (theme == MapGenTheme.Forest && noTrees && template == MapGenTemplate.Flat) { map = MapGenerator.GenerateFlatgrass(args.MapWidth, args.MapLength, args.MapHeight); } else { MapGenerator generator = new MapGenerator(args); map = generator.Generate(); } } catch (Exception ex) { Logger.Log(LogType.Error, "MapGenerator: Generation failed: {0}", ex); player.MessageNow("&WAn error occured while generating the map."); return; } if (fileName != null) { if (map.Save(fullFileName)) { player.MessageNow("Generation done. Saved to {0}", fileName); } else { player.Message("&WAn error occured while saving generated map to {0}", fileName); } } else { player.MessageNow("Generation done. Changing map..."); player.World.ChangeMap(map); } }
static void TPHandler( Player player, Command cmd ) { string name = cmd.Next(); if( name == null ) { CdTP.PrintUsage( player ); return; } if( cmd.Next() != null ) { cmd.Rewind(); int x, y, z; if( cmd.NextInt( out x ) && cmd.NextInt( out y ) && cmd.NextInt( out z ) ) { if( x <= -1024 || x >= 1024 || y <= -1024 || y >= 1024 || z <= -1024 || z >= 1024 ) { player.Message( "Coordinates are outside the valid range!" ); } else { player.previousLocation = player.Position; player.previousWorld = null; player.TeleportTo( new Position { X = (short)(x * 32 + 16), Y = (short)(y * 32 + 16), Z = (short)(z * 32 + 16), R = player.Position.R, L = player.Position.L } ); } } else { CdTP.PrintUsage( player ); } } else { if( name == "-" ) { if( player.LastUsedPlayerName != null ) { name = player.LastUsedPlayerName; } else { player.Message( "Cannot repeat player name: you haven't used any names yet." ); return; } } Player[] matches = Server.FindPlayers( player, name, true ); if( matches.Length == 1 ) { Player target = matches[0]; World targetWorld = target.World; if( targetWorld == null ) PlayerOpException.ThrowNoWorld( target ); if( targetWorld == player.World ) { player.previousLocation = player.Position; player.previousWorld = null; player.TeleportTo( target.Position ); } else { switch( targetWorld.AccessSecurity.CheckDetailed( player.Info ) ) { case SecurityCheckResult.Allowed: case SecurityCheckResult.WhiteListed: if( targetWorld.IsFull ) { player.Message( "Cannot teleport to {0}&S because world {1}&S is full.", target.ClassyName, targetWorld.ClassyName ); return; } player.StopSpectating(); player.previousLocation = player.Position; player.previousWorld = player.World; player.JoinWorld( targetWorld, WorldChangeReason.Tp, target.Position ); break; case SecurityCheckResult.BlackListed: player.Message( "Cannot teleport to {0}&S because you are blacklisted on world {1}", target.ClassyName, targetWorld.ClassyName ); break; case SecurityCheckResult.RankTooLow: player.Message( "Cannot teleport to {0}&S because world {1}&S requires {2}+&S to join.", target.ClassyName, targetWorld.ClassyName, targetWorld.AccessSecurity.MinRank.ClassyName ); break; // TODO: case PermissionType.RankTooHigh: } } } else if( matches.Length > 1 ) { player.MessageManyMatches( "player", matches ); } else { // Try to guess if player typed "/TP" instead of "/Join" World[] worlds = WorldManager.FindWorlds( player, name ); if( worlds.Length == 1 ) { player.LastUsedWorldName = worlds[0].Name; player.StopSpectating(); player.ParseMessage( "/Join " + worlds[0].Name, false, true ); } else { player.MessageNoPlayer( name ); } } } }
public void AskForConfirmation( Command cmd, string message, params object[] args ) { if( cmd == null ) throw new ArgumentNullException( "cmd" ); if( message == null ) throw new ArgumentNullException( "message" ); CommandToConfirm = cmd; CommandToConfirmDate = DateTime.UtcNow; Message( "{0} Type &H/ok&S to continue.", String.Format( message, args ) ); CommandToConfirm.Rewind(); }
// Parses message incoming from the player public void ParseMessage( string rawMessage, bool fromConsole ) { if( rawMessage == null ) throw new ArgumentNullException( "rawMessage" ); if( partialMessage != null ) { rawMessage = partialMessage + rawMessage; partialMessage = null; } switch( CommandManager.GetMessageType( rawMessage ) ) { case MessageType.Chat: { if( !Can( Permission.Chat ) ) return; if( Info.IsMuted ) { MutedMessage(); return; } if( DetectChatSpam() ) return; if( World != null && !World.FireSentMessageEvent( this, ref rawMessage ) || !Server.FireSentMessageEvent( this, ref rawMessage ) ) return; Info.LinesWritten++; Logger.Log( "{0}: {1}", LogType.GlobalChat, Name, rawMessage ); // 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 ); } if( Can( Permission.UseColorCodes ) && rawMessage.Contains( "%" ) ) { rawMessage = Color.ReplacePercentCodes( rawMessage ); } Server.SendToAllExceptIgnored( this, "{0}{1}: {2}", Console, GetClassyName(), Color.White, rawMessage ); } break; case MessageType.Command: { if( rawMessage.EndsWith( "//" ) ) { rawMessage = rawMessage.Substring( 0, rawMessage.Length - 1 ); } Logger.Log( "{0}: {1}", LogType.UserCommand, Name, rawMessage ); Command cmd = new Command( rawMessage ); LastCommand = cmd; CommandManager.ParseCommand( this, cmd, fromConsole ); } break; case MessageType.RepeatCommand: { if( LastCommand == null ) { Message( "No command to repeat." ); } else { LastCommand.Rewind(); Logger.Log( "{0}: repeat {1}", LogType.UserCommand, Name, LastCommand.Message ); Message( "Repeat: {0}", LastCommand.Message ); CommandManager.ParseCommand( this, LastCommand, fromConsole ); } } break; case MessageType.PrivateChat: { if( !Can( Permission.Chat ) ) return; if( Info.IsMuted ) { MutedMessage(); return; } if( DetectChatSpam() ) return; if( rawMessage.EndsWith( "//" ) ) { rawMessage = rawMessage.Substring( 0, rawMessage.Length - 1 ); } string otherPlayerName, messageText; if( rawMessage[1] == ' ' ) { otherPlayerName = rawMessage.Substring( 2, rawMessage.IndexOf( ' ', 2 ) - 2 ); messageText = rawMessage.Substring( rawMessage.IndexOf( ' ', 2 ) + 1 ); } else { otherPlayerName = rawMessage.Substring( 1, rawMessage.IndexOf( ' ' ) - 1 ); messageText = rawMessage.Substring( rawMessage.IndexOf( ' ' ) + 1 ); } if( messageText.Contains( "%" ) && Can( Permission.UseColorCodes ) ) { messageText = Color.ReplacePercentCodes( messageText ); } // first, find ALL players (visible and hidden) Player[] allPlayers = Server.FindPlayers( otherPlayerName ); // if there is more than 1 target player, exclude hidden players if( allPlayers.Length > 1 ) { allPlayers = Server.FindPlayers( this, otherPlayerName ); } if( allPlayers.Length == 1 ) { Player target = allPlayers[0]; if( target.IsIgnoring( Info ) ) { if( CanSee( target ) ) { MessageNow( "&WCannot PM {0}&W: you are ignored.", target.GetClassyName() ); } } else { Logger.Log( "{0} to {1}: {2}", LogType.PrivateChat, Name, target.Name, messageText ); target.Message( "{0}from {1}: {2}", Color.PM, Name, messageText ); if( CanSee( target ) ) { Message( "{0}to {1}: {2}", Color.PM, target.Name, messageText ); } else { NoPlayerMessage( otherPlayerName ); } } } else if( allPlayers.Length == 0 ) { NoPlayerMessage( otherPlayerName ); } else { ManyMatchesMessage( "player", allPlayers ); } } break; case MessageType.RankChat: { if( !Can( Permission.Chat ) ) return; if( Info.IsMuted ) { MutedMessage(); return; } if( DetectChatSpam() ) return; if( rawMessage.EndsWith( "//" ) ) { rawMessage = rawMessage.Substring( 0, rawMessage.Length - 1 ); } string rankName = rawMessage.Substring( 2, rawMessage.IndexOf( ' ' ) - 2 ); Rank rank = RankManager.FindRank( rankName ); if( rank != null ) { Logger.Log( "{0} to rank {1}: {2}", LogType.RankChat, Name, rank.Name, rawMessage ); string messageText = rawMessage.Substring( rawMessage.IndexOf( ' ' ) + 1 ); if( messageText.Contains( "%" ) && Can( Permission.UseColorCodes ) ) { messageText = Color.ReplacePercentCodes( messageText ); } string formattedMessage = String.Format( "{0}({1}{2}){3}{4}: {5}", rank.Color, (ConfigKey.RankPrefixesInChat.GetBool() ? rank.Prefix : ""), rank.Name, Color.PM, Name, messageText ); Server.SendToRank( this, formattedMessage, rank ); if( Info.Rank != rank ) { Message( formattedMessage ); } } else { Message( "No rank found matching \"{0}\"", rankName ); } } break; case MessageType.Confirmation: { if( CommandToConfirm != null ) { if( DateTime.UtcNow.Subtract( CommandToConfirmDate ).TotalSeconds < ConfirmationTimeout ) { CommandToConfirm.IsConfirmed = true; CommandManager.ParseCommand( this, CommandToConfirm, fromConsole ); CommandToConfirm = null; } else { MessageNow( "Confirmation timed out. Enter the command again." ); } } else { MessageNow( "There is no command to confirm." ); } } break; case MessageType.PartialMessage: partialMessage = rawMessage.Substring( 0, rawMessage.Length - 1 ); MessageNow( "Partial: &F{0}", partialMessage ); break; case MessageType.Invalid: { Message( "Unknown command." ); } break; } }
internal static void TP( Player player, Command cmd ) { if( player.Can( Permissions.Teleport ) ) { string name = cmd.Next(); if( name == null ) { player.Send( PacketWriter.MakeTeleport( 255, player.world.map.spawn ) ); } else { Player target = player.world.FindPlayer( name ); if( target != null ) { Position pos = target.pos; pos.x += 1; pos.y += 1; pos.h += 1; player.Send( PacketWriter.MakeTeleport( 255, pos ) ); } else if( cmd.Next() == null ) { player.NoPlayerMessage( name ); } else { cmd.Rewind(); int x, y, h; if( cmd.NextInt( out x ) && cmd.NextInt( out y ) && cmd.NextInt( out h ) ) { if( x < 0 || x > player.world.map.widthX || y < 0 || y > player.world.map.widthY || y < 0 || y > player.world.map.height ) { player.Message( "Specified coordinates are outside the map!" ); } else { player.pos.Set( x * 32 + 16, y * 32 + 16, h * 32 + 16, player.pos.r, player.pos.l ); player.Send( PacketWriter.MakeTeleport( 255, player.pos ) ); } } else { player.Message( "See " + Color.Help + "/help tp" + Color.Sys + " for information on using /tp" ); } } } } else { player.NoAccessMessage( Permissions.Teleport ); } }
internal static void Generate( Player player, Command cmd ) { if( !player.Can( Permissions.ManageWorlds ) ) { player.NoAccessMessage( Permissions.ManageWorlds ); return; } int wx, wy, height; if( !(cmd.NextInt( out wx ) && cmd.NextInt( out wy ) && cmd.NextInt( out height )) ) { if( player.world != null ) { wx = player.world.map.widthX; wy = player.world.map.widthY; height = player.world.map.height; } else { player.Message( "Usage: " + Color.Help + "/gen widthX widthY height type filename" ); return; } cmd.Rewind(); } string mode = cmd.Next(); string filename = cmd.Next(); if( mode == null || filename == null ) { player.Message( "Usage: " + Color.Help + "/gen widthX widthY height type filename" ); return; } filename += ".fcm"; int seed; if( !cmd.NextInt( out seed ) ) { seed = new Random().Next(); } Random rand = new Random( seed ); //player.Message( "Seed: " + Convert.ToBase64String( BitConverter.GetBytes( seed ) ) ); Map map = new Map( player.world, wx, wy, height ); map.spawn.Set( map.widthX / 2 * 32 + 16, map.widthY / 2 * 32 + 16, map.height * 32, 0, 0 ); DoGenerate( map, player, mode, filename, rand, false ); }
static void GenHandler( Player player, Command cmd ) { World playerWorld = player.World; Map oldMap = player.WorldMap; string themeName = cmd.Next(); string templateName; bool genOcean = false; bool genEmpty = false; bool noTrees = false; if( themeName == null ) { CdGenerate.PrintUsage( player ); return; } MapGenTheme theme = MapGenTheme.Forest; MapGenTemplate template = MapGenTemplate.Flat; // parse special template names (which do not need a theme) if( themeName.Equals( "ocean" ) ) { genOcean = true; } else if( themeName.Equals( "empty" ) ) { genEmpty = true; } else { templateName = cmd.Next(); if( templateName == null ) { CdGenerate.PrintUsage( player ); return; } // parse theme bool swapThemeAndTemplate = false; if( themeName.Equals( "grass", StringComparison.OrdinalIgnoreCase ) ) { theme = MapGenTheme.Forest; noTrees = true; } else if( templateName.Equals( "grass", StringComparison.OrdinalIgnoreCase ) ) { theme = MapGenTheme.Forest; noTrees = true; swapThemeAndTemplate = true; } else if( EnumUtil.TryParse( themeName, out theme, true ) ) { noTrees = (theme != MapGenTheme.Forest); } else if( EnumUtil.TryParse( templateName, out theme, true ) ) { noTrees = (theme != MapGenTheme.Forest); swapThemeAndTemplate = true; } else { player.Message( "Gen: Unrecognized theme \"{0}\". Available themes are: {1}", themeName, Enum.GetNames( typeof( MapGenTheme ) ).JoinToString() ); return; } // parse template if( swapThemeAndTemplate ) { if( !EnumUtil.TryParse( themeName, out template, true ) ) { player.Message( "Unrecognized template \"{0}\". Available templates are: {1}", themeName, Enum.GetNames( typeof( MapGenTemplate ) ).JoinToString() ); return; } } else { if( !EnumUtil.TryParse( templateName, out template, true ) ) { player.Message( "Unrecognized template \"{0}\". Available templates are: {1}", templateName, Enum.GetNames( typeof( MapGenTemplate ) ).JoinToString() ); return; } } } // parse map dimensions int mapWidth, mapLength, mapHeight; if( !(cmd.NextInt( out mapWidth ) && cmd.NextInt( out mapLength ) && cmd.NextInt( out mapHeight )) ) { if( playerWorld != null ) { // If map dimensions were not given, use current map's dimensions mapWidth = oldMap.Width; mapLength = oldMap.Length; mapHeight = oldMap.Height; } else { player.Message( "When used from console, /Gen requires map dimensions." ); CdGenerate.PrintUsage( player ); return; } cmd.Rewind(); cmd.Next(); cmd.Next(); } // Check map dimensions const string dimensionRecommendation = "Dimensions must be between 1 and 2047. " + "Recommended values: 16, 32, 64, 128, 256, 512, and 1024."; if( !Map.IsValidDimension( mapWidth ) ) { player.Message( "Cannot make map with width {0}. {1}", mapWidth, dimensionRecommendation ); return; } else if( !Map.IsValidDimension( mapLength ) ) { player.Message( "Cannot make map with length {0}. {1}", mapLength, dimensionRecommendation ); return; } else if( !Map.IsValidDimension( mapHeight ) ) { player.Message( "Cannot make map with height {0}. {1}", mapHeight, dimensionRecommendation ); return; } if( !cmd.IsConfirmed && (!Map.IsRecommendedDimension( mapWidth ) || !Map.IsRecommendedDimension( mapLength )) ) { player.Message( "&WThe map will have non-standard dimensions. " + "You may see glitched blocks or visual artifacts. " + "The only recommended map dimensions are: 16, 32, 64, 128, 256, 512, and 1024." ); } // check file/world name string fileName = cmd.Next(); string fullFileName = null; if( fileName == null ) { // replacing current world if( playerWorld == null ) { player.Message( "When used from console, /Gen requires FileName." ); CdGenerate.PrintUsage( player ); return; } if( !cmd.IsConfirmed ) { player.Confirm( cmd, "Replace this world's map with a generated one?" ); return; } } else { // saving to file fileName = fileName.Replace( Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar ); if( !fileName.EndsWith( ".fcm", StringComparison.OrdinalIgnoreCase ) ) { fileName += ".fcm"; } if( !Paths.IsValidPath( fileName ) ) { player.Message( "Invalid filename." ); return; } fullFileName = Path.Combine( Paths.MapPath, fileName ); if( !Paths.Contains( Paths.MapPath, fullFileName ) ) { player.MessageUnsafePath(); return; } string dirName = fullFileName.Substring( 0, fullFileName.LastIndexOf( Path.DirectorySeparatorChar ) ); if( !Directory.Exists( dirName ) ) { Directory.CreateDirectory( dirName ); } if( !cmd.IsConfirmed && File.Exists( fullFileName ) ) { player.Confirm( cmd, "The mapfile \"{0}\" already exists. Overwrite?", fileName ); return; } } // generating bool genFlatgrass = (theme == MapGenTheme.Forest && noTrees && template == MapGenTemplate.Flat); Map map; if( genEmpty ) { player.MessageNow( "Generating empty map..." ); map = MapGenerator.GenerateEmpty( mapWidth, mapLength, mapHeight ); } else if( genOcean ) { player.MessageNow( "Generating ocean map..." ); map = MapGenerator.GenerateOcean( mapWidth, mapLength, mapHeight ); } else if( genFlatgrass ) { player.MessageNow( "Generating flatgrass..." ); map = MapGenerator.GenerateFlatgrass( mapWidth, mapLength, mapHeight ); } else { // fancy generation MapGeneratorArgs args = MapGenerator.MakeTemplate( template ); if( theme == MapGenTheme.Desert ) { args.AddWater = false; } float ratio = mapHeight / (float)args.MapHeight; args.MapWidth = mapWidth; args.MapLength = mapLength; args.MapHeight = mapHeight; args.MaxHeight = (int)Math.Round( args.MaxHeight * ratio ); args.MaxDepth = (int)Math.Round( args.MaxDepth * ratio ); args.SnowAltitude = (int)Math.Round( args.SnowAltitude * ratio ); args.Theme = theme; args.AddTrees = !noTrees; try { if( theme == MapGenTheme.Forest && noTrees ) { player.MessageNow( "Generating Grass {0}...", template ); } else { player.MessageNow( "Generating {0} {1}...", theme, template ); } MapGenerator generator = new MapGenerator( args ); map = generator.Generate(); } catch( Exception ex ) { Logger.Log( LogType.Error, "MapGenerator: Generation failed: {0}", ex ); player.Message( "&WAn error occured while generating the map." ); return; } } if( fileName != null ) { if( map.Save( fullFileName ) ) { player.Message( "Generation done. Saved to {0}", fileName ); } else { player.Message( "&WAn error occured while saving generated map to {0}", fileName ); } } else { if( playerWorld == null ) PlayerOpException.ThrowNoWorld( player ); player.MessageNow( "Generation done. Changing map..." ); playerWorld.MapChangedBy = player.Name; playerWorld.ChangeMap( map ); } }
// Parses message incoming from the player public void ParseMessage(string rawMessage, bool fromConsole) { if (rawMessage == null) { throw new ArgumentNullException("rawMessage"); } if (partialMessage != null) { rawMessage = partialMessage + rawMessage; partialMessage = null; } switch (CommandManager.GetMessageType(rawMessage)) { case MessageType.Chat: { if (!Can(Permission.Chat)) { return; } if (Info.IsMuted) { MutedMessage(); return; } if (DetectChatSpam()) { return; } if (World != null && !World.FireSentMessageEvent(this, ref rawMessage) || !Server.FireSentMessageEvent(this, ref rawMessage)) { return; } Info.LinesWritten++; Logger.Log("{0}: {1}", LogType.GlobalChat, Name, rawMessage); // 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); } if (Can(Permission.UseColorCodes) && rawMessage.Contains("%")) { rawMessage = Color.ReplacePercentCodes(rawMessage); } Server.SendToAllExceptIgnored(this, "{0}{1}: {2}", Console, GetClassyName(), Color.White, rawMessage); } break; case MessageType.Command: { if (rawMessage.EndsWith("//")) { rawMessage = rawMessage.Substring(0, rawMessage.Length - 1); } Logger.Log("{0}: {1}", LogType.UserCommand, Name, rawMessage); Command cmd = new Command(rawMessage); LastCommand = cmd; CommandManager.ParseCommand(this, cmd, fromConsole); } break; case MessageType.RepeatCommand: { if (LastCommand == null) { Message("No command to repeat."); } else { LastCommand.Rewind(); Logger.Log("{0}: repeat {1}", LogType.UserCommand, Name, LastCommand.Message); Message("Repeat: {0}", LastCommand.Message); CommandManager.ParseCommand(this, LastCommand, fromConsole); } } break; case MessageType.PrivateChat: { if (!Can(Permission.Chat)) { return; } if (Info.IsMuted) { MutedMessage(); return; } if (DetectChatSpam()) { return; } if (rawMessage.EndsWith("//")) { rawMessage = rawMessage.Substring(0, rawMessage.Length - 1); } string otherPlayerName, messageText; if (rawMessage[1] == ' ') { otherPlayerName = rawMessage.Substring(2, rawMessage.IndexOf(' ', 2) - 2); messageText = rawMessage.Substring(rawMessage.IndexOf(' ', 2) + 1); } else { otherPlayerName = rawMessage.Substring(1, rawMessage.IndexOf(' ') - 1); messageText = rawMessage.Substring(rawMessage.IndexOf(' ') + 1); } if (messageText.Contains("%") && Can(Permission.UseColorCodes)) { messageText = Color.ReplacePercentCodes(messageText); } // first, find ALL players (visible and hidden) Player[] allPlayers = Server.FindPlayers(otherPlayerName); // if there is more than 1 target player, exclude hidden players if (allPlayers.Length > 1) { allPlayers = Server.FindPlayers(this, otherPlayerName); } if (allPlayers.Length == 1) { Player target = allPlayers[0]; if (target.IsIgnoring(Info)) { if (CanSee(target)) { MessageNow("&WCannot PM {0}&W: you are ignored.", target.GetClassyName()); } } else { Logger.Log("{0} to {1}: {2}", LogType.PrivateChat, Name, target.Name, messageText); target.Message("{0}from {1}: {2}", Color.PM, Name, messageText); if (CanSee(target)) { Message("{0}to {1}: {2}", Color.PM, target.Name, messageText); } else { NoPlayerMessage(otherPlayerName); } } } else if (allPlayers.Length == 0) { NoPlayerMessage(otherPlayerName); } else { ManyMatchesMessage("player", allPlayers); } } break; case MessageType.RankChat: { if (!Can(Permission.Chat)) { return; } if (Info.IsMuted) { MutedMessage(); return; } if (DetectChatSpam()) { return; } if (rawMessage.EndsWith("//")) { rawMessage = rawMessage.Substring(0, rawMessage.Length - 1); } string rankName = rawMessage.Substring(2, rawMessage.IndexOf(' ') - 2); Rank rank = RankManager.FindRank(rankName); if (rank != null) { Logger.Log("{0} to rank {1}: {2}", LogType.RankChat, Name, rank.Name, rawMessage); string messageText = rawMessage.Substring(rawMessage.IndexOf(' ') + 1); if (messageText.Contains("%") && Can(Permission.UseColorCodes)) { messageText = Color.ReplacePercentCodes(messageText); } string formattedMessage = String.Format("{0}({1}{2}){3}{4}: {5}", rank.Color, (ConfigKey.RankPrefixesInChat.GetBool() ? rank.Prefix : ""), rank.Name, Color.PM, Name, messageText); Server.SendToRank(this, formattedMessage, rank); if (Info.Rank != rank) { Message(formattedMessage); } } else { Message("No rank found matching \"{0}\"", rankName); } } break; case MessageType.Confirmation: { if (CommandToConfirm != null) { if (DateTime.UtcNow.Subtract(CommandToConfirmDate).TotalSeconds < ConfirmationTimeout) { CommandToConfirm.IsConfirmed = true; CommandManager.ParseCommand(this, CommandToConfirm, fromConsole); CommandToConfirm = null; } else { MessageNow("Confirmation timed out. Enter the command again."); } } else { MessageNow("There is no command to confirm."); } } break; case MessageType.PartialMessage: partialMessage = rawMessage.Substring(0, rawMessage.Length - 1); MessageNow("Partial: &F{0}", partialMessage); break; case MessageType.Invalid: { Message("Unknown command."); } break; } }
static void Restart( Player player, Command cmd ) { int delay; if( !cmd.NextInt( out delay ) ) { delay = 5; cmd.Rewind(); } string reason = cmd.Next(); Server.SendToAll( "&WServer restarting in {0} seconds.", delay ); if( reason == null ) { Logger.Log( "{0} restarted the server ({1} second delay).", LogType.UserActivity, player.Name, delay ); ShutdownParams sp = new ShutdownParams( ShutdownReason.Restarting, delay, true, true ); Server.Shutdown( sp, false ); } else { Logger.Log( "{0} restarted the server ({1} second delay). Reason: {2}", LogType.UserActivity, player.Name, delay, reason ); ShutdownParams sp = new ShutdownParams( ShutdownReason.Restarting, delay, true, true, reason, player ); Server.Shutdown( sp, false ); } }
static void Shutdown( Player player, Command cmd ) { int delay; if( !cmd.NextInt( out delay ) ) { delay = 5; cmd.Rewind(); } string reason = cmd.NextAll(); if( reason.Equals( "abort", StringComparison.OrdinalIgnoreCase ) ) { if( Server.CancelShutdown() ) { Logger.Log( "Shutdown aborted by {0}.", LogType.UserActivity, player.Name ); Server.SendToAll( "&WShutdown aborted by {0}", player.GetClassyName() ); } else { player.MessageNow( "Cannot abort shutdown - too late." ); } return; } Server.SendToAll( "&WServer shutting down in {0} seconds.", delay ); if( String.IsNullOrEmpty( reason ) ) { Logger.Log( "{0} shut down the server ({1} second delay).", LogType.UserActivity, player.Name, delay ); ShutdownParams sp = new ShutdownParams( ShutdownReason.ShuttingDown, delay, true, false ); Server.Shutdown( sp, false ); } else { Server.SendToAll( "&WShutdown reason: {0}", reason ); Logger.Log( "{0} shut down the server ({1} second delay). Reason: {2}", LogType.UserActivity, player.Name, delay, reason ); ShutdownParams sp = new ShutdownParams( ShutdownReason.ShuttingDown, delay, true, false, reason, player ); Server.Shutdown( sp, false ); } }