mirror of
https://github.com/MCCTeam/Minecraft-Console-Client
synced 2026-08-15 13:04:36 +00:00
Address PR #2927 review: revert files, translate Polish comments to English
Co-authored-by: milutinke <441903+milutinke@users.noreply.github.com> Agent-Logs-Url: https://github.com/MCCTeam/Minecraft-Console-Client/sessions/fa881b38-28d9-4e46-9c06-8ddab0dcac78
This commit is contained in:
parent
2a6922ccba
commit
56fffc6716
10 changed files with 852 additions and 515 deletions
|
|
@ -24,33 +24,18 @@ namespace MinecraftClientGUI
|
|||
private Process Client;
|
||||
private Thread Reader;
|
||||
|
||||
/// <summary>
|
||||
/// Start a client using command-line arguments
|
||||
/// </summary>
|
||||
/// <param name="args">Arguments to pass</param>
|
||||
|
||||
public MinecraftClient(string[] args)
|
||||
{
|
||||
initClient("\"" + String.Join("\" \"", args) + "\" BasicIO");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Start the client using username, password and server IP
|
||||
/// </summary>
|
||||
/// <param name="username">Username or email</param>
|
||||
/// <param name="password">Password for the given username</param>
|
||||
/// <param name="serverip">Server IP to join</param>
|
||||
|
||||
public MinecraftClient(string username, string password, string serverip)
|
||||
{
|
||||
// If password is empty, send an empty string to handle Microsoft/Browser login
|
||||
if (password == null) password = "";
|
||||
initClient('"' + username + "\" \"" + password + "\" \"" + serverip + "\" BasicIO");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Inner function for launching the external console application
|
||||
/// </summary>
|
||||
/// <param name="arguments">Arguments to pass</param>
|
||||
|
||||
private void initClient(string arguments)
|
||||
{
|
||||
if (File.Exists(ExePath))
|
||||
|
|
@ -59,7 +44,10 @@ namespace MinecraftClientGUI
|
|||
Client.StartInfo.FileName = ExePath;
|
||||
Client.StartInfo.Arguments = arguments;
|
||||
Client.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
|
||||
Client.StartInfo.StandardOutputEncoding = Encoding.GetEncoding(System.Globalization.CultureInfo.CurrentCulture.TextInfo.ANSICodePage);
|
||||
|
||||
// FIX: Force UTF-8 encoding to fix special characters and colors
|
||||
Client.StartInfo.StandardOutputEncoding = System.Text.Encoding.UTF8;
|
||||
|
||||
Client.StartInfo.UseShellExecute = false;
|
||||
Client.StartInfo.RedirectStandardOutput = true;
|
||||
Client.StartInfo.RedirectStandardInput = true;
|
||||
|
|
@ -69,66 +57,58 @@ namespace MinecraftClientGUI
|
|||
Reader = new Thread(new ThreadStart(t_reader));
|
||||
Reader.Start();
|
||||
}
|
||||
else throw new FileNotFoundException("Cannot find Minecraft Client Executable!", ExePath);
|
||||
else throw new FileNotFoundException("MinecraftClient.exe not found!", ExePath);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Thread for reading output and app messages from the console
|
||||
/// </summary>
|
||||
|
||||
private void t_reader()
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
try
|
||||
{
|
||||
string line = "";
|
||||
while (line.Trim() == "")
|
||||
if (Client.HasExited) { disconnected = true; break; }
|
||||
|
||||
string line = Client.StandardOutput.ReadLine();
|
||||
if (line != null)
|
||||
{
|
||||
line = Client.StandardOutput.ReadLine() + Client.MainWindowTitle;
|
||||
if (line.Length > 0)
|
||||
if (line.Trim() != "")
|
||||
{
|
||||
if (line == "Server was successfuly joined.") { disconnected = false; }
|
||||
if (line == "You have left the server.") { disconnected = true; }
|
||||
if (line[0] == (char)0x00)
|
||||
if (line.Contains("Server was successfuly joined")) { disconnected = false; }
|
||||
if (line.Contains("You have left the server")) { disconnected = true; }
|
||||
|
||||
if (line.Length > 0 && line[0] == (char)0x00)
|
||||
{
|
||||
//App message from the console
|
||||
string[] command = line.Substring(1).Split((char)0x00);
|
||||
switch (command[0].ToLower())
|
||||
if (command[0].ToLower() == "autocomplete")
|
||||
{
|
||||
case "autocomplete":
|
||||
if (command.Length > 1) { tabAutoCompleteBuffer.AddLast(command[1]); }
|
||||
else tabAutoCompleteBuffer.AddLast("");
|
||||
break;
|
||||
if (command.Length > 1) { tabAutoCompleteBuffer.AddLast(command[1]); }
|
||||
else tabAutoCompleteBuffer.AddLast("");
|
||||
}
|
||||
}
|
||||
else OutputBuffer.AddLast(line);
|
||||
else
|
||||
{
|
||||
OutputBuffer.AddLast(line);
|
||||
}
|
||||
}
|
||||
}
|
||||
else { Thread.Sleep(10); } // Small pause to avoid high CPU usage
|
||||
}
|
||||
catch (NullReferenceException) { break; }
|
||||
catch (Exception) { break; }
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the first queuing output line to print
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
|
||||
public string ReadLine()
|
||||
{
|
||||
while (OutputBuffer.Count < 1) { }
|
||||
while (OutputBuffer.Count < 1)
|
||||
{
|
||||
if (disconnected) return "";
|
||||
Thread.Sleep(10); // Save CPU while waiting for data
|
||||
}
|
||||
string line = OutputBuffer.First.Value;
|
||||
OutputBuffer.RemoveFirst();
|
||||
return line;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Complete a playername or a command, usually by pressing the TAB key
|
||||
/// </summary>
|
||||
/// <param name="text_behindcursor">Text to complete</param>
|
||||
/// <returns>Returns an autocompletion for the provided text</returns>
|
||||
|
||||
public string tabAutoComplete(string text_behindcursor)
|
||||
{
|
||||
tabAutoCompleteBuffer.Clear();
|
||||
|
|
@ -136,7 +116,12 @@ namespace MinecraftClientGUI
|
|||
{
|
||||
text_behindcursor = text_behindcursor.Trim();
|
||||
SendText((char)0x00 + "autocomplete" + (char)0x00 + text_behindcursor);
|
||||
int maxwait = 30; while (tabAutoCompleteBuffer.Count < 1 && maxwait > 0) { Thread.Sleep(100); maxwait--; }
|
||||
int maxwait = 30;
|
||||
while (tabAutoCompleteBuffer.Count < 1 && maxwait > 0)
|
||||
{
|
||||
Thread.Sleep(100);
|
||||
maxwait--;
|
||||
}
|
||||
if (tabAutoCompleteBuffer.Count > 0)
|
||||
{
|
||||
string text_completed = tabAutoCompleteBuffer.First.Value;
|
||||
|
|
@ -148,14 +133,9 @@ namespace MinecraftClientGUI
|
|||
else return "";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Send a message or a command to the server
|
||||
/// </summary>
|
||||
/// <param name="text">Text to send</param>
|
||||
|
||||
public void SendText(string text)
|
||||
{
|
||||
if (text != null)
|
||||
if (text != null && !Client.HasExited)
|
||||
{
|
||||
text = text.Replace("\t", "");
|
||||
text = text.Replace("\r", "");
|
||||
|
|
@ -168,18 +148,17 @@ namespace MinecraftClientGUI
|
|||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Properly disconnect from the server and dispose the client
|
||||
/// </summary>
|
||||
|
||||
public void Close()
|
||||
{
|
||||
Client.StandardInput.WriteLine("/quit");
|
||||
if (Reader.IsAlive) { Reader.Abort(); }
|
||||
if (!Client.WaitForExit(3000))
|
||||
if (!Client.HasExited)
|
||||
{
|
||||
try { Client.Kill(); } catch { }
|
||||
Client.StandardInput.WriteLine("/quit");
|
||||
if (Reader.IsAlive) { Reader.Abort(); }
|
||||
if (!Client.WaitForExit(2000))
|
||||
{
|
||||
try { Client.Kill(); } catch { }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue