2013-07-18 09:27:19 +02:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
using System.Net.Sockets;
|
|
|
|
|
|
using System.Threading;
|
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
using System.Net;
|
2014-05-31 01:59:03 +02:00
|
|
|
|
using MinecraftClient.Protocol;
|
|
|
|
|
|
using MinecraftClient.Proxy;
|
2013-07-18 09:27:19 +02:00
|
|
|
|
|
|
|
|
|
|
namespace MinecraftClient
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// The main client class, used to connect to a Minecraft server.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
|
2014-05-31 01:59:03 +02:00
|
|
|
|
public class McTcpClient : IMinecraftComHandler
|
2013-07-18 09:27:19 +02:00
|
|
|
|
{
|
2014-05-31 01:59:03 +02:00
|
|
|
|
private List<ChatBot> bots = new List<ChatBot>();
|
|
|
|
|
|
private static List<ChatBots.Script> scripts_on_hold = new List<ChatBots.Script>();
|
|
|
|
|
|
public void BotLoad(ChatBot b) { b.SetHandler(this); bots.Add(b); b.Initialize(); Settings.SingleCommand = ""; }
|
|
|
|
|
|
public void BotUnLoad(ChatBot b) { bots.RemoveAll(item => object.ReferenceEquals(item, b)); }
|
|
|
|
|
|
public void BotClear() { bots.Clear(); }
|
|
|
|
|
|
|
2013-07-18 09:27:19 +02:00
|
|
|
|
public static int AttemptsLeft = 0;
|
|
|
|
|
|
|
2014-05-31 01:59:03 +02:00
|
|
|
|
private string host;
|
|
|
|
|
|
private int port;
|
|
|
|
|
|
private string username;
|
|
|
|
|
|
private string uuid;
|
|
|
|
|
|
private string sessionid;
|
|
|
|
|
|
|
|
|
|
|
|
public int getServerPort() { return port; }
|
|
|
|
|
|
public string getServerHost() { return host; }
|
|
|
|
|
|
public string getUsername() { return username; }
|
|
|
|
|
|
public string getUserUUID() { return uuid; }
|
|
|
|
|
|
public string getSessionID() { return sessionid; }
|
|
|
|
|
|
|
2013-07-18 09:27:19 +02:00
|
|
|
|
TcpClient client;
|
2014-05-31 01:59:03 +02:00
|
|
|
|
IMinecraftCom handler;
|
2013-07-18 09:27:19 +02:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2014-05-31 01:59:03 +02:00
|
|
|
|
/// Starts the main chat client
|
2013-07-18 09:27:19 +02:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="username">The chosen username of a premium Minecraft Account</param>
|
|
|
|
|
|
/// <param name="sessionID">A valid sessionID obtained with MinecraftCom.GetLogin()</param>
|
|
|
|
|
|
/// <param name="server_port">The server IP (serveradress or serveradress:port)</param>
|
|
|
|
|
|
|
2014-05-31 01:59:03 +02:00
|
|
|
|
public McTcpClient(string username, string uuid, string sessionID, int protocolversion, string server_port)
|
2013-07-18 09:27:19 +02:00
|
|
|
|
{
|
2014-05-31 01:59:03 +02:00
|
|
|
|
StartClient(username, uuid, sessionID, server_port, protocolversion, false, "");
|
2013-07-18 09:27:19 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2014-05-31 01:59:03 +02:00
|
|
|
|
/// Starts the main chat client in single command sending mode
|
2013-07-18 09:27:19 +02:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="username">The chosen username of a premium Minecraft Account</param>
|
|
|
|
|
|
/// <param name="sessionID">A valid sessionID obtained with MinecraftCom.GetLogin()</param>
|
|
|
|
|
|
/// <param name="server_port">The server IP (serveradress or serveradress:port)</param>
|
|
|
|
|
|
/// <param name="command">The text or command to send.</param>
|
|
|
|
|
|
|
2014-05-31 01:59:03 +02:00
|
|
|
|
public McTcpClient(string username, string uuid, string sessionID, string server_port, int protocolversion, string command)
|
2013-07-18 09:27:19 +02:00
|
|
|
|
{
|
2014-05-31 01:59:03 +02:00
|
|
|
|
StartClient(username, uuid, sessionID, server_port, protocolversion, true, command);
|
2013-07-18 09:27:19 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Starts the main chat client, wich will login to the server using the MinecraftCom class.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="user">The chosen username of a premium Minecraft Account</param>
|
|
|
|
|
|
/// <param name="sessionID">A valid sessionID obtained with MinecraftCom.GetLogin()</param>
|
|
|
|
|
|
/// <param name="server_port">The server IP (serveradress or serveradress:port)/param>
|
|
|
|
|
|
/// <param name="singlecommand">If set to true, the client will send a single command and then disconnect from the server</param>
|
|
|
|
|
|
/// <param name="command">The text or command to send. Will only be sent if singlecommand is set to true.</param>
|
|
|
|
|
|
|
2014-05-31 01:59:03 +02:00
|
|
|
|
private void StartClient(string user, string uuid, string sessionID, string server_port, int protocolversion, bool singlecommand, string command)
|
2013-07-18 09:27:19 +02:00
|
|
|
|
{
|
|
|
|
|
|
string[] sip = server_port.Split(':');
|
2014-05-31 01:59:03 +02:00
|
|
|
|
|
|
|
|
|
|
this.sessionid = sessionID;
|
|
|
|
|
|
this.uuid = uuid;
|
|
|
|
|
|
this.username = user;
|
|
|
|
|
|
this.host = sip[0];
|
|
|
|
|
|
|
2013-07-18 09:27:19 +02:00
|
|
|
|
if (sip.Length == 1)
|
|
|
|
|
|
{
|
|
|
|
|
|
port = 25565;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
port = Convert.ToInt32(sip[1]);
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (FormatException) { port = 25565; }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2014-05-31 01:59:03 +02:00
|
|
|
|
if (!singlecommand)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (Settings.AntiAFK_Enabled) { BotLoad(new ChatBots.AntiAFK(Settings.AntiAFK_Delay)); }
|
|
|
|
|
|
if (Settings.Hangman_Enabled) { BotLoad(new ChatBots.HangmanGame(Settings.Hangman_English)); }
|
|
|
|
|
|
if (Settings.Alerts_Enabled) { BotLoad(new ChatBots.Alerts()); }
|
|
|
|
|
|
if (Settings.ChatLog_Enabled) { BotLoad(new ChatBots.ChatLog(Settings.ChatLog_File.Replace("%username%", Settings.Username), Settings.ChatLog_Filter, Settings.ChatLog_DateTime)); }
|
|
|
|
|
|
if (Settings.PlayerLog_Enabled) { BotLoad(new ChatBots.PlayerListLogger(Settings.PlayerLog_Delay, Settings.PlayerLog_File.Replace("%username%", Settings.Username))); }
|
|
|
|
|
|
if (Settings.AutoRelog_Enabled) { BotLoad(new ChatBots.AutoRelog(Settings.AutoRelog_Delay, Settings.AutoRelog_Retries)); }
|
|
|
|
|
|
if (Settings.ScriptScheduler_Enabled) { BotLoad(new ChatBots.ScriptScheduler(Settings.ScriptScheduler_TasksFile.Replace("%username%", Settings.Username))); }
|
|
|
|
|
|
if (Settings.RemoteCtrl_Enabled) { BotLoad(new ChatBots.RemoteControl()); }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2013-07-18 09:27:19 +02:00
|
|
|
|
try
|
|
|
|
|
|
{
|
2014-05-31 01:59:03 +02:00
|
|
|
|
client = ProxyHandler.newTcpClient(host, port);
|
2013-07-18 09:27:19 +02:00
|
|
|
|
client.ReceiveBufferSize = 1024 * 1024;
|
2014-05-31 01:59:03 +02:00
|
|
|
|
handler = Protocol.ProtocolHandler.getProtocolHandler(client, protocolversion, this);
|
2014-05-31 12:56:54 +02:00
|
|
|
|
Console.WriteLine("Version is supported.\nLogging in...");
|
2014-05-31 01:59:03 +02:00
|
|
|
|
|
|
|
|
|
|
if (handler.Login())
|
2013-07-18 09:27:19 +02:00
|
|
|
|
{
|
2014-01-08 23:58:49 +01:00
|
|
|
|
if (singlecommand)
|
2013-07-18 09:27:19 +02:00
|
|
|
|
{
|
2014-01-08 23:58:49 +01:00
|
|
|
|
handler.SendChatMessage(command);
|
2014-05-31 01:59:03 +02:00
|
|
|
|
ConsoleIO.WriteLineFormatted("§7Command §8" + command + "§7 sent.", false);
|
2014-01-08 23:58:49 +01:00
|
|
|
|
Thread.Sleep(5000);
|
2014-05-31 01:59:03 +02:00
|
|
|
|
handler.Disconnect();
|
2014-01-08 23:58:49 +01:00
|
|
|
|
Thread.Sleep(1000);
|
2013-07-18 09:27:19 +02:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2014-05-31 01:59:03 +02:00
|
|
|
|
foreach (ChatBot bot in scripts_on_hold) { bots.Add(bot); }
|
|
|
|
|
|
scripts_on_hold.Clear();
|
2014-03-11 08:14:58 -06:00
|
|
|
|
Console.WriteLine("Server was successfully joined.\nType '/quit' to leave the server.");
|
2014-05-31 01:59:03 +02:00
|
|
|
|
StartTalk();
|
2013-07-18 09:27:19 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (SocketException)
|
|
|
|
|
|
{
|
|
|
|
|
|
Console.WriteLine("Failed to connect to this IP.");
|
|
|
|
|
|
if (AttemptsLeft > 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
ChatBot.LogToConsole("Waiting 5 seconds (" + AttemptsLeft + " attempts left)...");
|
|
|
|
|
|
Thread.Sleep(5000); AttemptsLeft--; Program.Restart();
|
|
|
|
|
|
}
|
2013-07-20 11:01:49 +10:00
|
|
|
|
else if (!singlecommand) { Console.ReadLine(); }
|
2013-07-18 09:27:19 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Allows the user to send chat messages, commands, and to leave the server.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
|
|
|
|
|
|
private void StartTalk()
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2014-05-31 01:59:03 +02:00
|
|
|
|
string text = "";
|
|
|
|
|
|
Thread.Sleep(500);
|
2014-01-12 13:38:52 +01:00
|
|
|
|
handler.SendRespawnPacket();
|
|
|
|
|
|
|
2013-07-18 09:27:19 +02:00
|
|
|
|
while (client.Client.Connected)
|
|
|
|
|
|
{
|
|
|
|
|
|
text = ConsoleIO.ReadLine();
|
2013-08-18 18:26:20 +02:00
|
|
|
|
if (ConsoleIO.basicIO && text.Length > 0 && text[0] == (char)0x00)
|
2013-07-18 09:27:19 +02:00
|
|
|
|
{
|
2013-08-18 18:26:20 +02:00
|
|
|
|
//Process a request from the GUI
|
|
|
|
|
|
string[] command = text.Substring(1).Split((char)0x00);
|
|
|
|
|
|
switch (command[0].ToLower())
|
2013-07-18 09:27:19 +02:00
|
|
|
|
{
|
2013-08-18 18:26:20 +02:00
|
|
|
|
case "autocomplete":
|
|
|
|
|
|
if (command.Length > 1) { ConsoleIO.WriteLine((char)0x00 + "autocomplete" + (char)0x00 + handler.AutoComplete(command[1])); }
|
|
|
|
|
|
else Console.WriteLine((char)0x00 + "autocomplete" + (char)0x00);
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2014-01-12 13:38:52 +01:00
|
|
|
|
text = text.Trim();
|
|
|
|
|
|
if (text.ToLower() == "/quit" || text.ToLower() == "/reco")
|
|
|
|
|
|
{
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (text.ToLower() == "/respawn")
|
|
|
|
|
|
{
|
|
|
|
|
|
handler.SendRespawnPacket();
|
|
|
|
|
|
ConsoleIO.WriteLine("You have respawned.");
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (text.ToLower().StartsWith("/script "))
|
|
|
|
|
|
{
|
2014-05-31 01:59:03 +02:00
|
|
|
|
BotLoad(new ChatBots.Script(text.Substring(8)));
|
2014-01-12 13:38:52 +01:00
|
|
|
|
}
|
|
|
|
|
|
else if (text != "")
|
2013-08-18 18:26:20 +02:00
|
|
|
|
{
|
|
|
|
|
|
//Message is too long
|
|
|
|
|
|
if (text.Length > 100)
|
2013-07-18 09:27:19 +02:00
|
|
|
|
{
|
2013-08-18 18:26:20 +02:00
|
|
|
|
if (text[0] == '/')
|
|
|
|
|
|
{
|
|
|
|
|
|
//Send the first 100 chars of the command
|
|
|
|
|
|
text = text.Substring(0, 100);
|
|
|
|
|
|
handler.SendChatMessage(text);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
2013-07-18 09:27:19 +02:00
|
|
|
|
{
|
2014-05-31 01:59:03 +02:00
|
|
|
|
//Send the message splitted into several messages
|
2013-08-18 18:26:20 +02:00
|
|
|
|
while (text.Length > 100)
|
|
|
|
|
|
{
|
|
|
|
|
|
handler.SendChatMessage(text.Substring(0, 100));
|
|
|
|
|
|
text = text.Substring(100, text.Length - 100);
|
|
|
|
|
|
}
|
|
|
|
|
|
handler.SendChatMessage(text);
|
2013-07-18 09:27:19 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2013-08-18 18:26:20 +02:00
|
|
|
|
else handler.SendChatMessage(text);
|
2013-07-18 09:27:19 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2014-01-12 13:38:52 +01:00
|
|
|
|
switch (text.ToLower())
|
2013-07-18 09:27:19 +02:00
|
|
|
|
{
|
2014-01-12 13:38:52 +01:00
|
|
|
|
case "/quit": Program.Exit(); break;
|
|
|
|
|
|
case "/reco": Program.Restart(); break;
|
2013-07-18 09:27:19 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (IOException) { }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2014-05-31 01:59:03 +02:00
|
|
|
|
/// Disconnect the client from the server
|
2013-07-18 09:27:19 +02:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
|
2014-05-31 01:59:03 +02:00
|
|
|
|
public void Disconnect()
|
2013-07-18 09:27:19 +02:00
|
|
|
|
{
|
2014-05-31 01:59:03 +02:00
|
|
|
|
foreach (ChatBot bot in bots)
|
|
|
|
|
|
if (bot is ChatBots.Script)
|
|
|
|
|
|
scripts_on_hold.Add((ChatBots.Script)bot);
|
|
|
|
|
|
|
|
|
|
|
|
handler.Disconnect();
|
|
|
|
|
|
handler.Dispose();
|
|
|
|
|
|
Thread.Sleep(1000);
|
|
|
|
|
|
|
|
|
|
|
|
if (client != null) { client.Close(); }
|
|
|
|
|
|
}
|
2013-07-18 09:27:19 +02:00
|
|
|
|
|
2014-05-31 01:59:03 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Received some text from the server
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="text">Text received</param>
|
|
|
|
|
|
|
|
|
|
|
|
public void OnTextReceived(string text)
|
|
|
|
|
|
{
|
|
|
|
|
|
ConsoleIO.WriteLineFormatted(text, false);
|
|
|
|
|
|
foreach (ChatBot bot in bots)
|
|
|
|
|
|
bot.GetText(text);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// When connection has been lost
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
|
|
|
|
|
|
public void OnConnectionLost(ChatBot.DisconnectReason reason, string message)
|
|
|
|
|
|
{
|
|
|
|
|
|
bool will_restart = false;
|
|
|
|
|
|
|
|
|
|
|
|
switch (reason)
|
2013-07-18 09:27:19 +02:00
|
|
|
|
{
|
2014-05-31 01:59:03 +02:00
|
|
|
|
case ChatBot.DisconnectReason.ConnectionLost:
|
|
|
|
|
|
message = "Connection has been lost.";
|
|
|
|
|
|
ConsoleIO.WriteLine(message);
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
case ChatBot.DisconnectReason.InGameKick:
|
|
|
|
|
|
ConsoleIO.WriteLine("Disconnected by Server :");
|
|
|
|
|
|
ConsoleIO.WriteLineFormatted(message, true);
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
case ChatBot.DisconnectReason.LoginRejected:
|
|
|
|
|
|
ConsoleIO.WriteLine("Login failed :");
|
|
|
|
|
|
ConsoleIO.WriteLineFormatted(message, true);
|
|
|
|
|
|
break;
|
2013-07-18 09:27:19 +02:00
|
|
|
|
}
|
2014-05-31 01:59:03 +02:00
|
|
|
|
|
|
|
|
|
|
foreach (ChatBot bot in bots)
|
|
|
|
|
|
will_restart |= bot.OnDisconnect(reason, message);
|
|
|
|
|
|
|
|
|
|
|
|
if (!will_restart) { Program.ReadLineReconnect(); }
|
2013-07-18 09:27:19 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2014-05-31 01:59:03 +02:00
|
|
|
|
/// Called ~10 times per second by the protocol handler
|
2013-07-18 09:27:19 +02:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
|
2014-05-31 01:59:03 +02:00
|
|
|
|
public void OnUpdate()
|
2013-07-18 09:27:19 +02:00
|
|
|
|
{
|
2014-05-31 01:59:03 +02:00
|
|
|
|
for (int i = 0; i < bots.Count; i++)
|
|
|
|
|
|
bots[i].Update();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Send a chat message to the server
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
|
|
|
|
|
|
public void SendChatMessage(string message)
|
|
|
|
|
|
{
|
|
|
|
|
|
handler.SendChatMessage(message);
|
2013-07-18 09:27:19 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|