Ejemplo n.º 1
0
 public NetworkHandler(MinecraftClient mc) {
     _mainMc = mc;
     LoginHandlers = new PacketHandler[3];
     PlayHandlers = new PacketHandler[65];
     StatusHandlers = new PacketHandler[2];
     PopulateLists();
 } 
Ejemplo n.º 2
0
 public NetworkHandler(MinecraftClient mc)
 {
     _mainMc        = mc;
     LoginHandlers  = new PacketHandler[3];
     PlayHandlers   = new PacketHandler[65];
     StatusHandlers = new PacketHandler[2];
     PopulateLists();
 }
Ejemplo n.º 3
0
        static void Main(string[] args) {
            var MinecraftServer = new MinecraftClient(args[0], int.Parse(args[1]), "USERNAME HERE", "PASSWORD HERE", true);
            MinecraftServer.ServerState = 2;

            MinecraftServer.Message += (sender, message, name) => {
                Console.WriteLine("<" + name + "> " + message);
            };

            //MinecraftServer.DebugMessage += (sender, message) => {
            //    Console.WriteLine("[DEBUG][" + sender.ToString() + "] " + message);
            //};

            MinecraftServer.LoginFailure += (sender, message) => {
                Console.WriteLine("Login Error: " + message);
            };

            MinecraftServer.ErrorMessage += (sender, message) => {
                Console.WriteLine("[ERROR][" + sender.ToString() + "] " + message);
            };

            MinecraftServer.InfoMessage += (sender, message) => {
                Console.WriteLine("[INFO][" + sender.ToString() + "] " + message);
            };

            MinecraftServer.PlayerRespawned += () => {
                Console.WriteLine("[Info] You respawned!");
            };

            if (MinecraftServer.VerifyNames)
                MinecraftServer.Login();

            MinecraftServer.Connect();

            string command;

            do {
                command = Console.ReadLine();

                if (command.StartsWith("say ")) 
                    MinecraftServer.SendChat(command.Substring(4));

                if (command.StartsWith("respawn")) {
                    MinecraftServer.Respawn();
                }
            } while (command != "quit");

            MinecraftServer.Disconnect();

            Console.ReadKey();
        }
Ejemplo n.º 4
0
        public void HandleKeepAlive(MinecraftClient client, IPacket packet) {
            var KA = (CBKeepAlive)packet;

            var KAS = new SBKeepAlive();
            KAS.KeepAliveID = KA.KeepAliveID;
            KAS.Write(client.nh.wSock);
        }
Ejemplo n.º 5
0
        static void Main(string[] args) {
			string serverIp = "127.0.0.1";
			int serverPort = 25565;
			string userName = "******";
			string userPassword = null;

			OptionSet p = new OptionSet ()
				.Add ("ip=", v => serverIp = v)
				.Add ("port=", v => int.Parse (v))
				.Add ("user="******"pass", v => userPassword = ReadPassword());

			p.Parse (args);

			var MinecraftServer = new MinecraftClient(serverIp, serverPort, userName, userPassword, !String.IsNullOrWhiteSpace(userPassword));
            MinecraftServer.ServerState = 2;

            MinecraftServer.Message += (sender, message, name) => {
                Console.WriteLine("<" + name + "> " + message);
            };

            //MinecraftServer.DebugMessage += (sender, message) => {
            //    Console.WriteLine("[DEBUG][" + sender.ToString() + "] " + message);
            //};

            MinecraftServer.LoginFailure += (sender, message) => {
                Console.WriteLine("Login Error: " + message);
            };

            MinecraftServer.ErrorMessage += (sender, message) => {
                Console.WriteLine("[ERROR][" + sender.ToString() + "] " + message);
            };

            MinecraftServer.InfoMessage += (sender, message) => {
                Console.WriteLine("[INFO][" + sender.ToString() + "] " + message);
            };

            MinecraftServer.PlayerRespawned += () => {
                Console.WriteLine("[Info] You respawned!");
            };

            if (MinecraftServer.VerifyNames)
                MinecraftServer.Login();

            MinecraftServer.Connect();

            string command;

            do {
                command = Console.ReadLine();

                if (command.StartsWith("say ")) 
                    MinecraftServer.SendChat(command.Substring(4));

                if (command.StartsWith("respawn")) {
                    MinecraftServer.Respawn();
                }
            } while (command != "quit");

            MinecraftServer.Disconnect();

            Console.ReadKey();
        }
