From 1013adcf544ea592abbdd90a22dd292a267fff50 Mon Sep 17 00:00:00 2001 From: Silke pilon Date: Fri, 17 Nov 2023 16:33:24 +0100 Subject: [PATCH] added collect_block --- lodestone/bot.py | 26 +++++++++++++--- lodestone/exceptions.py | 5 +++ lodestone/tasks.py | 67 +++++++++++++++++------------------------ pyproject.toml | 2 +- 4 files changed, 55 insertions(+), 45 deletions(-) diff --git a/lodestone/bot.py b/lodestone/bot.py index 5c930d3..242e029 100644 --- a/lodestone/bot.py +++ b/lodestone/bot.py @@ -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 @@ -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 diff --git a/lodestone/exceptions.py b/lodestone/exceptions.py index e69de29..fc9e1e8 100644 --- a/lodestone/exceptions.py +++ b/lodestone/exceptions.py @@ -0,0 +1,5 @@ +import lodestone + +class CustomException(Exception): + pass + diff --git a/lodestone/tasks.py b/lodestone/tasks.py index bf7e05b..c86ead3 100644 --- a/lodestone/tasks.py +++ b/lodestone/tasks.py @@ -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): @@ -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 @@ -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: @@ -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 @@ -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!") \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml index 48cffa6..7982476 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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 = "silkepilon2009@gmail.com" },