Port sand gathering to 26.3

This commit is contained in:
Tanner committed 2026-11-16 22:55:06 +00:00
1 parent f28269f71c
commit 3e8b95c895
9 files changed
+424 -158

No files matched your search

+84 -52
View File
@@ -1,89 +1,121 @@
import re
"""Pick up dropped sand using 26.3 entity and navigation state."""
import time
import importlib
import random
from itertools import count
from math import floor
from minecraft.networking.types import BlockFace
from mosfet.protocol.managers import ChunkNotLoadedException
from mosfet import utils
from mosfet import path
from mosfet.info import blocks
from mosfet.info import items
from mosfet.info import mcdata
from mosfet.info import mobs
class GrabSandStates:
PICKUP_DISTANCE = 1.5
PICKUP_TIMEOUT = 5.0
def idle(self):
return None
def init(self):
self.eid_blacklist.clear()
self.state = self.find_sand
print('Looking for dropped sand')
def _object(self, entity_id):
obj = self.g.get('objects', {}).get(entity_id)
if obj is None or obj.get('item_id') != items.SAND_ID:
return None
return obj
def _skip_target(self):
if self.entity_id is not None:
self.eid_blacklist.add(self.entity_id)
self.g.path = []
self.g.look_at = None
self.state = self.find_sand
print('Trying to grab sand')
def find_sand(self):
w = self.g.world
p = utils.pint(self.g.pos)
world = self.g.world
position = utils.pint(self.g.pos)
candidates = [
(entity_id, obj) for entity_id, obj in
self.g.get('objects', {}).items()
if obj.get('item_id') == items.SAND_ID and
entity_id not in self.eid_blacklist
]
candidates.sort(key=lambda pair: utils.phyp(
position, (pair[1].x, pair[1].y, pair[1].z)))
sand = w.find_objects([items.SAND_ID])
for entity_id, obj in candidates:
item_position = (obj.x, obj.y, obj.z)
if utils.phyp(position, item_position) > 6:
continue
item_block = utils.pint(item_position)
support = world.block_at(*utils.padd(item_block, path.BLOCK_BELOW))
if support is None or support in blocks.NON_SOLID_IDS:
continue
if not sand:
print('No sand objects found, aborting')
self.state = self.cleanup
self.entity_id = entity_id
self.sand = item_block
if utils.phyp(tuple(self.g.pos), item_position) <= self.PICKUP_DISTANCE:
self.pickup_deadline = time.monotonic() + self.PICKUP_TIMEOUT
self.state = self.wait_for_pickup
return
navpath = world.path_to_place(position, item_block)
if not navpath:
self.eid_blacklist.add(entity_id)
continue
self.entity_id = entity_id
self.sand = item_block
self.g.path = navpath
self.g.look_at = item_block
print('Going to dropped sand', self.sand)
self.state = self.going_to_sand
return
sand.sort(key=lambda s: utils.phyp(p, (s.x, s.y, s.z)))
for s in sand:
s_pos = utils.pint((s.x, s.y, s.z))
check = utils.padd(s_pos, path.BLOCK_BELOW)
if utils.phyp(p, s_pos) > 6:
continue
# skip if the sand is floating
if self.g.chunks.get_block_at(*check) in {0}:
continue
if s.entity_id in self.eid_blacklist:
continue
self.eid_blacklist.append(s.entity_id)
navpath = w.path_to_place(p, s_pos)
if navpath:
self.g.path = navpath
self.state = self.going_to_sand
self.sand = s_pos
print('Going to sand', self.sand)
return
else:
print('Cant get to sand', self.sand)
time.sleep(0.1)
print('Cant get to any more sand, aborting')
print('No reachable dropped sand nearby')
self.state = self.cleanup
def going_to_sand(self):
if utils.pint(self.g.pos) == self.sand:
self.state = self.cleanup
obj = self._object(self.entity_id)
if obj is None:
self.g.path = []
self.state = self.find_sand
return
item_position = (obj.x, obj.y, obj.z)
distance = utils.phyp(tuple(self.g.pos), item_position)
if distance <= self.PICKUP_DISTANCE:
self.g.path = []
self.pickup_deadline = time.monotonic() + self.PICKUP_TIMEOUT
self.state = self.wait_for_pickup
elif not self.g.path:
self._skip_target()
def wait_for_pickup(self):
if self._object(self.entity_id) is None:
print('Picked up dropped sand')
self.g.look_at = None
self.state = self.find_sand
elif time.monotonic() > self.pickup_deadline:
print('Could not pick up dropped sand; skipping', self.sand)
self._skip_target()
def cleanup(self):
self.g.path = []
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.sand = None
self.eid_blacklist = []
self.entity_id = None
self.eid_blacklist = set()
self.pickup_deadline = None
def run(self):
self.state()