Ejemplo n.º 6
0
        public void HandleEntityHeadLook(MinecraftClient client, IPacket packet) {
            var myPacket = (CBEntityHeadLook)packet;

            if (client.MinecraftWorld != null) {
                int eIndex = client.MinecraftWorld.GetEntityById(myPacket.EntityID);

                if (eIndex != -1)
                    client.MinecraftWorld.Entities[eIndex].headPitch = myPacket.HeadYaw;
            }

            client.RaiseEntityHeadLookChanged(myPacket.EntityID, myPacket.HeadYaw);
        }
Ejemplo n.º 7
0
        public void HandlePlayerPositionAndLook(MinecraftClient client, IPacket packet) {
            var myPacket = (CBPlayerPositionAndLook)packet;

            if (client.ThisPlayer == null)
                client.ThisPlayer = new Player();

            client.ThisPlayer.location.x = myPacket.X;
            client.ThisPlayer.location.y = myPacket.Y;
            client.ThisPlayer.location.z = myPacket.Z;
            client.ThisPlayer.look[0] = myPacket.Yaw;
            client.ThisPlayer.look[1] = myPacket.Pitch;
            client.ThisPlayer.onGround = myPacket.OnGround;

            client.RaiseLocationChanged();

            var sending = new SBPlayerPositionAndLook();
            sending.X = client.ThisPlayer.location.x;
            sending.FeetY = client.ThisPlayer.location.y - 1.620;
            sending.HeadY = client.ThisPlayer.location.y;
            sending.Z = client.ThisPlayer.location.z;
            sending.Yaw = client.ThisPlayer.look[0];
            sending.Pitch = client.ThisPlayer.look[1];
            sending.OnGround = client.ThisPlayer.onGround;
            sending.Write(client.nh.wSock);

        }
Ejemplo n.º 8
0
        public void HandleRemoveEntityEffect(MinecraftClient client, IPacket packet) {
            var myPacket = (CBRemoveEntityEffect)packet;

            if (client.MinecraftWorld != null) {
                int eIndex = client.MinecraftWorld.GetEntityById(myPacket.EntityID);

                if (eIndex != -1) {
                    client.MinecraftWorld.Entities[eIndex].status = myPacket.EffectID;
                    client.RaiseEntityStatus(myPacket.EntityID);
                }
            }
        }
Ejemplo n.º 9
0
        public void HandleMultiBlockChange(MinecraftClient client, IPacket packet) {
            var myPacket = (CBMultiBlockChange)packet;
            int chunkID = client.MinecraftWorld.GetChunk(myPacket.ChunkX, myPacket.ChunkZ);

            if (chunkID == -1) {
                client.RaiseError(this, "Attempted to access uninitialized chunk");
                return;
            }

            var thisChunk = client.MinecraftWorld.worldChunks[chunkID];

            for (int i = 0; i < myPacket.Recordcount - 1; i++) {
                var thisRecord = myPacket.Records[i];

                thisChunk.UpdateBlock(thisRecord.X, thisRecord.Y, thisRecord.Z, thisRecord.BlockID);
                thisChunk.SetBlockData(thisRecord.X, thisRecord.Y, thisRecord.Z, thisRecord.Metadata);
            }

            client.RaiseMultiBlockChange(myPacket.ChunkX, myPacket.ChunkX);
        }
Ejemplo n.º 10
0
 //TODO: Particle
 public void HandlePlayerAbilities(MinecraftClient client, IPacket packet) {
     var myPacket = (CBPlayerAbilities)packet;
     client.ThisPlayer.flyingSpeed = myPacket.Flyingspeed;
     client.ThisPlayer.WalkingSpeed = myPacket.Walkingspeed;
 }
