Skip to content

Dispensers example

adepierre edited this page Feb 10, 2023 · 2 revisions

Automatic Dispenser Farm

This page presents in details the Botcraft example project 6_DispenserFarmExample. If you'd like to run it yourself, a world download is available along the project files.

The main idea is quite simple: use villagers to buy redstone dust and bow and mine cobblestone to craft dispensers. The emeralds are coming from four different sources: a zombie spawner creating rotten flesh, a skeleton spawner creating bones (crafted into bone meal, crafted into white dye), some potato crops and some carrot crops. In addition to villager trading and cobblestone mining, a lot of other tasks are required to keep the script running indefinitely without issues: eating, sleeping, destroying unused items etc...

All the positions (crops, chest, bed, crafting table...) are gathered when the program is started, none are hardcoded. It means the same code could be applied to variants of this world. For example the bot searches an orange shulker box and will expect the rotten flesh to appear in it.

The goal of this example is not to be as efficient as possible (a TNT-based cobblestone generator coupled to a raid farm would be way much faster), but rather to show as many Botcraft functionalities as possible (trading, crafting, inventory management, some simple pathfinding, block breaking...).

dispensers

Main behaviour tree

The code of the bot is based on the Botcraft Behaviour tree system, if you don't know what it is, you can check the corresponding wiki page. The main tree can be found here. Let's see what is all this code about.

image

All the functions prefixed with Botcraft:: are the one provided by the library. All the others are behaviours coded specifically for this application (but they also call some generic Botcraft tasks, MineCobblestone uses the Dig function).

The root of the tree is a sequence, meaning that it will execute each child until either it reaches the end or one child fails, in which case it will start back from the begining. The first selector child is used to initialize the bot only the first time the tree is executed. Then, the tree is just a succession of subtrees or simple functions. From the start to the end:

  • a subtree to collect emeralds
  • a subtree to clean the output chests of the two mob spawners (i.e. delete unused items)
  • a call to SortInventory that will stack same items into one slot in the inventory
  • a leaf to destroy the poisonous potatoes against the cactus
  • a subtree to deal with mobs with frost walker boots
  • a subtree to sleep
  • a subtree to eat (and buy food if necessary)
  • two times one inventory check followed by an item buying tree (bow then redstone)
  • a subtree to collect cobblestone
  • a subtree to craft the dispenser, and store it in the output shulker box

This is obviously only one way of ordering tasks.

Clone this wiki locally