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)
|
||||
{
|
||||
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 "";
|
||||
}
|
||||
else return "PlayerList: " + String.Join(", ", onlinePlayers);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -20,6 +20,7 @@ namespace MinecraftClient
|
|||
private static List<string> cmd_names = new List<string>();
|
||||
private static Dictionary<string, Command> cmds = new Dictionary<string, Command>();
|
||||
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>();
|
||||
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)); }
|
||||
|
|
@ -33,8 +34,6 @@ namespace MinecraftClient
|
|||
private string uuid;
|
||||
private string sessionid;
|
||||
|
||||
public Dictionary<string, string> players;
|
||||
|
||||
public int getServerPort() { return port; }
|
||||
public string getServerHost() { return host; }
|
||||
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)
|
||||
{
|
||||
StartClient(username, uuid, sessionID, server_ip, port, protocolversion, false, "");
|
||||
players = new Dictionary<string, string>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -391,19 +389,6 @@ namespace MinecraftClient
|
|||
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>
|
||||
/// Allow to respawn after death
|
||||
/// </summary>
|
||||
|
|
@ -411,16 +396,38 @@ namespace MinecraftClient
|
|||
|
||||
public bool SendRespawnPacket()
|
||||
{
|
||||
return handler.SendRespawnPacket ();
|
||||
return handler.SendRespawnPacket();
|
||||
}
|
||||
|
||||
public void addPlayer(string uuid, string name) {
|
||||
players[uuid] = name;
|
||||
}
|
||||
public void removePlayer(string uuid){
|
||||
players.Remove (uuid);
|
||||
/// <summary>
|
||||
/// Triggered when a new player joins the game
|
||||
/// </summary>
|
||||
/// <param name="uuid">UUID of the player</param>
|
||||
/// <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)
|
||||
{
|
||||
case 0x00:
|
||||
case 0x00: //Keep-Alive
|
||||
SendPacket(0x00, getVarInt(readNextVarInt(ref packetData)));
|
||||
break;
|
||||
case 0x02:
|
||||
case 0x02: //Chat message
|
||||
handler.OnTextReceived(ChatParser.ParseText(readNextString(ref packetData)));
|
||||
break;
|
||||
case 0x0C: //Entity Look and Relative Move
|
||||
//ConsoleIO.WriteLineFormatted("§8 0x0C entity:" + readNextVarInt(ref packetData) + " has come in to sight");
|
||||
break;
|
||||
case 0x38: // update player list
|
||||
int action = readNextVarInt (ref packetData);
|
||||
int numActions = readNextVarInt (ref packetData);
|
||||
string uuid = readNextUUID (ref packetData);
|
||||
switch (action) {
|
||||
case 0x00:
|
||||
string name = readNextString (ref packetData);
|
||||
handler.addPlayer (uuid, name);
|
||||
case 0x38: //Player List update
|
||||
int action = readNextVarInt(ref packetData);
|
||||
int numActions = readNextVarInt(ref packetData);
|
||||
Guid uuid = readNextUUID(ref packetData);
|
||||
switch (action)
|
||||
{
|
||||
case 0x00: //Player Join
|
||||
string name = readNextString(ref packetData);
|
||||
handler.OnPlayerJoin(uuid, name);
|
||||
break;
|
||||
case 0x04:
|
||||
handler.removePlayer (uuid);
|
||||
case 0x04: //Player Leave
|
||||
handler.OnPlayerLeave(uuid);
|
||||
break;
|
||||
default:
|
||||
//do nothing
|
||||
//Unknown player list item type
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case 0x3A:
|
||||
case 0x3A: //Tab-Complete Result
|
||||
int autocomplete_count = readNextVarInt(ref packetData);
|
||||
string tab_list = "";
|
||||
for (int i = 0; i < autocomplete_count; i++)
|
||||
|
|
@ -177,10 +175,10 @@ namespace MinecraftClient.Protocol.Handlers
|
|||
if (tab_list.Length > 0)
|
||||
ConsoleIO.WriteLineFormatted("§8" + tab_list, false);
|
||||
break;
|
||||
case 0x40:
|
||||
case 0x40: //Kick Packet
|
||||
handler.OnConnectionLost(ChatBot.DisconnectReason.InGameKick, ChatParser.ParseText(readNextString(ref packetData)));
|
||||
return false;
|
||||
case 0x46:
|
||||
case 0x46: //Network Compression Treshold Info
|
||||
compression_treshold = readNextVarInt(ref packetData);
|
||||
break;
|
||||
default:
|
||||
|
|
@ -280,11 +278,11 @@ namespace MinecraftClient.Protocol.Handlers
|
|||
/// Read a uuid from a cache of bytes and remove it from the cache
|
||||
/// </summary>
|
||||
/// <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>
|
||||
|
|
|
|||
|
|
@ -21,10 +21,7 @@ namespace MinecraftClient.Protocol
|
|||
string getUsername();
|
||||
string getUserUUID();
|
||||
string getSessionID();
|
||||
|
||||
void addPlayer(string uuid, string name);
|
||||
void removePlayer(string uuid);
|
||||
HashSet<string> getPlayers();
|
||||
string[] getOnlinePlayers();
|
||||
|
||||
/// <summary>
|
||||
/// This method is called when the protocol handler receives a chat message
|
||||
|
|
@ -32,6 +29,21 @@ namespace MinecraftClient.Protocol
|
|||
|
||||
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>
|
||||
/// This method is called when the connection has been lost
|
||||
/// </summary>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue