From e569ffe0cc67c692214cc053e0b390b488e1902b Mon Sep 17 00:00:00 2001 From: oldkingOK Date: Tue, 20 Feb 2024 15:37:57 +0800 Subject: [PATCH 1/4] fix: IndexOutOfRange on packet reading (Forge) Add two missing forge Command Packet Parsers, which won't affect the vanilla parsers. The ids of the two Command Packet Parsers `forge:enum` and `forge:modid` [Forge once added in order](https://github.com/MinecraftForge/MinecraftForge/blob/19f8d2a7937dc7968ecbef2f9785687193fcd210/src/main/java/net/minecraftforge/common/ForgeMod.java#L175) are the maximum value of the Vanilla Parser id plus 1 or plus 2. `forge:enum` has a [String Type argument](https://wiki.vg/Command_Data#forge:enum). The specific id is from [wiki.vg](https://wiki.vg/Command_Data) or Forge-generated minecraft source code. --- .../Handlers/Packet/s2c/DeclareCommands.cs | 37 ++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/MinecraftClient/Protocol/Handlers/Packet/s2c/DeclareCommands.cs b/MinecraftClient/Protocol/Handlers/Packet/s2c/DeclareCommands.cs index 0311c9f7f6..be0315dfd2 100644 --- a/MinecraftClient/Protocol/Handlers/Packet/s2c/DeclareCommands.cs +++ b/MinecraftClient/Protocol/Handlers/Packet/s2c/DeclareCommands.cs @@ -29,7 +29,19 @@ public static void Read(DataTypes dataTypes, Queue packetData, int protoco Parser? parser = null; if ((flags & 0x03) == 2) { - if (protocolVersion <= Protocol18Handler.MC_1_19_2_Version) + if (protocolVersion switch + { + Protocol18Handler.MC_1_19_Version => parserId == 50, + Protocol18Handler.MC_1_19_2_Version => parserId == 50, + Protocol18Handler.MC_1_19_3_Version => parserId == 50, + Protocol18Handler.MC_1_19_4_Version => parserId == 50, + Protocol18Handler.MC_1_20_Version => parserId == 51, + Protocol18Handler.MC_1_20_2_Version => parserId == 51, + _ => false + }) + parser = new ParserForgeEnum(dataTypes, packetData); + + else if (protocolVersion <= Protocol18Handler.MC_1_19_2_Version) parser = parserId switch { 1 => new ParserFloat(dataTypes, packetData), @@ -644,5 +656,28 @@ public override string GetName() return "minecraft:time"; } } + + internal class ParserForgeEnum : Parser + { + public ParserForgeEnum(DataTypes dataTypes, Queue packetData) + { + dataTypes.ReadNextString(packetData); + } + + public override bool Check(string text) + { + return true; + } + + public override int GetArgCnt() + { + return 1; + } + + public override string GetName() + { + return "forge:enum"; + } + } } } From 576575ff65230e0f78bfeae85ddb5ed2362877dd Mon Sep 17 00:00:00 2001 From: oldkingOK Date: Tue, 20 Feb 2024 20:22:37 +0800 Subject: [PATCH 2/4] refactor(DeclareCommands.cs): Move forge to switch version block --- .../Handlers/Packet/s2c/DeclareCommands.cs | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/MinecraftClient/Protocol/Handlers/Packet/s2c/DeclareCommands.cs b/MinecraftClient/Protocol/Handlers/Packet/s2c/DeclareCommands.cs index be0315dfd2..9e0e9a0878 100644 --- a/MinecraftClient/Protocol/Handlers/Packet/s2c/DeclareCommands.cs +++ b/MinecraftClient/Protocol/Handlers/Packet/s2c/DeclareCommands.cs @@ -29,19 +29,7 @@ public static void Read(DataTypes dataTypes, Queue packetData, int protoco Parser? parser = null; if ((flags & 0x03) == 2) { - if (protocolVersion switch - { - Protocol18Handler.MC_1_19_Version => parserId == 50, - Protocol18Handler.MC_1_19_2_Version => parserId == 50, - Protocol18Handler.MC_1_19_3_Version => parserId == 50, - Protocol18Handler.MC_1_19_4_Version => parserId == 50, - Protocol18Handler.MC_1_20_Version => parserId == 51, - Protocol18Handler.MC_1_20_2_Version => parserId == 51, - _ => false - }) - parser = new ParserForgeEnum(dataTypes, packetData); - - else if (protocolVersion <= Protocol18Handler.MC_1_19_2_Version) + if (protocolVersion <= Protocol18Handler.MC_1_19_2_Version) parser = parserId switch { 1 => new ParserFloat(dataTypes, packetData), @@ -59,6 +47,7 @@ public static void Read(DataTypes dataTypes, Queue packetData, int protoco 29 => new ParserScoreHolder(dataTypes, packetData), 43 => new ParserResourceOrTag(dataTypes, packetData), 44 => new ParserResource(dataTypes, packetData), + 50 => new ParserForgeEnum(dataTypes, packetData), _ => new ParserEmpty(dataTypes, packetData), }; else if (protocolVersion <= Protocol18Handler.MC_1_19_3_Version) // 1.19.3 @@ -81,6 +70,7 @@ public static void Read(DataTypes dataTypes, Queue packetData, int protoco 42 => new ParserResourceOrTag(dataTypes, packetData), 43 => new ParserResource(dataTypes, packetData), 44 => new ParserResource(dataTypes, packetData), + 50 => new ParserForgeEnum(dataTypes, packetData), _ => new ParserEmpty(dataTypes, packetData), }; else // 1.19.4+ @@ -104,6 +94,16 @@ public static void Read(DataTypes dataTypes, Queue packetData, int protoco 42 => new ParserResourceOrTag(dataTypes, packetData), 43 => new ParserResource(dataTypes, packetData), 44 => new ParserResource(dataTypes, packetData), + 50 => protocolVersion == Protocol18Handler.MC_1_19_4_Version ? + new ParserForgeEnum(dataTypes, packetData) : + new ParserEmpty(dataTypes, packetData), + 51 => (protocolVersion >= Protocol18Handler.MC_1_20_Version && + protocolVersion <= Protocol18Handler.MC_1_20_2_Version) ? // 1.20 - 1.20.2 + new ParserForgeEnum(dataTypes, packetData) : + new ParserEmpty(dataTypes, packetData), + 52 => protocolVersion > Protocol18Handler.MC_1_20_2_Version ? // 1.20.2 + + new ParserForgeEnum(dataTypes, packetData) : + new ParserEmpty(dataTypes, packetData), _ => new ParserEmpty(dataTypes, packetData), }; } From 8e1822b0d24fae4c29ea41c0c4acea4902fe0791 Mon Sep 17 00:00:00 2001 From: oldkingOK Date: Wed, 21 Feb 2024 10:10:03 +0800 Subject: [PATCH 3/4] feat(DeclareCommands.cs): Remove 1.20.2+ version check --- .../Protocol/Handlers/Packet/s2c/DeclareCommands.cs | 3 --- 1 file changed, 3 deletions(-) diff --git a/MinecraftClient/Protocol/Handlers/Packet/s2c/DeclareCommands.cs b/MinecraftClient/Protocol/Handlers/Packet/s2c/DeclareCommands.cs index 9e0e9a0878..7c215bb246 100644 --- a/MinecraftClient/Protocol/Handlers/Packet/s2c/DeclareCommands.cs +++ b/MinecraftClient/Protocol/Handlers/Packet/s2c/DeclareCommands.cs @@ -101,9 +101,6 @@ public static void Read(DataTypes dataTypes, Queue packetData, int protoco protocolVersion <= Protocol18Handler.MC_1_20_2_Version) ? // 1.20 - 1.20.2 new ParserForgeEnum(dataTypes, packetData) : new ParserEmpty(dataTypes, packetData), - 52 => protocolVersion > Protocol18Handler.MC_1_20_2_Version ? // 1.20.2 + - new ParserForgeEnum(dataTypes, packetData) : - new ParserEmpty(dataTypes, packetData), _ => new ParserEmpty(dataTypes, packetData), }; } From 4bb25c377e3666cb2681cda7d9d4caa444db4ffb Mon Sep 17 00:00:00 2001 From: oldkingOK Date: Sun, 10 Mar 2024 12:02:22 +0800 Subject: [PATCH 4/4] feat(DeclareCommands.cs): Add 1.20.3+ version check --- .../Protocol/Handlers/Packet/s2c/DeclareCommands.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/MinecraftClient/Protocol/Handlers/Packet/s2c/DeclareCommands.cs b/MinecraftClient/Protocol/Handlers/Packet/s2c/DeclareCommands.cs index 6824729451..07d37c7207 100644 --- a/MinecraftClient/Protocol/Handlers/Packet/s2c/DeclareCommands.cs +++ b/MinecraftClient/Protocol/Handlers/Packet/s2c/DeclareCommands.cs @@ -96,7 +96,7 @@ public static void Read(DataTypes dataTypes, Queue packetData, int protoco 44 => new ParserResource(dataTypes, packetData), 50 => protocolVersion == Protocol18Handler.MC_1_19_4_Version ? new ParserForgeEnum(dataTypes, packetData) : - new ParserEmpty(dataTypes, packetData), + new ParserEmpty(dataTypes, packetData), 51 => (protocolVersion >= Protocol18Handler.MC_1_20_Version && protocolVersion <= Protocol18Handler.MC_1_20_2_Version) ? // 1.20 - 1.20.2 new ParserForgeEnum(dataTypes, packetData) : @@ -124,6 +124,7 @@ public static void Read(DataTypes dataTypes, Queue packetData, int protoco 43 => new ParserResourceOrTag(dataTypes, packetData), 44 => new ParserResource(dataTypes, packetData), 45 => new ParserResource(dataTypes, packetData), + 52 => new ParserForgeEnum(dataTypes, packetData), _ => new ParserEmpty(dataTypes, packetData), }; }