mirror of
https://github.com/MCCTeam/Minecraft-Console-Client
synced 2025-10-14 21:22:49 +00:00
150 lines
No EOL
6.6 KiB
C#
150 lines
No EOL
6.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using MinecraftClient.Mapping;
|
|
|
|
namespace MinecraftClient.Commands
|
|
{
|
|
public class BedCommand : Command
|
|
{
|
|
public override string CmdName { get { return "bed"; } }
|
|
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 Run(McClient handler, string command, Dictionary<string, object> localVars)
|
|
{
|
|
string[] args = getArgs(command);
|
|
|
|
if (args.Length >= 1)
|
|
{
|
|
string subcommand = args[0].ToLower().Trim();
|
|
|
|
if (subcommand.Equals("leave") || subcommand.Equals("l"))
|
|
{
|
|
handler.SendEntityAction(Protocol.EntityActionType.LeaveBed);
|
|
return Translations.TryGet("cmd.bed.leaving");
|
|
}
|
|
|
|
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)
|
|
{
|
|
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();
|
|
|
|
if (!handler.GetWorld().GetBlock(block).Type.IsBed())
|
|
return Translations.TryGet("cmd.bed.not_a_bed", blockCenter.X, blockCenter.Y, blockCenter.Z);
|
|
|
|
bool res = handler.PlaceBlock(block, Direction.Down);
|
|
|
|
return Translations.TryGet(
|
|
"cmd.bed.trying_to_use",
|
|
blockCenter.X,
|
|
blockCenter.Y,
|
|
blockCenter.Z,
|
|
Translations.TryGet(res ? "cmd.bed.in" : "cmd.bed.not_in")
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
return CmdUsage;
|
|
}
|
|
}
|
|
} |