minecraft-bot/jobs/plant_tree.py

117 lines
3.0 KiB
Python
Raw Normal View History

2021-04-19 01:34:54 +00:00
import re
import time
import importlib
import random
from itertools import count
from math import hypot, floor
from minecraft.networking.types import BlockFace
from protocol.managers import ChunkNotLoadedException
import utils
importlib.reload(utils)
import path
importlib.reload(path)
import blocks
importlib.reload(blocks)
import items
importlib.reload(items)
import mcdata
importlib.reload(mcdata)
import mobs
importlib.reload(mobs)
class PlantTreeStates:
def idle(self):
return None
def init(self):
if self.g.chopped_tree:
self.state = self.check_feet
else:
print('Aborting planting, did not plant')
self.state = self.cleanup
def check_feet(self):
p = utils.pint(self.g.pos)
# check for air at feet
if self.g.chunks.get_block_at(*p) in [0]:
self.state = self.select_sapling
else:
print('Aborting planting, feet not air')
self.state = self.cleanup
def select_sapling(self):
p = utils.pint(self.g.pos)
sapling_type = self.g.chopped_tree + '_sapling'
sapling_item = items.get_id(sapling_type)
if self.g.game.select_item([sapling_item]):
self.g.look_at = utils.padd(p, path.BLOCK_BELOW)
self.state = self.wait_select
self.wait_time = 1
else:
print('Aborting planting, no', sapling_type)
self.state = self.cleanup
def wait_select(self):
# wait a bit to look down
if self.wait_time > 0:
self.wait_time -= utils.TICK
else:
self.state = self.place_sapling
def place_sapling(self):
p = utils.pint(self.g.pos)
self.g.game.place_block(p, BlockFace.TOP)
print('Placed sapling')
self.state = self.wait_place
self.wait_time = 1
def wait_place(self):
# wait a bit for chunk data to update
if self.wait_time > 0:
self.wait_time -= utils.TICK
else:
self.state = self.find_open_spot
def find_open_spot(self):
w = self.g.world
p = utils.pint(self.g.pos)
for opening in w.find_tree_openings(p)[::-1]:
print('trying sapling opening', opening)
navpath = w.path_to_place(p, opening)
if navpath:
self.g.path = navpath
self.area = opening
self.state = self.going_to_area
return
else: # for
print('cant escape sapling')
self.state = self.cleanup
def going_to_area(self):
if utils.pint(self.g.pos) == self.area:
self.state = self.cleanup
def cleanup(self):
self.g.look_at = None
self.state = self.done
def done(self):
# never gets ran, placeholder
return None
def __init__(self, global_state):
self.g = global_state
self.state = self.idle
self.wait_time = 0
self.area = None
def run(self):
self.state()