Store extended player info, list display names

- Add 1.10.1 and 1.10.2 in supported version list
 - Store both player name and player display names
 - List command will sort players by player name
 - List command will now display by display name
 - Ability to use /list raw to display by real name

Suggestion by Johngreen123
This commit is contained in:
ORelio 2016-08-22 19:09:43 +02:00
parent a4cc36ce05
commit 9a98a9d46f
10 changed files with 156 additions and 85 deletions

View file

@ -160,7 +160,7 @@ namespace MinecraftClient.Protocol.Handlers
case 0xC9:
string name = readNextString(); bool online = readNextByte() != 0x00; readData(2);
Guid FakeUUID = new Guid(MD5.Create().ComputeHash(Encoding.UTF8.GetBytes(name)).Take(16).ToArray());
if (online) { handler.OnPlayerJoin(FakeUUID, name); } else { handler.OnPlayerLeave(FakeUUID); }
if (online) { handler.OnPlayerJoin(new PlayerInfo(FakeUUID, name)); } else { handler.OnPlayerLeave(FakeUUID); }
break;
case 0xCA: if (protocolversion >= 72) { readData(9); } else readData(3); break;
case 0xCB: autocomplete_result = readNextString(); autocomplete_received = true; break;

View file

@ -437,9 +437,10 @@ namespace MinecraftClient.Protocol.Handlers
}
readNextVarInt(packetData);
readNextVarInt(packetData);
string displayName = name;
if (readNextBool(packetData))
readNextString(packetData);
handler.OnPlayerJoin(uuid, name);
displayName = readNextString(packetData);
handler.OnPlayerJoin(new PlayerInfo(uuid, name, displayName));
break;
case 0x01: //Update gamemode
case 0x02: //Update latency
@ -465,7 +466,7 @@ namespace MinecraftClient.Protocol.Handlers
short ping = readNextShort(packetData);
Guid FakeUUID = new Guid(MD5.Create().ComputeHash(Encoding.UTF8.GetBytes(name)).Take(16).ToArray());
if (online)
handler.OnPlayerJoin(FakeUUID, name);
handler.OnPlayerJoin(new PlayerInfo(FakeUUID, name));
else handler.OnPlayerLeave(FakeUUID);
}
break;

View file

@ -22,55 +22,48 @@ namespace MinecraftClient.Protocol
string GetUsername();
string GetUserUUID();
string GetSessionID();
string[] GetOnlinePlayers();
PlayerInfo[] GetOnlinePlayers();
Location GetCurrentLocation();
World GetWorld();
/// <summary>
/// Called when a server was successfully joined
/// </summary>
void OnGameJoined();
/// <summary>
/// This method is called when the protocol handler receives a chat message
/// </summary>
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);
/// <param name="info">Info about this player</param>
void OnPlayerJoin(PlayerInfo info);
/// <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>
/// Called when the server sets the new location for the player
/// </summary>
/// <param name="location">New location of the player</param>
void UpdateLocation(Location location);
/// <summary>
/// This method is called when the connection has been lost
/// </summary>
void OnConnectionLost(ChatBot.DisconnectReason reason, string message);
/// <summary>
/// Called ~10 times per second (10 ticks per second)
/// Useful for updating bots in other parts of the program
/// </summary>
void OnUpdate();
/// <summary>
@ -78,7 +71,6 @@ namespace MinecraftClient.Protocol
/// </summary>
/// <param name="channel">The channel to register.</param>
/// <param name="bot">The bot to register the channel for.</param>
void RegisterPluginChannel(string channel, ChatBot bot);
/// <summary>
@ -86,7 +78,6 @@ namespace MinecraftClient.Protocol
/// </summary>
/// <param name="channel">The channel to unregister.</param>
/// <param name="bot">The bot to unregister the channel for.</param>
void UnregisterPluginChannel(string channel, ChatBot bot);
/// <summary>
@ -97,7 +88,6 @@ namespace MinecraftClient.Protocol
/// <param name="data">The payload for the packet.</param>
/// <param name="sendEvenIfNotRegistered">Whether the packet should be sent even if the server or the client hasn't registered it yet.</param>
/// <returns>Whether the packet was sent: true if it was sent, false if there was a connection error or it wasn't registered.</returns>
bool SendPluginChannelMessage(string channel, byte[] data, bool sendEvenIfNotRegistered = false);
/// <summary>
@ -105,7 +95,6 @@ namespace MinecraftClient.Protocol
/// </summary>
/// <param name="channel">The channel the message was sent on</param>
/// <param name="data">The data from the channel</param>
void OnPluginChannelMessage(string channel, byte[] data);
}
}

View file

