Basic support for minecraft 1.19 (#2084)

* merge commit from milutinke
* chat signature & encrypted login
* Bug fix :EncryptionResponse format error below 1.18.2
* Implemented chat command signature
* Chat message parsing and verification for 1.19
* Add signature settings
* Update Simplified Chinese Translation
* Clear up comments
* Fix wrong variable naming
* Bug fix: SignatureV2 Processing
This commit is contained in:
BruceChen 2022-08-15 23:55:44 +08:00 committed by GitHub
parent d9f1a77ac2
commit a8bbb1ac76
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
55 changed files with 5218 additions and 1174 deletions

View file

@ -0,0 +1,61 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MinecraftClient.Protocol
{
public class ChatMessage
{
// in 1.19 and above, isSignedChat = true
public readonly bool isSignedChat;
public readonly string content;
public readonly bool isJson;
// 0: chat (chat box), 1: system message (chat box), 2: game info (above hotbar), 3: say command,
// 4: msg command, 5: team msg command, 6: emote command, 7: tellraw command
public readonly int chatType;
public readonly Guid senderUUID;
public readonly bool isSystemChat;
public readonly string? unsignedContent;
public readonly string? displayName;
public readonly string? teamName;
public readonly DateTime? timestamp;
public readonly bool? isSignatureLegal;
public ChatMessage(string content, bool isJson, int chatType, Guid senderUUID, string? unsignedContent, string displayName, string? teamName, long timestamp, bool isSignatureLegal)
{
this.isSignedChat = true;
this.isSystemChat = false;
this.content = content;
this.isJson = isJson;
this.chatType = chatType;
this.senderUUID = senderUUID;
this.unsignedContent = unsignedContent;
this.displayName = displayName;
this.teamName = teamName;
this.timestamp = DateTimeOffset.FromUnixTimeMilliseconds(timestamp).DateTime;
this.isSignatureLegal = isSignatureLegal;
}
public ChatMessage(string content, bool isJson, int chatType, Guid senderUUID, bool isSystemChat = false)
{
this.isSignedChat = isSystemChat;
this.isSystemChat = isSystemChat;
this.content = content;
this.isJson = isJson;
this.chatType = chatType;
this.senderUUID = senderUUID;
}
}
}

View file

@ -18,11 +18,98 @@ namespace MinecraftClient.Protocol
/// <param name="json">JSON serialized text</param>
/// <param name="links">Optional container for links from JSON serialized text</param>
/// <returns>Returns the translated text</returns>
public static string ParseText(string json, List<string> links = null)
public static string ParseText(string json, List<string>? links = null)
{
return JSONData2String(Json.ParseJson(json), "", links);
}
/// <summary>
/// The main function to convert text from MC 1.9+ JSON to MC 1.5.2 formatted text
/// </summary>
/// <param name="message">Message received</param>
/// <param name="links">Optional container for links from JSON serialized text</param>
/// <returns>Returns the translated text</returns>
public static string ParseSignedChat(ChatMessage message, List<string>? links = null)
{
string content;
if (Settings.ShowModifiedChat && message.unsignedContent != null)
content = ChatParser.ParseText(message.unsignedContent, links);
else
content = ChatParser.ParseText(message.content, links);
string sender = message.displayName!;
string text;
List<string> usingData = new();
switch (message.chatType)
{
case 0: // chat (chat box)
usingData.Add(sender);
usingData.Add(content);
text = TranslateString("chat.type.text", usingData);
break;
case 1: // system message (chat box)
text = content;
break;
case 2: // game info (above hotbar)
text = content;
break;
case 3: // say command
usingData.Add(sender);
usingData.Add(content);
text = TranslateString("chat.type.announcement", usingData);
break;
case 4: // msg command
usingData.Add(sender);
usingData.Add(content);
text = TranslateString("commands.message.display.incoming", usingData);
break;
case 5: // team msg command (/teammsg)
usingData.Add(message.teamName!);
usingData.Add(sender);
usingData.Add(content);
text = TranslateString("chat.type.team.text", usingData);
break;
case 6: // emote command (/me)
usingData.Add(sender);
usingData.Add(content);
text = TranslateString("chat.type.emote", usingData);
break;
case 7: // tellraw command
text = content;
break;
default:
text = $"{sender}: {content}";
break;
}
string color = String.Empty;
if (message.isSystemChat)
{
if (Settings.MarkSystemMessage)
color = "§z §r "; // Custom: Background Gray
}
else
{
if ((bool)message.isSignatureLegal!)
{
if (Settings.ShowModifiedChat && message.unsignedContent != null)
{
if (Settings.MarkModifiedMsg)
color = "§x §r "; // Custom: Background Yellow
}
else
{
if (Settings.MarkLegallySignedMsg)
color = "§y §r "; // Custom: Background Green
}
}
else
{
if (Settings.MarkIllegallySignedMsg)
color = "§w §r "; // Custom: Background Red
}
}
return color + text;
}
/// <summary>
/// Get the classic color tag corresponding to a color name
/// </summary>
@ -212,7 +299,7 @@ namespace MinecraftClient.Protocol
/// <param name="colorcode">Allow parent color code to affect child elements (set to "" for function init)</param>
/// <param name="links">Container for links from JSON serialized text</param>
/// <returns>returns the Minecraft-formatted string</returns>
private static string JSONData2String(Json.JSONData data, string colorcode, List<string> links)
private static string JSONData2String(Json.JSONData data, string colorcode, List<string>? links)
{
string extra_result = "";
switch (data.Type)

View file

@ -131,7 +131,7 @@ namespace MinecraftClient.Protocol.Handlers
{
ulong locEncoded = ReadNextULong(cache);
int x, y, z;
if (protocolversion >= Protocol18Handler.MC114Version)
if (protocolversion >= Protocol18Handler.MC_1_14_Version)
{
x = (int)(locEncoded >> 38);
y = (int)(locEncoded & 0xFFF);
@ -186,7 +186,7 @@ namespace MinecraftClient.Protocol.Handlers
/// <returns>The byte array</returns>
public byte[] ReadNextByteArray(Queue<byte> cache)
{
int len = protocolversion >= Protocol18Handler.MC18Version
int len = protocolversion >= Protocol18Handler.MC_1_8_Version
? ReadNextVarInt(cache)
: ReadNextShort(cache);
return ReadData(len, cache);
@ -349,7 +349,7 @@ namespace MinecraftClient.Protocol.Handlers
public Item ReadNextItemSlot(Queue<byte> cache, ItemPalette itemPalette)
{
List<byte> slotData = new List<byte>();
if (protocolversion > Protocol18Handler.MC113Version)
if (protocolversion > Protocol18Handler.MC_1_13_Version)
{
// MC 1.13 and greater
bool itemPresent = ReadNextBool(cache);
@ -385,15 +385,13 @@ namespace MinecraftClient.Protocol.Handlers
{
int entityID = ReadNextVarInt(cache);
Guid entityUUID = Guid.Empty;
if (protocolversion > Protocol18Handler.MC18Version)
if (protocolversion > Protocol18Handler.MC_1_8_Version)
{
entityUUID = ReadNextUUID(cache);
}
EntityType entityType;
// Entity type data type change from byte to varint after 1.14
if (protocolversion > Protocol18Handler.MC113Version)
if (protocolversion > Protocol18Handler.MC_1_13_Version)
{
entityType = entityPalette.FromId(ReadNextVarInt(cache), living);
}
@ -401,27 +399,35 @@ namespace MinecraftClient.Protocol.Handlers
{
entityType = entityPalette.FromId(ReadNextByte(cache), living);
}
Double entityX = ReadNextDouble(cache);
Double entityY = ReadNextDouble(cache);
Double entityZ = ReadNextDouble(cache);
byte entityYaw = ReadNextByte(cache);
byte entityPitch = ReadNextByte(cache);
int metadata = -1;
if (living)
{
entityPitch = ReadNextByte(cache);
if (protocolversion >= Protocol18Handler.MC_1_18_2_Version)
entityYaw = ReadNextByte(cache);
else
entityPitch = ReadNextByte(cache);
}
else
{
int metadata = ReadNextInt(cache);
if (protocolversion >= Protocol18Handler.MC_1_19_Version)
{
metadata = ReadNextVarInt(cache);
entityYaw = ReadNextByte(cache);
}
else
metadata = ReadNextInt(cache);
}
short velocityX = ReadNextShort(cache);
short velocityY = ReadNextShort(cache);
short velocityZ = ReadNextShort(cache);
return new Entity(entityID, entityType, new Location(entityX, entityY, entityZ), entityYaw, entityPitch);
return new Entity(entityID, entityType, new Location(entityX, entityY, entityZ), entityYaw, entityPitch, metadata);
}
/// <summary>
@ -527,7 +533,7 @@ namespace MinecraftClient.Protocol.Handlers
// Increase type ID by 1 if
// - below 1.13
// - type ID larger than 4
if (protocolversion < Protocol18Handler.MC113Version)
if (protocolversion < Protocol18Handler.MC_1_13_Version)
{
if (type > 4)
{
@ -949,7 +955,7 @@ namespace MinecraftClient.Protocol.Handlers
/// <returns>Array ready to send</returns>
public byte[] GetArray(byte[] array)
{
if (protocolversion < Protocol18Handler.MC18Version)
if (protocolversion < Protocol18Handler.MC_1_8_Version)
{
byte[] length = BitConverter.GetBytes((short)array.Length);
Array.Reverse(length); //Endianness
@ -982,7 +988,7 @@ namespace MinecraftClient.Protocol.Handlers
public byte[] GetLocation(Location location)
{
byte[] locationBytes;
if (protocolversion >= Protocol18Handler.MC114Version)
if (protocolversion >= Protocol18Handler.MC_1_14_Version)
{
locationBytes = BitConverter.GetBytes(((((ulong)location.X) & 0x3FFFFFF) << 38) | ((((ulong)location.Z) & 0x3FFFFFF) << 12) | (((ulong)location.Y) & 0xFFF));
}
@ -1000,7 +1006,7 @@ namespace MinecraftClient.Protocol.Handlers
public byte[] GetItemSlot(Item item, ItemPalette itemPalette)
{
List<byte> slotData = new List<byte>();
if (protocolversion > Protocol18Handler.MC113Version)
if (protocolversion > Protocol18Handler.MC_1_13_Version)
{
// MC 1.13 and greater
if (item == null || item.IsEmpty)
@ -1069,5 +1075,10 @@ namespace MinecraftClient.Protocol.Handlers
result.AddRange(array);
return result.ToArray();
}
public string ByteArrayToString(byte[] bytes)
{
return BitConverter.ToString(bytes).Replace("-", " ");
}
}
}

View file

@ -0,0 +1,180 @@
using System.Collections.Generic;
namespace MinecraftClient.Protocol.Handlers.PacketPalettes
{
public class PacketPalette119 : PacketTypePalette
{
private Dictionary<int, PacketTypesIn> typeIn = new Dictionary<int, PacketTypesIn>()
{
{ 0x00, PacketTypesIn.SpawnEntity }, // Changed in 1.19 (Wiki name: Spawn Entity) - DONE
{ 0x01, PacketTypesIn.SpawnExperienceOrb }, //(Wiki name: Spawn Exeprience Orb)
{ 0x02, PacketTypesIn.SpawnPlayer },
{ 0x03, PacketTypesIn.EntityAnimation }, //(Wiki name: Entity Animation (clientbound))
{ 0x04, PacketTypesIn.Statistics }, //(Wiki name: Award Statistics)
{ 0x05, PacketTypesIn.BlockChangedAck }, //Added 1.19 (Wiki name: Acknowledge Block Change) - DONE
{ 0x06, PacketTypesIn.BlockBreakAnimation }, //(Wiki name: Set Block Destroy Stage)
{ 0x07, PacketTypesIn.BlockEntityData },
{ 0x08, PacketTypesIn.BlockAction },
{ 0x09, PacketTypesIn.BlockChange }, //(Wiki name: Block Update)
{ 0x0A, PacketTypesIn.BossBar },
{ 0x0B, PacketTypesIn.ServerDifficulty }, // (Wiki name: Change Difficulty)
{ 0x0C, PacketTypesIn.ChatPreview }, // Added 1.19
{ 0x0D, PacketTypesIn.ClearTiles },
{ 0x0E, PacketTypesIn.TabComplete }, // (Wiki name: Command Suggestions Response)
{ 0x0F, PacketTypesIn.DeclareCommands }, // (Wiki name: Commands)
{ 0x10, PacketTypesIn.CloseWindow }, // (Wiki name: Close Container (clientbound))
{ 0x11, PacketTypesIn.WindowItems }, // (Wiki name: Set Container Content)
{ 0x12, PacketTypesIn.WindowProperty }, // (Wiki name: Set Container Property)
{ 0x13, PacketTypesIn.SetSlot }, // (Wiki name: Set Container Slot)
{ 0x14, PacketTypesIn.SetCooldown },
{ 0x15, PacketTypesIn.PluginMessage }, // (Wiki name: Plugin Message (clientbound))
{ 0x16, PacketTypesIn.NamedSoundEffect }, // Changed in 1.19 (Added "Speed" field) (Wiki name: Custom Sound Effect) - DONE (No need to be implemented)
{ 0x17, PacketTypesIn.Disconnect },
{ 0x18, PacketTypesIn.EntityStatus }, // (Wiki name: Entity Event)
{ 0x19, PacketTypesIn.Explosion }, // Changed in 1.19 (Location fields are now Double instead of Float) (Wiki name: Explosion) - DONE
{ 0x1A, PacketTypesIn.UnloadChunk }, // (Wiki name: Forget Chunk)
{ 0x1B, PacketTypesIn.ChangeGameState }, // (Wiki name: Game Event)
{ 0x1C, PacketTypesIn.OpenHorseWindow }, // (Wiki name: Horse Screen Open)
{ 0x1D, PacketTypesIn.InitializeWorldBorder },
{ 0x1E, PacketTypesIn.KeepAlive },
{ 0x1F, PacketTypesIn.ChunkData },
{ 0x20, PacketTypesIn.Effect }, // (Wiki name: Level Event)
{ 0x21, PacketTypesIn.Particle }, // Changed in 1.19 ("Particle Data" field is now "Max Speed", it's the same Float data type) (Wiki name: Level Particle) - DONE (No need to be implemented)
{ 0x22, PacketTypesIn.UpdateLight }, // (Wiki name: Light Update)
{ 0x23, PacketTypesIn.JoinGame }, // Changed in 1.19 (lot's of changes) (Wiki name: Login (play)) - DONE
{ 0x24, PacketTypesIn.MapData }, // (Wiki name: Map Item Data)
{ 0x25, PacketTypesIn.TradeList }, // (Wiki name: Merchant Offers)
{ 0x26, PacketTypesIn.EntityPosition }, // (Wiki name: Move Entity Position)
{ 0x27, PacketTypesIn.EntityPositionAndRotation }, // (Wiki name: Move Entity Position and Rotation)
{ 0x28, PacketTypesIn.EntityRotation }, // (Wiki name: Move Entity Rotation)
{ 0x29, PacketTypesIn.VehicleMove }, // (Wiki name: Move Vehicle)
{ 0x2A, PacketTypesIn.OpenBook },
{ 0x2B, PacketTypesIn.OpenWindow }, // (Wiki name: Open Screen)
{ 0x2C, PacketTypesIn.OpenSignEditor },
{ 0x2D, PacketTypesIn.Ping }, // (Wiki name: Ping (play))
{ 0x2E, PacketTypesIn.CraftRecipeResponse }, // (Wiki name: Place Ghost Recipe)
{ 0x2F, PacketTypesIn.PlayerAbilities },
{ 0x30, PacketTypesIn.ChatMessage }, // Changed in 1.19 (Completely changed) (Wiki name: Player Chat Message)
{ 0x31, PacketTypesIn.EndCombatEvent }, // (Wiki name: Player Combat End)
{ 0x32, PacketTypesIn.EnterCombatEvent }, // (Wiki name: Player Combat Enter)
{ 0x33, PacketTypesIn.DeathCombatEvent }, // (Wiki name: Player Combat Kill)
{ 0x34, PacketTypesIn.PlayerInfo }, // Changed in 1.19 (Heavy changes) - DONE
{ 0x35, PacketTypesIn.FacePlayer }, // (Wiki name: Player Look At)
{ 0x36, PacketTypesIn.PlayerPositionAndLook }, // (Wiki name: Player Position)
{ 0x37, PacketTypesIn.UnlockRecipes }, // (Wiki name: Recipe)
{ 0x38, PacketTypesIn.DestroyEntity }, // (Wiki name: Remove Entites)
{ 0x39, PacketTypesIn.RemoveEntityEffect },
{ 0x3A, PacketTypesIn.ResourcePackSend }, // (Wiki name: Resource Pack)
{ 0x3B, PacketTypesIn.Respawn }, // Changed in 1.19 (Heavy changes) - DONE
{ 0x3C, PacketTypesIn.EntityHeadLook }, // (Wiki name: Rotate Head)
{ 0x3D, PacketTypesIn.MultiBlockChange }, // (Wiki name: Sections Block Update)
{ 0x3E, PacketTypesIn.SelectAdvancementTab },
{ 0x3F, PacketTypesIn.ServerData }, // Added in 1.19
{ 0x40, PacketTypesIn.ActionBar }, // (Wiki name: Set Action Bar Text)
{ 0x41, PacketTypesIn.WorldBorderCenter }, // (Wiki name: Set Border Center)
{ 0x42, PacketTypesIn.WorldBorderLerpSize },
{ 0x43, PacketTypesIn.WorldBorderSize }, // (Wiki name: Set World Border Size)
{ 0x44, PacketTypesIn.WorldBorderWarningDelay }, // (Wiki name: Set World Border Warning Delay)
{ 0x45, PacketTypesIn.WorldBorderWarningReach }, // (Wiki name: Set Border Warning Distance)
{ 0x46, PacketTypesIn.Camera }, // (Wiki name: Set Camera)
{ 0x47, PacketTypesIn.HeldItemChange }, // (Wiki name: Set Carried Item (clientbound))
{ 0x48, PacketTypesIn.UpdateViewPosition }, // (Wiki name: Set Chunk Cache Center)
{ 0x49, PacketTypesIn.UpdateViewDistance }, // (Wiki name: Set Chunk Cache Radius)
{ 0x4A, PacketTypesIn.SpawnPosition }, // (Wiki name: Set Default Spawn Position)
{ 0x4B, PacketTypesIn.SetDisplayChatPreview }, // Added in 1.19 (Wiki name: Set Display Chat Preview)
{ 0x4C, PacketTypesIn.DisplayScoreboard }, // (Wiki name: Set Display Objective)
{ 0x4D, PacketTypesIn.EntityMetadata }, // (Wiki name: Set Entity Metadata)
{ 0x4E, PacketTypesIn.AttachEntity }, // (Wiki name: Set Entity Link)
{ 0x4F, PacketTypesIn.EntityVelocity }, // (Wiki name: Set Entity Motion)
{ 0x50, PacketTypesIn.EntityEquipment }, // (Wiki name: Set Equipment)
{ 0x51, PacketTypesIn.SetExperience },
{ 0x52, PacketTypesIn.UpdateHealth }, // (Wiki name: Set Health)
{ 0x53, PacketTypesIn.ScoreboardObjective }, // (Wiki name: Set Objective)
{ 0x54, PacketTypesIn.SetPassengers },
{ 0x55, PacketTypesIn.Teams }, // (Wiki name: Set Player Team)
{ 0x56, PacketTypesIn.UpdateScore }, // (Wiki name: Set Score)
{ 0x57, PacketTypesIn.UpdateSimulationDistance }, // (Wiki name: Set Simulation Distance)
{ 0x58, PacketTypesIn.SetTitleSubTitle }, // (Wiki name: Set Subtitle Test)
{ 0x59, PacketTypesIn.TimeUpdate }, // (Wiki name: Set Time)
{ 0x5A, PacketTypesIn.SetTitleText }, // (Wiki name: Set Title)
{ 0x5B, PacketTypesIn.SetTitleTime }, // (Wiki name: Set Titles Animation)
{ 0x5C, PacketTypesIn.EntitySoundEffect }, // (Wiki name: Sound Entity)
{ 0x5D, PacketTypesIn.SoundEffect }, // Changed in 1.19 (Added "Seed" field) (Wiki name: Sound Effect) - DONE (No need to be implemented)
{ 0x5E, PacketTypesIn.StopSound },
{ 0x5F, PacketTypesIn.SystemChat }, // Added in 1.19 (Wiki name: System Chat Message)
{ 0x60, PacketTypesIn.PlayerListHeaderAndFooter }, // (Wiki name: Tab List)
{ 0x61, PacketTypesIn.NBTQueryResponse }, // (Wiki name: Tab Query)
{ 0x62, PacketTypesIn.CollectItem }, // (Wiki name: Take Item Entity)
{ 0x63, PacketTypesIn.EntityTeleport }, // (Wiki name: Teleport Entity)
{ 0x64, PacketTypesIn.Advancements }, // (Wiki name: Update Advancements)
{ 0x65, PacketTypesIn.EntityProperties }, // (Wiki name: Update Attributes)
{ 0x66, PacketTypesIn.EntityEffect }, // Changed in 1.19 (Added "Has Factor Data" and "Factor Codec" fields) (Wiki name: Entity Effect) - DONE
{ 0x67, PacketTypesIn.DeclareRecipes }, // (Wiki name: Update Recipes)
{ 0x68, PacketTypesIn.Tags }, // (Wiki name: Update Tags)
};
private Dictionary<int, PacketTypesOut> typeOut = new Dictionary<int, PacketTypesOut>()
{
{ 0x00, PacketTypesOut.TeleportConfirm }, // (Wiki name: Confirm Teleportation)
{ 0x01, PacketTypesOut.QueryBlockNBT }, // (Wiki name: Query Block Entity Tag)
{ 0x02, PacketTypesOut.SetDifficulty }, // (Wiki name: Change Difficutly)
{ 0x03, PacketTypesOut.ChatCommand }, // Added in 1.19
{ 0x04, PacketTypesOut.ChatMessage }, // Changed in 1.19 (Completely changed) (Wiki name: Chat)
{ 0x05, PacketTypesOut.ChatPreview }, // Added in 1.19 (Wiki name: Chat Preview (serverbound))
{ 0x06, PacketTypesOut.ClientStatus }, // (Wiki name: Client Command)
{ 0x07, PacketTypesOut.ClientSettings }, // (Wiki name: Client Information)
{ 0x08, PacketTypesOut.TabComplete }, // (Wiki name: Command Suggestions Request)
{ 0x09, PacketTypesOut.ClickWindowButton }, // (Wiki name: Click Container Button)
{ 0x0A, PacketTypesOut.ClickWindow }, // (Wiki name: Click Container)
{ 0x0B, PacketTypesOut.CloseWindow }, // (Wiki name: Close Container (serverbound))
{ 0x0C, PacketTypesOut.PluginMessage }, // (Wiki name: Plugin Message (serverbound))
{ 0x0D, PacketTypesOut.EditBook },
{ 0x0E, PacketTypesOut.EntityNBTRequest }, // (Wiki name: Query Entity Tag)
{ 0x0F, PacketTypesOut.InteractEntity }, // (Wiki name: Interact)
{ 0x10, PacketTypesOut.GenerateStructure }, // (Wiki name: Jigsaw Generate)
{ 0x11, PacketTypesOut.KeepAlive },
{ 0x12, PacketTypesOut.LockDifficulty },
{ 0x13, PacketTypesOut.PlayerPosition }, // (Wiki name: Move Player Position)
{ 0x14, PacketTypesOut.PlayerPositionAndRotation }, // (Wiki name: Set Player Position and Rotation)
{ 0x15, PacketTypesOut.PlayerRotation }, // (Wiki name: Set Player Rotation)
{ 0x16, PacketTypesOut.PlayerMovement }, // (Wiki name: Set Player On Ground)
{ 0x17, PacketTypesOut.VehicleMove }, // (Wiki name: Move Vehicle (serverbound))
{ 0x18, PacketTypesOut.SteerBoat }, // (Wiki name: Paddle Boat)
{ 0x19, PacketTypesOut.PickItem },
{ 0x1A, PacketTypesOut.CraftRecipeRequest }, // (Wiki name: Place recipe)
{ 0x1B, PacketTypesOut.PlayerAbilities },
{ 0x1C, PacketTypesOut.PlayerDigging }, // Changed in 1.19 (Added a "Sequence" field) (Wiki name: Player Action) - DONE
{ 0x1D, PacketTypesOut.EntityAction }, // (Wiki name: Player Command)
{ 0x1E, PacketTypesOut.SteerVehicle }, // (Wiki name: Player Input)
{ 0x1F, PacketTypesOut.Pong }, // (Wiki name: Pong (play))
{ 0x20, PacketTypesOut.SetDisplayedRecipe }, // (Wiki name: Recipe Book Change Settings)
{ 0x21, PacketTypesOut.SetRecipeBookState }, // (Wiki name: Recipe Book Seen Recipe)
{ 0x22, PacketTypesOut.NameItem }, // (Wiki name: Rename Item)
{ 0x23, PacketTypesOut.ResourcePackStatus }, // (Wiki name: Resource Pack (serverbound))
{ 0x24, PacketTypesOut.AdvancementTab }, // (Wiki name: Seen Advancements)
{ 0x25, PacketTypesOut.SelectTrade },
{ 0x26, PacketTypesOut.SetBeaconEffect }, // Changed in 1.19 (Added a "Secondary Effect Present" and "Secondary Effect" fields) (Wiki name: Set Beacon) - DONE - (No need to be implemented)
{ 0x27, PacketTypesOut.HeldItemChange }, // (Wiki name: Set Carried Item (serverbound))
{ 0x28, PacketTypesOut.UpdateCommandBlock }, // (Wiki name: Set Command Block)
{ 0x29, PacketTypesOut.UpdateCommandBlockMinecart },
{ 0x2A, PacketTypesOut.CreativeInventoryAction }, // (Wiki name: Set Creative Mode Slot)
{ 0x2B, PacketTypesOut.UpdateJigsawBlock }, // (Wiki name: Set Jigsaw Block)
{ 0x2C, PacketTypesOut.UpdateStructureBlock }, // (Wiki name: Set Structure Block)
{ 0x2D, PacketTypesOut.UpdateSign }, // (Wiki name: Sign Update)
{ 0x2E, PacketTypesOut.Animation }, // (Wiki name: Swing)
{ 0x2F, PacketTypesOut.Spectate }, // (Wiki name: Teleport To Entity)
{ 0x30, PacketTypesOut.PlayerBlockPlacement }, // Changed in 1.19 (Added a "Sequence" field) (Wiki name: Use Item On) - DONE
{ 0x31, PacketTypesOut.UseItem }, // Changed in 1.19 (Added a "Sequence" field) (Wiki name: Use Item) - DONE
};
protected override Dictionary<int, PacketTypesIn> GetListIn()
{
return typeIn;
}
protected override Dictionary<int, PacketTypesOut> GetListOut()
{
return typeOut;
}
}
}

View file

@ -51,30 +51,32 @@ namespace MinecraftClient.Protocol.Handlers
public PacketTypePalette GetTypeHandler(int protocol)
{
PacketTypePalette p;
if (protocol > Protocol18Handler.MC1182Version)
if (protocol > Protocol18Handler.MC_1_19_Version)
throw new NotImplementedException(Translations.Get("exception.palette.packet"));
if (protocol <= Protocol18Handler.MC18Version)
if (protocol <= Protocol18Handler.MC_1_8_Version)
p = new PacketPalette17();
else if (protocol <= Protocol18Handler.MC1112Version)
else if (protocol <= Protocol18Handler.MC_1_11_2_Version)
p = new PacketPalette110();
else if (protocol <= Protocol18Handler.MC112Version)
else if (protocol <= Protocol18Handler.MC_1_12_Version)
p = new PacketPalette112();
else if (protocol <= Protocol18Handler.MC1122Version)
else if (protocol <= Protocol18Handler.MC_1_12_2_Version)
p = new PacketPalette1122();
else if (protocol <= Protocol18Handler.MC114Version)
else if (protocol <= Protocol18Handler.MC_1_14_Version)
p = new PacketPalette113();
else if (protocol <= Protocol18Handler.MC115Version)
else if (protocol <= Protocol18Handler.MC_1_15_Version)
p = new PacketPalette114();
else if (protocol <= Protocol18Handler.MC1152Version)
else if (protocol <= Protocol18Handler.MC_1_15_2_Version)
p = new PacketPalette115();
else if (protocol <= Protocol18Handler.MC1161Version)
else if (protocol <= Protocol18Handler.MC_1_16_1_Version)
p = new PacketPalette116();
else if (protocol <= Protocol18Handler.MC1165Version)
else if (protocol <= Protocol18Handler.MC_1_16_5_Version)
p = new PacketPalette1162();
else if (protocol <= Protocol18Handler.MC1171Version)
else if (protocol <= Protocol18Handler.MC_1_17_1_Version)
p = new PacketPalette117();
else
else if (protocol <= Protocol18Handler.MC_1_18_2_Version)
p = new PacketPalette118();
else
p = new PacketPalette119();
p.SetForgeEnabled(this.forgeEnabled);
return p;

View file

@ -127,5 +127,12 @@ namespace MinecraftClient.Protocol.Handlers
UpdateEntityNBT, // For 1.8 or below
Unknown, // For old version packet that have been removed and not used by mcc
UpdateSimulationDistance,
// 1.19 Additions
BlockChangedAck,
ChatPreview,
ServerData,
SetDisplayChatPreview,
SystemChat
}
}

View file

@ -62,6 +62,10 @@ namespace MinecraftClient.Protocol.Handlers
GenerateStructure, // Added in 1.16
SetDisplayedRecipe, // Added in 1.16.2
SetRecipeBookState, // Added in 1.16.2
Unknown // For old version packet that have been removed and not used by mcc
Unknown, // For old version packet that have been removed and not used by mcc
// Added in 1.19
ChatCommand,
ChatPreview,
}
}

View file

@ -9,6 +9,7 @@ using MinecraftClient.Proxy;
using System.Security.Cryptography;
using MinecraftClient.Mapping;
using MinecraftClient.Inventory;
using MinecraftClient.Protocol.Keys;
namespace MinecraftClient.Protocol.Handlers
{
@ -62,12 +63,12 @@ namespace MinecraftClient.Protocol.Handlers
private void Updater(object? o)
{
if (((CancellationToken) o!).IsCancellationRequested)
if (((CancellationToken)o!).IsCancellationRequested)
return;
try
{
while (!((CancellationToken) o!).IsCancellationRequested)
while (!((CancellationToken)o!).IsCancellationRequested)
{
do
{
@ -79,9 +80,9 @@ namespace MinecraftClient.Protocol.Handlers
catch (SocketException) { }
catch (ObjectDisposedException) { }
if (((CancellationToken) o!).IsCancellationRequested)
if (((CancellationToken)o!).IsCancellationRequested)
return;
handler.OnConnectionLost(ChatBot.DisconnectReason.ConnectionLost, "");
}
@ -102,7 +103,8 @@ namespace MinecraftClient.Protocol.Handlers
int nbr = 0;
switch (id)
{
case 0x00: byte[] keepalive = new byte[5] { 0, 0, 0, 0, 0 };
case 0x00:
byte[] keepalive = new byte[5] { 0, 0, 0, 0, 0 };
Receive(keepalive, 1, 4, SocketFlags.None);
handler.OnServerKeepAlive();
Send(keepalive); break;
@ -110,7 +112,7 @@ namespace MinecraftClient.Protocol.Handlers
case 0x02: readData(1); readNextString(); readNextString(); readData(4); break;
case 0x03:
string message = readNextString();
handler.OnTextReceived(message, protocolversion >= 72); break;
handler.OnTextReceived(new ChatMessage(message, protocolversion >= 72, 0, Guid.Empty)); break;
case 0x04: readData(16); break;
case 0x05: readData(6); readNextItemSlot(); break;
case 0x06: readData(12); break;
@ -181,7 +183,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(name, FakeUUID)); } 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;
@ -191,18 +193,20 @@ namespace MinecraftClient.Protocol.Handlers
case 0xCF: if (protocolversion > 51) { readNextString(); readData(1); readNextString(); } readData(4); break;
case 0xD0: if (protocolversion > 51) { readData(1); readNextString(); } break;
case 0xD1: if (protocolversion > 51) { readNextTeamData(); } break;
case 0xFA: string channel = readNextString();
case 0xFA:
string channel = readNextString();
byte[] payload = readNextByteArray();
handler.OnPluginChannelMessage(channel, payload);
break;
case 0xFF: string reason = readNextString();
case 0xFF:
string reason = readNextString();
handler.OnConnectionLost(ChatBot.DisconnectReason.InGameKick, reason); break;
default: return false; //unknown packet!
}
return true; //packet has been successfully skipped
}
private void StartUpdating()
private void StartUpdating()
{
netRead = new(new Thread(new ParameterizedThreadStart(Updater)), new CancellationTokenSource());
netRead.Item1.Name = "ProtocolPacketHandler";
@ -553,7 +557,7 @@ namespace MinecraftClient.Protocol.Handlers
}
}
public bool Login()
public bool Login(PlayerKeyPair playerKeyPair)
{
if (Handshake(handler.GetUserUUID(), handler.GetUsername(), handler.GetSessionID(), handler.GetServerHost(), handler.GetServerPort()))
{
@ -639,7 +643,7 @@ namespace MinecraftClient.Protocol.Handlers
return protocolversion;
}
public bool SendChatMessage(string message)
public bool SendChatMessage(string message, PlayerKeyPair? playerKeyPair)
{
if (String.IsNullOrEmpty(message))
return true;
@ -674,12 +678,12 @@ namespace MinecraftClient.Protocol.Handlers
}
catch (SocketException) { return false; }
}
public bool SendUpdateSign(Location location, string line1, string line2, string line3, string line4)
{
return false; //Currently not implemented
}
public bool SendBrandInfo(string brandInfo)
{
return false; //Only supported since MC 1.7
@ -709,18 +713,18 @@ namespace MinecraftClient.Protocol.Handlers
{
return false; //Currently not implemented
}
public bool SendInteractEntity(int EntityID, int type, int hand)
{
return false; //Currently not implemented
}
public bool UpdateCommandBlock(Location location, string command, CommandBlockMode mode, CommandBlockFlags flags)
{
return false; //Currently not implemented
}
public bool SendUseItem(int hand)
public bool SendUseItem(int hand, int sequenceId)
{
return false; //Currently not implemented
}
@ -735,7 +739,7 @@ namespace MinecraftClient.Protocol.Handlers
return false; //Currently not implemented
}
public bool SendCreativeInventoryAction(int slot, ItemType item, int count, Dictionary<string, object> nbt)
public bool SendCreativeInventoryAction(int slot, ItemType item, int count, Dictionary<string, object>? nbt)
{
return false; //Currently not implemented
}
@ -745,7 +749,7 @@ namespace MinecraftClient.Protocol.Handlers
return false; //Currently not implemented
}
public bool SendPlayerBlockPlacement(int hand, Location location, Direction face)
public bool SendPlayerBlockPlacement(int hand, Location location, Direction face, int sequenceId)
{
return false; //Currently not implemented
}
@ -755,7 +759,7 @@ namespace MinecraftClient.Protocol.Handlers
return false; //Currently not implemented
}
public bool SendPlayerDigging(int status, Location location, Direction face)
public bool SendPlayerDigging(int status, Location location, Direction face, int sequenceId)
{
return false; //Currently not implemented
}
@ -767,7 +771,8 @@ namespace MinecraftClient.Protocol.Handlers
/// <param name="data">packet Data</param>
public bool SendPluginChannelPacket(string channel, byte[] data)
{
try {
try
{
byte[] channelLength = BitConverter.GetBytes((short)channel.Length);
Array.Reverse(channelLength);

File diff suppressed because it is too large Load diff

View file

@ -179,7 +179,7 @@ namespace MinecraftClient.Protocol.Handlers
if (discriminator != FMLHandshakeDiscriminator.RegistryData)
return false;
if (protocolversion < Protocol18Handler.MC18Version)
if (protocolversion < Protocol18Handler.MC_1_8_Version)
{
// 1.7.10 and below have one registry
// with blocks and items.
@ -440,7 +440,7 @@ namespace MinecraftClient.Protocol.Handlers
/// <param name="jsonData">JSON data returned by the server</param>
/// <param name="forgeInfo">ForgeInfo to populate</param>
/// <returns>True if the server is running Forge</returns>
public static bool ServerInfoCheckForge(Json.JSONData jsonData, ref ForgeInfo forgeInfo)
public static bool ServerInfoCheckForge(Json.JSONData jsonData, ref ForgeInfo? forgeInfo)
{
return ServerInfoCheckForgeSub(jsonData, ref forgeInfo, FMLVersion.FML) // MC 1.12 and lower
|| ServerInfoCheckForgeSub(jsonData, ref forgeInfo, FMLVersion.FML2); // MC 1.13 and greater
@ -477,7 +477,7 @@ namespace MinecraftClient.Protocol.Handlers
/// <param name="forgeInfo">ForgeInfo to populate</param>
/// <param name="fmlVersion">Forge protocol version</param>
/// <returns>True if the server is running Forge</returns>
private static bool ServerInfoCheckForgeSub(Json.JSONData jsonData, ref ForgeInfo forgeInfo, FMLVersion fmlVersion)
private static bool ServerInfoCheckForgeSub(Json.JSONData jsonData, ref ForgeInfo? forgeInfo, FMLVersion fmlVersion)
{
string forgeDataTag;
string versionField;

View file

@ -40,7 +40,7 @@ namespace MinecraftClient.Protocol.Handlers
/// <param name="cache">Cache for reading chunk data</param>
public void ProcessChunkColumnData(int chunkX, int chunkZ, ushort chunkMask, ushort chunkMask2, bool hasSkyLight, bool chunksContinuous, int currentDimension, Queue<byte> cache)
{
if (protocolversion >= Protocol18Handler.MC19Version)
if (protocolversion >= Protocol18Handler.MC_1_9_Version)
{
// 1.9 and above chunk format
// Unloading chunks is handled by a separate packet
@ -49,7 +49,7 @@ namespace MinecraftClient.Protocol.Handlers
if ((chunkMask & (1 << chunkY)) != 0)
{
// 1.14 and above Non-air block count inside chunk section, for lighting purposes
if (protocolversion >= Protocol18Handler.MC114Version)
if (protocolversion >= Protocol18Handler.MC_1_14_Version)
dataTypes.ReadNextShort(cache);
byte bitsPerBlock = dataTypes.ReadNextByte(cache);
@ -62,7 +62,7 @@ namespace MinecraftClient.Protocol.Handlers
// MC 1.9 to 1.12 will set palette length field to 0 when palette
// is not used, MC 1.13+ does not send the field at all in this case
int paletteLength = 0; // Assume zero when length is absent
if (usePalette || protocolversion < Protocol18Handler.MC113Version)
if (usePalette || protocolversion < Protocol18Handler.MC_1_13_Version)
paletteLength = dataTypes.ReadNextVarInt(cache);
int[] palette = new int[paletteLength];
@ -101,7 +101,7 @@ namespace MinecraftClient.Protocol.Handlers
if ((startOffset + bitsPerBlock) > 64)
{
if (protocolversion >= Protocol18Handler.MC116Version)
if (protocolversion >= Protocol18Handler.MC_1_16_Version)
{
// In MC 1.16+, padding is applied to prevent overlapping between Longs:
// [ LONG INTEGER ][ LONG INTEGER ]
@ -170,7 +170,7 @@ namespace MinecraftClient.Protocol.Handlers
});
//Pre-1.14 Lighting data
if (protocolversion < Protocol18Handler.MC114Version)
if (protocolversion < Protocol18Handler.MC_1_14_Version)
{
//Skip block light
dataTypes.ReadData((Chunk.SizeX * Chunk.SizeY * Chunk.SizeZ) / 2, cache);
@ -186,7 +186,7 @@ namespace MinecraftClient.Protocol.Handlers
// Don't worry about skipping remaining data since there is no useful data afterwards in 1.9
// (plus, it would require parsing the tile entity lists' NBT)
}
else if (protocolversion >= Protocol18Handler.MC18Version)
else if (protocolversion >= Protocol18Handler.MC_1_8_Version)
{
// 1.8 chunk format
if (chunksContinuous && chunkMask == 0)

View file

@ -82,7 +82,7 @@ namespace MinecraftClient.Protocol.Handlers
Receive(cache, 0, length, SocketFlags.None);
return cache;
}
return new byte[] { };
return Array.Empty<byte>();
}
/// <summary>

View file

@ -5,6 +5,7 @@ using System.Text;
using MinecraftClient.Crypto;
using MinecraftClient.Mapping;
using MinecraftClient.Inventory;
using MinecraftClient.Protocol.Keys;
namespace MinecraftClient.Protocol
{
@ -21,7 +22,7 @@ namespace MinecraftClient.Protocol
/// Start the login procedure once connected to the server
/// </summary>
/// <returns>True if login was successful</returns>
bool Login();
bool Login(PlayerKeyPair playerKeyPair);
/// <summary>
/// Disconnect from the server
@ -48,7 +49,7 @@ namespace MinecraftClient.Protocol
/// </summary>
/// <param name="message">Text to send</param>
/// <returns>True if successfully sent</returns>
bool SendChatMessage(string message);
bool SendChatMessage(string message, PlayerKeyPair? playerKeyPair = null);
/// <summary>
/// Allow to respawn after death
@ -154,8 +155,9 @@ namespace MinecraftClient.Protocol
/// Send a use item packet to the server
/// </summary>
/// <param name="hand">0: main hand, 1: off hand</param>
/// <param name="sequenceId">Sequence ID used for synchronization</param>
/// <returns>True if packet was successfully sent</returns>
bool SendUseItem(int hand);
bool SendUseItem(int hand, int sequenceId);
/// <summary>
/// Send a click window slot packet to the server
@ -176,7 +178,7 @@ namespace MinecraftClient.Protocol
/// <param name="count">Item count</param>
/// <param name="nbt">Optional item NBT</param>
/// <returns>TRUE if item given successfully</returns>
bool SendCreativeInventoryAction(int slot, ItemType itemType, int count, Dictionary<string, object> nbt);
bool SendCreativeInventoryAction(int slot, ItemType itemType, int count, Dictionary<string, object>? nbt);
/// <summary>
/// Plays animation
@ -198,8 +200,9 @@ namespace MinecraftClient.Protocol
/// <param name="hand">0: main hand, 1: off hand</param>
/// <param name="location">Location to place block at</param>
/// <param name="face">Block face</param>
/// <param name="sequenceId">Sequence ID (use for synchronization)</param>
/// <returns>True if packet was successfully sent</returns>
bool SendPlayerBlockPlacement(int hand, Location location, Direction face);
bool SendPlayerBlockPlacement(int hand, Location location, Direction face, int sequenceId);
/// <summary>
/// Send player blog digging packet to the server. This packet needs to be called at least twice: Once to begin digging, then a second time to finish digging
@ -207,8 +210,9 @@ namespace MinecraftClient.Protocol
/// <param name="status">0 to start digging, 1 to cancel, 2 to finish ( https://wiki.vg/Protocol#Player_Digging )</param>
/// <param name="location">Location</param>
/// <param name="face">Block face</param>
/// <param name="sequenceId">Sequence ID (use for synchronization)</param>
/// <returns>True if packet was succcessfully sent</returns>
bool SendPlayerDigging(int status, Location location, Direction face);
bool SendPlayerDigging(int status, Location location, Direction face, int sequenceId);
/// <summary>
/// Change text on a sign

View file

@ -26,8 +26,10 @@ namespace MinecraftClient.Protocol
string GetSessionID();
string[] GetOnlinePlayers();
Dictionary<string, string> GetOnlinePlayersWithUUID();
PlayerInfo? GetPlayerInfo(Guid uuid);
Location GetCurrentLocation();
World GetWorld();
bool GetIsSupportPreviewsChat();
bool GetTerrainEnabled();
bool SetTerrainEnabled(bool enabled);
bool GetInventoryEnabled();
@ -78,11 +80,10 @@ namespace MinecraftClient.Protocol
void OnGameJoined();
/// <summary>
/// This method is called when the protocol handler receives a chat message
/// Received chat/system message from the server
/// </summary>
/// <param name="text">Text received from the server</param>
/// <param name="isJson">TRUE if the text is JSON-Encoded</param>
void OnTextReceived(string text, bool isJson);
/// <param name="message">Message received</param>
public void OnTextReceived(ChatMessage message);
/// <summary>
/// Will be called every animations of the hit and place block
@ -91,6 +92,12 @@ namespace MinecraftClient.Protocol
/// <param name="animation">0 = LMB, 1 = RMB (RMB Corrent not work)</param>
void OnEntityAnimation(int entityID, byte animation);
/// <summary>
/// Will be called when a Synchronization sequence is recevied, this sequence need to be sent when breaking or placing blocks
/// </summary>
/// <param name="sequenceId">Sequence ID</param>
void OnBlockChangeAck(int sequenceId);
/// <summary>
/// Will be called every player break block in gamemode 0
/// </summary>
@ -109,6 +116,22 @@ namespace MinecraftClient.Protocol
/// </summary>
void OnServerKeepAlive();
/// <summary>
/// This method is called when the protocol handler receives server data
/// </summary>
/// <param name="hasMotd">Indicates if the server has a motd message</param>
/// <param name="motd">Server MOTD message</param>
/// <param name="hasIcon">Indicates if the server has a an icon</param>
/// <param name="iconBase64">Server icon in Base 64 format</param>
/// <param name="previewsChat">Indicates if the server previews chat</param>
void OnServerDataRecived(bool hasMotd, string motd, bool hasIcon, string iconBase64, bool previewsChat);
/// <summary>
/// This method is called when the protocol handler receives "Set Display Chat Preview" packet
/// </summary>
/// <param name="previewsChat">Indicates if the server previews chat</param>
public void OnChatPreviewSettingUpdate(bool previewsChat);
/// <summary>
/// Called when an inventory is opened
/// </summary>
@ -125,11 +148,10 @@ namespace MinecraftClient.Protocol
void OnRespawn();
/// <summary>
/// This method is called when a new player joins the game
/// 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>
void OnPlayerJoin(Guid uuid, string name);
/// <param name="player">player info</param>
public void OnPlayerJoin(PlayerInfo player);
/// <summary>
/// This method is called when a player has left the game
@ -346,7 +368,7 @@ namespace MinecraftClient.Protocol
/// </summary>
/// <param name="EntityID">Player entity ID</param>
void OnReceivePlayerEntityID(int EntityID);
/// <summary>
/// Called when the Entity use effects
/// </summary>
@ -355,8 +377,10 @@ namespace MinecraftClient.Protocol
/// <param name="amplifier">effect amplifier</param>
/// <param name="duration">effect duration</param>
/// <param name="flags">effect flags</param>
void OnEntityEffect(int entityid, Effects effect, int amplifier, int duration, byte flags);
/// <param name="hasFactorData">has factor data</param>
/// <param name="factorCodec">factorCodec</param>
void OnEntityEffect(int entityid, Effects effect, int amplifier, int duration, byte flags, bool hasFactorData, Dictionary<String, object>? factorCodec);
/// <summary>
/// Called when coreboardObjective
/// </summary>
@ -385,5 +409,13 @@ namespace MinecraftClient.Protocol
/// <param name="isRegularVillager">True if regular villagers and false if the wandering trader.</param>
/// <param name="canRestock">If the villager can restock his trades at a workstation, True for regular villagers and false for the wandering trader.</param>
void OnTradeList(int windowID, List<VillagerTrade> trades, VillagerInfo villagerInfo);
/// <summary>
/// This method is called when the protocol handler receives "Login Success" packet
/// </summary>
/// <param name="uuid">The player's UUID received from the server</param>
/// <param name="userName">The player's username received from the server</param>
/// <param name="playerProperty">Tuple<Name, Value, Signature(empty if there is no signature)></param>
public void OnLoginSuccess(Guid uuid, string userName, Tuple<string, string, string>[]? playerProperty);
}
}

View file

@ -51,7 +51,7 @@ namespace MinecraftClient.Protocol
}
/// <summary>
/// Perform request to obtain access token by code or by refresh token
/// Perform request to obtain access token by code or by refresh token
/// </summary>
/// <param name="postData">Complete POST data for the request</param>
/// <returns></returns>
@ -72,7 +72,7 @@ namespace MinecraftClient.Protocol
string accessToken = jsonData.Properties["access_token"].StringValue;
string refreshToken = jsonData.Properties["refresh_token"].StringValue;
int expiresIn = int.Parse(jsonData.Properties["expires_in"].StringValue);
// Extract email from JWT
string payload = JwtPayloadDecode.GetPayload(jsonData.Properties["id_token"].StringValue);
var jsonPayload = Json.ParseJson(payload);
@ -391,6 +391,7 @@ namespace MinecraftClient.Protocol
string jsonString = response.Body;
Json.JSONData json = Json.ParseJson(jsonString);
return json.Properties["access_token"].StringValue;
}

View file

@ -0,0 +1,83 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MinecraftClient.Protocol.Keys;
namespace MinecraftClient.Protocol
{
public class PlayerInfo
{
public readonly Guid UUID;
public readonly string Name;
// Tuple<Name, Value, Signature(empty if there is no signature)
public readonly Tuple<string, string, string>[]? Property;
public int Gamemode;
public int Ping;
public string? DisplayName;
private readonly PublicKey? PublicKey;
private readonly DateTime? KeyExpiresAt;
public PlayerInfo(Guid uuid, string name, Tuple<string, string, string>[]? property, int gamemode, int ping, string? displayName, long? timeStamp, byte[]? publicKey, byte[]? signature)
{
UUID = uuid;
Name = name;
if (property != null)
Property = property;
Gamemode = gamemode;
Ping = ping;
DisplayName = displayName;
if (timeStamp != null && publicKey != null && signature != null)
{
DateTimeOffset dateTimeOffset = DateTimeOffset.FromUnixTimeMilliseconds((long)timeStamp);
KeyExpiresAt = dateTimeOffset.UtcDateTime;
try
{
PublicKey = new PublicKey(publicKey, signature);
}
catch (System.Security.Cryptography.CryptographicException)
{
PublicKey = null;
}
}
}
public PlayerInfo(string name, Guid uuid)
{
Name = name;
UUID = uuid;
Gamemode = -1;
Ping = 0;
}
public bool IsKeyVaild()
{
return PublicKey != null && DateTime.Now.ToUniversalTime() > this.KeyExpiresAt;
}
public bool VerifyMessage(string message, Guid uuid, long timestamp, long salt, ref byte[] signature)
{
if (PublicKey == null)
return false;
else
{
string uuidString = uuid.ToString().Replace("-", string.Empty);
DateTimeOffset timeOffset = DateTimeOffset.FromUnixTimeMilliseconds(timestamp);
byte[] saltByte = BitConverter.GetBytes(salt);
Array.Reverse(saltByte);
return PublicKey.VerifyMessage(message, uuidString, timeOffset, ref saltByte, ref signature);
}
}
}
}

View file

@ -0,0 +1,137 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MinecraftClient.Protocol.Keys
{
static class KeyUtils
{
private static string certificates = "https://api.minecraftservices.com/player/certificates";
public static PlayerKeyPair? GetKeys(string accessToken)
{
ProxiedWebRequest.Response? response = null;
try
{
var request = new ProxiedWebRequest(certificates)
{
Accept = "application/json"
};
request.Headers.Add("Authorization", string.Format("Bearer {0}", accessToken));
response = request.Post("application/json", "");
if (Settings.DebugMessages)
{
ConsoleIO.WriteLine(response.Body.ToString());
}
string jsonString = response.Body;
Json.JSONData json = Json.ParseJson(jsonString);
PublicKey publicKey = new(pemKey: json.Properties["keyPair"].Properties["publicKey"].StringValue,
sig: json.Properties["publicKeySignature"].StringValue,
sigV2: json.Properties["publicKeySignatureV2"].StringValue);
PrivateKey privateKey = new(pemKey: json.Properties["keyPair"].Properties["privateKey"].StringValue);
return new PlayerKeyPair(publicKey, privateKey,
expiresAt: json.Properties["expiresAt"].StringValue,
refreshedAfter: json.Properties["refreshedAfter"].StringValue);
}
catch (Exception e)
{
int code = (response == null) ? 0 : response.StatusCode;
ConsoleIO.WriteLineFormatted("§cFetch profile key failed: HttpCode = " + code + ", Error = " + e.Message);
if (Settings.DebugMessages)
{
ConsoleIO.WriteLineFormatted("§c" + e.StackTrace);
}
return null;
}
}
public static byte[] DecodePemKey(String key, String prefix, String suffix)
{
int i = key.IndexOf(prefix);
if (i != -1)
{
i += prefix.Length;
int j = key.IndexOf(suffix, i);
key = key[i..j];
}
key = key.Replace("\r", String.Empty);
key = key.Replace("\n", String.Empty);
return Convert.FromBase64String(key);
}
public static byte[] GetSignatureData(string message, string uuid, DateTimeOffset timestamp, ref byte[] salt)
{
List<byte> data = new();
data.AddRange(salt);
byte[] UUIDLeastSignificantBits = BitConverter.GetBytes(Convert.ToInt64(uuid[..16], 16));
Array.Reverse(UUIDLeastSignificantBits);
data.AddRange(UUIDLeastSignificantBits);
byte[] UUIDMostSignificantBits = BitConverter.GetBytes(Convert.ToInt64(uuid.Substring(16, 16), 16));
Array.Reverse(UUIDMostSignificantBits);
data.AddRange(UUIDMostSignificantBits);
byte[] timestampByte = BitConverter.GetBytes(timestamp.ToUnixTimeSeconds());
Array.Reverse(timestampByte);
data.AddRange(timestampByte);
data.AddRange(Encoding.UTF8.GetBytes(message));
return data.ToArray();
}
// https://github.com/mono/mono/blob/master/mcs/class/System.Json/System.Json/JsonValue.cs
public static string EscapeString(string src)
{
StringBuilder sb = new();
int start = 0;
for (int i = 0; i < src.Length; i++)
{
char c = src[i];
bool needEscape = c < 32 || c == '"' || c == '\\';
// Broken lead surrogate
needEscape = needEscape || (c >= '\uD800' && c <= '\uDBFF' &&
(i == src.Length - 1 || src[i + 1] < '\uDC00' || src[i + 1] > '\uDFFF'));
// Broken tail surrogate
needEscape = needEscape || (c >= '\uDC00' && c <= '\uDFFF' &&
(i == 0 || src[i - 1] < '\uD800' || src[i - 1] > '\uDBFF'));
// To produce valid JavaScript
needEscape = needEscape || c == '\u2028' || c == '\u2029';
if (needEscape)
{
sb.Append(src, start, i - start);
switch (src[i])
{
case '\b': sb.Append("\\b"); break;
case '\f': sb.Append("\\f"); break;
case '\n': sb.Append("\\n"); break;
case '\r': sb.Append("\\r"); break;
case '\t': sb.Append("\\t"); break;
case '\"': sb.Append("\\\""); break;
case '\\': sb.Append("\\\\"); break;
default:
sb.Append("\\u");
sb.Append(((int)src[i]).ToString("x04"));
break;
}
start = i + 1;
}
}
sb.Append(src, start, src.Length - start);
return sb.ToString();
}
}
}

View file

@ -0,0 +1,196 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Timers;
using MinecraftClient.Protocol.Session;
namespace MinecraftClient.Protocol.Keys
{
/// <summary>
/// Handle keys caching and storage.
/// </summary>
public static class KeysCache
{
private const string KeysCacheFilePlaintext = "ProfileKeyCache.ini";
private static FileMonitor cachemonitor;
private static Dictionary<string, PlayerKeyPair> keys = new Dictionary<string, PlayerKeyPair>();
private static Timer updatetimer = new Timer(100);
private static List<KeyValuePair<string, PlayerKeyPair>> pendingadds = new List<KeyValuePair<string, PlayerKeyPair>>();
private static BinaryFormatter formatter = new BinaryFormatter();
/// <summary>
/// Retrieve whether KeysCache contains a keys for the given login.
/// </summary>
/// <param name="login">User login used with Minecraft.net</param>
/// <returns>TRUE if keys are available</returns>
public static bool Contains(string login)
{
return keys.ContainsKey(login);
}
/// <summary>
/// Store keys and save it to disk if required.
/// </summary>
/// <param name="login">User login used with Minecraft.net</param>
/// <param name="playerKeyPair">User keys</param>
public static void Store(string login, PlayerKeyPair playerKeyPair)
{
if (Contains(login))
{
keys[login] = playerKeyPair;
}
else
{
keys.Add(login, playerKeyPair);
}
if (Settings.ProfileKeyCaching == CacheType.Disk && updatetimer.Enabled == true)
{
pendingadds.Add(new KeyValuePair<string, PlayerKeyPair>(login, playerKeyPair));
}
else if (Settings.ProfileKeyCaching == CacheType.Disk)
{
SaveToDisk();
}
}
/// <summary>
/// Retrieve keys for the given login.
/// </summary>
/// <param name="login">User login used with Minecraft.net</param>
/// <returns>PlayerKeyPair for given login</returns>
public static PlayerKeyPair Get(string login)
{
return keys[login];
}
/// <summary>
/// Initialize cache monitoring to keep cache updated with external changes.
/// </summary>
/// <returns>TRUE if keys are seeded from file</returns>
public static bool InitializeDiskCache()
{
cachemonitor = new FileMonitor(AppDomain.CurrentDomain.BaseDirectory, KeysCacheFilePlaintext, new FileSystemEventHandler(OnChanged));
updatetimer.Elapsed += HandlePending;
return LoadFromDisk();
}
/// <summary>
/// Reloads cache on external cache file change.
/// </summary>
/// <param name="sender">Sender</param>
/// <param name="e">Event data</param>
private static void OnChanged(object sender, FileSystemEventArgs e)
{
updatetimer.Stop();
updatetimer.Start();
}
/// <summary>
/// Called after timer elapsed. Reads disk cache and adds new/modified keys back.
/// </summary>
/// <param name="sender">Sender</param>
/// <param name="e">Event data</param>
private static void HandlePending(object sender, ElapsedEventArgs e)
{
updatetimer.Stop();
LoadFromDisk();
foreach (KeyValuePair<string, PlayerKeyPair> pending in pendingadds.ToArray())
{
Store(pending.Key, pending.Value);
pendingadds.Remove(pending);
}
}
/// <summary>
/// Reads cache file and loads KeysInfos into KeysCache.
/// </summary>
/// <returns>True if data is successfully loaded</returns>
private static bool LoadFromDisk()
{
//User-editable keys cache file in text format
if (File.Exists(KeysCacheFilePlaintext))
{
if (Settings.DebugMessages)
ConsoleIO.WriteLineFormatted(Translations.Get("cache.loading_keys", KeysCacheFilePlaintext));
try
{
foreach (string line in FileMonitor.ReadAllLinesWithRetries(KeysCacheFilePlaintext))
{
if (!line.Trim().StartsWith("#"))
{
int separatorIdx = line.IndexOf('=');
if (separatorIdx >= 1 && line.Length > separatorIdx + 1)
{
string login = line.Substring(0, separatorIdx);
string value = line.Substring(separatorIdx + 1);
try
{
PlayerKeyPair playerKeyPair = PlayerKeyPair.FromString(value);
keys[login] = playerKeyPair;
if (Settings.DebugMessages)
ConsoleIO.WriteLineFormatted(Translations.Get("cache.loaded_keys", playerKeyPair.ExpiresAt.ToString()));
}
catch (InvalidDataException e)
{
if (Settings.DebugMessages)
ConsoleIO.WriteLineFormatted(Translations.Get("cache.ignore_string_keys", value, e.Message));
}
catch (FormatException e)
{
if (Settings.DebugMessages)
ConsoleIO.WriteLineFormatted(Translations.Get("cache.ignore_string_keys", value, e.Message));
}
catch (ArgumentNullException e)
{
if (Settings.DebugMessages)
ConsoleIO.WriteLineFormatted(Translations.Get("cache.ignore_string_keys", value, e.Message));
}
}
else if (Settings.DebugMessages)
{
ConsoleIO.WriteLineFormatted(Translations.Get("cache.ignore_line_keys", line));
}
}
}
}
catch (IOException e)
{
ConsoleIO.WriteLineFormatted(Translations.Get("cache.read_fail_plain_keys", e.Message));
}
}
return keys.Count > 0;
}
/// <summary>
/// Saves player's keypair from KeysCache into cache file.
/// </summary>
private static void SaveToDisk()
{
if (Settings.DebugMessages)
Translations.WriteLineFormatted("cache.saving_keys");
List<string> KeysCacheLines = new List<string>();
KeysCacheLines.Add("# Generated by MCC v" + Program.Version + " - Keep it secret & Edit at own risk!");
KeysCacheLines.Add("# ProfileKey=PublicKey(base64),PublicKeySignature(base64),PublicKeySignatureV2(base64),PrivateKey(base64),ExpiresAt,RefreshAfter");
foreach (KeyValuePair<string, PlayerKeyPair> entry in keys)
KeysCacheLines.Add(entry.Key + '=' + entry.Value.ToString());
try
{
FileMonitor.WriteAllLinesWithRetries(KeysCacheFilePlaintext, KeysCacheLines);
}
catch (IOException e)
{
ConsoleIO.WriteLineFormatted(Translations.Get("cache.save_fail_keys", e.Message));
}
}
}
}

View file

@ -0,0 +1,79 @@
using System;
using System.Collections.Generic;
using System.IO;
namespace MinecraftClient.Protocol.Keys
{
public class PlayerKeyPair
{
public PublicKey PublicKey;
public PrivateKey PrivateKey;
public DateTime ExpiresAt;
public DateTime RefreshedAfter; // Todo: add a timer
private const string DataTimeFormat = "O";
public PlayerKeyPair(PublicKey keyPublic, PrivateKey keyPrivate, string expiresAt, string refreshedAfter)
{
PublicKey = keyPublic;
PrivateKey = keyPrivate;
ExpiresAt = DateTime.Parse(expiresAt).ToUniversalTime();
RefreshedAfter = DateTime.Parse(refreshedAfter).ToUniversalTime();
}
public bool NeedRefresh()
{
return DateTime.Now.ToUniversalTime() > this.RefreshedAfter;
}
public bool IsExpired()
{
return DateTime.Now.ToUniversalTime() > this.ExpiresAt;
}
public long GetExpirationMilliseconds()
{
DateTimeOffset timeOffset = new(ExpiresAt);
return timeOffset.ToUnixTimeMilliseconds();
}
public long GetExpirationSeconds()
{
DateTimeOffset timeOffset = new(ExpiresAt);
return timeOffset.ToUnixTimeSeconds();
}
public static PlayerKeyPair FromString(string tokenString)
{
string[] fields = tokenString.Split(',');
if (fields.Length < 6)
throw new InvalidDataException("Invalid string format");
PublicKey publicKey = new PublicKey(pemKey: fields[0].Trim(),
sig: fields[1].Trim(), sigV2: fields[2].Trim());
PrivateKey privateKey = new PrivateKey(pemKey: fields[3].Trim());
return new PlayerKeyPair(publicKey, privateKey, fields[4].Trim(), fields[5].Trim());
}
public override string ToString()
{
List<string> datas = new List<string>();
datas.Add(Convert.ToBase64String(PublicKey.Key));
datas.Add(Convert.ToBase64String(PublicKey.Signature));
if (PublicKey.SignatureV2 == null)
datas.Add(String.Empty);
else
datas.Add(Convert.ToBase64String(PublicKey.SignatureV2));
datas.Add(Convert.ToBase64String(PrivateKey.Key));
datas.Add(ExpiresAt.ToString(DataTimeFormat));
datas.Add(RefreshedAfter.ToString(DataTimeFormat));
return String.Join(",", datas.ToArray());
}
}
}

View file

@ -0,0 +1,39 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace MinecraftClient.Protocol.Keys
{
public class PrivateKey
{
public byte[] Key { get; set; }
private readonly RSA rsa;
public PrivateKey(string pemKey)
{
this.Key = KeyUtils.DecodePemKey(pemKey, "-----BEGIN RSA PRIVATE KEY-----", "-----END RSA PRIVATE KEY-----");
this.rsa = RSA.Create();
rsa.ImportPkcs8PrivateKey(this.Key, out _);
}
public byte[] SignData(byte[] data)
{
return rsa.SignData(data, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
}
public byte[] SignMessage(string message, string uuid, DateTimeOffset timestamp, ref byte[] salt)
{
string messageJson = "{\"text\":\"" + KeyUtils.EscapeString(message) + "\"}";
byte[] data = KeyUtils.GetSignatureData(messageJson, uuid, timestamp, ref salt);
return SignData(data);
}
}
}

View file

@ -0,0 +1,54 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace MinecraftClient.Protocol.Keys
{
public class PublicKey
{
public byte[] Key { get; set; }
public byte[] Signature { get; set; }
public byte[]? SignatureV2 { get; set; }
private readonly RSA rsa;
public PublicKey(string pemKey, string sig, string? sigV2 = null)
{
this.Key = KeyUtils.DecodePemKey(pemKey, "-----BEGIN RSA PUBLIC KEY-----", "-----END RSA PUBLIC KEY-----");
this.rsa = RSA.Create();
rsa.ImportSubjectPublicKeyInfo(this.Key, out _);
this.Signature = Convert.FromBase64String(sig);
if (!string.IsNullOrEmpty(sigV2))
this.SignatureV2 = Convert.FromBase64String(sigV2!);
}
public PublicKey(byte[] key, byte[] signature)
{
this.Key = key;
this.rsa = RSA.Create();
rsa.ImportSubjectPublicKeyInfo(this.Key, out _);
this.Signature = signature;
}
public bool VerifyData(byte[] data, byte[] signature)
{
return rsa.VerifyData(data, signature, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
}
public bool VerifyMessage(string message, string uuid, DateTimeOffset timestamp, ref byte[] salt, ref byte[] signature)
{
byte[] data = KeyUtils.GetSignatureData(message, uuid, timestamp, ref salt);
return VerifyData(data, signature);
}
}
}

View file

@ -79,11 +79,11 @@ namespace MinecraftClient.Protocol
/// <param name="serverPort">Server Port to ping</param>
/// <param name="protocolversion">Will contain protocol version, if ping successful</param>
/// <returns>TRUE if ping was successful</returns>
public static bool GetServerInfo(string serverIP, ushort serverPort, ref int protocolversion, ref ForgeInfo forgeInfo)
public static bool GetServerInfo(string serverIP, ushort serverPort, ref int protocolversion, ref ForgeInfo? forgeInfo)
{
bool success = false;
int protocolversionTmp = 0;
ForgeInfo forgeInfoTmp = null;
ForgeInfo? forgeInfoTmp = null;
if (AutoTimeout.Perform(() =>
{
try
@ -127,11 +127,15 @@ namespace MinecraftClient.Protocol
public static IMinecraftCom GetProtocolHandler(TcpClient Client, int ProtocolVersion, ForgeInfo forgeInfo, IMinecraftComHandler Handler)
{
int[] supportedVersions_Protocol16 = { 51, 60, 61, 72, 73, 74, 78 };
if (Array.IndexOf(supportedVersions_Protocol16, ProtocolVersion) > -1)
return new Protocol16Handler(Client, ProtocolVersion, Handler);
int[] supportedVersions_Protocol18 = { 4, 5, 47, 107, 108, 109, 110, 210, 315, 316, 335, 338, 340, 393, 401, 404, 477, 480, 485, 490, 498, 573, 575, 578, 735, 736, 751, 753, 754, 755, 756, 757, 758 };
int[] supportedVersions_Protocol18 = { 4, 5, 47, 107, 108, 109, 110, 210, 315, 316, 335, 338, 340, 393, 401, 404, 477, 480, 485, 490, 498, 573, 575, 578, 735, 736, 751, 753, 754, 755, 756, 757, 758, 759 };
if (Array.IndexOf(supportedVersions_Protocol18, ProtocolVersion) > -1)
return new Protocol18Handler(Client, ProtocolVersion, Handler, forgeInfo);
throw new NotSupportedException(Translations.Get("exception.version_unsupport", ProtocolVersion));
}
@ -257,6 +261,8 @@ namespace MinecraftClient.Protocol
return 757;
case "1.18.2":
return 758;
case "1.19":
return 759;
default:
return 0;
}
@ -322,6 +328,7 @@ namespace MinecraftClient.Protocol
case 756: return "1.17.1";
case 757: return "1.18.1";
case 758: return "1.18.2";
case 759: return "1.19";
default: return "0.0";
}
}
@ -815,9 +822,9 @@ namespace MinecraftClient.Protocol
/// <returns>HTTP Status code</returns>
private static int DoHTTPSRequest(List<string> headers, string host, ref string result)
{
string postResult = null;
string? postResult = null;
int statusCode = 520;
Exception exception = null;
Exception? exception = null;
AutoTimeout.Perform(() =>
{
try
@ -859,7 +866,8 @@ namespace MinecraftClient.Protocol
}
}
}, TimeSpan.FromSeconds(30));
result = postResult;
if (postResult != null)
result = postResult;
if (exception != null)
throw exception;
return statusCode;

View file

@ -307,7 +307,7 @@ namespace MinecraftClient.Protocol
if (isLogin && packetID == 0x02)
{
Guid uuid;
if (protocolVersion < Protocol18Handler.MC116Version)
if (protocolVersion < Protocol18Handler.MC_1_16_Version)
{
if (Guid.TryParse(dataTypes.ReadNextString(p), out uuid))
{
@ -350,7 +350,7 @@ namespace MinecraftClient.Protocol
playerLastPitch = pitch;
playerLastYaw = yaw;
if (protocolVersion >= Protocol18Handler.MC18Version)
if (protocolVersion >= Protocol18Handler.MC_1_8_Version)
{
playerLastPosition.X = (locMask & 1 << 0) != 0 ? playerLastPosition.X + x : x;
playerLastPosition.Y = (locMask & 1 << 1) != 0 ? playerLastPosition.Y + y : y;

View file

@ -251,7 +251,7 @@ namespace MinecraftClient.Protocol.Session
Translations.WriteLineFormatted("cache.saving");
List<string> sessionCacheLines = new List<string>();
sessionCacheLines.Add("# Generated by MCC v" + Program.Version + " - Edit at own risk!");
sessionCacheLines.Add("# Generated by MCC v" + Program.Version + " - Keep it secret & Edit at own risk!");
sessionCacheLines.Add("# Login=SessionID,PlayerName,UUID,ClientID");
foreach (KeyValuePair<string, SessionToken> entry in sessions)
sessionCacheLines.Add(entry.Key + '=' + entry.Value.ToString());