/// <summary> /// Main IO loop, handles the networking of the bot. /// </summary> private void NetworkLoop() { if (isMoving) { if (followTarget != null) { Player p = Server.FindPlayers(followTarget.Name, false)[0]; Logger.LogToConsole("Chaning pos to " + p.Name + " at " + p.Position.ToString()); NewPosition = p.Position; beganMoving = false; } //move bot 1 block after every period if (timeCheck.ElapsedMilliseconds > period) { Move(); timeCheck.Restart(); } } //If bot is not flying, drop down to nearest solid block if (!isFlying) { //Drop(); } }
internal static Player FindPlayerMatches(Player player, string namePart, SearchOptions options) { // Repeat last-used player name if (namePart == "-") { if (player.LastUsedPlayerName != null) { namePart = player.LastUsedPlayerName; } else { player.Message("Cannot repeat player name: you haven't used any names yet."); return null; } } // in case someone tries to use the "!" prefix in an online-only search if (namePart.Length > 0 && namePart[0] == '!') namePart = namePart.Substring(1); // Make sure player name is valid if (!Player.ContainsValidCharacters(namePart)) { player.MessageInvalidPlayerName(namePart); return null; } Player[] matches = Server.FindPlayers(player, namePart, options); if (matches.Length == 0) { player.MessageNoPlayer(namePart); return null; } else if (matches.Length > 1) { player.MessageManyMatches("player", matches); return null; } else { player.LastUsedPlayerName = matches[0].Name; return matches[0]; } }
// 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; } }
public static bool HandlePM(string nick, string userNick, string rawMessage) { nick = nick.ToLower(); if (!(rawMessage[0] == '@' || rawMessage.CaselessStarts(nick + " @"))) { return(false); } if (DateTime.UtcNow.Subtract(lastIrcCommand).TotalSeconds <= 5) { return(true); } int start = rawMessage[0] == '@' ? 1 : nick.Length + 2; rawMessage = rawMessage.Substring(start); string[] parts = rawMessage.Split(trimChars, 2); if (parts.Length == 1) { IRC.SendChannelMessage("Please specify a message to send."); return(true); } string name = parts[0], contents = parts[1]; // first, find ALL players (visible and hidden) Player[] matches = Server.FindPlayers(name, SearchOptions.IncludeHidden); // if there is more than 1 target player, exclude hidden players if (matches.Length > 1) { matches = Server.FindPlayers(name, SearchOptions.Default); } if (matches.Length == 1) { Player target = matches[0]; if (target.Info.ReadIRC && !target.IsDeaf) { Chat.IRCSendPM(userNick, target, contents); lastIrcCommand = DateTime.UtcNow; } if (target.Info.IsHidden) { // message was sent to a hidden player IRC.SendChannelMessage("No players found matching \"" + Bold + name + Reset + "\""); lastIrcCommand = DateTime.UtcNow; } else { // message was sent normally if (!target.Info.ReadIRC) { if (!target.Info.IsHidden) { IRC.SendChannelMessage("&WCannot PM " + Bold + target.ClassyName + Reset + "&W: they have IRC ignored."); } } else if (target.IsDeaf) { IRC.SendChannelMessage("&WCannot PM " + Bold + target.ClassyName + Reset + "&W: they are currently deaf."); } else { IRC.SendChannelMessage("to " + Bold + target.Name + Reset + ": " + contents); } lastIrcCommand = DateTime.UtcNow; } } else if (matches.Length == 0) { IRC.SendChannelMessage("No players found matching \"" + Bold + name + Reset + "\""); } else { string list = matches.Take(15).JoinToString(", ", p => p.ClassyName); if (matches.Length > 15) { IRC.SendChannelMessage("More than " + Bold + matches.Length + Reset + " players matched: " + list); } else { IRC.SendChannelMessage("More than one player matched: " + list); } lastIrcCommand = DateTime.UtcNow; } return(true); }
void HandlePrivateChatMessage([NotNull] string rawMessage) { if (rawMessage == null) { throw new ArgumentNullException("rawMessage"); } if (!Can(Permission.Chat)) { return; } if (Info.IsMuted) { MessageMuted(); 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 (otherPlayerName == "-") { if (LastUsedPlayerName != null) { otherPlayerName = LastUsedPlayerName; } else { Message("Cannot repeat player name: you haven't used any names yet."); return; } } // first, find ALL players (visible and hidden) Player[] allPlayers = Server.FindPlayers(otherPlayerName, SearchOptions.Default); // if there is more than 1 target player, exclude hidden players if (allPlayers.Length > 1) { allPlayers = Server.FindPlayers(this, otherPlayerName, SearchOptions.ReturnSelfIfOnlyMatch); } switch (allPlayers.Length) { case 0: MessageNoPlayer(otherPlayerName); break; case 1: { Player target = allPlayers[0]; if (target == this) { Message("Trying to talk to yourself?"); return; } bool messageSent = false; if (target.CanHear(this)) { messageSent = Chat.SendPM(this, target, messageText); // Echo this message to spectators, // excluding the PM target, and anyone from whom the target is hiding. Server.Players .Where(p => p.spectatedPlayer == this && p != target && p.CanSee(target)) .Message("[Spectate]: &Fto {0}&F: {1}", target.ClassyName, messageText); } if (!CanSee(target)) { // message was sent to a hidden player MessageNoPlayer(otherPlayerName); if (messageSent) { Info.DecrementMessageWritten(); } } else { // message was sent normally LastUsedPlayerName = target.Name; if (target.IsIgnoring(Info)) { if (CanSee(target)) { MessageNow("&WCannot PM {0}&W: you are ignored.", target.ClassyName); } } else if (target.IsDeaf) { MessageNow("Cannot PM {0}&S: they are currently deaf.", target.ClassyName); } else { MessageNow("&Pto {0}: {1}", target.Name, messageText); } } } break; default: MessageManyMatches("player", allPlayers); break; } }