Ejemplo n.º 11
0
        public void HandleEntityRelMove(MinecraftClient client, IPacket packet) {
            var myPacket = (CBEntityRelativeMove)packet;

            if (client.MinecraftWorld != null) {
                int eIndex = client.MinecraftWorld.GetEntityById(myPacket.EntityID);

                if (eIndex != -1) {
                    client.MinecraftWorld.Entities[eIndex].Location.x += (myPacket.DX * 32);
                    client.MinecraftWorld.Entities[eIndex].Location.y += (myPacket.DY * 32);
                    client.MinecraftWorld.Entities[eIndex].Location.z += (myPacket.DZ * 32);
                }
            }

            client.RaiseEntityRelMove(myPacket.EntityID, myPacket.DX, myPacket.DY, myPacket.DZ);
        }
Ejemplo n.º 12
0
        public void HandleEntityStatus(MinecraftClient client, IPacket packet) {
            var myPacket = (CBEntityStatus)packet;

            if (client.MinecraftWorld != null) {
                int eIndex = client.MinecraftWorld.GetEntityById(myPacket.EntityID);

                if (eIndex != -1)
                    client.MinecraftWorld.Entities[eIndex].status = myPacket.EntityStatus;
            }

            client.RaiseEntityStatus(myPacket.EntityID);
        }
Ejemplo n.º 13
0
 public void HandleEntityMetadata(MinecraftClient client, IPacket packet) {
     //TODO:
     // -- This needs to be written
 }
Ejemplo n.º 14
0
        public void HandleEntityProperties(MinecraftClient client, IPacket packet) {
            var myPacket = (CBEntityProperties)packet;

            //TODO: This
        }
Ejemplo n.º 15
0
        public void HandleLookEntityRelMove(MinecraftClient client, IPacket packet) {
            var myPacket = (CBEntityLookandRelativeMove)packet;

            if (client.MinecraftWorld != null) {
                int eIndex = client.MinecraftWorld.GetEntityById(myPacket.EntityID);

                if (eIndex != -1) {
                    client.MinecraftWorld.Entities[eIndex].Location.x += (myPacket.DX * 32);
                    client.MinecraftWorld.Entities[eIndex].Location.y += (myPacket.DY * 32);
                    client.MinecraftWorld.Entities[eIndex].Location.z += (myPacket.DZ * 32);
                    client.MinecraftWorld.Entities[eIndex].yaw = myPacket.Yaw;
                    client.MinecraftWorld.Entities[eIndex].pitch = myPacket.Pitch;
                }
            }

            client.RaiseEntityRelMove(myPacket.EntityID, myPacket.DX * 32, myPacket.DY * 32, myPacket.DZ * 32);
            client.RaiseEntityLookChanged(myPacket.EntityID, myPacket.Yaw, myPacket.Pitch);
        }
Ejemplo n.º 16
0
        public void HandleEntityLook(MinecraftClient client, IPacket packet) {
            var myPacket = (CBEntityLook)packet;

            if (client.MinecraftWorld != null) {
                int eIndex = client.MinecraftWorld.GetEntityById(myPacket.EntityID);

                if (eIndex != -1) {
                    client.MinecraftWorld.Entities[eIndex].pitch = myPacket.Pitch;
                    client.MinecraftWorld.Entities[eIndex].yaw = myPacket.Yaw;
                }
            }

            client.RaiseEntityLookChanged(myPacket.EntityID, myPacket.Yaw, myPacket.Pitch);
        }
Ejemplo n.º 17
0
        public void HandleLoginDisconnect(MinecraftClient client, IPacket packet) {
            var Disconnect = (CBLoginDisconnect)packet;

            client.RaiseLoginFailure(this, Disconnect.JSONData);
            client.Disconnect();
        }
Ejemplo n.º 18
0
        public void HandleMapChunkBulk(MinecraftClient client, IPacket packet) {
            var ChunkPacket = (CBMapChunkBulk)packet;
            int Offset = 0;

            byte[] trim = new byte[ChunkPacket.Datalength - 2];
            byte[] DecompressedData;

            Chunk[] chunks = new Chunk[ChunkPacket.Chunkcolumncount];

            Buffer.BlockCopy(ChunkPacket.Data, 2, trim, 0, trim.Length);

            DecompressedData = Decompressor.Decompress(trim);

            for (int i = 0; ChunkPacket.Chunkcolumncount > i; i++) {
                int x = BitConverter.ToInt32(ChunkPacket.Metainformation, Offset);
                int z = BitConverter.ToInt32(ChunkPacket.Metainformation, Offset + 4);
                short pbitmap = ReverseBytes(BitConverter.ToInt16(ChunkPacket.Metainformation, Offset + 8));
                short abitmap = ReverseBytes(BitConverter.ToInt16(ChunkPacket.Metainformation, Offset + 10));
                Offset += 12;

                chunks[i] = new Chunk(x, z, pbitmap, abitmap, ChunkPacket.Skylightsent, true); // -- Assume true for Ground Up Continuous

                DecompressedData = chunks[i].GetData(DecompressedData); // -- Calls the chunk class to take all of the bytes it needs, and return whats left.

                if (client.MinecraftWorld == null)
                    client.MinecraftWorld = new WorldClass();

                client.MinecraftWorld.worldChunks.Add(chunks[i]);
            }
        }
Ejemplo n.º 19
0
        public void HandleEncryptionRequest(MinecraftClient client, IPacket packet) {
            var ER = (CBEncryptionRequest)packet;
            var SharedKey = new byte[16];

            var Random = RandomNumberGenerator.Create(); // -- Generate a random shared key.
            Random.GetBytes(SharedKey);

            if (ER.ServerID == "" && client.VerifyNames) {
                // -- Verify with Minecraft.net.
                // -- At this point, the server requires a hash containing the server id,
                // -- shared key, and original public key. So we make this, and then pass to Minecraft.net

                List<byte> HashList = new List<byte>();
                HashList.AddRange(Encoding.ASCII.GetBytes(ER.ServerID));
                HashList.AddRange(SharedKey);
                HashList.AddRange(ER.PublicKey);

                var HashData = HashList.ToArray();
                var Hash = JavaHexDigest(HashData);

                var Verify = new Minecraft_Net_Interaction();

                if (!Verify.VerifyName(client.ClientName, client.AccessToken, client.SelectedProfile, Hash)) {
                    client.RaiseLoginFailure(this, "Failed to verify name with Minecraft session server.");
                    client.Disconnect();
                    return;
                }
            } else
                client.RaiseInfo(this, "Name verification disabled, skipping authentication.");

            // -- AsnKeyParser is a part of the cryptography.dll, which is simply a compiled version
            // -- of SMProxy's Cryptography.cs, with the server side parts stripped out.
            // -- You pass it the key data and ask it to parse, and it will 
            // -- Extract the server's public key, then parse that into RSA for us.

            var KeyParser = new AsnKeyParser(ER.PublicKey);
            var Dekey = KeyParser.ParseRSAPublicKey();

            // -- Now we create an encrypter, and encrypt the token sent to us by the server
            // -- as well as our newly made shared key (Which can then only be decrypted with the server's private key)
            // -- and we send it to the server.

            var cryptoService = new RSACryptoServiceProvider(); // -- RSA Encryption class
            cryptoService.ImportParameters(Dekey); // -- Import the Server's public key to use as the RSA encryption key.

            byte[] EncryptedSecret = cryptoService.Encrypt(SharedKey, false); // -- Encrypt the Secret key and verification token.
            byte[] EncryptedVerify = cryptoService.Encrypt(ER.VerifyToken, false);

            client.nh.wSock.InitEncryption(SharedKey); // -- Give the shared secret key to the socket

            var Response = new SBEncryptionResponse(); // -- Respond to the server

            Response.SharedLength = (short)EncryptedSecret.Length;
            Response.SharedSecret = EncryptedSecret;
            Response.VerifyLength = (short)EncryptedVerify.Length;
            Response.VerifyToken = EncryptedVerify;

            Response.Write(client.nh.wSock);

            client.nh.wSock.EncEnabled = true;
            client.nh.RaiseSocketInfo(this, "Encryption Enabled.");
        }
