Example #1
0
 protected override void OnClose(CloseEventArgs e)
 {
     base.OnClose(e);
     Client?.SendDisconnectionNotification();
     Client?.StopClient();
     Client = null;
 }
Example #2
0
        static void Main(string[] args)
        {
            var logRepository = LogManager.GetRepository(Assembly.GetEntryAssembly());

            XmlConfigurator.Configure(logRepository, new FileInfo(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "log4net.xml")));

            Log.Info(MiNET);
            Console.WriteLine(MiNET);
            Console.WriteLine("Starting client...");

            var client = new MiNetClient(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 19132), "TheGrey");

            //var client = new MiNetClient(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 19132), "TheGrey");
            //var client = new MiNetClient(new IPEndPoint(Dns.GetHostEntry("test.pmmp.io").AddressList[0], 19132), "TheGrey", new DedicatedThreadPool(new DedicatedThreadPoolSettings(Environment.ProcessorCount)));
            //var client = new MiNetClient(new IPEndPoint(IPAddress.Parse("192.168.0.4"), 19162), "TheGrey", new DedicatedThreadPool(new DedicatedThreadPoolSettings(Environment.ProcessorCount)));
            //var client = new MiNetClient(new IPEndPoint(IPAddress.Parse("213.89.103.206"), 19132), "TheGrey", new DedicatedThreadPool(new DedicatedThreadPoolSettings(Environment.ProcessorCount)));
            //var client = new MiNetClient(new IPEndPoint(Dns.GetHostEntry("yodamine.com").AddressList[0], 19132), "TheGrey");
            //var client = new MiNetClient(new IPEndPoint(IPAddress.Loopback, 19132), "TheGrey", new DedicatedThreadPool(new DedicatedThreadPoolSettings(Environment.ProcessorCount)));

            client.MessageHandler = new BedrockTraceHandler(client);

            client.StartClient();
            Log.Warn("Client listening for connecting on: " + client.ClientEndpoint);

            Console.WriteLine("Looking for server...");

            if (!client.Connection.TryLocate(client.ServerEndPoint, out (IPEndPoint serverEndPoint, string serverName)info))
            {
                Console.WriteLine($"Failed to locate server at {client.ServerEndPoint}");
                return;
            }

            Console.WriteLine($"... YEAH! FOUND SERVER! It's at {info.serverEndPoint}, \"{info.serverName}\"");

            if (!client.Connection.TryConnect(info.serverEndPoint))
            {
                Console.WriteLine($"Failed to connect to server at {info.serverEndPoint}");
                return;
            }

            Console.WriteLine("Waiting for spawn...");
            client.PlayerStatusChangedWaitHandle.WaitOne();
            Console.WriteLine("... spawned");

            client.HasSpawned = true;

            Action <Task, PlayerLocation> doMoveTo = BotHelpers.DoMoveTo(client);

            Action <Task, string> doSendCommand = BotHelpers.DoSendCommand(client);

            Task.Run(BotHelpers.DoWaitForSpawn(client))
            .ContinueWith(t => doSendCommand(t, $"/me says \"I spawned at {client.CurrentLocation}\""))
            //.ContinueWith(task =>
            //{
            //	var request = new McpeCommandRequest();
            //	request.command = "/setblock ~ ~-1 ~ log 0 replace";
            //	request.unknownUuid = new UUID(Guid.NewGuid().ToString());
            //	client.SendPacket(request);

            //	var coord =  (BlockCoordinates) client.CurrentLocation;
            //	var pick = McpeBlockPickRequest.CreateObject();
            //	pick.x = coord.X;
            //	pick.y = coord.Y;
            //	pick.z = coord.Z;
            //	client.SendPacket(request);
            //})

            //.ContinueWith(t => BotHelpers.DoMobEquipment(client)(t, new ItemBlock(new Cobblestone(), 0) {Count = 64}, 0))
            //.ContinueWith(t => BotHelpers.DoMoveTo(client)(t, new PlayerLocation(client.CurrentLocation.ToVector3() - new Vector3(0, 1, 0), 180, 180, 180)))
            //.ContinueWith(t => doMoveTo(t, new PlayerLocation(40, 5.62f, -20, 180, 180, 180)))
            //.ContinueWith(t => doMoveTo(t, new PlayerLocation(0, 5.62, 0, 180 + 45, 180 + 45, 180)))
            //.ContinueWith(t => doMoveTo(t, new PlayerLocation(0, 5.62, 0, 180 + 45, 180 + 45, 180)))
            //.ContinueWith(t => doMoveTo(t, new PlayerLocation(22, 5.62, 40, 180 + 45, 180 + 45, 180)))
            //.ContinueWith(t => doMoveTo(t, new PlayerLocation(50, 5.62f, 17, 180, 180, 180)))
            .ContinueWith(t => doSendCommand(t, "/me says \"Hi guys! It is I!!\""))
            //.ContinueWith(t => Task.Delay(500).Wait())
            //.ContinueWith(t => doSendCommand(t, "/summon sheep"))
            //.ContinueWith(t => Task.Delay(500).Wait())
            //.ContinueWith(t => doSendCommand(t, "/kill @e[type=sheep]"))
            .ContinueWith(t => Task.Delay(5000).Wait())
            //.ContinueWith(t =>
            //{
            //	Random rnd = new Random();
            //	while (true)
            //	{
            //		doMoveTo(t, new PlayerLocation(rnd.Next(10, 40), 5.62f, rnd.Next(-50, -10), 180, 180, 180));
            //		//doMoveTo(t, new PlayerLocation(50, 5.62f, 17, 180, 180, 180));
            //		doMoveTo(t, new PlayerLocation(rnd.Next(40, 50), 5.62f, rnd.Next(0, 20), 180, 180, 180));
            //	}
            //})
            ;

            //string fileName = Path.GetTempPath() + "MobSpawns_" + Guid.NewGuid() + ".txt";
            //FileStream file = File.OpenWrite(fileName);
            //Log.Info($"Writing mob spawns to file:\n{fileName}");
            //_mobWriter = new IndentedTextWriter(new StreamWriter(file));
            //Task.Run(BotHelpers.DoWaitForSpawn(client))
            //	.ContinueWith(task =>
            //	{
            //		foreach (EntityType entityType in Enum.GetValues(typeof(EntityType)))
            //		{
            //			if (entityType == EntityType.Wither) continue;
            //			if (entityType == EntityType.Dragon) continue;
            //			if (entityType == EntityType.Slime) continue;

            //			string entityName = entityType.ToString();
            //			entityName = Regex.Replace(entityName, "([A-Z])", "_$1").TrimStart('_').ToLower();
            //			{
            //				string command = $"/summon {entityName}";
            //				McpeCommandRequest request = new McpeCommandRequest();
            //				request.command = command;
            //				request.unknownUuid = new UUID(Guid.NewGuid().ToString());
            //				client.SendPackage(request);
            //			}

            //			Task.Delay(500).Wait();

            //			{
            //				McpeCommandRequest request = new McpeCommandRequest();
            //				request.command = $"/kill @e[type={entityName}]";
            //				request.unknownUuid = new UUID(Guid.NewGuid().ToString());
            //				client.SendPackage(request);
            //			}
            //		}

            //		{
            //			McpeCommandRequest request = new McpeCommandRequest();
            //			request.command = $"/kill @e[type=!player]";
            //			request.unknownUuid = new UUID(Guid.NewGuid().ToString());
            //			client.SendPackage(request);
            //		}

            //	});

            Console.WriteLine("<Enter> to exit!");
            Console.ReadLine();
            if (client.IsConnected)
            {
                client.SendDisconnectionNotification();
            }
            Thread.Sleep(50);
            client.StopClient();
        }
