LocationBlacklistCollection

Allows you to "blacklist" (set as invalid) locations for a certain amount of times. Create with 'LocationBlacklistCollection.CreatePerBot(...)' or 'LocationBlacklistCollection.CreateGlobal(...)'.

Example usage

The code below finds the closest diamond block and attempts to move to it and then mine it. If the block is unreachable then it's added to the blacklist for 1 hour. The blacklist ensures that the same block is no longer picked in the search with the code !blacklist.IsBlocked(context, block.GetLocation()).

Optionally the code below could also use LocationWhitelistCollection to make sure that multiple bots don't try to move to the same diamond ore block.

private static LocationBlacklistCollection blacklist = 
LocationBlacklistCollection.CreateGlobal(3, // 3 bots need to blacklist to blacklist globally.
                                         13600000, // 1 hour in milliseconds.
                                         2); // block a 2x2 radius around a block as well.

public async Task OnTick() {
    const ushort diamondOre = 56;
    var block = Context.World.FindClosest(128, 64, // search a 256x128 area.
    diamondOre, CpuMode.Medium_Usage, // search for diamond ore blocks.
    block => !blacklist.IsBlocked(context, block.GetLocation())); // do not include blocks that are blacklisted.

    if(block == null) {
        Console.WriteLine("Could not find any reachable diamond ores");
        return;
    }
    
    // Move to the closest diamond ore.
    var moveTask = await block.MoveToInteractionRange().Task;
    if(moveTask.Result != MoveResultType.Completed) {
        Console.WriteLine($"Could not reach ore at {block.GetLocation()}, blacklisting it.");
        blacklist.AddToBlockCounter(context, block.GetLocation());
        return;
    }
    
    // Reached the block, mine it.
    await block.Dig();
}

Last updated