diff --git a/MinecraftClient/Commands/Leavebed.cs b/MinecraftClient/Commands/Leavebed.cs index cfc0c4ce..89f6c7a7 100644 --- a/MinecraftClient/Commands/Leavebed.cs +++ b/MinecraftClient/Commands/Leavebed.cs @@ -1,16 +1,15 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Text; +using System.Threading.Tasks; using MinecraftClient.Mapping; -using PInvoke; namespace MinecraftClient.Commands { public class BedCommand : Command { public override string CmdName { get { return "bed"; } } - public override string CmdUsage { get { return "bed leave|sleep "; } } + public override string CmdUsage { get { return "bed leave|sleep |sleep "; } } public override string CmdDesc { get { return "cmd.bed.desc"; } } public override string Run(McClient handler, string command, Dictionary localVars) @@ -29,6 +28,97 @@ namespace MinecraftClient.Commands if (subcommand.Equals("sleep") || subcommand.Equals("s")) { + if (args.Length == 2) + { + if (!int.TryParse(args[1], out int radius)) + return CmdUsage; + + handler.GetLogger().Info(Translations.TryGet("cmd.bed.searching", radius)); + + Location current = handler.GetCurrentLocation(); + Location bedLocation = current; + + Material[] bedMaterialList = new Material[]{ + Material.BlackBed, + Material.BlueBed, + Material.BrownBed, + Material.CyanBed, + Material.GrayBed, + Material.GreenBed, + Material.LightBlueBed, + Material.LightGrayBed, + Material.LimeBed, + Material.MagentaBed, + Material.OrangeBed, + Material.PinkBed, + Material.PurpleBed, + Material.RedBed, + Material.WhiteBed, + Material.YellowBed + }; + + bool found = false; + foreach (Material material in bedMaterialList) + { + List beds = handler.GetWorld().FindBlock(current, material, radius); + + if (beds.Count > 0) + { + found = true; + bedLocation = beds.First(); + break; + } + } + + if (!found) + return Translations.TryGet("cmd.bed.bed_not_found"); + + handler.Log.Info(Translations.TryGet("cmd.bed.found_a_bed_at", bedLocation.X, bedLocation.Y, bedLocation.Z)); + + if (!Movement.CheckChunkLoading(handler.GetWorld(), current, bedLocation)) + return Translations.Get("cmd.move.chunk_not_loaded", bedLocation.X, bedLocation.Y, bedLocation.Z); + + if (handler.MoveTo(bedLocation)) + { + Task.Factory.StartNew(() => + { + bool atTheLocation = false; + DateTime timeout = DateTime.Now.AddSeconds(60); + + while (DateTime.Now < timeout) + { + if (handler.GetCurrentLocation() == bedLocation || handler.GetCurrentLocation().Distance(bedLocation) <= 2.0) + { + atTheLocation = true; + break; + } + } + + if (!atTheLocation) + { + handler.Log.Info(Translations.TryGet("cmd.bed.failed_to_reach_in_time", bedLocation.X, bedLocation.Y, bedLocation.Z)); + return; + } + + handler.Log.Info(Translations.TryGet("cmd.bed.moving", bedLocation.X, bedLocation.Y, bedLocation.Z)); + + bool res = handler.PlaceBlock(bedLocation, Direction.Down); + + handler.Log.Info(Translations.TryGet( + "cmd.bed.trying_to_use", + bedLocation.X, + bedLocation.Y, + bedLocation.Z, + Translations.TryGet(res ? "cmd.bed.in" : "cmd.bed.not_in") + )); + }); + + return ""; + } + + return Translations.Get("cmd.bed.cant_reach_safely"); + } + if (args.Length >= 3) { Location current = handler.GetCurrentLocation(); diff --git a/MinecraftClient/Resources/lang/en.ini b/MinecraftClient/Resources/lang/en.ini index bbcfaddf..92f0b614 100644 --- a/MinecraftClient/Resources/lang/en.ini +++ b/MinecraftClient/Resources/lang/en.ini @@ -350,6 +350,12 @@ cmd.bed.trying_to_use=Trying to sleep in a bed on location (X: {0:0.0}, Y: {1:0. cmd.bed.in=Succesfully laid in bed! cmd.bed.not_in=Could not lay in bed. Are you trying to sleep in a bed? (PS: You must use the head block coordinates of the bed) cmd.bed.not_a_bed=The block on (X: {0:0.0}, Y: {1:0.0}, Z: {2:0.0}) is not a bed! +cmd.bed.searching=Searching for a bed in radius of {0}... +cmd.bed.bed_not_found=Could not find a bed! +cmd.bed.found_a_bed_at=Found a bet at (X: {0:0.0}, Y: {1:0.0}, Z: {2:0.0}). +cmd.bed.cant_reach_safely=Can not reach the bed safely! +cmd.bed.moving=Moving to (X: {0:0.0}, Y: {1:0.0}, Z: {2:0.0}) where the bed is located. +cmd.bed.failed_to_reach_in_time=Failed to reach the bed position (X: {0:0.0}, Y: {1:0.0}, Z: {2:0.0}) in time (30 seconds). Giving up! # List cmd.list.desc=get the player list.