Added an option to find a bed and sleep in it.

This commit is contained in:
Milutinke 2022-09-29 19:28:34 +02:00
parent cd3adfa14c
commit a52da095b4
2 changed files with 99 additions and 3 deletions

View file

@ -1,16 +1,15 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Threading.Tasks;
using MinecraftClient.Mapping; using MinecraftClient.Mapping;
using PInvoke;
namespace MinecraftClient.Commands namespace MinecraftClient.Commands
{ {
public class BedCommand : Command public class BedCommand : Command
{ {
public override string CmdName { get { return "bed"; } } public override string CmdName { get { return "bed"; } }
public override string CmdUsage { get { return "bed leave|sleep <x> <y> <z>"; } } public override string CmdUsage { get { return "bed leave|sleep <x> <y> <z>|sleep <radius>"; } }
public override string CmdDesc { get { return "cmd.bed.desc"; } } public override string CmdDesc { get { return "cmd.bed.desc"; } }
public override string Run(McClient handler, string command, Dictionary<string, object> localVars) public override string Run(McClient handler, string command, Dictionary<string, object> localVars)
@ -29,6 +28,97 @@ namespace MinecraftClient.Commands
if (subcommand.Equals("sleep") || subcommand.Equals("s")) 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<Location> 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) if (args.Length >= 3)
{ {
Location current = handler.GetCurrentLocation(); Location current = handler.GetCurrentLocation();

View file

@ -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.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_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.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 # List
cmd.list.desc=get the player list. cmd.list.desc=get the player list.