Added slab handling for 1.20/.1

Added Farm bot crops handling for 1.20/.1
Added utilities for Containers/Inventories
Added bot movement lock to prevent multiple bots that use movements from running at the same time.
General code improvements.
This commit is contained in:
Anon 2023-06-23 16:25:18 +02:00
parent 497a1174de
commit 272900d52e
11 changed files with 941 additions and 797 deletions

View file

@ -8,7 +8,6 @@ namespace MinecraftClient.ChatBots
/// <summary> /// <summary>
/// This bot sends a command every 60 seconds in order to stay non-afk. /// This bot sends a command every 60 seconds in order to stay non-afk.
/// </summary> /// </summary>
public class AntiAFK : ChatBot public class AntiAFK : ChatBot
{ {
public static Configs Config = new(); public static Configs Config = new();
@ -16,8 +15,7 @@ namespace MinecraftClient.ChatBots
[TomlDoNotInlineObject] [TomlDoNotInlineObject]
public class Configs public class Configs
{ {
[NonSerialized] [NonSerialized] private const string BotName = "AntiAFK";
private const string BotName = "AntiAFK";
public bool Enabled = false; public bool Enabled = false;
@ -99,6 +97,13 @@ namespace MinecraftClient.ChatBots
{ {
LogToConsole(Translations.bot_antiafk_not_using_terrain_handling); LogToConsole(Translations.bot_antiafk_not_using_terrain_handling);
} }
else
{
var movementLock = BotMovementLock.Instance;
if (movementLock is { IsLocked: true })
LogToConsole(
$"§§6§1§0{string.Format(Translations.bot_antiafk_may_not_move, movementLock.LockedBy)}");
}
} }
} }
@ -106,25 +111,22 @@ namespace MinecraftClient.ChatBots
{ {
count++; count++;
if (count >= nextrun) if (count < nextrun) return;
{
DoAntiAfkStuff(); DoAntiAfkStuff();
count = 0; count = 0;
nextrun = random.Next(Settings.DoubleToTick(Config.Delay.min), Settings.DoubleToTick(Config.Delay.max)); nextrun = random.Next(Settings.DoubleToTick(Config.Delay.min), Settings.DoubleToTick(Config.Delay.max));
} }
}
private void DoAntiAfkStuff() private void DoAntiAfkStuff()
{ {
if (Config.Use_Terrain_Handling && GetTerrainEnabled()) var isMovementLocked = BotMovementLock.Instance;
if (Config.Use_Terrain_Handling && GetTerrainEnabled() && isMovementLocked is {IsLocked: false})
{ {
Location currentLocation = GetCurrentLocation(); var currentLocation = GetCurrentLocation();
Location goal;
bool moved = false; var moved = false;
bool useAlternativeMethod = false; var useAlternativeMethod = false;
int triesCounter = 0; var triesCounter = 0;
while (!moved) while (!moved)
{ {
@ -134,10 +136,11 @@ namespace MinecraftClient.ChatBots
break; break;
} }
goal = GetRandomLocationWithinRangeXZ(currentLocation, Config.Walk_Range); var goal = GetRandomLocationWithinRangeXZ(currentLocation, Config.Walk_Range);
// Prevent getting the same location // Prevent getting the same location
while ((currentLocation.X == goal.X) && (currentLocation.Y == goal.Y) && (currentLocation.Z == goal.Z)) while ((currentLocation.X == goal.X) && (currentLocation.Y == goal.Y) &&
(currentLocation.Z == goal.Z))
{ {
LogToConsole("Same location!, generating new one"); LogToConsole("Same location!, generating new one");
goal = GetRandomLocationWithinRangeXZ(currentLocation, Config.Walk_Range); goal = GetRandomLocationWithinRangeXZ(currentLocation, Config.Walk_Range);
@ -148,11 +151,9 @@ namespace MinecraftClient.ChatBots
useAlternativeMethod = true; useAlternativeMethod = true;
break; break;
} }
else
{
moved = MoveToLocation(goal, allowUnsafe: false, allowDirectTeleport: false); moved = MoveToLocation(goal, allowUnsafe: false, allowDirectTeleport: false);
} }
}
if (!useAlternativeMethod && Config.Use_Sneak) if (!useAlternativeMethod && Config.Use_Sneak)
{ {
@ -169,12 +170,14 @@ namespace MinecraftClient.ChatBots
Sneak(previousSneakState); Sneak(previousSneakState);
previousSneakState = !previousSneakState; previousSneakState = !previousSneakState;
} }
count = 0; count = 0;
} }
private Location GetRandomLocationWithinRangeXZ(Location currentLocation, int range) private Location GetRandomLocationWithinRangeXZ(Location currentLocation, int range)
{ {
return new Location(currentLocation.X + random.Next(range * -1, range), currentLocation.Y, currentLocation.Z + random.Next(range * -1, range)); return new Location(currentLocation.X + random.Next(range * -1, range), currentLocation.Y,
currentLocation.Z + random.Next(range * -1, range));
} }
} }
} }

View file

