Skip to content

Commit

Permalink
added collect_block
Browse files Browse the repository at this point in the history
  • Loading branch information
SilkePilon committed Nov 17, 2023
1 parent e457203 commit 1013adc
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 45 deletions.
26 changes: 22 additions & 4 deletions lodestone/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -967,7 +967,7 @@ def collect_block(self, block:str, amount:int=1, max_distance:int=64):
with self.console.status(f"[bold]Collecting {block}...") as status:
blockType = self.bot.registry.blocksByName[block]
if not blockType:
self.log("No blocks with that name.")
self.log("No blocks with that name.", error=True)
status.stop()
return
# Try and find that block type in the world
Expand All @@ -978,19 +978,37 @@ def find_block():
# found_block = self.bot.collectBlock.findFromVein(found_block)
return found_block
except:
self.log(f"No {block} found nearby.")
self.log(f"No {block} found nearby.", error=True)
for i in range(0, amount):
if i == 0: i = 1
current_block = find_block()
if not current_block:
self.log(f"No {block} found nearby. Try increasing the `max_distance` pram")
self.log(f"No {block} found nearby. Try increasing the `max_distance` pram", warning=True)
return
# Collect the block if we found one
try:
self.bot.collectBlock.collect(current_block)
except:
self.log(f"No {block} found nearby.")
self.log(f"No {block} found nearby.", error=True)
status.update(f"[bold]Collecting {block}... ({i}/{amount})\n")

def goto(self, x:int, z:int, y:int=0, timeout:int=600000000):
"""
Go to x, y, z\n
`amount` does not consider the amount the block gives once broken.
\n
... # Code example\n
`bot.collect_block("oak_log", amount=20)`
"""
# Get the correct block type
with self.console.status(f"[bold]Moving to ({x}, {y}, {z})...") as status:
if y == 0:
self.bot.bot.pathfinder.goto(self.bot.bot.pathfinder.goals.GoalNearXZ(int(x), int(z), 1), timeout=timeout)
else:
self.bot.bot.pathfinder.goto(self.bot.bot.pathfinder.goals.GoalNear(int(x), int(y), int(z), 1), timeout=timeout)
while self.bot.bot.pathfinder.isMoving:
time.sleep(1)
return


createBot = Bot
5 changes: 5 additions & 0 deletions lodestone/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import lodestone

class CustomException(Exception):
pass

67 changes: 27 additions & 40 deletions lodestone/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,25 @@
class Idkwhattocallthis:
def __init__(self, bot: lodestone.bot):
self.bot = bot
self.create_tree()
self.create_priority_queues()
self.add_tasks()
self.start()
self.__create_tree()
self.__create_priority_queues()
self.__add_tasks()
self.__start()




class TreeTask:
def __init__(self, action, item_or_block, bot: lodestone.bot, craft_count=1):
self.action = action
self.bot = bot
self.craft_count = craft_count
class create_task:
def __init__(self, item_or_block, amount=1, obtain=None,craft=None):
self.obtain = obtain
self.craft = craft
self.bot = Idkwhattocallthis.bot
self.amount = amount
self.item_or_block = item_or_block
self.dependencies = []
self.completed = False

def add_dependency(self, task, craft_count=1):
task.craft_count = craft_count
print(task.craft_count)
def add_dependency(self, task):
self.dependencies.append(task)

def is_available(self):
Expand Down Expand Up @@ -64,23 +66,8 @@ def complete(self):
self.bot.bot.chat(f"unknown item: {self.item_or_block}")


if self.action == "break":
# Get the correct block type
blockType = self.bot.bot.registry.blocksByName[self.item_or_block]
if not blockType:
self.bot.bot.chat("I don't know any blocks with that name.")
return
self.bot.bot.chat('Collecting the nearest ' + blockType.name)
# Try and find that block type in the world
def find_block():
block = self.bot.bot.findBlock({ 'matching': blockType.id, 'maxDistance': 64})
return block
block = find_block()
if not block:
self.bot.bot.chat("I don't see that block nearby.")
return
# Collect the block if we found one
self.bot.bot.collectBlock.collect(block)
if self.obtain:
self.bot.collect_block(f"{self.item_or_block}", amount=amount, max_distance=100)
self.completed = True

# Priority queues
Expand All @@ -99,37 +86,37 @@ def empty(self):
return self.queue.empty()


def create_tree(self):
def __create_tree(self):
# Create sample tree

self.chop_wood = self.TreeTask(action="break", item_or_block="oak_log", bot=self.bot)
self.wood = self.create_task(obtain=True, item_or_block="oak_log", bot=self.bot, amount=1)

self.make_planks = self.TreeTask(action="craft", item_or_block="oak_planks", bot=self.bot)
self.make_planks = self.create_task(action="craft", item_or_block="oak_planks", bot=self.bot)
self.make_planks.add_dependency(self.chop_wood)

self.make_stick = self.TreeTask(action="craft", item_or_block="stick", bot=self.bot)
self.make_stick = self.create_task(action="craft", item_or_block="stick", bot=self.bot)
self.make_stick.add_dependency(self.make_planks)

self.make_pickaxe = self.TreeTask(action="craft", item_or_block="wooden_pickaxe", bot=self.bot)
self.make_pickaxe = self.create_task(action="craft", item_or_block="wooden_pickaxe", bot=self.bot)
self.make_pickaxe.add_dependency(self.make_planks)
self.make_pickaxe.add_dependency(self.make_stick)


def create_priority_queues(self):
def __create_priority_queues(self):
# Create priority queues

self.urgent_tasks = PriorityQueue()
self.normal_tasks = PriorityQueue()


def add_tasks(self):
def __add_tasks(self):
# Add tasks

# urgent_tasks.put(10, defend_task)
self.normal_tasks.put((5, self.make_pickaxe))


def process_tasks(self):
def __process_tasks(self):
if self.urgent_tasks.empty():
task = self.normal_tasks.get()
else:
Expand All @@ -139,7 +126,7 @@ def process_tasks(self):

# Gather function

def gather(self, task: TreeTask):
def __gather(self, task: create_task):
if task.is_available():
task.complete()
return
Expand All @@ -149,7 +136,7 @@ def gather(self, task: TreeTask):
self.gather(task)

# Main bot loop
def start(self):
def __start(self):
while not self.normal_tasks.empty() or not self.urgent_tasks.empty():
self.process_tasks()
self.__process_tasks()
print("DONE!")
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "lodestone"
version = "0.0.27"
version = "0.0.28"
description = "🤖 Create Minecraft bots with a powerful, stable, and high level Python API."
authors = [
{ name = "Silke Pilon", email = "[email protected]" },
Expand Down

0 comments on commit 1013adc

Please sign in to comment.