From d0c9695a795c889e3fef7d87a4de9fc9a05543e0 Mon Sep 17 00:00:00 2001 From: vinicius Date: Thu, 5 Dec 2024 02:12:39 +0000 Subject: [PATCH] Fixed bug in `SetDimension` method of `World` class, where it would crash if joining a paper server. Added error handling. --- MinecraftClient/Mapping/World.cs | 32 ++++++++++++++++--- .../Protocol/Handlers/Protocol18.cs | 3 +- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/MinecraftClient/Mapping/World.cs b/MinecraftClient/Mapping/World.cs index 0b2e02f9..c499d13a 100644 --- a/MinecraftClient/Mapping/World.cs +++ b/MinecraftClient/Mapping/World.cs @@ -19,7 +19,7 @@ namespace MinecraftClient.Mapping /// /// The dimension info of the world /// - private static Dimension curDimension = new(); + private static Dimension curDimension= new(); private static readonly Dictionary dimensionList = new(); @@ -87,10 +87,32 @@ namespace MinecraftClient.Mapping /// /// The name of the dimension type /// The dimension type (NBT Tag Compound) - public static void SetDimension(string name) - { - curDimension = dimensionList[name]; // Should not fail - } + public static void SetDimension(string name) + { + // Try to get the dimension using the name as is + if (dimensionList.TryGetValue(name, out Dimension dimension)) + { + curDimension = dimension; + return; // Dimension found + } + + // If not found, check if name lacks 'minecraft:' prefix and try again + if (!name.StartsWith("minecraft:")) + { + string prefixedName = "minecraft:" + name; + if (dimensionList.TryGetValue(prefixedName, out dimension)) + { + curDimension = dimension; + return; // Dimension found with prefixed name + } + } + + // If still not found, dimension does not exist + throw new KeyNotFoundException($"Dimension '{name}' not found in dimensions dictionary."); + } + + + /// diff --git a/MinecraftClient/Protocol/Handlers/Protocol18.cs b/MinecraftClient/Protocol/Handlers/Protocol18.cs index 6b0fa3db..d089fa00 100644 --- a/MinecraftClient/Protocol/Handlers/Protocol18.cs +++ b/MinecraftClient/Protocol/Handlers/Protocol18.cs @@ -660,7 +660,8 @@ namespace MinecraftClient.Protocol.Handlers { case >= MC_1_16_2_Version and <= MC_1_18_2_Version: World.StoreOneDimension(dimensionName, dimensionType!); - World.SetDimension(dimensionName); + // World.SetDimension(dimensionName); + World.SetDimension(dimensionName); break; default: World.SetDimension(dimensionTypeName!);