Ejemplo n.º 20
0
 public void HandleMaps(MinecraftClient client, IPacket packet) {
     var myPacket = (CBMaps)packet;
     // -- Still don't know what this is for.
 }
Ejemplo n.º 21
0
        public void HandleEntityTeleport(MinecraftClient client, IPacket packet) {
            var myPacket = (CBEntityTeleport)packet;

            if (client.MinecraftWorld != null) {
                int eIndex = client.MinecraftWorld.GetEntityById(myPacket.EntityID);

                if (eIndex != -1) {
                    client.MinecraftWorld.Entities[eIndex].Location.x = myPacket.X;
                    client.MinecraftWorld.Entities[eIndex].Location.y = myPacket.Y;
                    client.MinecraftWorld.Entities[eIndex].Location.z = myPacket.Z;
                    client.MinecraftWorld.Entities[eIndex].yaw = myPacket.Yaw;
                    client.MinecraftWorld.Entities[eIndex].pitch = myPacket.Pitch;
                }
            }

            client.RaiseEntityTeleport(myPacket.EntityID, myPacket.X, myPacket.Y, myPacket.Z);
            client.RaiseEntityLookChanged(myPacket.EntityID, myPacket.Yaw, myPacket.Pitch);
        }
Ejemplo n.º 22
0
 public void HandleOpenWindow(MinecraftClient client, IPacket packet) {
     var myPacket = (CBOpenWindow)packet;
     client.RaiseOpenWindow(myPacket.Windowid, myPacket.InventoryType, myPacket.Windowtitle, myPacket.NumberofSlots, myPacket.Useprovidedwindowtitle);
     client.RaiseDebug(this, "Window opened forcibly");
 }
Ejemplo n.º 23
0
        public void HandleEntityVelocity(MinecraftClient client, IPacket packet) {
            var myPacket = (CBEntityVelocity)packet;

            if (client.MinecraftWorld != null) {
                int eIndex = client.MinecraftWorld.GetEntityById(myPacket.EntityID);

                if (eIndex != -1) {
                    client.MinecraftWorld.Entities[eIndex].Velocity_X = myPacket.VelocityX;
                    client.MinecraftWorld.Entities[eIndex].Velocity_Y = myPacket.VelocityY;
                    client.MinecraftWorld.Entities[eIndex].Velocity_Z = myPacket.VelocityZ;
                }
            }

            client.RaiseEntityVelocityChanged(myPacket.EntityID, myPacket.VelocityX, myPacket.VelocityY, myPacket.VelocityZ);
        }
Ejemplo n.º 24
0
        public void HandlePlayerListItem(MinecraftClient client, IPacket packet) {
            var myPacket = (CBPlayerListItem)packet;

            if (myPacket.Online) {
                if (client.Players.ContainsKey(myPacket.Playername)) {
                    client.Players[myPacket.Playername] = myPacket.Ping;
                    client.RaisePlayerlistUpdate(myPacket.Playername, myPacket.Ping);
                } else {
                    client.Players.Add(myPacket.Playername, myPacket.Ping);
                    client.RaisePlayerlistAdd(myPacket.Playername, myPacket.Ping);
                }
            } else {
                if (client.Players.ContainsKey(myPacket.Playername)) {
                    client.Players.Remove(myPacket.Playername);
                    client.RaisePlayerlistRemove(myPacket.Playername);
                }
            }
        }
