mirror of
https://github.com/MCCTeam/Minecraft-Console-Client
synced 2025-10-14 21:22:49 +00:00
/list command improvements
Coding style, Guid, interface, Fallback Command
This commit is contained in:
parent
5d1aee46c2
commit
f82041288d
4 changed files with 72 additions and 49 deletions
|
|
@ -12,8 +12,14 @@ namespace MinecraftClient.Commands
|
||||||
|
|
||||||
public override string Run(McTcpClient handler, string command)
|
public override string Run(McTcpClient handler, string command)
|
||||||
{
|
{
|
||||||
handler.ListPlayers();
|
string[] onlinePlayers = handler.getOnlinePlayers();
|
||||||
|
if (onlinePlayers.Length == 0) //Not properly handled by Protocol handler?
|
||||||
|
{
|
||||||
|
//Fallback to server /list command
|
||||||
|
handler.SendText("/list");
|
||||||
return "";
|
return "";
|
||||||
|
}
|
||||||
|
else return "PlayerList: " + String.Join(", ", onlinePlayers);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,7 @@ namespace MinecraftClient
|
||||||
private static List<string> cmd_names = new List<string>();
|
private static List<string> cmd_names = new List<string>();
|
||||||
private static Dictionary<string, Command> cmds = new Dictionary<string, Command>();
|
private static Dictionary<string, Command> cmds = new Dictionary<string, Command>();
|
||||||
private List<ChatBot> bots = new List<ChatBot>();
|
private List<ChatBot> bots = new List<ChatBot>();
|
||||||
|
private Dictionary<Guid, string> onlinePlayers = new Dictionary<Guid,string>();
|
||||||
private static List<ChatBots.Script> scripts_on_hold = new List<ChatBots.Script>();
|
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 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 BotUnLoad(ChatBot b) { bots.RemoveAll(item => object.ReferenceEquals(item, b)); }
|
||||||
|
|
@ -33,8 +34,6 @@ namespace MinecraftClient
|
||||||
private string uuid;
|
private string uuid;
|
||||||
private string sessionid;
|
private string sessionid;
|
||||||
|
|
||||||
public Dictionary<string, string> players;
|
|
||||||
|
|
||||||
public int getServerPort() { return port; }
|
public int getServerPort() { return port; }
|
||||||
public string getServerHost() { return host; }
|
public string getServerHost() { return host; }
|
||||||
public string getUsername() { return username; }
|
public string getUsername() { return username; }
|
||||||
|
|
@ -58,7 +57,6 @@ namespace MinecraftClient
|
||||||
public McTcpClient(string username, string uuid, string sessionID, int protocolversion, string server_ip, ushort port)
|
public McTcpClient(string username, string uuid, string sessionID, int protocolversion, string server_ip, ushort port)
|
||||||
{
|
{
|
||||||
StartClient(username, uuid, sessionID, server_ip, port, protocolversion, false, "");
|
StartClient(username, uuid, sessionID, server_ip, port, protocolversion, false, "");
|
||||||
players = new Dictionary<string, string>();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
@ -391,19 +389,6 @@ namespace MinecraftClient
|
||||||
else return handler.SendChatMessage(text);
|
else return handler.SendChatMessage(text);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Display a list of players
|
|
||||||
/// </summary>
|
|
||||||
/// <returns>True if the players can be listed</returns>
|
|
||||||
|
|
||||||
public bool ListPlayers()
|
|
||||||
{
|
|
||||||
ConsoleIO.WriteLine ("Player List");
|
|
||||||
foreach (string player in players.Values)
|
|
||||||
ConsoleIO.WriteLine (player);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Allow to respawn after death
|
/// Allow to respawn after death
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
@ -411,16 +396,38 @@ namespace MinecraftClient
|
||||||
|
|
||||||
public bool SendRespawnPacket()
|
public bool SendRespawnPacket()
|
||||||
{
|
{
|
||||||
return handler.SendRespawnPacket ();
|
return handler.SendRespawnPacket();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addPlayer(string uuid, string name) {
|
/// <summary>
|
||||||
players[uuid] = name;
|
/// Triggered when a new player joins the game
|
||||||
}
|
/// </summary>
|
||||||
public void removePlayer(string uuid){
|
/// <param name="uuid">UUID of the player</param>
|
||||||
players.Remove (uuid);
|
/// <param name="name">Name of the player</param>
|
||||||
|
|
||||||
|
public void OnPlayerJoin(Guid uuid, string name)
|
||||||
|
{
|
||||||
|
onlinePlayers[uuid] = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
public HashSet<string> getPlayers() { return new HashSet<string>(players.Values); }
|
/// <summary>
|
||||||
|
/// Triggered when a player has left the game
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="uuid">UUID of the player</param>
|
||||||
|
|
||||||
|
public void OnPlayerLeave(Guid uuid)
|
||||||
|
{
|
||||||
|
onlinePlayers.Remove(uuid);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Get a set of online player names
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>Online player names</returns>
|
||||||
|
|
||||||
|
public string[] getOnlinePlayers()
|
||||||
|
{
|
||||||
|
return onlinePlayers.Values.Distinct().ToArray();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -137,33 +137,31 @@ namespace MinecraftClient.Protocol.Handlers
|
||||||
{
|
{
|
||||||
switch (packetID)
|
switch (packetID)
|
||||||
{
|
{
|
||||||
case 0x00:
|
case 0x00: //Keep-Alive
|
||||||
SendPacket(0x00, getVarInt(readNextVarInt(ref packetData)));
|
SendPacket(0x00, getVarInt(readNextVarInt(ref packetData)));
|
||||||
break;
|
break;
|
||||||
case 0x02:
|
case 0x02: //Chat message
|
||||||
handler.OnTextReceived(ChatParser.ParseText(readNextString(ref packetData)));
|
handler.OnTextReceived(ChatParser.ParseText(readNextString(ref packetData)));
|
||||||
break;
|
break;
|
||||||
case 0x0C: //Entity Look and Relative Move
|
case 0x38: //Player List update
|
||||||
//ConsoleIO.WriteLineFormatted("§8 0x0C entity:" + readNextVarInt(ref packetData) + " has come in to sight");
|
int action = readNextVarInt(ref packetData);
|
||||||
break;
|
int numActions = readNextVarInt(ref packetData);
|
||||||
case 0x38: // update player list
|
Guid uuid = readNextUUID(ref packetData);
|
||||||
int action = readNextVarInt (ref packetData);
|
switch (action)
|
||||||
int numActions = readNextVarInt (ref packetData);
|
{
|
||||||
string uuid = readNextUUID (ref packetData);
|
case 0x00: //Player Join
|
||||||
switch (action) {
|
string name = readNextString(ref packetData);
|
||||||
case 0x00:
|
handler.OnPlayerJoin(uuid, name);
|
||||||
string name = readNextString (ref packetData);
|
|
||||||
handler.addPlayer (uuid, name);
|
|
||||||
break;
|
break;
|
||||||
case 0x04:
|
case 0x04: //Player Leave
|
||||||
handler.removePlayer (uuid);
|
handler.OnPlayerLeave(uuid);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
//do nothing
|
//Unknown player list item type
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 0x3A:
|
case 0x3A: //Tab-Complete Result
|
||||||
int autocomplete_count = readNextVarInt(ref packetData);
|
int autocomplete_count = readNextVarInt(ref packetData);
|
||||||
string tab_list = "";
|
string tab_list = "";
|
||||||
for (int i = 0; i < autocomplete_count; i++)
|
for (int i = 0; i < autocomplete_count; i++)
|
||||||
|
|
@ -177,10 +175,10 @@ namespace MinecraftClient.Protocol.Handlers
|
||||||
if (tab_list.Length > 0)
|
if (tab_list.Length > 0)
|
||||||
ConsoleIO.WriteLineFormatted("§8" + tab_list, false);
|
ConsoleIO.WriteLineFormatted("§8" + tab_list, false);
|
||||||
break;
|
break;
|
||||||
case 0x40:
|
case 0x40: //Kick Packet
|
||||||
handler.OnConnectionLost(ChatBot.DisconnectReason.InGameKick, ChatParser.ParseText(readNextString(ref packetData)));
|
handler.OnConnectionLost(ChatBot.DisconnectReason.InGameKick, ChatParser.ParseText(readNextString(ref packetData)));
|
||||||
return false;
|
return false;
|
||||||
case 0x46:
|
case 0x46: //Network Compression Treshold Info
|
||||||
compression_treshold = readNextVarInt(ref packetData);
|
compression_treshold = readNextVarInt(ref packetData);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
|
|
@ -280,11 +278,11 @@ namespace MinecraftClient.Protocol.Handlers
|
||||||
/// Read a uuid from a cache of bytes and remove it from the cache
|
/// Read a uuid from a cache of bytes and remove it from the cache
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="cache">Cache of bytes to read from</param>
|
/// <param name="cache">Cache of bytes to read from</param>
|
||||||
/// <returns>The uuid as a string</returns>
|
/// <returns>The uuid</returns>
|
||||||
|
|
||||||
private string readNextUUID(ref byte[] cache)
|
private Guid readNextUUID(ref byte[] cache)
|
||||||
{
|
{
|
||||||
return BitConverter.ToString(readData (16, ref cache)).Replace ("-", string.Empty).ToLower();
|
return new Guid(readData(16, ref cache));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
|
||||||
|
|
@ -21,10 +21,7 @@ namespace MinecraftClient.Protocol
|
||||||
string getUsername();
|
string getUsername();
|
||||||
string getUserUUID();
|
string getUserUUID();
|
||||||
string getSessionID();
|
string getSessionID();
|
||||||
|
string[] getOnlinePlayers();
|
||||||
void addPlayer(string uuid, string name);
|
|
||||||
void removePlayer(string uuid);
|
|
||||||
HashSet<string> getPlayers();
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// This method is called when the protocol handler receives a chat message
|
/// This method is called when the protocol handler receives a chat message
|
||||||
|
|
@ -32,6 +29,21 @@ namespace MinecraftClient.Protocol
|
||||||
|
|
||||||
void OnTextReceived(string text);
|
void OnTextReceived(string text);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// This method is called when a new player joins the game
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="uuid">UUID of the player</param>
|
||||||
|
/// <param name="name">Name of the player</param>
|
||||||
|
|
||||||
|
void OnPlayerJoin(Guid uuid, string name);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// This method is called when a player has left the game
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="uuid">UUID of the player</param>
|
||||||
|
|
||||||
|
void OnPlayerLeave(Guid uuid);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// This method is called when the connection has been lost
|
/// This method is called when the connection has been lost
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue