diff --git a/MinecraftClient/Commands/Bed.cs b/MinecraftClient/Commands/Bed.cs index 89f6c7a7..37936fd5 100644 --- a/MinecraftClient/Commands/Bed.cs +++ b/MinecraftClient/Commands/Bed.cs @@ -121,12 +121,8 @@ namespace MinecraftClient.Commands if (args.Length >= 3) { - Location current = handler.GetCurrentLocation(); - double x = args[1].StartsWith('~') ? current.X + (args[1].Length > 1 ? double.Parse(args[1][1..]) : 0) : double.Parse(args[1]); - double y = args[2].StartsWith('~') ? current.Y + (args[2].Length > 1 ? double.Parse(args[2][1..]) : 0) : double.Parse(args[2]); - double z = args[3].StartsWith('~') ? current.Z + (args[3].Length > 1 ? double.Parse(args[3][1..]) : 0) : double.Parse(args[3]); - - Location block = new Location(x, y, z).ToFloor(), blockCenter = block.ToCenter(); + Location block = Location.Parse(handler.GetCurrentLocation(), args[1], args[2], args[3]).ToFloor(); + Location blockCenter = block.ToCenter(); if (!handler.GetWorld().GetBlock(block).Type.IsBed()) return Translations.TryGet("cmd.bed.not_a_bed", blockCenter.X, blockCenter.Y, blockCenter.Z); diff --git a/MinecraftClient/Commands/Dig.cs b/MinecraftClient/Commands/Dig.cs index 9a303561..eadf13db 100644 --- a/MinecraftClient/Commands/Dig.cs +++ b/MinecraftClient/Commands/Dig.cs @@ -25,17 +25,13 @@ namespace MinecraftClient.Commands try { Location current = handler.GetCurrentLocation(); - double x = args[0].StartsWith('~') ? current.X + (args[0].Length > 1 ? double.Parse(args[0][1..]) : 0) : double.Parse(args[0]); - double y = args[1].StartsWith('~') ? current.Y + (args[1].Length > 1 ? double.Parse(args[1][1..]) : 0) : double.Parse(args[1]); - double z = args[2].StartsWith('~') ? current.Z + (args[2].Length > 1 ? double.Parse(args[2][1..]) : 0) : double.Parse(args[2]); - - Location blockToBreak = new Location(x, y, z); - if (blockToBreak.DistanceSquared(handler.GetCurrentLocation().EyesLocation()) > 25) + Location blockToBreak = Location.Parse(current, args[0], args[1], args[2]); + if (blockToBreak.DistanceSquared(current.EyesLocation()) > 25) return Translations.Get("cmd.dig.too_far"); if (handler.GetWorld().GetBlock(blockToBreak).Type == Material.Air) return Translations.Get("cmd.dig.no_block"); if (handler.DigBlock(blockToBreak)) - return Translations.Get("cmd.dig.dig", x, y, z); + return Translations.Get("cmd.dig.dig", blockToBreak.X, blockToBreak.Y, blockToBreak.Z); else return "cmd.dig.fail"; } catch (FormatException) { return GetCmdDescTranslated(); } diff --git a/MinecraftClient/Commands/Look.cs b/MinecraftClient/Commands/Look.cs index d27c09f5..68b1cab4 100644 --- a/MinecraftClient/Commands/Look.cs +++ b/MinecraftClient/Commands/Look.cs @@ -51,12 +51,9 @@ namespace MinecraftClient.Commands { try { - int x = int.Parse(args[0]); - int y = int.Parse(args[1]); - int z = int.Parse(args[2]); - - Location block = new Location(x, y, z); - handler.UpdateLocation(handler.GetCurrentLocation(), block); + Location current = handler.GetCurrentLocation(); + Location block = Location.Parse(current, args[0], args[1], args[2]); + handler.UpdateLocation(current, block); return Translations.Get("cmd.look.block", block); } diff --git a/MinecraftClient/Commands/Move.cs b/MinecraftClient/Commands/Move.cs index da58e76b..edd95ccd 100644 --- a/MinecraftClient/Commands/Move.cs +++ b/MinecraftClient/Commands/Move.cs @@ -91,14 +91,10 @@ namespace MinecraftClient.Commands try { Location current = handler.GetCurrentLocation(), currentCenter = current.ToCenter(); - - double x = args[0].StartsWith('~') ? current.X + (args[0].Length > 1 ? double.Parse(args[0][1..]) : 0) : double.Parse(args[0]); - double y = args[1].StartsWith('~') ? current.Y + (args[1].Length > 1 ? double.Parse(args[1][1..]) : 0) : double.Parse(args[1]); - double z = args[2].StartsWith('~') ? current.Z + (args[2].Length > 1 ? double.Parse(args[2][1..]) : 0) : double.Parse(args[2]); - Location goal = new(x, y, z); + Location goal = Location.Parse(current, args[0], args[1], args[2]); if (!Movement.CheckChunkLoading(handler.GetWorld(), current, goal)) - return Translations.Get("cmd.move.chunk_not_loaded", x, y, z); + return Translations.Get("cmd.move.chunk_not_loaded", goal.X, goal.Y, goal.Z); if (takeRisk || Movement.PlayerFitsHere(handler.GetWorld(), goal)) { diff --git a/MinecraftClient/Commands/Useblock.cs b/MinecraftClient/Commands/Useblock.cs index bf72dd5b..f35ac385 100644 --- a/MinecraftClient/Commands/Useblock.cs +++ b/MinecraftClient/Commands/Useblock.cs @@ -21,11 +21,8 @@ namespace MinecraftClient.Commands string[] args = getArgs(command); if (args.Length >= 3) { - Location current = handler.GetCurrentLocation(); - double x = args[0].StartsWith('~') ? current.X + (args[0].Length > 1 ? double.Parse(args[0][1..]) : 0) : double.Parse(args[0]); - double y = args[1].StartsWith('~') ? current.Y + (args[1].Length > 1 ? double.Parse(args[1][1..]) : 0) : double.Parse(args[1]); - double z = args[2].StartsWith('~') ? current.Z + (args[2].Length > 1 ? double.Parse(args[2][1..]) : 0) : double.Parse(args[2]); - Location block = new Location(x, y, z).ToFloor(), blockCenter = block.ToCenter(); + Location block = Location.Parse(handler.GetCurrentLocation(), args[0], args[1], args[2]).ToFloor(); + Location blockCenter = block.ToCenter(); bool res = handler.PlaceBlock(block, Direction.Down); return Translations.Get("cmd.useblock.use", blockCenter.X, blockCenter.Y, blockCenter.Z, res ? "succeeded" : "failed"); } diff --git a/MinecraftClient/Mapping/Location.cs b/MinecraftClient/Mapping/Location.cs index edc8404f..339ec562 100644 --- a/MinecraftClient/Mapping/Location.cs +++ b/MinecraftClient/Mapping/Location.cs @@ -73,6 +73,95 @@ namespace MinecraftClient.Mapping Z = chunkZ * Chunk.SizeZ + blockZ; } + /// + /// Parse location from the string. + /// return NULL if the parsing fails. + /// + /// The string representation of the X-axis coordinate. + /// The string representation of the Y-axis coordinate. + /// The string representation of the Z-axis coordinate. + /// The location represented by the string. + public static Location Parse(string x, string y, string z) + { + Location.TryParse(x, y, z, out Location? res); + if (res == null) + throw new FormatException(); + else + return (Location)res; + } + + public static bool TryParse(string x, string y, string z, out Location? location) + { + string[] coord_str = new string[] { x.Trim(), y.Trim(), z.Trim() }; + double[] coord_res = new double[3]; + + for (int i = 0; i < 3; ++i) + { + if (!double.TryParse(coord_str[i], out coord_res[i])) + { + location = null; + return false; + } + } + + location = new Location(coord_res[0], coord_res[1], coord_res[2]); + return true; + } + + /// + /// Parse location from the string (relative coordinate representation is supported). + /// return NULL if the parsing fails. + /// + /// Relative position base point. + /// The string representation of the X-axis coordinate. + /// The string representation of the Y-axis coordinate. + /// The string representation of the Z-axis coordinate. + /// The location represented by the string. + public static Location Parse(Location current, string x, string y, string z) + { + Location.TryParse(current, x, y, z, out Location? res); + if (res == null) + throw new FormatException(); + else + return (Location)res; + } + + public static bool TryParse(Location current, string x, string y, string z, out Location? location) + { + string[] coord_str = new string[] { x.Trim(), y.Trim(), z.Trim() }; + double[] coord_res = new double[3]; + double[] coord_cur = new double[] { current.X, current.Y, current.Z }; + + for (int i = 0; i < 3; ++i) + { + if (coord_str[i].StartsWith('~')) + { + if (coord_str[i].Length > 1) + { + if (!double.TryParse(coord_str[i][1..], out coord_res[i])) + { + location = null; + return false; + } + coord_res[i] += coord_cur[i]; + } + else + coord_res[i] = coord_cur[i]; + } + else + { + if (!double.TryParse(coord_str[i], out coord_res[i])) + { + location = null; + return false; + } + } + } + + location = new Location(coord_res[0], coord_res[1], coord_res[2]); + return true; + } + /// /// Round coordinates ///