private ReplacePercentColorCodes ( [ message, bool allowNewlines ) : string | ||
message | [ | |
allowNewlines | bool | |
Результат | string |
public static string StripColors([NotNull] string message, bool replacePercents) { if (message == null) { throw new ArgumentNullException("message"); } if (replacePercents) { message = Chat.ReplacePercentColorCodes(message, false); } int start = message.IndexOf('&'); if (start == -1) { return(message); } int lastInsert = 0; StringBuilder output = new StringBuilder(message.Length); while (start != -1) { output.Append(message, lastInsert, start - lastInsert); lastInsert = Math.Min(start + 2, message.Length); start = message.IndexOf('&', lastInsert); } output.Append(message, lastInsert, message.Length - lastInsert); return(output.ToString()); }
bool HandleOpcode(byte opcode) { switch (opcode) { case (byte)OpCode.Handshake: return(true); case 0xFE: // SMP ping packet id // 1.4 - 1.6 string name = Chat.ReplacePercentColorCodes(ConfigKey.ServerName.GetString(), false).Replace('&', '§'); string data = "§1\078\00.30c\0§E" + name + '\0' + Server.CountPlayers(false) + '\0' + ConfigKey.MaxPlayers.GetInt(); SendOldSMPKick(data); return(false); case 0x02: case 0xFA: SendOldSMPKick("§EPlease join us at §9http://classicube.net/"); Logger.Log(LogType.Warning, "Player.LoginSequence: A player tried connecting with Minecraft Beta client from {0}.", IP); // ignore SMP pings return(false); case (byte)'G': // WoM GET requests return(false); default: if (CheckModernSMP(opcode)) { return(false); } Logger.Log(LogType.Error, "Player.LoginSequence: Unexpected op code in the first packet from {0}: {1}.", IP, opcode); KickNow("Incompatible client, or a network error.", LeaveReason.ProtocolViolation); return(false); } }
bool CheckModernSMP(byte length) { // Ensure that we have enough bytes for packet data if (client.Available < length || reader.ReadByte() != 0) { return(false); } int protocolVer = ReadVarInt(); // protocol version int hostLen = ReadVarInt(); if (protocolVer == -1 || hostLen == -1) { return(false); } if (client.Available < hostLen) { return(false); } reader.ReadBytes(hostLen); // hostname if (client.Available < 3) { return(false); } reader.ReadInt16(); // port byte nextState = reader.ReadByte(); string data = null; if (nextState == 1) // status state { string name = Chat.ReplacePercentColorCodes(ConfigKey.ServerName.GetString(), false).Replace('&', '§'); data = @"{""version"": { ""name"": ""0.30c"", ""protocol"": " + protocolVer + @" }, ""players"": " + ServiceStack.Text.JsonSerializer.SerializeToString(new Players()) + @", ""description"": {""text"": ""§6" + name + "\n§EPlease join us at §9http://ClassiCube.net/" + @"""},""favicon"": """ + CheckForFavicon() + @"""}"; } else if (nextState == 2) // game state { data = @"{""text"": ""§EPlease join us at §9http://classicube.net/""}"; Logger.Log(LogType.Warning, "Player.LoginSequence: A player tried connecting with Minecraft premium client from {0}.", IP); } if (data == null) { return(false); } int strLength = Encoding.UTF8.GetByteCount(data); int strLength_S = VarIntBytes(strLength); int dataLength = 1 + strLength_S + strLength; int dataLength_S = VarIntBytes(dataLength); byte[] packet = new byte[dataLength_S + dataLength]; WriteVarInt(dataLength, packet, 0); packet[dataLength_S] = 0; // opcode WriteVarInt(strLength, packet, dataLength_S + 1); Encoding.UTF8.GetBytes(data, 0, data.Length, packet, strLength_S + dataLength_S + 1); writer.Write(packet); BytesSent += packet.Length; if (IP.IsLocal()) { System.Threading.Thread.Sleep(10); //localhost gets disconnected too quickly causing connection error client side } writer.Flush(); return(true); }
/// <summary> Parses a message on behalf of this player. </summary> /// <param name="rawMessage"> Message to parse. </param> /// <param name="fromConsole"> Whether the message originates from console. </param> /// <exception cref="ArgumentNullException"> rawMessage is null. </exception> public void ParseMessage([NotNull] string rawMessage, bool fromConsole) { if (rawMessage == null) { throw new ArgumentNullException("rawMessage"); } // handle canceling selections and partial messages if (rawMessage.StartsWith("/nvm", StringComparison.OrdinalIgnoreCase) || rawMessage.StartsWith("/cancel", StringComparison.OrdinalIgnoreCase)) { if (partialMessage != null) { MessageNow("Partial message cancelled."); partialMessage = null; } else if (IsMakingSelection) { SelectionCancel(); MessageNow("Selection cancelled."); } else { MessageNow("There is currently nothing to cancel."); } return; } if (partialMessage != null) { rawMessage = partialMessage + rawMessage; partialMessage = null; } // replace %-codes with &-codes if (Can(Permission.UseColorCodes)) { rawMessage = Chat.ReplacePercentColorCodes(rawMessage, true); } // replace emotes if (Can(Permission.UseEmotes)) { rawMessage = Chat.ReplaceEmoteKeywords(rawMessage); } rawMessage = Chat.UnescapeBackslashes(rawMessage); switch (Chat.GetRawMessageType(rawMessage)) { case RawMessageType.Chat: HandleChatMessage(rawMessage); break; case RawMessageType.Command: HandleCommandMessage(rawMessage, fromConsole); break; case RawMessageType.PrivateChat: HandlePrivateChatMessage(rawMessage); break; case RawMessageType.RankChat: HandleRankChatMessage(rawMessage); break; case RawMessageType.RepeatCommand: if (LastCommand == null) { Message("No command to repeat."); } else { if (Info.IsFrozen && (LastCommand.Descriptor == null || !LastCommand.Descriptor.UsableByFrozenPlayers)) { MessageNow("&WYou cannot use this command while frozen."); return; } LastCommand.Rewind(); Logger.Log(LogType.UserCommand, "{0} repeated: {1}", Name, LastCommand.RawMessage); Message("Repeat: {0}", LastCommand.RawMessage); SendToSpectators(LastCommand.RawMessage); CommandManager.ParseCommand(this, LastCommand, fromConsole); } break; case RawMessageType.Confirmation: if (Info.IsFrozen) { MessageNow("&WYou cannot use any commands while frozen."); return; } if (ConfirmCallback != null) { if (DateTime.UtcNow.Subtract(ConfirmRequestTime) < ConfirmationTimeout) { Logger.Log(LogType.UserCommand, "{0}: /ok", Name); SendToSpectators("/ok"); ConfirmCallback(this, ConfirmParameter, fromConsole); ConfirmCancel(); } else { MessageNow("Confirmation timed out. Enter the command again."); } } else { MessageNow("There is no command to confirm."); } break; case RawMessageType.PartialMessage: partialMessage = rawMessage.Substring(0, rawMessage.Length - 1); MessageNow("Partial: &F{0}", partialMessage); break; case RawMessageType.Invalid: MessageNow("Could not parse message."); break; } }