Skip to content

Commit

Permalink
0.0.33
Browse files Browse the repository at this point in the history
  • Loading branch information
SilkePilon committed Nov 20, 2023
1 parent 9abac05 commit dfe12a4
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 65 deletions.
75 changes: 55 additions & 20 deletions lodestone/bot.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from javascript import require, On, Once
from javascript import require, On, Once, off, once
from javascript.proxy import Proxy
from rich.console import Console
from tinydb import TinyDB, Query
Expand Down Expand Up @@ -357,6 +357,7 @@ def __init__(
pass # needs plugin

self.mineflayer = require('mineflayer')
self.once_with_cleanup = require('mineflayer').promise_utils
if not self.disable_viewer:
self.mineflayer_viewer = require('prismarine-viewer').mineflayer
self.python_command = self.__check_python_command()
Expand Down Expand Up @@ -433,7 +434,7 @@ def __wait_for_msa(self):
@self.once('login')
def await_login(*args):
logger.info("Logged in successfully!")
self.__load_plugins()


def __msa(self, *msa):
with self.console.status("[bold]Waiting for login...\n") as login_status:
Expand Down Expand Up @@ -521,6 +522,7 @@ def __start(self):
self.log(
f'Coordinates: {int(self.bot.entity.position.x)}, {int(self.bot.entity.position.y)}, {int(self.bot.entity.position.z)}',
info=True)
self.__load_plugins()

def on(self, event: str):
"""
Expand All @@ -535,6 +537,21 @@ def chat(_, message, *args):
def inner(function):
On(self.proxy, event)(function)
return inner

def off(self, event: str, function):
"""
Decorator for event unregistering
```python
@bot.off('messagestr')
def chat(_, message, *args):
...
```
"""
print(function)
off(emitter=self.proxy, event=f"{event}", handler=function)
return


def once(self, event: str):
"""
Expand Down Expand Up @@ -1053,25 +1070,43 @@ def goto(self, x: int, z: int, y: int = 0, timeout: int = 600000000):
self.bot.pathfinder.goto(self.goals.GoalNear(int(x), int(y), int(z), 1), timeout=timeout)
return

def goto_entity(self, entity: str, timeout: int = 600000000):
"""
Go to the specified entity.
Args:
entity (str): The name of the entity to go to.
timeout (int, optional): The timeout in milliseconds. Defaults to 600000000.
Returns:
None
Examples:
To move to the nearest chicken with a timeout of 10 seconds:
>>> bot.goto_entity("chicken", timeout=10000)
"""
# Get the correct block type
with self.console.status(f"[bold]Moving to {entity}...") as status:
self.bot.bot.pathfinder.goto(self.bot.bot.pathfinder.goals.GoalNear(entity, 1), timeout=timeout)
def placeBlockWithOptions(self, referenceBlock, faceVector, options):
dest = referenceBlock.position.plus(faceVector)
oldBlock = self.bot.blockAt(dest)
self.bot._genericPlace(referenceBlock, faceVector, options)
newBlock = self.bot.blockAt(dest)
if oldBlock.type == newBlock.type:
@On(self.bot.world, f"blockUpdate:{dest}")
def block_update(oldBlock, newBlock):
print(newBlock)
print(oldBlock)
if not oldBlock or not newBlock or oldBlock.type != newBlock.type:
return
else:
raise Exception(f"No block has been placed: the block is still {oldBlock.name}")
if not oldBlock and not newBlock:
return
if oldBlock and oldBlock.type == newBlock.type:
raise Exception(f"No block has been placed: the block is still {oldBlock.name}")
else:
self.emit("blockPlaced", oldBlock, newBlock)

def placeBlock(self, referenceBlock, faceVector, no_checks=False):
if no_checks:
try:
dest = referenceBlock.position.plus(faceVector)
# self.bot.lookAt(referenceBlock)
oldBlock = self.bot.blockAt(dest)
self.bot._genericPlace(referenceBlock, faceVector, { "swingArm": "right" })
# self.bot.waitForTicks(ticks)

newBlock = self.bot.blockAt(dest)
self.emit("blockPlaced", oldBlock, newBlock)
return True
except:
return False
else:
self.placeBlockWithOptions(referenceBlock, faceVector, { "swingArm": "right" })



Expand Down
113 changes: 70 additions & 43 deletions lodestone/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import math
import threading
import json
import math
from types import SimpleNamespace
import asyncio
from math import sqrt
Expand Down Expand Up @@ -610,6 +611,7 @@ def __init__(self, bot: lodestone.Bot):
plugins_loaded = list(bot.loaded_plugins.keys())
plugins_loaded.append(self.__class__.__name__) # add the plugin to the list
bot.emit('plugin_loaded', *plugins_loaded)
self.building_up = False
self.bot.build_cactus:callable = self.start

def __equip_item(self, name):
Expand Down Expand Up @@ -645,48 +647,72 @@ def __dig_layer(self):


def __build_up(self):
print(self.bot.bot.physics)
# self.bot.bot.physics.gravity = 0.06

# self.bot.pathfinder.goto(self.bot.goals.GoalBlock(self.bot.entity.position))
self.__equip_item('dirt')

def getBlock(*args):
blockBelow = self.bot.bot.blockAt(self.bot.bot.entity.position.offset(0, -1, 0))
if blockBelow and blockBelow.name == "air":
self.bot.bot.setControlState("jump", False)
self.bot.bot.placeBlock(blockBelow, self.Vec3(0, 0.8, 0))
self.bot.bot.removeListener("move", getBlock)


self.bot.bot.setControlState("jump", True)
self.bot.bot.on("move", getBlock)



# referenceBlock = self.bot.bot.blockAt(self.bot.entity.position.offset(0, -1, 0))
# jumpY = math.floor(self.bot.entity.position.y) + 1.0
# self.bot.set_control_state('jump', True)
# @self.bot.once('move')
# def place_if_high_enough(*arg):
# print("trying")
# jumpY = math.floor(self.bot.entity.position.y) + 1.1
# self.bot.bot.setControlState('jump', True)
# def placeIfHighEnough(*args):
# nonlocal tryCount
# if self.bot.entity.position.y > jumpY:
# if self.bot.bot.entity.position.y > jumpY:
# try:
# self.bot.bot.placeBlock(referenceBlock, self.Vec3(0, 1, 0))
# self.bot.set_control_state('jump', False)
# #bot.chat('Placing a block was successful')
# self.bot.bot.setControlState('jump', False)
# self.bot.bot.removeListener('move', placeIfHighEnough)
# self.bot.chat('Placing a block was successful')
# except Exception as err:
# tryCount += 1
# if tryCount > 10:
# self.bot.chat(err.message)
# print(err)
# self.bot.bot.setControlState('jump', False)
# self.bot.bot.removeListener('move', placeIfHighEnough)
# self.bot.bot.waitForTicks(50)
# self.bot.bot.on('move', placeIfHighEnough)
# tryCount = 0



# placeIfHighEnough()




# self.bot.bot.waitForTicks(50)
# self.allow_build = True
# self.__equip_item('dirt')
# self.bot.set_control_state('jump', True)
# @self.bot.on('move')
# def placeIfHighEnough(*args):
# if self.allow_build:
# print(self.bot.entity.velocity.y)
# if self.bot.entity.velocity.y > -1:
# referenceBlock = self.bot.bot.blockAt(self.bot.entity.position.offset(0, -1, 0))
# if self.bot.bot.placeBlock(referenceBlock, self.Vec3(0, -1, 0)):
# self.bot.set_control_state('jump', False)
# self.allow_build = False

# tryCount = 0

self.bot.set_control_state("jump", True)
# Wait until the bot is high enough
while True:
positionBelow = self.bot.entity.position.offset(0, -0.5, 0)
blockBelow = self.bot.bot.blockAt(positionBelow)
if blockBelow.name == "air":
break
self.bot.bot.waitForTicks(1)
# Place a block
sourcePosition = self.bot.entity.position.offset(0, -1.5, 0)
sourceBlock = self.bot.bot.blockAt(sourcePosition)
faceVector = {"x": 0, "y": 1, "z": 0}
while True:
try:
self.bot.bot.placeBlock(sourceBlock, faceVector)
break
except:
continue

# Stop jump
self.bot.set_control_state("jump", False)






Expand Down Expand Up @@ -775,43 +801,44 @@ def __place_last_cactus(self):
self.bot.bot.dig(self.bot.bot.blockAt(self.bot.entity.position.offset(0, -2, 0)), True)
self.bot.bot.dig(self.bot.bot.blockAt(self.bot.entity.position.offset(0, -3, 0)), True)
self.bot.bot.dig(self.bot.bot.blockAt(self.bot.entity.position.offset(0, -4, 0)), True)
time.sleep(1)
self.bot.bot.waitForTicks(50)
self.__equip_item('sand')

self.bot.bot.placeBlock(self.bot.bot.blockAt(self.bot.entity.position.offset(0, -5, 0)), self.Vec3(0, 1, 0))
self.__equip_item('cactus')
time.sleep(1)
self.bot.bot.waitForTicks(50)
self.bot.bot.placeBlock(self.bot.bot.blockAt(self.bot.entity.position.offset(0, -4, 0)), self.Vec3(0, 1, 0))

def __cactus(self, layers):
with Progress(console=self.console) as progress:
task = progress.add_task(description="Building cactus farm...", total=9 * layers)
task = progress.add_task(description="Building cactus farm...", total=8 * layers)
for layer in range(0, layers):
self.__build_layer()
progress.update(task, advance=1)
# time.sleep(2)
self.bot.bot.waitForTicks(50)
self.__build_up()
progress.update(task, advance=1)
# time.sleep(2)
self.bot.bot.waitForTicks(50)
self.__build_up()
progress.update(task, advance=1)
# time.sleep(2)
self.bot.bot.waitForTicks(50)
self.__build_fence_dirt()
progress.update(task, advance=1)
# time.sleep(2)
self.bot.bot.waitForTicks(50)
self.__build_up()
progress.update(task, advance=1)
# time.sleep(2)
self.bot.bot.waitForTicks(50)
self.__place_dirt_layer()
progress.update(task, advance=1)
# time.sleep(2)
self.bot.bot.waitForTicks(50)
self.__build_up()
progress.update(task, advance=1)
# time.sleep(2)
self.bot.bot.waitForTicks(50)
self.__dig_layer()
progress.update(task, advance=1)
# time.sleep(2)
self.bot.bot.waitForTicks(50)
self.__place_last_cactus()
# time.sleep(2)
self.bot.bot.waitForTicks(50)
progress.update(task, advance=1)

progress.stop_task(task)
Expand Down
3 changes: 1 addition & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "lodestone"
version = "0.0.32"
version = "0.0.33"
description = "🤖 Create Minecraft bots with a powerful, stable, and high level Python API."
authors = [
{ name = "Silke Pilon", email = "[email protected]" },
Expand All @@ -20,7 +20,6 @@ dependencies = [
"uvicorn",
"tinydb",
"fuzzyfinder",
"g4f",
"requests",
"py-cord",
"python-dotenv",
Expand Down

0 comments on commit dfe12a4

Please sign in to comment.