@ -7,6 +7,7 @@ using Brigadier.NET;
using Brigadier.NET.Builder; using Brigadier.NET.Builder;
using MinecraftClient.CommandHandler; using MinecraftClient.CommandHandler;
using MinecraftClient.CommandHandler.Patch; using MinecraftClient.CommandHandler.Patch;
using MinecraftClient.Commands;
using MinecraftClient.Inventory; using MinecraftClient.Inventory;
using MinecraftClient.Mapping; using MinecraftClient.Mapping;
using MinecraftClient.Protocol.Handlers; using MinecraftClient.Protocol.Handlers;
@ -24,8 +25,7 @@ namespace MinecraftClient.ChatBots
[TomlDoNotInlineObject] [TomlDoNotInlineObject]
public class Configs public class Configs
{ {
[NonSerialized] [NonSerialized] private const string BotName = "Farmer";
private const string BotName = "Farmer";
public bool Enabled = false; public bool Enabled = false;
@ -39,12 +39,12 @@ namespace MinecraftClient.ChatBots
} }
} }
public enum State private enum State
{ {
SearchingForCropsToBreak = 0, SearchingForCropsToBreak = 0,
SearchingForFarmlandToPlant, SearchingForFarmlandToPlant,
PlantingCrops, BoneMealingCrops,
BonemealingCrops CollectingItems
} }
public enum CropType public enum CropType
@ -52,7 +52,7 @@ namespace MinecraftClient.ChatBots
Beetroot, Beetroot,
Carrot, Carrot,
Melon, Melon,
Netherwart, NetherWart,
Pumpkin, Pumpkin,
Potato, Potato,
Wheat Wheat
@ -66,9 +66,10 @@ namespace MinecraftClient.ChatBots
private bool allowTeleport = false; private bool allowTeleport = false;
private bool debugEnabled = false; private bool debugEnabled = false;
public int Delay_Between_Tasks_Millisecond => (int)Math.Round(Config.Delay_Between_Tasks * 1000); private int Delay_Between_Tasks_Millisecond => (int)Math.Round(Config.Delay_Between_Tasks * 1000);
private const string commandDescription = "farmer <start <crop type> [radius:<radius = 30>] [unsafe:<true/false>] [teleport:<true/false>] [debug:<true/false>]|stop>"; private const string commandDescription =
"farmer <start <crop type> [radius:<radius = 30>] [unsafe:<true/false>] [teleport:<true/false>] [debug:<true/false>]|stop>";
public override void Initialize() public override void Initialize()
{ {
@ -103,7 +104,8 @@ namespace MinecraftClient.ChatBots
.Then(l => l.Argument("CropType", MccArguments.FarmerCropType()) .Then(l => l.Argument("CropType", MccArguments.FarmerCropType())
.Executes(r => OnCommandStart(r.Source, MccArguments.GetFarmerCropType(r, "CropType"), null)) .Executes(r => OnCommandStart(r.Source, MccArguments.GetFarmerCropType(r, "CropType"), null))
.Then(l => l.Argument("OtherArgs", Arguments.GreedyString()) .Then(l => l.Argument("OtherArgs", Arguments.GreedyString())
.Executes(r => OnCommandStart(r.Source, MccArguments.GetFarmerCropType(r, "CropType"), Arguments.GetString(r, "OtherArgs")))))) .Executes(r => OnCommandStart(r.Source, MccArguments.GetFarmerCropType(r, "CropType"),
Arguments.GetString(r, "OtherArgs"))))))
.Then(l => l.Literal("_help") .Then(l => l.Literal("_help")
.Executes(r => OnCommandHelp(r.Source, string.Empty)) .Executes(r => OnCommandHelp(r.Source, string.Empty))
.Redirect(McClient.dispatcher.GetRoot().GetChild("help").GetChild(CommandName))) .Redirect(McClient.dispatcher.GetRoot().GetChild("help").GetChild(CommandName)))
@ -112,6 +114,8 @@ namespace MinecraftClient.ChatBots
public override void OnUnload() public override void OnUnload()
{ {
running = false;
BotMovementLock.Instance?.UnLock("Farmer");
McClient.dispatcher.Unregister(CommandName); McClient.dispatcher.Unregister(CommandName);
McClient.dispatcher.GetRoot().GetChild("help").RemoveChild(CommandName); McClient.dispatcher.GetRoot().GetChild("help").RemoveChild(CommandName);
} }
@ -145,7 +149,12 @@ namespace MinecraftClient.ChatBots
if (running) if (running)
return r.SetAndReturn(CmdResult.Status.Fail, Translations.bot_farmer_already_running); return r.SetAndReturn(CmdResult.Status.Fail, Translations.bot_farmer_already_running);
int radius = 30; var movementLock = BotMovementLock.Instance;
if (movementLock is { IsLocked: true })
return r.SetAndReturn(CmdResult.Status.Fail,
string.Format(Translations.bot_common_movement_lock_held, "Farmer", movementLock.LockedBy));
var radius = 30;
state = State.SearchingForFarmlandToPlant; state = State.SearchingForFarmlandToPlant;
cropType = whatToFarm; cropType = whatToFarm;
@ -155,20 +164,22 @@ namespace MinecraftClient.ChatBots
if (!string.IsNullOrWhiteSpace(otherArgs)) if (!string.IsNullOrWhiteSpace(otherArgs))
{ {
string[] args = otherArgs.ToLower().Split(' ', StringSplitOptions.TrimEntries); var args = otherArgs.ToLower().Split(' ', StringSplitOptions.TrimEntries);
foreach (string currentArg in args) foreach (var currentArg in args)
{ {
if (!currentArg.Contains(':')) if (!currentArg.Contains(':'))
{ {
LogToConsole("§§6§1§0" + string.Format(Translations.bot_farmer_warining_invalid_parameter, currentArg)); LogToConsole(
$"§§6§1§0{string.Format(Translations.bot_farmer_warining_invalid_parameter, currentArg)}");
continue; continue;
} }
string[] parts = currentArg.Split(":", StringSplitOptions.TrimEntries); var parts = currentArg.Split(":", StringSplitOptions.TrimEntries);
if (parts.Length != 2) if (parts.Length != 2)
{ {
LogToConsole("§§6§1§0" + string.Format(Translations.bot_farmer_warining_invalid_parameter, currentArg)); LogToConsole(
$"§§6§1§0{string.Format(Translations.bot_farmer_warining_invalid_parameter, currentArg)}");
continue; continue;
} }
@ -177,11 +188,11 @@ namespace MinecraftClient.ChatBots
case "r": case "r":
case "radius": case "radius":
if (!int.TryParse(parts[1], NumberStyles.Any, CultureInfo.CurrentCulture, out radius)) if (!int.TryParse(parts[1], NumberStyles.Any, CultureInfo.CurrentCulture, out radius))
LogToConsole("§§6§1§0" + Translations.bot_farmer_invalid_radius); LogToConsole($"§§6§1§0{Translations.bot_farmer_invalid_radius}");
if (radius <= 0) if (radius <= 0)
{ {
LogToConsole("§§6§1§0" + Translations.bot_farmer_invalid_radius); LogToConsole($"§§6§1§0{Translations.bot_farmer_invalid_radius}");
radius = 30; radius = 30;
} }
@ -194,7 +205,7 @@ namespace MinecraftClient.ChatBots
if (parts[1].Equals("true") || parts[1].Equals("1")) if (parts[1].Equals("true") || parts[1].Equals("1"))
{ {
LogToConsole("§§6§1§0" + Translations.bot_farmer_warining_force_unsafe); LogToConsole($"§§6§1§0{Translations.bot_farmer_warining_force_unsafe}");
allowUnsafe = true; allowUnsafe = true;
} }
else allowUnsafe = false; else allowUnsafe = false;
@ -208,7 +219,7 @@ namespace MinecraftClient.ChatBots
if (parts[1].Equals("true") || parts[1].Equals("1")) if (parts[1].Equals("true") || parts[1].Equals("1"))
{ {
LogToConsole("§§4§1§f" + Translations.bot_farmer_warining_allow_teleport); LogToConsole($"§§4§1§f{Translations.bot_farmer_warining_allow_teleport}");
allowTeleport = true; allowTeleport = true;
} }
else allowTeleport = false; else allowTeleport = false;
@ -234,27 +245,57 @@ namespace MinecraftClient.ChatBots
farmingRadius = radius; farmingRadius = radius;
running = true; running = true;
new Thread(() => MainPorcess()).Start(); new Thread(() => MainProcess()).Start();
return r.SetAndReturn(CmdResult.Status.Done); return r.SetAndReturn(CmdResult.Status.Done);
} }
public override void AfterGameJoined() public override void AfterGameJoined()
{ {
BotMovementLock.Instance?.UnLock("Farmer");
running = false; running = false;
} }
public override bool OnDisconnect(DisconnectReason reason, string message) public override bool OnDisconnect(DisconnectReason reason, string message)
{ {
BotMovementLock.Instance?.UnLock("Farmer");
running = false; running = false;
return true; return true;
} }
private void MainPorcess() private void MainProcess()
{ {
LogToConsole("§§2§1§f" + Translations.bot_farmer_started); var movementLock = BotMovementLock.Instance;
LogToConsole("§§2§1§f " + Translations.bot_farmer_crop_type + ": " + cropType); switch (movementLock)
LogToConsole("§§2§1§f " + Translations.bot_farmer_radius + ": " + farmingRadius); {
case { IsLocked: false }:
if (!movementLock.Lock("Farmer"))
{
running = false;
LogToConsole($"§§6§1§0Farmer bot failed to obtain the movement lock for some reason!");
LogToConsole($"§§6§1§0Disable other bots who have movement mechanics, and try again!");
return;
}
LogDebug($"Locked the movement for other bots!");
break;
case { IsLocked: true }:
running = false;
LogToConsole($"§§6§1§0Farmer bot failed to obtain the movement lock for some reason!");
LogToConsole($"§§6§1§0Disable other bots who have movement mechanics, and try again!");
return;
}
LogToConsole($"§§2§1§f{Translations.bot_farmer_started}");
LogToConsole($"§§2§1§f {Translations.bot_farmer_crop_type}: {cropType}");
LogToConsole($"§§2§1§f {Translations.bot_farmer_radius}: {farmingRadius}");
var itemTypes = new List<ItemType>
{
GetSeedItemTypeForCropType(cropType),
GetCropItemTypeForCropType(cropType)
};
itemTypes = itemTypes.Distinct().ToList();
while (running) while (running)
{ {
@ -271,7 +312,7 @@ namespace MinecraftClient.ChatBots
case State.SearchingForFarmlandToPlant: case State.SearchingForFarmlandToPlant:
LogDebug("Looking for farmland..."); LogDebug("Looking for farmland...");
ItemType cropTypeToPlant = GetSeedItemTypeForCropType(cropType); var cropTypeToPlant = GetSeedItemTypeForCropType(cropType);
// If we don't have any seeds on our hot bar, skip this step and try collecting some // If we don't have any seeds on our hot bar, skip this step and try collecting some
if (!SwitchToItem(cropTypeToPlant)) if (!SwitchToItem(cropTypeToPlant))
@ -282,7 +323,7 @@ namespace MinecraftClient.ChatBots
continue; continue;
} }
List<Location> farmlandToPlantOn = findEmptyFarmland(farmingRadius); var farmlandToPlantOn = FindEmptyFarmland(farmingRadius);
if (farmlandToPlantOn.Count == 0) if (farmlandToPlantOn.Count == 0)
{ {
@ -292,11 +333,9 @@ namespace MinecraftClient.ChatBots
continue; continue;
} }
int i = 0; var i = 0;
foreach (Location location in farmlandToPlantOn) foreach (var location in farmlandToPlantOn.TakeWhile(location => running))
{ {
if (!running) break;
// Check only every second iteration, minor optimization xD // Check only every second iteration, minor optimization xD
if (i % 2 == 0) if (i % 2 == 0)
{ {
@ -309,14 +348,15 @@ namespace MinecraftClient.ChatBots
} }
} }
double yValue = Math.Floor(location.Y) + 1; var yValue = Math.Floor(location.Y) + 1;
// TODO: Figure out why this is not working. // TODO: Figure out why this is not working.
// Why we need this: sometimes the server kicks the player for "invalid movement" packets. // Why we need this: sometimes the server kicks the player for "invalid movement" packets.
/*if (cropType == CropType.Netherwart) /*if (cropType == CropType.NetherWart)
yValue = (double)(Math.Floor(location.Y) - 1.0) + (double)0.87500;*/ yValue = (double)(Math.Floor(location.Y) - 1.0) + (double)0.87500;*/
Location location2 = new Location(Math.Floor(location.X) + 0.5, yValue, Math.Floor(location.Z) + 0.5); var location2 = new Location(Math.Floor(location.X) + 0.5, yValue,
Math.Floor(location.Z) + 0.5);
if (WaitForMoveToLocation(location2)) if (WaitForMoveToLocation(location2))
{ {
@ -329,7 +369,8 @@ namespace MinecraftClient.ChatBots
break; break;
} }
Location loc = new Location(Math.Floor(location.X), Math.Floor(location2.Y), Math.Floor(location.Z)); var loc = new Location(Math.Floor(location.X), Math.Floor(location2.Y),
Math.Floor(location.Z));
LogDebug("Sending placeblock to: " + loc); LogDebug("Sending placeblock to: " + loc);
SendPlaceBlock(loc, Direction.Up); SendPlaceBlock(loc, Direction.Up);
@ -347,21 +388,21 @@ namespace MinecraftClient.ChatBots
case State.SearchingForCropsToBreak: case State.SearchingForCropsToBreak:
LogDebug("Searching for crops to break..."); LogDebug("Searching for crops to break...");
List<Location> cropsToCollect = findCrops(farmingRadius, cropType, true); var cropsToCollect = findCrops(farmingRadius, cropType, true);
if (cropsToCollect.Count == 0) if (cropsToCollect.Count == 0)
{ {
LogToConsole("No crops to break, trying to bonemeal ungrown ones"); LogToConsole("No crops to break, trying to bone meal un-grown ones");
state = State.BonemealingCrops; state = State.BoneMealingCrops;
Thread.Sleep(Delay_Between_Tasks_Millisecond); Thread.Sleep(Delay_Between_Tasks_Millisecond);
continue; continue;
} }
// Switch to an axe for faster breaking if the bot has one in his inventory // Switch to an axe for faster breaking if the bot has one in his inventory
if (cropType == CropType.Melon || cropType == CropType.Pumpkin) if (cropType is CropType.Melon or CropType.Pumpkin)
{ {
// Start from Diamond axe, if not found, try a tier lower axe // Start from Diamond axe, if not found, try a tier lower axe
bool switched = SwitchToItem(ItemType.DiamondAxe); var switched = SwitchToItem(ItemType.DiamondAxe);
if (!switched) if (!switched)
switched = SwitchToItem(ItemType.IronAxe); switched = SwitchToItem(ItemType.IronAxe);
@ -373,10 +414,8 @@ namespace MinecraftClient.ChatBots
SwitchToItem(ItemType.StoneAxe); SwitchToItem(ItemType.StoneAxe);
} }
foreach (Location location in cropsToCollect) foreach (var location in cropsToCollect.TakeWhile(location => running))
{ {
if (!running) break;
// God damn C# rounding it to 0.94 // God damn C# rounding it to 0.94
// This will be needed when bot bone meals carrots or potatoes which are at the first stage of growth, // This will be needed when bot bone meals carrots or potatoes which are at the first stage of growth,
// because sometimes the bot walks over crops and breaks them // because sometimes the bot walks over crops and breaks them
@ -387,16 +426,16 @@ namespace MinecraftClient.ChatBots
WaitForDigBlock(location); WaitForDigBlock(location);
// Allow some time to pickup the item // Allow some time to pickup the item
Thread.Sleep(cropType == CropType.Melon || cropType == CropType.Pumpkin ? 400 : 200); Thread.Sleep(cropType is CropType.Melon or CropType.Pumpkin ? 400 : 200);
} }
LogDebug("Finished breaking crops!"); LogDebug("Finished breaking crops!");
state = State.BonemealingCrops; state = State.BoneMealingCrops;
break; break;
case State.BonemealingCrops: case State.BoneMealingCrops:
// Can't be bone mealed // Can't be bone mealed
if (cropType == CropType.Netherwart) if (cropType == CropType.NetherWart)
{ {
state = State.SearchingForFarmlandToPlant; state = State.SearchingForFarmlandToPlant;
Thread.Sleep(Delay_Between_Tasks_Millisecond); Thread.Sleep(Delay_Between_Tasks_Millisecond);
@ -412,7 +451,7 @@ namespace MinecraftClient.ChatBots
continue; continue;
} }
List<Location> cropsToBonemeal = findCrops(farmingRadius, cropType, false); var cropsToBonemeal = findCrops(farmingRadius, cropType, false);
if (cropsToBonemeal.Count == 0) if (cropsToBonemeal.Count == 0)
{ {
@ -422,11 +461,9 @@ namespace MinecraftClient.ChatBots
continue; continue;
} }
int i2 = 0; var i2 = 0;
foreach (Location location in cropsToBonemeal) foreach (var location in cropsToBonemeal.TakeWhile(location => running))
{ {
if (!running) break;
// Check only every second iteration, minor optimization xD // Check only every second iteration, minor optimization xD
if (i2 % 2 == 0) if (i2 % 2 == 0)
{ {
@ -448,11 +485,14 @@ namespace MinecraftClient.ChatBots
break; break;
} }
Location location2 = new Location(Math.Floor(location.X) + 0.5, location.Y, Math.Floor(location.Z) + 0.5); var location2 = new Location(Math.Floor(location.X) + 0.5, location.Y,
Math.Floor(location.Z) + 0.5);
LogDebug("Trying to bone meal: " + location2); LogDebug("Trying to bone meal: " + location2);
// Send like 4 bone meal attempts, it should do the job with 2-3, but sometimes doesn't do // Send like 4 bone meal attempts, it should do the job with 2-3, but sometimes doesn't do
for (int boneMealTimes = 0; boneMealTimes < (cropType == CropType.Beetroot ? 6 : 5); boneMealTimes++) for (var boneMealTimes = 0;
boneMealTimes < (cropType == CropType.Beetroot ? 6 : 5);
boneMealTimes++)
{ {
// TODO: Do a check if the carrot/potato is on the first growth stage // TODO: Do a check if the carrot/potato is on the first growth stage
// if so, use: new Location(location.X, (double)(location.Y - 1) + (double)0.93750, location.Z) // if so, use: new Location(location.X, (double)(location.Y - 1) + (double)0.93750, location.Z)
@ -466,99 +506,117 @@ namespace MinecraftClient.ChatBots
} }
LogDebug("Finished bone mealing crops!"); LogDebug("Finished bone mealing crops!");
state = State.CollectingItems;
break;
case State.CollectingItems:
LogDebug("Searching for items to collect...");
var currentLocation = GetCurrentLocation();
var items = GetEntities()
.Where(x =>
x.Value.Type == EntityType.Item &&
x.Value.Location.Distance(currentLocation) <= farmingRadius &&
itemTypes.Contains(x.Value.Item.Type))
.Select(x => x.Value)
.ToList();
items = items.OrderBy(x => x.Location.Distance(currentLocation)).ToList();
if (items.Any())
{
LogDebug("Collecting items...");
foreach (var entity in items.TakeWhile(entity => running))
WaitForMoveToLocation(entity.Location);
LogDebug("Finished collecting items!");
}
else LogDebug("No items to collect!");
state = State.SearchingForFarmlandToPlant; state = State.SearchingForFarmlandToPlant;
break; break;
} }
LogDebug(string.Format("Waiting for {0:0.00} seconds for next cycle.", Config.Delay_Between_Tasks)); LogDebug($"Waiting for {Config.Delay_Between_Tasks:0.00} seconds for next cycle.");
Thread.Sleep(Delay_Between_Tasks_Millisecond); Thread.Sleep(Delay_Between_Tasks_Millisecond);
} }
movementLock?.UnLock("Farmer");
LogDebug($"Unlocked the movement for other bots!");
LogToConsole(Translations.bot_farmer_stopped); LogToConsole(Translations.bot_farmer_stopped);
} }
private Material GetMaterialForCropType(CropType type) private static Material GetMaterialForCropType(CropType type)
{ {
switch (type) return type switch
{ {
case CropType.Beetroot: CropType.Beetroot => Material.Beetroots,
return Material.Beetroots; CropType.Carrot => Material.Carrots,
CropType.Melon => Material.Melon,
case CropType.Carrot: CropType.NetherWart => Material.NetherWart,
return Material.Carrots; CropType.Pumpkin => Material.Pumpkin,
CropType.Potato => Material.Potatoes,
case CropType.Melon: CropType.Wheat => Material.Wheat,
return Material.Melon; _ => throw new Exception("Material type for " + type.GetType().Name + " has not been mapped!")
};
case CropType.Netherwart:
return Material.NetherWart;
case CropType.Pumpkin:
return Material.Pumpkin;
case CropType.Potato:
return Material.Potatoes;
case CropType.Wheat:
return Material.Wheat;
} }
throw new Exception("Material type for " + type.GetType().Name + " has not been mapped!"); private static ItemType GetSeedItemTypeForCropType(CropType type)
}
private ItemType GetSeedItemTypeForCropType(CropType type)
{ {
switch (type) return type switch
{ {
case CropType.Beetroot: CropType.Beetroot => ItemType.BeetrootSeeds,
return ItemType.BeetrootSeeds; CropType.Carrot => ItemType.Carrot,
CropType.Melon => ItemType.MelonSeeds,
case CropType.Carrot: CropType.NetherWart => ItemType.NetherWart,
return ItemType.Carrot; CropType.Pumpkin => ItemType.PumpkinSeeds,
CropType.Potato => ItemType.Potato,
case CropType.Melon: CropType.Wheat => ItemType.WheatSeeds,
return ItemType.MelonSeeds; _ => throw new Exception("Seed type for " + type.GetType().Name + " has not been mapped!")
};
case CropType.Netherwart:
return ItemType.NetherWart;
case CropType.Pumpkin:
return ItemType.PumpkinSeeds;
case CropType.Potato:
return ItemType.Potato;
case CropType.Wheat:
return ItemType.WheatSeeds;
} }
throw new Exception("Seed type for " + type.GetType().Name + " has not been mapped!"); private static ItemType GetCropItemTypeForCropType(CropType type)
{
return type switch
{
CropType.Beetroot => ItemType.Beetroot,
CropType.Carrot => ItemType.Carrot,
CropType.Melon => ItemType.Melon,
CropType.NetherWart => ItemType.NetherWart,
CropType.Pumpkin => ItemType.Pumpkin,
CropType.Potato => ItemType.Potato,
CropType.Wheat => ItemType.Wheat,
_ => throw new Exception("Item type for " + type.GetType().Name + " has not been mapped!")
};
} }
private List<Location> findEmptyFarmland(int radius) private List<Location> FindEmptyFarmland(int radius)
{ {
return GetWorld() return GetWorld()
.FindBlock(GetCurrentLocation(), cropType == CropType.Netherwart ? Material.SoulSand : Material.Farmland, radius) .FindBlock(GetCurrentLocation(),
.Where(location => GetWorld().GetBlock(new Location(location.X, location.Y + 1, location.Z)).Type == Material.Air) cropType == CropType.NetherWart ? Material.SoulSand : Material.Farmland, radius)
.Where(location => GetWorld().GetBlock(new Location(location.X, location.Y + 1, location.Z)).Type ==
Material.Air)
.ToList(); .ToList();
} }
private List<Location> findCrops(int radius, CropType cropType, bool fullyGrown) private List<Location> findCrops(int radius, CropType cropType, bool fullyGrown)
{ {
Material material = GetMaterialForCropType(cropType); var material = GetMaterialForCropType(cropType);
// A bit of a hack to enable bone mealing melon and pumpkin stems // A bit of a hack to enable bone mealing melon and pumpkin stems
if (!fullyGrown && (cropType == CropType.Melon || cropType == CropType.Pumpkin)) if (!fullyGrown && cropType is CropType.Melon or CropType.Pumpkin)
material = cropType == CropType.Melon ? Material.MelonStem : Material.PumpkinStem; material = cropType == CropType.Melon ? Material.MelonStem : Material.PumpkinStem;
return GetWorld() return GetWorld()
.FindBlock(GetCurrentLocation(), material, radius) .FindBlock(GetCurrentLocation(), material, radius)
.Where(location => .Where(location =>
{ {
if (fullyGrown && (material == Material.Melon || material == Material.Pumpkin)) if (fullyGrown && material is Material.Melon or Material.Pumpkin)
return true; return true;
bool isFullyGrown = IsCropFullyGrown(GetWorld().GetBlock(location), cropType); var isFullyGrown = IsCropFullyGrown(GetWorld().GetBlock(location), cropType);
return fullyGrown ? isFullyGrown : !isFullyGrown; return fullyGrown ? isFullyGrown : !isFullyGrown;
}) })
.ToList(); .ToList();
@ -566,83 +624,47 @@ namespace MinecraftClient.ChatBots
private bool IsCropFullyGrown(Block block, CropType cropType) private bool IsCropFullyGrown(Block block, CropType cropType)
{ {
int protocolVersion = GetProtocolVersion(); var protocolVersion = GetProtocolVersion();
switch (cropType) switch (cropType)
{ {
case CropType.Beetroot: case CropType.Beetroot:
if (protocolVersion == Protocol18Handler.MC_1_19_4_Version) switch (protocolVersion)
{ {
if (block.BlockId == 12356) case Protocol18Handler.MC_1_20_Version when block.BlockId == 12371:
return true; case Protocol18Handler.MC_1_19_4_Version when block.BlockId == 12356:
} case Protocol18Handler.MC_1_19_3_Version when block.BlockId == 11887:
else if (protocolVersion == Protocol18Handler.MC_1_19_3_Version) case >= Protocol18Handler.MC_1_19_Version and <= Protocol18Handler.MC_1_19_2_Version
{ when block.BlockId == 10103:
if (block.BlockId == 11887) case >= Protocol18Handler.MC_1_17_Version and <= Protocol18Handler.MC_1_18_2_Version
return true; when block.BlockId == 9472:
} case >= Protocol18Handler.MC_1_16_Version and <= Protocol18Handler.MC_1_16_5_Version
else if (protocolVersion >= Protocol18Handler.MC_1_19_Version && protocolVersion <= Protocol18Handler.MC_1_19_2_Version) when block.BlockId == 9226:
{ case >= Protocol18Handler.MC_1_14_Version and <= Protocol18Handler.MC_1_15_2_Version
if (block.BlockId == 10103) when block.BlockId == 8686:
return true; case >= Protocol18Handler.MC_1_13_Version and < Protocol18Handler.MC_1_14_Version
} when block.BlockId == 8162:
else if (protocolVersion >= Protocol18Handler.MC_1_17_Version && protocolVersion <= Protocol18Handler.MC_1_18_2_Version)
{
if (block.BlockId == 9472)
return true;
}
else if (protocolVersion >= Protocol18Handler.MC_1_16_Version && protocolVersion <= Protocol18Handler.MC_1_16_5_Version)
{
if (block.BlockId == 9226)
return true;
}
else if (protocolVersion >= Protocol18Handler.MC_1_14_Version && protocolVersion <= Protocol18Handler.MC_1_15_2_Version)
{
if (block.BlockId == 8686)
return true;
}
else if (protocolVersion >= Protocol18Handler.MC_1_13_Version && protocolVersion < Protocol18Handler.MC_1_14_Version)
{
if (block.BlockId == 8162)
return true; return true;
} }
break; break;
case CropType.Carrot: case CropType.Carrot:
if (protocolVersion == Protocol18Handler.MC_1_19_4_Version) switch (protocolVersion)
{ {
if (block.BlockId == 8598) case Protocol18Handler.MC_1_20_Version when block.BlockId == 8602:
return true; case Protocol18Handler.MC_1_19_4_Version when block.BlockId == 8598:
} case Protocol18Handler.MC_1_19_3_Version when block.BlockId == 8370:
else if (protocolVersion == Protocol18Handler.MC_1_19_3_Version) case >= Protocol18Handler.MC_1_19_Version and <= Protocol18Handler.MC_1_19_2_Version
{ when block.BlockId == 6930:
if (block.BlockId == 8370) case >= Protocol18Handler.MC_1_17_Version and <= Protocol18Handler.MC_1_18_2_Version
return true; when block.BlockId == 6543:
} case >= Protocol18Handler.MC_1_16_Version and <= Protocol18Handler.MC_1_16_5_Version
else if (protocolVersion >= Protocol18Handler.MC_1_19_Version && protocolVersion <= Protocol18Handler.MC_1_19_2_Version) when block.BlockId == 6341:
{ case >= Protocol18Handler.MC_1_14_Version and <= Protocol18Handler.MC_1_15_2_Version
if (block.BlockId == 6930) when block.BlockId == 5801:
return true; case >= Protocol18Handler.MC_1_13_Version and < Protocol18Handler.MC_1_14_Version
} when block.BlockId == 5295:
else if (protocolVersion >= Protocol18Handler.MC_1_17_Version && protocolVersion <= Protocol18Handler.MC_1_18_2_Version)
{
if (block.BlockId == 6543)
return true;
}
else if (protocolVersion >= Protocol18Handler.MC_1_16_Version && protocolVersion <= Protocol18Handler.MC_1_16_5_Version)
{
if (block.BlockId == 6341)
return true;
}
else if (protocolVersion >= Protocol18Handler.MC_1_14_Version && protocolVersion <= Protocol18Handler.MC_1_15_2_Version)
{
if (block.BlockId == 5801)
return true;
}
else if (protocolVersion >= Protocol18Handler.MC_1_13_Version && protocolVersion < Protocol18Handler.MC_1_14_Version)
{
if (block.BlockId == 5295)
return true; return true;
} }
@ -650,193 +672,106 @@ namespace MinecraftClient.ChatBots
// Checkin for stems and attached stems instead of Melons themselves // Checkin for stems and attached stems instead of Melons themselves
case CropType.Melon: case CropType.Melon:
if (protocolVersion == Protocol18Handler.MC_1_19_4_Version) switch (protocolVersion)
{ {
if (block.BlockId == 6808 || block.BlockId == 6606) case Protocol18Handler.MC_1_20_Version when block.BlockId is 6836 or 6820:
return true; case Protocol18Handler.MC_1_19_4_Version when block.BlockId is 6808 or 6606:
} case Protocol18Handler.MC_1_19_3_Version when block.BlockId is 6582 or 6832:
else if (protocolVersion == Protocol18Handler.MC_1_19_3_Version) case >= Protocol18Handler.MC_1_19_Version and <= Protocol18Handler.MC_1_19_2_Version
{ when block.BlockId is 5166 or 5150:
if (block.BlockId == 6582 || block.BlockId == 6832) case >= Protocol18Handler.MC_1_17_Version and <= Protocol18Handler.MC_1_18_2_Version
return true; when block.BlockId is 4860 or 4844:
} case >= Protocol18Handler.MC_1_16_Version and <= Protocol18Handler.MC_1_16_5_Version
else if (protocolVersion >= Protocol18Handler.MC_1_19_Version && protocolVersion <= Protocol18Handler.MC_1_19_2_Version) when block.BlockId is 4791 or 4775:
{ case >= Protocol18Handler.MC_1_14_Version and <= Protocol18Handler.MC_1_15_2_Version
if (block.BlockId == 5166 || block.BlockId == 5150) when block.BlockId is 4771 or 4755:
return true; case >= Protocol18Handler.MC_1_13_Version and < Protocol18Handler.MC_1_14_Version
} when block.BlockId is 4268 or 4252:
else if (protocolVersion >= Protocol18Handler.MC_1_17_Version && protocolVersion <= Protocol18Handler.MC_1_18_2_Version)
{
if (block.BlockId == 4860 || block.BlockId == 4844)
return true;
}
else if (protocolVersion >= Protocol18Handler.MC_1_16_Version && protocolVersion <= Protocol18Handler.MC_1_16_5_Version)
{
if (block.BlockId == 4791 || block.BlockId == 4775)
return true;
}
else if (protocolVersion >= Protocol18Handler.MC_1_14_Version && protocolVersion <= Protocol18Handler.MC_1_15_2_Version)
{
if (block.BlockId == 4771 || block.BlockId == 4755)
return true;
}
else if (protocolVersion >= Protocol18Handler.MC_1_13_Version && protocolVersion < Protocol18Handler.MC_1_14_Version)
{
if (block.BlockId == 4268 || block.BlockId == 4252)
return true; return true;
} }
break; break;
case CropType.Netherwart: case CropType.NetherWart:
if (protocolVersion == Protocol18Handler.MC_1_19_4_Version) switch (protocolVersion)
{ {
if (block.BlockId == 7384) case Protocol18Handler.MC_1_20_Version when block.BlockId == 7388:
return true; case Protocol18Handler.MC_1_19_4_Version when block.BlockId == 7384:
} case Protocol18Handler.MC_1_19_3_Version when block.BlockId == 7158:
else if (protocolVersion == Protocol18Handler.MC_1_19_3_Version) case >= Protocol18Handler.MC_1_19_Version and <= Protocol18Handler.MC_1_19_2_Version
{ when block.BlockId == 5718:
if (block.BlockId == 7158) case >= Protocol18Handler.MC_1_17_Version and <= Protocol18Handler.MC_1_18_2_Version
return true; when block.BlockId == 5332:
} case >= Protocol18Handler.MC_1_16_Version and <= Protocol18Handler.MC_1_16_5_Version
else if (protocolVersion >= Protocol18Handler.MC_1_19_Version && protocolVersion <= Protocol18Handler.MC_1_19_2_Version) when block.BlockId == 5135:
{ case >= Protocol18Handler.MC_1_14_Version and <= Protocol18Handler.MC_1_15_2_Version
if (block.BlockId == 5718) when block.BlockId == 5115:
return true; case >= Protocol18Handler.MC_1_13_Version and < Protocol18Handler.MC_1_14_Version
} when block.BlockId == 4612:
else if (protocolVersion >= Protocol18Handler.MC_1_17_Version && protocolVersion <= Protocol18Handler.MC_1_18_2_Version)
{
if (block.BlockId == 5332)
return true;
}
else if (protocolVersion >= Protocol18Handler.MC_1_16_Version && protocolVersion <= Protocol18Handler.MC_1_16_5_Version)
{
if (block.BlockId == 5135)
return true;
}
else if (protocolVersion >= Protocol18Handler.MC_1_14_Version && protocolVersion <= Protocol18Handler.MC_1_15_2_Version)
{
if (block.BlockId == 5115)
return true;
}
else if (protocolVersion >= Protocol18Handler.MC_1_13_Version && protocolVersion < Protocol18Handler.MC_1_14_Version)
{
if (block.BlockId == 4612)
return true; return true;
} }
break; break;
// Checkin for stems and attached stems instead of Pumpkins themselves // Checkin for stems and attached stems instead of Pumpkins themselves
case CropType.Pumpkin: case CropType.Pumpkin:
if (protocolVersion == Protocol18Handler.MC_1_19_4_Version) switch (protocolVersion)
{ {
if (block.BlockId == 5845 || block.BlockId == 6824) case Protocol18Handler.MC_1_20_Version when block.BlockId is 5849 or 6816:
return true; case Protocol18Handler.MC_1_19_4_Version when block.BlockId is 5845 or 6824:
} case Protocol18Handler.MC_1_19_3_Version when block.BlockId is 5683 or 6598:
else if (protocolVersion == Protocol18Handler.MC_1_19_3_Version) case >= Protocol18Handler.MC_1_19_Version and <= Protocol18Handler.MC_1_19_2_Version
{ when block.BlockId is 5158 or 5146:
if (block.BlockId == 5683 || block.BlockId == 6598) case >= Protocol18Handler.MC_1_17_Version and <= Protocol18Handler.MC_1_18_2_Version
return true; when block.BlockId is 4852 or 4840:
} case >= Protocol18Handler.MC_1_16_Version and <= Protocol18Handler.MC_1_16_5_Version
else if (protocolVersion >= Protocol18Handler.MC_1_19_Version && protocolVersion <= Protocol18Handler.MC_1_19_2_Version) when block.BlockId is 4783 or 4771:
{ case >= Protocol18Handler.MC_1_14_Version and <= Protocol18Handler.MC_1_15_2_Version
if (block.BlockId == 5158 || block.BlockId == 5146) when block.BlockId is 4763 or 4751:
return true; case >= Protocol18Handler.MC_1_13_Version and < Protocol18Handler.MC_1_14_Version
} when block.BlockId is 4260 or 4248:
else if (protocolVersion >= Protocol18Handler.MC_1_17_Version && protocolVersion <= Protocol18Handler.MC_1_18_2_Version)
{
if (block.BlockId == 4852 || block.BlockId == 4840)
return true;
}
else if (protocolVersion >= Protocol18Handler.MC_1_16_Version && protocolVersion <= Protocol18Handler.MC_1_16_5_Version)
{
if (block.BlockId == 4783 || block.BlockId == 4771)
return true;
}
else if (protocolVersion >= Protocol18Handler.MC_1_14_Version && protocolVersion <= Protocol18Handler.MC_1_15_2_Version)
{
if (block.BlockId == 4763 || block.BlockId == 4751)
return true;
}
else if (protocolVersion >= Protocol18Handler.MC_1_13_Version && protocolVersion < Protocol18Handler.MC_1_14_Version)
{
if (block.BlockId == 4260 || block.BlockId == 4248)
return true; return true;
} }
break; break;
case CropType.Potato: case CropType.Potato:
if (protocolVersion == Protocol18Handler.MC_1_19_4_Version) switch (protocolVersion)
{ {
if (block.BlockId == 8606) case Protocol18Handler.MC_1_20_Version when block.BlockId == 8610:
return true; case Protocol18Handler.MC_1_19_4_Version when block.BlockId == 8606:
} case Protocol18Handler.MC_1_19_3_Version when block.BlockId == 8378:
else if (protocolVersion == Protocol18Handler.MC_1_19_3_Version) case >= Protocol18Handler.MC_1_19_Version and <= Protocol18Handler.MC_1_19_2_Version
{ when block.BlockId == 6938:
if (block.BlockId == 8378) case >= Protocol18Handler.MC_1_17_Version and <= Protocol18Handler.MC_1_18_2_Version
return true; when block.BlockId == 6551:
} case >= Protocol18Handler.MC_1_16_Version and <= Protocol18Handler.MC_1_16_5_Version
else if (protocolVersion >= Protocol18Handler.MC_1_19_Version && protocolVersion <= Protocol18Handler.MC_1_19_2_Version) when block.BlockId == 6349:
{ case >= Protocol18Handler.MC_1_14_Version and <= Protocol18Handler.MC_1_15_2_Version
if (block.BlockId == 6938) when block.BlockId == 5809:
return true; case >= Protocol18Handler.MC_1_13_Version and < Protocol18Handler.MC_1_14_Version
} when block.BlockId == 5303:
else if (protocolVersion >= Protocol18Handler.MC_1_17_Version && protocolVersion <= Protocol18Handler.MC_1_18_2_Version)
{
if (block.BlockId == 6551)
return true;
}
else if (protocolVersion >= Protocol18Handler.MC_1_16_Version && protocolVersion <= Protocol18Handler.MC_1_16_5_Version)
{
if (block.BlockId == 6349)
return true;
}
else if (protocolVersion >= Protocol18Handler.MC_1_14_Version && protocolVersion <= Protocol18Handler.MC_1_15_2_Version)
{
if (block.BlockId == 5809)
return true;
}
else if (protocolVersion >= Protocol18Handler.MC_1_13_Version && protocolVersion < Protocol18Handler.MC_1_14_Version)
{
if (block.BlockId == 5303)
return true; return true;
} }
break; break;
case CropType.Wheat: case CropType.Wheat:
if (protocolVersion == Protocol18Handler.MC_1_19_4_Version) switch (protocolVersion)
{ {
if (block.BlockId == 4281) case Protocol18Handler.MC_1_20_Version when block.BlockId == 4285:
return true; case Protocol18Handler.MC_1_19_4_Version when block.BlockId == 4281:
} case Protocol18Handler.MC_1_19_3_Version when block.BlockId == 4233:
else if (protocolVersion == Protocol18Handler.MC_1_19_3_Version) case >= Protocol18Handler.MC_1_19_Version and <= Protocol18Handler.MC_1_19_2_Version
{ when block.BlockId == 3619:
if (block.BlockId == 4233) case >= Protocol18Handler.MC_1_17_Version and <= Protocol18Handler.MC_1_18_2_Version
return true; when block.BlockId == 3421:
} case >= Protocol18Handler.MC_1_16_Version and <= Protocol18Handler.MC_1_16_5_Version
else if (protocolVersion >= Protocol18Handler.MC_1_19_Version && protocolVersion <= Protocol18Handler.MC_1_19_2_Version) when block.BlockId == 3364:
{ case >= Protocol18Handler.MC_1_14_Version and <= Protocol18Handler.MC_1_15_2_Version
if (block.BlockId == 3619) when block.BlockId == 3362:
return true; case >= Protocol18Handler.MC_1_13_Version and < Protocol18Handler.MC_1_14_Version
} when block.BlockId == 3059:
else if (protocolVersion >= Protocol18Handler.MC_1_17_Version && protocolVersion <= Protocol18Handler.MC_1_18_2_Version)
{
if (block.BlockId == 3421)
return true;
}
else if (protocolVersion >= Protocol18Handler.MC_1_16_Version && protocolVersion <= Protocol18Handler.MC_1_16_5_Version)
{
if (block.BlockId == 3364)
return true;
}
else if (protocolVersion >= Protocol18Handler.MC_1_14_Version && protocolVersion <= Protocol18Handler.MC_1_15_2_Version)
{
if (block.BlockId == 3362)
return true;
}
else if (protocolVersion >= Protocol18Handler.MC_1_13_Version && protocolVersion < Protocol18Handler.MC_1_14_Version)
{
if (block.BlockId == 3059)
return true; return true;
} }
@ -849,28 +784,27 @@ namespace MinecraftClient.ChatBots
// Yoinked from ReinforceZwei's AutoTree and adapted to search the whole of inventory in additon to the hotbar // Yoinked from ReinforceZwei's AutoTree and adapted to search the whole of inventory in additon to the hotbar
private bool SwitchToItem(ItemType itemType) private bool SwitchToItem(ItemType itemType)
{ {
Container playerInventory = GetPlayerInventory(); var playerInventory = GetPlayerInventory();
if (playerInventory.Items.ContainsKey(GetCurrentSlot() - 36) if (playerInventory.Items.TryGetValue(GetCurrentSlot() - 36, out Item value) && value.Type == itemType)
&& playerInventory.Items[GetCurrentSlot() - 36].Type == itemType)
return true; // Already selected return true; // Already selected
// Search the full inventory // Search the full inventory
List<int> fullInventorySearch = new List<int>(playerInventory.SearchItem(itemType)); var fullInventorySearch = new List<int>(playerInventory.SearchItem(itemType));
// Search for the seed in the hot bar // Search for the seed in the hot bar
List<int> hotbarSerch = fullInventorySearch.Where(slot => slot >= 36 && slot <= 44).ToList(); var hotBarSearch = fullInventorySearch.Where(slot => slot is >= 36 and <= 44).ToList();
if (hotbarSerch.Count > 0) if (hotBarSearch.Count > 0)
{ {
ChangeSlot((short)(hotbarSerch[0] - 36)); ChangeSlot((short)(hotBarSearch[0] - 36));
return true; return true;
} }
if (fullInventorySearch.Count == 0) if (fullInventorySearch.Count == 0)
return false; return false;
ItemMovingHelper movingHelper = new ItemMovingHelper(playerInventory, Handler); var movingHelper = new ItemMovingHelper(playerInventory, Handler);
movingHelper.Swap(fullInventorySearch[0], 36); movingHelper.Swap(fullInventorySearch[0], 36);
ChangeSlot(0); ChangeSlot(0);
@ -897,8 +831,7 @@ namespace MinecraftClient.ChatBots
// Yoinked from Daenges's Sugarcane Farmer // Yoinked from Daenges's Sugarcane Farmer
private bool WaitForDigBlock(Location block, int digTimeout = 1000) private bool WaitForDigBlock(Location block, int digTimeout = 1000)
{ {
if (DigBlock(block.ToFloor())) if (!DigBlock(block.ToFloor())) return false;
{
short i = 0; // Maximum wait time of 10 sec. short i = 0; // Maximum wait time of 10 sec.
while (GetWorld().GetBlock(block).Type != Material.Air && i <= digTimeout) while (GetWorld().GetBlock(block).Type != Material.Air && i <= digTimeout)
{ {
@ -909,9 +842,6 @@ namespace MinecraftClient.ChatBots
return i <= digTimeout; return i <= digTimeout;
} }
return false;
}
private bool HasItemOfTypeInInventory(ItemType itemType) private bool HasItemOfTypeInInventory(ItemType itemType)
{ {
return GetPlayerInventory().SearchItem(itemType).Length > 0; return GetPlayerInventory().SearchItem(itemType).Length > 0;

View file

@ -19,8 +19,7 @@ namespace MinecraftClient.ChatBots
[TomlDoNotInlineObject] [TomlDoNotInlineObject]
public class Configs public class Configs
{ {
[NonSerialized] [NonSerialized] private const string BotName = "FollowPlayer";
private const string BotName = "FollowPlayer";
public bool Enabled = false; public bool Enabled = false;
@ -73,7 +72,8 @@ namespace MinecraftClient.ChatBots
.Then(l => l.Argument("PlayerName", MccArguments.PlayerName()) .Then(l => l.Argument("PlayerName", MccArguments.PlayerName())
.Executes(r => OnCommandStart(r.Source, Arguments.GetString(r, "PlayerName"), takeRisk: false)) .Executes(r => OnCommandStart(r.Source, Arguments.GetString(r, "PlayerName"), takeRisk: false))
.Then(l => l.Literal("-f") .Then(l => l.Literal("-f")
.Executes(r => OnCommandStart(r.Source, Arguments.GetString(r, "PlayerName"), takeRisk: true))))) .Executes(r =>
OnCommandStart(r.Source, Arguments.GetString(r, "PlayerName"), takeRisk: true)))))
.Then(l => l.Literal("stop") .Then(l => l.Literal("stop")
.Executes(r => OnCommandStop(r.Source))) .Executes(r => OnCommandStop(r.Source)))
.Then(l => l.Literal("_help") .Then(l => l.Literal("_help")
@ -84,6 +84,7 @@ namespace MinecraftClient.ChatBots
public override void OnUnload() public override void OnUnload()
{ {
BotMovementLock.Instance?.UnLock("Follow Player");
McClient.dispatcher.Unregister(CommandName); McClient.dispatcher.Unregister(CommandName);
McClient.dispatcher.GetRoot().GetChild("help").RemoveChild(CommandName); McClient.dispatcher.GetRoot().GetChild("help").RemoveChild(CommandName);
} }
@ -104,7 +105,7 @@ namespace MinecraftClient.ChatBots
if (!IsValidName(name)) if (!IsValidName(name))
return r.SetAndReturn(CmdResult.Status.Fail, Translations.cmd_follow_invalid_name); return r.SetAndReturn(CmdResult.Status.Fail, Translations.cmd_follow_invalid_name);
Entity? player = GetEntities().Values.ToList().Find(entity => var player = GetEntities().Values.ToList().Find(entity =>
entity.Type == EntityType.Player entity.Type == EntityType.Player
&& !string.IsNullOrEmpty(entity.Name) && !string.IsNullOrEmpty(entity.Name)
&& entity.Name.Equals(name, StringComparison.OrdinalIgnoreCase)); && entity.Name.Equals(name, StringComparison.OrdinalIgnoreCase));
@ -116,22 +117,37 @@ namespace MinecraftClient.ChatBots
return r.SetAndReturn(CmdResult.Status.Fail, Translations.cmd_follow_cant_reach_player); return r.SetAndReturn(CmdResult.Status.Fail, Translations.cmd_follow_cant_reach_player);
if (_playerToFollow != null && _playerToFollow.Equals(name, StringComparison.OrdinalIgnoreCase)) if (_playerToFollow != null && _playerToFollow.Equals(name, StringComparison.OrdinalIgnoreCase))
return r.SetAndReturn(CmdResult.Status.Fail, string.Format(Translations.cmd_follow_already_following, _playerToFollow)); return r.SetAndReturn(CmdResult.Status.Fail,
string.Format(Translations.cmd_follow_already_following, _playerToFollow));
string result; var movementLock = BotMovementLock.Instance;
if (_playerToFollow != null) if (movementLock is { IsLocked: true })
result = string.Format(Translations.cmd_follow_switched, player.Name!); return r.SetAndReturn(CmdResult.Status.Fail,
else string.Format(Translations.bot_common_movement_lock_held, "Follow Player", movementLock.LockedBy));
result = string.Format(Translations.cmd_follow_started, player.Name!);
var result =
string.Format(
_playerToFollow != null ? Translations.cmd_follow_switched : Translations.cmd_follow_started,
player.Name!);
_playerToFollow = name.ToLower(); _playerToFollow = name.ToLower();
if (takeRisk) switch (movementLock)
{ {
_unsafeEnabled = true; case { IsLocked: false }:
return r.SetAndReturn(CmdResult.Status.Done, Translations.cmd_follow_note + '\n' + Translations.cmd_follow_unsafe_enabled); if (!movementLock.Lock("Follow Player"))
{
LogToConsole($"§§6§1§0Follow Player bot failed to obtain the movement lock for some reason!");
LogToConsole($"§§6§1§0Disable other bots who have movement mechanics, and try again!");
return r.SetAndReturn(CmdResult.Status.Fail);
} }
else
return r.SetAndReturn(CmdResult.Status.Done, Translations.cmd_follow_note); break;
}
if (!takeRisk) return r.SetAndReturn(CmdResult.Status.Done, Translations.cmd_follow_note);
_unsafeEnabled = true;
return r.SetAndReturn(CmdResult.Status.Done,
Translations.cmd_follow_note + '\n' + Translations.cmd_follow_unsafe_enabled);
} }
private int OnCommandStop(CmdResult r) private int OnCommandStop(CmdResult r)
@ -139,6 +155,8 @@ namespace MinecraftClient.ChatBots
if (_playerToFollow == null) if (_playerToFollow == null)
return r.SetAndReturn(CmdResult.Status.Fail, Translations.cmd_follow_already_stopped); return r.SetAndReturn(CmdResult.Status.Fail, Translations.cmd_follow_already_stopped);
var movementLock = BotMovementLock.Instance;
movementLock?.UnLock("Follow Player");
_playerToFollow = null; _playerToFollow = null;
return r.SetAndReturn(CmdResult.Status.Done, Translations.cmd_follow_stopping); return r.SetAndReturn(CmdResult.Status.Done, Translations.cmd_follow_stopping);
@ -168,8 +186,8 @@ namespace MinecraftClient.ChatBots
if (!CanMoveThere(entity.Location)) if (!CanMoveThere(entity.Location))
return; return;
// Stop at specified distance from plater (prevents pushing player around) // Stop at specified distance from player (prevents pushing player around)
double distance = entity.Location.Distance(GetCurrentLocation()); var distance = entity.Location.Distance(GetCurrentLocation());
if (distance < Config.Stop_At_Distance) if (distance < Config.Stop_At_Distance)
return; return;
@ -182,7 +200,8 @@ namespace MinecraftClient.ChatBots
if (entity.Type != EntityType.Player) if (entity.Type != EntityType.Player)
return; return;
if (_playerToFollow != null && !string.IsNullOrEmpty(entity.Name) && _playerToFollow.Equals(entity.Name, StringComparison.OrdinalIgnoreCase)) if (_playerToFollow != null && !string.IsNullOrEmpty(entity.Name) &&
_playerToFollow.Equals(entity.Name, StringComparison.OrdinalIgnoreCase))
{ {
LogToConsole(string.Format(Translations.cmd_follow_player_came_to_the_range, _playerToFollow)); LogToConsole(string.Format(Translations.cmd_follow_player_came_to_the_range, _playerToFollow));
LogToConsole(Translations.cmd_follow_resuming); LogToConsole(Translations.cmd_follow_resuming);
@ -194,7 +213,8 @@ namespace MinecraftClient.ChatBots
if (entity.Type != EntityType.Player) if (entity.Type != EntityType.Player)
return; return;
if (_playerToFollow != null && !string.IsNullOrEmpty(entity.Name) && _playerToFollow.Equals(entity.Name, StringComparison.OrdinalIgnoreCase)) if (_playerToFollow != null && !string.IsNullOrEmpty(entity.Name) &&
_playerToFollow.Equals(entity.Name, StringComparison.OrdinalIgnoreCase))
{ {
LogToConsole(string.Format(Translations.cmd_follow_player_left_the_range, _playerToFollow)); LogToConsole(string.Format(Translations.cmd_follow_player_left_the_range, _playerToFollow));
LogToConsole(Translations.cmd_follow_pausing); LogToConsole(Translations.cmd_follow_pausing);
@ -203,7 +223,8 @@ namespace MinecraftClient.ChatBots
public override void OnPlayerLeave(Guid uuid, string? name) public override void OnPlayerLeave(Guid uuid, string? name)
{ {
if (_playerToFollow != null && !string.IsNullOrEmpty(name) && _playerToFollow.Equals(name, StringComparison.OrdinalIgnoreCase)) if (_playerToFollow != null && !string.IsNullOrEmpty(name) &&
_playerToFollow.Equals(name, StringComparison.OrdinalIgnoreCase))
{ {
LogToConsole(string.Format(Translations.cmd_follow_player_left, _playerToFollow)); LogToConsole(string.Format(Translations.cmd_follow_player_left, _playerToFollow));
LogToConsole(Translations.cmd_follow_stopping); LogToConsole(Translations.cmd_follow_stopping);
@ -213,12 +234,8 @@ namespace MinecraftClient.ChatBots
private bool CanMoveThere(Location location) private bool CanMoveThere(Location location)
{ {
ChunkColumn? chunkColumn = GetWorld().GetChunkColumn(location); var chunkColumn = GetWorld().GetChunkColumn(location);
return chunkColumn != null && chunkColumn.FullyLoaded != false;
if (chunkColumn == null || chunkColumn.FullyLoaded == false)
return false;
return true;
} }
} }
} }

View file

@ -89,6 +89,7 @@ public class ItemsCollector : ChatBot
public override void OnUnload() public override void OnUnload()
{ {
StopTheMainProcess();
McClient.dispatcher.Unregister(CommandName); McClient.dispatcher.Unregister(CommandName);
McClient.dispatcher.GetRoot().GetChild("help").RemoveChild(CommandName); McClient.dispatcher.GetRoot().GetChild("help").RemoveChild(CommandName);
} }
@ -109,6 +110,18 @@ public class ItemsCollector : ChatBot
if (running) if (running)
return r.SetAndReturn(CmdResult.Status.Fail, Translations.cmd_items_collector_already_collecting); return r.SetAndReturn(CmdResult.Status.Fail, Translations.cmd_items_collector_already_collecting);
var movementLock = BotMovementLock.Instance;
if (movementLock is { IsLocked: true })
return r.SetAndReturn(CmdResult.Status.Fail,
string.Format(Translations.bot_common_movement_lock_held, "Items Collector", movementLock.LockedBy));
if (!movementLock!.Lock("Items Collector"))
{
LogToConsole($"§§6§1§0Items Collector bot failed to obtain the movement lock for some reason!");
LogToConsole($"§§6§1§0Disable other bots who have movement mechanics, and try again!");
return r.SetAndReturn(CmdResult.Status.Fail);
}
StartTheMainProcess(); StartTheMainProcess();
return r.SetAndReturn(CmdResult.Status.Done, Translations.cmd_items_collector_started); return r.SetAndReturn(CmdResult.Status.Done, Translations.cmd_items_collector_started);
} }
@ -132,6 +145,7 @@ public class ItemsCollector : ChatBot
private void StopTheMainProcess() private void StopTheMainProcess()
{ {
running = false; running = false;
BotMovementLock.Instance?.UnLock("Items Collector");
} }
private void MainProcess() private void MainProcess()
@ -163,14 +177,9 @@ public class ItemsCollector : ChatBot
if (items.Any()) if (items.Any())
{ {
foreach (var entity in items) foreach (var entity in items.TakeWhile(entity => running))
{
if (!running)
break;
WaitForMoveToLocation(entity.Location); WaitForMoveToLocation(entity.Location);
} }
}
else else
{ {
if (startingLocation.Distance(currentLocation) > 2f && Config.Always_Return_To_Start) if (startingLocation.Distance(currentLocation) > 2f && Config.Always_Return_To_Start)

View file

@ -283,7 +283,7 @@ public class WebSocketBot : ChatBot
public int Port = 8043; public int Port = 8043;
[TomlInlineComment("$ChatBot.WebSocketBot.Password$")] [TomlInlineComment("$ChatBot.WebSocketBot.Password$")]
public string? Password = "wspass12345"; public string? Password = Guid.NewGuid().ToString().Replace("-", "").Trim().ToLower();
[TomlInlineComment("$ChatBot.WebSocketBot.DebugMode$")] [TomlInlineComment("$ChatBot.WebSocketBot.DebugMode$")]
public bool DebugMode = false; public bool DebugMode = false;

View file

@ -0,0 +1,58 @@
using System;
namespace MinecraftClient.Inventory;
using System.Linq;
using System.Linq.Expressions;
public enum ComparisonType
{
Equals,
NotEquals,
Less,
LessOrEqual,
Greater,
GreaterOrEqual
}
public static class ContainerExtensions
{
public static bool IsFull(this Container container) =>
container.Items.Values.All(item => item.Count >= item.Type.StackCount());
public static int FirstEmptySlot(this Container container) =>
container.Items.FirstOrDefault(slot => slot.Value.IsEmpty).Key;
public static int FirstSlotWithItem(this Container container, ItemType type, int? count = null,
ComparisonType comparison = ComparisonType.Equals) =>
container.Items.FirstOrDefault(slot =>
slot.Value.Type == type &&
(!count.HasValue || CompareCounts(slot.Value.Count, count.Value, comparison))).Key;
public static int LastSlotWithItem(this Container container, ItemType type, int? count = null,
ComparisonType comparison = ComparisonType.Equals) =>
container.Items.LastOrDefault(slot =>
slot.Value.Type == type &&
(!count.HasValue || CompareCounts(slot.Value.Count, count.Value, comparison)))
.Key;
// We could have used "Expression<Func<int, int, bool>> comparison = null" instead of "ComparisonType comparison", but it would look ugly to use
private static bool CompareCounts(int value1, int value2, ComparisonType comparison)
{
var left = Expression.Constant(value1);
var right = Expression.Constant(value2);
Expression binaryExpression = comparison switch
{
ComparisonType.Less => Expression.LessThan(left, right),
ComparisonType.LessOrEqual => Expression.LessThanOrEqual(left, right),
ComparisonType.Greater => Expression.GreaterThan(left, right),
ComparisonType.GreaterOrEqual => Expression.GreaterThanOrEqual(left, right),
ComparisonType.Equals => Expression.Equal(left, right),
ComparisonType.NotEquals => Expression.NotEqual(left, right),
_ => Expression.Equal(left, right)
};
return Expression.Lambda<Func<bool>>(binaryExpression).Compile().Invoke();
}
}

View file

@ -6,8 +6,68 @@ public static class BlockExtension
{ {
public static bool IsTopSlab(this Block block, int protocolVersion) public static bool IsTopSlab(this Block block, int protocolVersion)
{ {
if (protocolVersion >= Protocol18Handler.MC_1_19_4_Version) switch (protocolVersion)
{ {
case >= Protocol18Handler.MC_1_20_Version:
switch (block.BlockId)
{
case 11022: //OakSlab
case 11028: //SpruceSlab
case 11034: //BirchSlab
case 11040: //JungleSlab
case 11046: //AcaciaSlab
case 11058: //DarkOakSlab
case 11064: //MangroveSlab
case 18528: //CrimsonSlab
case 18534: //WarpedSlab
case 11082: //StoneSlab
case 11112: //CobblestoneSlab
case 13966: //MossyCobblestoneSlab
case 11088: //SmoothStoneSlab
case 11124: //StoneBrickSlab
case 13954: //MossyStoneBrickSlab
case 13990: //GraniteSlab
case 13942: //PolishedGraniteSlab
case 14014: //DioriteSlab
case 13960: //PolishedDioriteSlab
case 13996: //AndesiteSlab
case 14008: //PolishedAndesiteSlab
case 22534: //CobbledDeepslateSlab
case 22945: //PolishedDeepslateSlab
case 23767: //DeepslateBrickSlab
case 23356: //DeepslateTileSlab
case 11118: //BrickSlab
case 11130: //MudBrickSlab
case 11094: //SandstoneSlab
case 13978: //SmoothSandstoneSlab
case 11100: //CutSandstoneSlab
case 11148: //RedSandstoneSlab
case 13948: //SmoothRedSandstoneSlab
case 11154: //CutRedSandstoneSlab
case 10566: //PrismarineSlab
case 10572: //PrismarineBrickSlab
case 10578: //DarkPrismarineSlab
case 11136: //NetherBrickSlab
case 14002: //RedNetherBrickSlab
case 19725: //BlackstoneSlab
case 20226: //PolishedBlackstoneSlab
case 19735: //PolishedBlackstoneBrickSlab
case 13972: //EndStoneBrickSlab
case 11160: //PurpurSlab
case 11142: //QuartzSlab
case 13984: //SmoothQuartzSlab
case 21912: //CutCopperSlab
case 21906: //ExposedCutCopperSlab
case 21900: //WeatheredCutCopperSlab
case 21894: //OxidizedCutCopperSlab
case 22264: //WaxedCutCopperSlab
case 22258: //WaxedExposedCutCopperSlab
case 22252: //WaxedWeatheredCutCopperSlab
return true;
}
break;
case Protocol18Handler.MC_1_19_4_Version:
switch (block.BlockId) switch (block.BlockId)
{ {
case 11018: // OakSlab case 11018: // OakSlab
@ -65,9 +125,9 @@ public static class BlockExtension
case 21844: // WaxedOxidizedCutCopperSlab case 21844: // WaxedOxidizedCutCopperSlab
return true; return true;
} }
}
else if (protocolVersion == Protocol18Handler.MC_1_19_3_Version) break;
{ case Protocol18Handler.MC_1_19_3_Version:
switch (block.BlockId) switch (block.BlockId)
{ {
case 10686: // OakSlab case 10686: // OakSlab
@ -125,9 +185,9 @@ public static class BlockExtension
case 21375: // WaxedOxidizedCutCopperSlab case 21375: // WaxedOxidizedCutCopperSlab
return true; return true;
} }
}
else if (protocolVersion >= Protocol18Handler.MC_1_19_Version) break;
{ case >= Protocol18Handler.MC_1_19_Version:
switch (block.BlockId) switch (block.BlockId)
{ {
case 19257: // CutCopperSlab case 19257: // CutCopperSlab
@ -186,9 +246,9 @@ public static class BlockExtension
case 17464: // PolishedBlackstoneBrickSlab case 17464: // PolishedBlackstoneBrickSlab
return true; return true;
} }
}
else if (protocolVersion >= Protocol18Handler.MC_1_17_Version) break;
{ case >= Protocol18Handler.MC_1_17_Version:
switch (block.BlockId) switch (block.BlockId)
{ {
case 18163: // CutCopperSlab case 18163: // CutCopperSlab
@ -245,9 +305,9 @@ public static class BlockExtension
case 16509: // PolishedBlackstoneBrickSlab case 16509: // PolishedBlackstoneBrickSlab
return true; return true;
} }
}
else if (protocolVersion >= Protocol18Handler.MC_1_16_Version) break;
{ case >= Protocol18Handler.MC_1_16_Version:
switch (block.BlockId) switch (block.BlockId)
{ {
case 8305: // OakSlab case 8305: // OakSlab
@ -292,9 +352,9 @@ public static class BlockExtension
case 16263: // PolishedBlackstoneBrickSlab case 16263: // PolishedBlackstoneBrickSlab
return true; return true;
} }
}
else if (protocolVersion >= Protocol18Handler.MC_1_15_Version) break;
{ case >= Protocol18Handler.MC_1_15_Version:
switch (block.BlockId) switch (block.BlockId)
{ {
case 7765: // OakSlab case 7765: // OakSlab
@ -333,9 +393,9 @@ public static class BlockExtension
case 10326: // DioriteSlab case 10326: // DioriteSlab
return true; return true;
} }
}
else if (protocolVersion >= Protocol18Handler.MC_1_14_Version) break;
{ case >= Protocol18Handler.MC_1_14_Version:
switch (block.BlockId) switch (block.BlockId)
{ {
case 7765: // OakSlab case 7765: // OakSlab
@ -375,6 +435,8 @@ public static class BlockExtension
case 10326: // DioriteSlab case 10326: // DioriteSlab
return true; return true;
} }
break;
} }
return false; return false;

View file

@ -24,6 +24,7 @@ using MinecraftClient.Protocol.ProfileKey;
using MinecraftClient.Protocol.Session; using MinecraftClient.Protocol.Session;
using MinecraftClient.Proxy; using MinecraftClient.Proxy;
using MinecraftClient.Scripting; using MinecraftClient.Scripting;
using Newtonsoft.Json;
using static MinecraftClient.Settings; using static MinecraftClient.Settings;
namespace MinecraftClient.Protocol.Handlers namespace MinecraftClient.Protocol.Handlers
@ -2348,6 +2349,17 @@ namespace MinecraftClient.Protocol.Handlers
// TODO: Use // TODO: Use
break; break;
// Temporarily disabled until I find a fix
/*case PacketTypesIn.BlockEntityData:
var location_ = dataTypes.ReadNextLocation(packetData);
var type_ = dataTypes.ReadNextInt(packetData);
var nbt = dataTypes.ReadNextNbt(packetData);
var nbtJson = JsonConvert.SerializeObject(nbt["messages"]);
//log.Info($"BLOCK ENTITY DATA -> {location_.ToString()} [{type_}] -> NBT: {nbtJson}");
break;*/
default: default:
return false; //Ignored packet return false; //Ignored packet
} }

View file

@ -1,4 +1,4 @@
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// <auto-generated> // <auto-generated>
// This code was generated by a tool. // This code was generated by a tool.
// //
@ -3956,5 +3956,17 @@ namespace MinecraftClient {
return ResourceManager.GetString("cmd.nameitem.desc", resourceCulture); return ResourceManager.GetString("cmd.nameitem.desc", resourceCulture);
} }
} }
internal static string bot_antiafk_may_not_move {
get {
return ResourceManager.GetString("bot.antiafk.may.not.move", resourceCulture);
}
}
internal static string bot_common_movement_lock_held {
get {
return ResourceManager.GetString("bot.common.movement.lock.held", resourceCulture);
}
}
} }
} }

View file

@ -2109,4 +2109,10 @@ Logging in...</value>
<data name="cmd.nameitem.desc" xml:space="preserve"> <data name="cmd.nameitem.desc" xml:space="preserve">
<value>Set an item name when an Anvil inventory is active and the item is in the first slot.</value> <value>Set an item name when an Anvil inventory is active and the item is in the first slot.</value>
</data> </data>
<data name="bot.antiafk.may.not.move" xml:space="preserve">
<value>Bot movement lock is held by bot {0}, so the Anti AFK bot might not move!</value>
</data>
<data name="bot.common.movement.lock.held" xml:space="preserve">
<value>You can not start/run/use the '{0}' bot because it requires movement, the movement is currently utilized by the '{1}' bot, stop it if you want to use this one.</value>
</data>
</root> </root>

View file

@ -0,0 +1,35 @@
namespace MinecraftClient.Scripting;
public class BotMovementLock
{
private static BotMovementLock? InstancePrivate;
private string _heldBy = string.Empty;
private BotMovementLock()
{
InstancePrivate = this;
}
public static BotMovementLock? Instance => InstancePrivate ??= new BotMovementLock();
public bool Lock(string owner)
{
if (owner.Trim().Length == 0 || _heldBy.Length > 0)
return false;
_heldBy = owner.Trim();
return true;
}
public bool UnLock(string owner)
{
if (owner.Trim().Length == 0 || _heldBy.Length == 0 || !_heldBy.ToLower().Equals(owner.ToLower().Trim()))
return false;
_heldBy = string.Empty;
return true;
}
public bool IsLocked => _heldBy.Length > 0;
public string LockedBy => _heldBy;
}