@ -0,0 +1,86 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MinecraftClient.Protocol
{
/// <summary>
/// Information about a player (player tab list item)
/// </summary>
public struct PlayerInfo : IComparable<PlayerInfo>
{
private Guid _uuid;
private string _name;
private string _displayName;
public Guid UUID { get { return _uuid; } }
public string Name { get { return _name; } }
public string DisplayName { get { return _displayName; } }
/// <summary>
/// Create a new PlayerInfo structure
/// </summary>
/// <param name="uuid">Player Id</param>
/// <param name="name">Player Name</param>
public PlayerInfo(Guid uuid, string name)
{
_uuid = uuid;
_name = name;
_displayName = name;
}
/// <summary>
/// Create a new PlayerInfo structure
/// </summary>
/// <param name="uuid">Player Id</param>
/// <param name="name">Player Name</param>
/// <param name="displayName">Player Display Name</param>
public PlayerInfo(Guid uuid, string name, string displayName)
: this(uuid, name)
{
_displayName = displayName;
}
/// <summary>
/// String representation of the player
/// </summary>
/// <returns>Player display name</returns>
public override string ToString()
{
return DisplayName;
}
/// <summary>
/// Compare a player to another player
/// </summary>
/// <param name="obj">Other player</param>
/// <returns>TRUE if same player Id</returns>
public override bool Equals(object obj)
{
if (obj is PlayerInfo)
return UUID.Equals(((PlayerInfo)obj).UUID);
return base.Equals(obj);
}
/// <summary>
/// Basic hash function for player, from Player Id
/// </summary>
/// <returns>Interger hash</returns>
/// <remarks>Required when overriding Equals()</remarks>
public override int GetHashCode()
{
return UUID.GetHashCode();
}
/// <summary>
/// Allows sorting players by name
/// </summary>
/// <param name="obj">Other player to compare to</param>
/// <returns>Comparition with the player's name</returns>
int IComparable<PlayerInfo>.CompareTo(PlayerInfo obj)
{
return Name.CompareTo(obj.Name);
}
}
}

View file

@ -96,6 +96,7 @@ namespace MinecraftClient.Protocol
return 60;
case "1.5.2":
return 61;
case "1.6":
case "1.6.0":
return 72;
case "1.6.1":
@ -114,6 +115,7 @@ namespace MinecraftClient.Protocol
case "1.7.9":
case "1.7.10":
return 5;
case "1.8":
case "1.8.0":
case "1.8.1":
case "1.8.2":
@ -125,6 +127,7 @@ namespace MinecraftClient.Protocol
case "1.8.8":
case "1.8.9":
return 47;
case "1.9":
case "1.9.0":
return 107;
case "1.9.1":
@ -134,7 +137,10 @@ namespace MinecraftClient.Protocol
case "1.9.3":
case "1.9.4":
return 110;
case "1.10":
case "1.10.0":
case "1.10.1":
case "1.10.2":
return 210;
default:
return 0;
@ -234,7 +240,6 @@ namespace MinecraftClient.Protocol
/// <param name="accesstoken">Will contain the cached access token previously returned by Minecraft.net</param>
/// <param name="clienttoken">Will contain the cached client token created on login</param>
/// <returns>Returns the status of the token (Valid, Invalid, etc.)</returns>
///
public static LoginResult GetTokenValidation(SessionToken session)
{
try
@ -269,7 +274,6 @@ namespace MinecraftClient.Protocol
/// <param name="clienttoken">Will contain the client token generated before sending to Minecraft.net</param>
/// <param name="uuid">Will contain the player's PlayerID, needed for multiplayer</param>
/// <returns>Returns the status of the new token request (Success, Failure, etc.)</returns>
///
public static LoginResult GetNewToken(SessionToken currentsession, out SessionToken newsession)
{
newsession = new SessionToken();
@ -319,7 +323,6 @@ namespace MinecraftClient.Protocol
/// <param name="accesstoken">Session ID</param>
/// <param name="serverhash">Server ID</param>
/// <returns>TRUE if session was successfully checked</returns>
public static bool SessionCheck(string uuid, string accesstoken, string serverhash)
{
try
@ -348,7 +351,6 @@ namespace MinecraftClient.Protocol
/// <param name="cookies">Cookies for making the request</param>
/// <param name="result">Request result</param>
/// <returns>HTTP Status code</returns>
private static int doHTTPSGet(string host, string endpoint, string cookies, ref string result)
{
List<String> http_request = new List<string>();
@ -372,7 +374,6 @@ namespace MinecraftClient.Protocol
/// <param name="request">Request payload</param>
/// <param name="result">Request result</param>
/// <returns>HTTP Status code</returns>
private static int doHTTPSPost(string host, string endpoint, string request, ref string result)
{
List<String> http_request = new List<string>();
@ -395,7 +396,6 @@ namespace MinecraftClient.Protocol
/// <param name="host">Host to connect to</param>
/// <param name="result">Request result</param>
/// <returns>HTTP Status code</returns>
private static int doHTTPSRequest(List<string> headers, string host, ref string result)
{
string postResult = null;
@ -425,7 +425,6 @@ namespace MinecraftClient.Protocol
/// </summary>
/// <param name="text">Source text</param>
/// <returns>Encoded text</returns>
private static string jsonEncode(string text)
{
StringBuilder result = new StringBuilder();