Example #3
0
        static void Main(string[] args)
        {
            var logRepository = LogManager.GetRepository(Assembly.GetEntryAssembly());

            XmlConfigurator.Configure(logRepository, new FileInfo(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "log4net.xml")));

            Log.Info(MiNET);
            Console.WriteLine(MiNET);
            Console.WriteLine("Starting client...");

            var client = new MiNetClient(new IPEndPoint(Dns.GetHostEntry("yodamine.com").AddressList[0], 19132), "TheGrey", new DedicatedThreadPool(new DedicatedThreadPoolSettings(Environment.ProcessorCount)));

            //var client = new MiNetClient(new IPEndPoint(IPAddress.Loopback, 19132), "TheGrey", new DedicatedThreadPool(new DedicatedThreadPoolSettings(Environment.ProcessorCount)));

            client.StartClient();
            Log.Warn("Client listening for connecting on: " + client.ClientEndpoint);
            Console.WriteLine("Started!");

            //client.SendOpenConnectionRequest1();

            Console.WriteLine("Looking for server...");
            if (client.ServerEndpoint != null)
            {
                while (!client.HaveServer)
                {
                    Console.WriteLine("... still looking ...");
                    client.SendUnconnectedPing();
                    Thread.Sleep(500);
                }
            }

            Console.WriteLine($"... YEAH! FOUND SERVER! It's at {client.ServerEndpoint?.Address}, port {client.ServerEndpoint?.Port}");

            Action <Task, PlayerLocation> doMoveTo      = BotHelpers.DoMoveTo(client);
            Action <Task, string>         doSendCommand = BotHelpers.DoSendCommand(client);

            Task.Run(BotHelpers.DoWaitForSpawn(client))
            .ContinueWith(t => doSendCommand(t, $"/me says \"I spawned at {client.CurrentLocation}\""))
            //.ContinueWith(t => BotHelpers.DoMobEquipment(client)(t, new ItemBlock(new Cobblestone(), 0) {Count = 64}, 0))
            //.ContinueWith(t => BotHelpers.DoMoveTo(client)(t, new PlayerLocation(client.CurrentLocation.ToVector3() - new Vector3(0, 1, 0), 180, 180, 180)))
            //.ContinueWith(t => doMoveTo(t, new PlayerLocation(40, 5.62f, -20, 180, 180, 180)))
            .ContinueWith(t => doMoveTo(t, new PlayerLocation(0, 5.62, 0, 180 + 45, 180 + 45, 180)))
            //.ContinueWith(t => doMoveTo(t, new PlayerLocation(0, 5.62, 0, 180 + 45, 180 + 45, 180)))
            //.ContinueWith(t => doMoveTo(t, new PlayerLocation(22, 5.62, 40, 180 + 45, 180 + 45, 180)))
            //.ContinueWith(t => doMoveTo(t, new PlayerLocation(50, 5.62f, 17, 180, 180, 180)))
            .ContinueWith(t => doSendCommand(t, "/me says \"Hi guys! It is I!!\""))
            //.ContinueWith(t => Task.Delay(500).Wait())
            //.ContinueWith(t => doSendCommand(t, "/summon sheep"))
            //.ContinueWith(t => Task.Delay(500).Wait())
            //.ContinueWith(t => doSendCommand(t, "/kill @e[type=sheep]"))
            .ContinueWith(t => Task.Delay(5000).Wait())
            //.ContinueWith(t =>
            //{
            //	Random rnd = new Random();
            //	while (true)
            //	{
            //		doMoveTo(t, new PlayerLocation(rnd.Next(10, 40), 5.62f, rnd.Next(-50, -10), 180, 180, 180));
            //		//doMoveTo(t, new PlayerLocation(50, 5.62f, 17, 180, 180, 180));
            //		doMoveTo(t, new PlayerLocation(rnd.Next(40, 50), 5.62f, rnd.Next(0, 20), 180, 180, 180));
            //	}
            //})
            ;

            //string fileName = Path.GetTempPath() + "MobSpawns_" + Guid.NewGuid() + ".txt";
            //FileStream file = File.OpenWrite(fileName);
            //Log.Info($"Writing mob spawns to file:\n{fileName}");
            //_mobWriter = new IndentedTextWriter(new StreamWriter(file));
            //Task.Run(BotHelpers.DoWaitForSpawn(client))
            //	.ContinueWith(task =>
            //	{
            //		foreach (EntityType entityType in Enum.GetValues(typeof(EntityType)))
            //		{
            //			if (entityType == EntityType.Wither) continue;
            //			if (entityType == EntityType.Dragon) continue;
            //			if (entityType == EntityType.Slime) continue;

            //			string entityName = entityType.ToString();
            //			entityName = Regex.Replace(entityName, "([A-Z])", "_$1").TrimStart('_').ToLower();
            //			{
            //				string command = $"/summon {entityName}";
            //				McpeCommandRequest request = new McpeCommandRequest();
            //				request.command = command;
            //				request.unknownUuid = new UUID(Guid.NewGuid().ToString());
            //				client.SendPackage(request);
            //			}

            //			Task.Delay(500).Wait();

            //			{
            //				McpeCommandRequest request = new McpeCommandRequest();
            //				request.command = $"/kill @e[type={entityName}]";
            //				request.unknownUuid = new UUID(Guid.NewGuid().ToString());
            //				client.SendPackage(request);
            //			}
            //		}

            //		{
            //			McpeCommandRequest request = new McpeCommandRequest();
            //			request.command = $"/kill @e[type=!player]";
            //			request.unknownUuid = new UUID(Guid.NewGuid().ToString());
            //			client.SendPackage(request);
            //		}

            //	});

            Console.WriteLine("<Enter> to exit!");
            Console.ReadLine();
            client.SendDisconnectionNotification();
            Thread.Sleep(2000);
            client.StopClient();
        }
