private void PacketReceived(Packet packet)
 {
     if (OnPacketReceived != null)
         OnPacketReceived(null, new PacketEventArgs(packet));
 }
Beispiel #2
0
        /// <summary>
        /// Sends the given packet to the connected Minecraft server.
        /// </summary>
        /// <param name="packet">The packet to send</param>
        public void SendPacket(Packet packet)
        {
            //_log.Debug("Sending packet (ID: " + packet.Type + ")");

            var type = packet.Type;
            var packetID = (byte) packet.Type;

            switch (type)
            {
                case PacketType.KeepAlive:
                    {
                        var pack = (KeepAlivePacket) packet;
                        //_log.Debug("Writing KeepAlive packet (" + pack.KeepAliveID + ")...");
                        _tools.WriteByte(packetID);
                        _tools.WriteInt32(pack.KeepAliveID);
                    }
                    break;
                case PacketType.LoginRequest:
                    {
                        //_log.Debug("Writing Login Request packet...");
                        var pack = (LoginRequestPacketCS)packet;
                        _tools.WriteByte(packetID);
                        _tools.WriteInt32(pack.ProtocolVersion);
                        _tools.WriteString(pack.Username);
                        _tools.WriteString(String.Empty);   		// Not Used
                        _tools.WriteInt64(0);		        		// Not Used
                        _tools.WriteInt32(0);						// Not Used
                        _tools.WriteByte(0);						// Not Used
                        _tools.WriteByte(0);						// Not Used
                        _tools.WriteByte(0);						// Not Used
                        _tools.WriteByte(0);						// Not Used
                    }
                    break;
                case PacketType.Handshake:
                    {
                        //_log.Debug("Writing Handshake packet.");
                        var pack = (HandshakePacketCS)packet;
                        _tools.WriteByte(packetID);
                        _tools.WriteString(pack.UsernameAndHost);
                    }
                    break;
                case PacketType.ChatMessage:
                    {
                        var pack = (ChatMessagePacket)packet;
                        _tools.WriteByte(packetID);
                        _tools.WriteString(pack.Message);
                    }
                    break;
                case PacketType.UseEntity:
                    {
                        var pack = (UseEntityPacket)packet;
                        _tools.WriteByte(packetID);
                        _tools.WriteInt32(pack.AttackerID);
                        _tools.WriteInt32(pack.TargetID);
                        _tools.WriteBoolean(pack.IsLeftClick);
                    }
                    break;
                case PacketType.DisconnectKick:
                    {
                         var pack = (DisconnectKickPacket)packet;
                        _tools.WriteByte(packetID);
                        _tools.WriteString(pack.Reason);
                    }
                    break;
                default:
                    _log.Warn(packet.Type + " has not been implemented in SendPacket, aborting...");
                    return;
            }

            //_log.Debug("Sending packet...");
            _stream.Flush();
            //_log.Debug("Packet sent!");
        }
Beispiel #3
0
 /// <summary>
 /// Initialize a new instance of <see cref="PacketEventArgs" />.
 /// </summary>
 /// <param name="packet"></param>
 internal PacketEventArgs(Packet packet)
 {
     Packet = packet;
 }