Ejemplo n.º 25
0
        public void HandleEntityEffect(MinecraftClient client, IPacket packet) {
            var myPacket = (CBEntityEffect)packet;

            if (client.MinecraftWorld == null)
                return;

            int eIndex = client.MinecraftWorld.GetEntityById(myPacket.EntityID);

            if (eIndex != -1) {
                client.MinecraftWorld.Entities[eIndex].amplifier = myPacket.Amplifier;
                client.MinecraftWorld.Entities[eIndex].duration = myPacket.Duration;
                client.MinecraftWorld.Entities[eIndex].status = myPacket.EffectID;
                client.RaiseEntityStatus(myPacket.EntityID);
            }
        }
Ejemplo n.º 26
0
 public void HandlePluginMessage(MinecraftClient client, IPacket packet) {
     var myPacket = (CBPluginMessage)packet;
     client.RaisePluginMessage(myPacket.Channel, myPacket.Data);
 }
Ejemplo n.º 27
0
        public void HandleHeldItemChange(MinecraftClient client, IPacket packet) {
            var myPacket = (CBHeldItemChange)packet;

            if (client.ThisPlayer == null)
                client.ThisPlayer = new Player();

            client.ThisPlayer.selectedSlot = myPacket.Slot;

            var mySend = new SBHeldItemChange();
            mySend.Slot = client.ThisPlayer.selectedSlot;
            mySend.Write(client.nh.wSock);
        }
Ejemplo n.º 28
0
 public TickHandler(ref MinecraftClient mc) {
     ThisMc = mc;
 }
Ejemplo n.º 29
0
        public void HandleJoinGame(MinecraftClient client, IPacket packet) {
            var myPacket = (CBJoinGame)packet;

            if (client.ThisPlayer == null)
                client.ThisPlayer = new Player();

            client.ThisPlayer.Entity_ID = myPacket.EntityID;
            client.ThisPlayer.gameMode = myPacket.Gamemode;

            if (client.MinecraftWorld == null)
                client.MinecraftWorld = new WorldClass();

            client.MinecraftWorld.difficulty = myPacket.Difficulty;
            client.MinecraftWorld.dimension = myPacket.Dimension;
            client.MinecraftWorld.maxPlayers = myPacket.MaxPlayers;
            client.MinecraftWorld.levelType = myPacket.LevelType;

            client.RaiseDebug(this, string.Format("Entity ID: {0}", myPacket.EntityID));
            client.RaiseGameJoined();

            var b = new SBClientSettings();
            b.Locale = "en_US";
            b.Viewdistance = 5;
            b.Chatflags = 3;
            b.Chatcolours = true;
            b.Difficulty = 1;
            b.ShowCape = false;
            b.Write(client.nh.wSock);

            var c = new SBPluginMessage();
            c.Channel = "MC|Brand";
            c.Data = Encoding.UTF8.GetBytes(client.ClientBrand);
            c.Length = (short)c.Data.Length;
            c.Write(client.nh.wSock);
        }
Ejemplo n.º 30
0
 public void HandleExplosion(MinecraftClient client, IPacket packet) {
     var myPacket = (CBExplosion)packet;
     //TODO: Handle more of this...
     client.RaiseExplosion(myPacket.X, myPacket.Y, myPacket.Z);
 }
Ejemplo n.º 31
0
        public void HandleEntityEquipment(MinecraftClient client, IPacket packet) {
            var myPacket = (CBEntityEquipment)packet;

            if (client.ThisPlayer != null && myPacket.EntityID == client.ThisPlayer.Entity_ID) { 
                client.ThisPlayer.SetInventory(Item.ItemFromSlot(myPacket.Item), myPacket.Slot);
                return;
            }

            if (client.MinecraftWorld != null && client.MinecraftWorld.Entities != null) {
                int eIndex = client.MinecraftWorld.GetEntityById(myPacket.EntityID);

                if (eIndex != -1)
                    client.MinecraftWorld.Entities[eIndex].HandleInventory(myPacket.Slot, Item.ItemFromSlot(myPacket.Item));
            }
        }