Example #4
0
        protected override void OnMessage(MessageEventArgs e)
        {
            try
            {
                var json = JObject.Parse(e.Data);
                if ((string)json["action"] == "send_packet")
                {
                    if (Client == null || !Client.HaveServer)
                    {
                        return;
                    }
                    var pk     = (string)json["pk"];
                    var packet = json["packet"];
                    switch (pk)
                    {
                    case "interact":
                        var interact = McpeInteract.CreateObject();
                        interact.actionId = (byte)packet["action"];
                        interact.targetRuntimeEntityId = (long)packet["entityId"];
                        Client.SendPackage(interact);
                        break;

                    case "move":
                        Client.CurrentLocation = new PlayerLocation((float)packet["x"], (float)packet["y"], (float)packet["z"]);
                        Console.WriteLine("move to " + Client.CurrentLocation);
                        Client.SendMcpeMovePlayer();
                        break;
                    }
                }
                else if ((string)json["action"] == "command")
                {
                    var args = ((string)json["message"]).Split(' ');
                    switch (args[0].ToLower())
                    {
                    case "chat":
                        Client.SendChat(string.Join(" ", args.Skip(1)));
                        break;

                    case "disconnect":
                        if (Client != null)
                        {
                            Client.SendDisconnectionNotification();
                            CommandResponse("Disconnected");
                        }
                        else
                        {
                            CommandResponse("Not connected");
                        }
                        break;

                    case "connect":
                        IPAddress ip;
                        if (args.Length <= 1)
                        {
                            ip = IPAddress.Loopback;
                        }
                        else
                        {
                            var addresses = Dns.GetHostEntry(args[1]).AddressList;
                            if (addresses.Length > 0)
                            {
                                ip = addresses[0];
                            }
                            else if (IPAddress.TryParse(args[1], out ip))
                            {
                                CommandResponse("Bad ip: " + args[1]);
                                return;
                            }
                        }

                        short port;
                        if (args.Length <= 2)
                        {
                            port = 19132;
                        }
                        else
                        {
                            if (!short.TryParse(args[2], out port))
                            {
                                CommandResponse("Bad port: " + args[2]);
                                return;
                            }
                        }

                        string username;
                        if (args.Length <= 3)
                        {
                            username = "******";
                        }
                        else
                        {
                            username = args[3];
                        }

                        CommandResponse("Connecting to " + ip + ":" + port);
                        var endpoint = new IPEndPoint(ip, port);
                        Connect(endpoint, username);
                        break;

                    default:
                        Send("Unknown command!");
                        break;
                    }
                }
                else if ((string)json["action"] == "chat")
                {
                    Client.SendChat((string)json["message"]);
                }
            } catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }