Confirm crop seed selection before planting

This commit is contained in:
Tanner committed 2026-11-10 00:52:26 +00:00
1 parent 1a7bdb2be4
commit 46b0657bcf
4 files changed
+105 -21

No files matched your search

+3 -2
View File
@@ -1283,7 +1283,8 @@ class Game(LegacyGame):
self.breaking_state = None self.breaking_state = None
self.next_swing_at = 0.0 self.next_swing_at = 0.0
def select_item(self, item_ids): def select_item(self, item_ids, force=False):
"""Select an item, optionally resending the hotbar slot authoritatively."""
item_ids = set(item_ids) item_ids = set(item_ids)
matches = [(slot, item) for slot, item in self.g.inv.items() matches = [(slot, item) for slot, item in self.g.inv.items()
if item.present and item.item_id in item_ids] if item.present and item.item_id in item_ids]
@@ -1317,7 +1318,7 @@ class Game(LegacyGame):
else: else:
return False return False
if self.g.get('holding_slot') != hotbar_slot: if force or self.g.get('holding_slot') != hotbar_slot:
self.g.connection.write_packet(SetCarriedItemPacket( self.g.connection.write_packet(SetCarriedItemPacket(
slot=hotbar_slot), force=True) slot=hotbar_slot), force=True)
self.g.holding_slot = hotbar_slot self.g.holding_slot = hotbar_slot
+54 -13
View File
@@ -372,6 +372,7 @@ class GatherCropStates:
self.crop_profile['seed_started_at'] = now self.crop_profile['seed_started_at'] = now
self.seed_deadline = now + 8.0 self.seed_deadline = now + 8.0
self.seed_wait_logged = False self.seed_wait_logged = False
self.seed_hand_wait_logged = False
self.state = self.select_seed self.state = self.select_seed
elif time.time() > self.break_deadline: elif time.time() > self.break_deadline:
print('Crop break timed out at {}: expected state {}, server still has {}'.format( print('Crop break timed out at {}: expected state {}, server still has {}'.format(
@@ -380,6 +381,20 @@ class GatherCropStates:
self.bad_crops.append(self.crop) self.bad_crops.append(self.crop)
self.state = self.find_new_crop self.state = self.find_new_crop
def _held_item_snapshot(self):
hand = int(self.g.get('holding_hand', 0) or 0)
slot = (45 if hand == 1 else
36 + int(self.g.get('holding_slot', 0) or 0))
item = (self.g.get('inv', {}) or {}).get(slot)
item_id = (item.item_id if item and
getattr(item, 'present', False) else None)
return hand, slot, item_id
def _seed_is_in_selected_hand(self):
hand, _slot, slot_item_id = self._held_item_snapshot()
return (self.g.get('holding') == self.target_seed and
slot_item_id == self.target_seed and hand in (0, 1))
def select_seed(self): def select_seed(self):
self._finish_crop_travel() self._finish_crop_travel()
crop_seeds = { crop_seeds = {
@@ -397,15 +412,13 @@ class GatherCropStates:
self.state = self.find_new_crop self.state = self.find_new_crop
return return
if self.target_seed == self.g.get('holding'): hand, slot, slot_item_id = self._held_item_snapshot()
if self.crop_profile: print('Selecting crop seed: target={}, local held={}, hand={}, '
self.crop_profile['seed'] = ( 'slot={}, slot item={}'.format(
time.monotonic() - self.target_seed, self.g.get('holding'), hand, slot,
self.crop_profile.pop('seed_started_at', time.monotonic())) slot_item_id))
self.seed_deadline = None if self.g.game.select_item([self.target_seed], force=True):
self.state = self.place_crop print('Crop seed selection sent; awaiting matching held-slot state')
elif self.g.game.select_item([self.target_seed]):
self.seed_deadline = None
self.state = self.wait_select self.state = self.wait_select
else: else:
if (self.seed_deadline is not None and if (self.seed_deadline is not None and
@@ -422,8 +435,24 @@ class GatherCropStates:
def wait_select(self): def wait_select(self):
self._finish_crop_travel() self._finish_crop_travel()
if self.target_seed != self.g.holding: if not self._seed_is_in_selected_hand():
if not getattr(self, 'seed_hand_wait_logged', False):
hand, slot, slot_item_id = self._held_item_snapshot()
print('Waiting for seed in selected hand: target={}, held={}, '
'hand={}, slot={}, slot item={}'.format(
self.target_seed, self.g.get('holding'), hand,
slot, slot_item_id))
self.seed_hand_wait_logged = True
if (self.seed_deadline is not None and
time.monotonic() > self.seed_deadline):
print('Seed did not reach the selected hand; skipping',
self.crop)
self.bad_crops.append(self.crop)
self.seed_deadline = None
self.state = self.find_new_crop
return return
self.seed_deadline = None
self.seed_hand_wait_logged = False
if self.crop_profile: if self.crop_profile:
self.crop_profile['seed'] = ( self.crop_profile['seed'] = (
time.monotonic() - time.monotonic() -
@@ -432,6 +461,13 @@ class GatherCropStates:
def place_crop(self): def place_crop(self):
self._finish_crop_travel() self._finish_crop_travel()
if not self._seed_is_in_selected_hand():
print('Seed is no longer in the selected hand; reselecting',
self.target_seed)
self.seed_deadline = time.monotonic() + 8.0
self.seed_hand_wait_logged = False
self.state = self.select_seed
return
position = self.g.get('pos') position = self.g.get('pos')
if position is not None and utils.pint(position) != self.crop: if position is not None and utils.pint(position) != self.crop:
return return
@@ -443,12 +479,16 @@ class GatherCropStates:
# Keep the existing path moving until the support face is # Keep the existing path moving until the support face is
# within reach and visible. # within reach and visible.
return return
hand, slot, slot_item_id = self._held_item_snapshot()
if self.crop_profile: if self.crop_profile:
self.crop_profile['plant_started_at'] = time.monotonic() self.crop_profile['plant_started_at'] = time.monotonic()
self.place_deadline = time.time() + 3.0 self.place_deadline = time.time() + 3.0
self.g.game.place_block( print('Sending crop placement: crop={}, held={}, hand={}, '
p, BlockFace.TOP, hand=self.g.get('holding_hand', 0)) 'slot={}, slot item={}, position={}'.format(
print('Placed crop') self.crop, self.g.get('holding'), hand, slot,
slot_item_id, tuple(position) if position is not None else None))
self.g.game.place_block(p, BlockFace.TOP, hand=hand)
print('Crop use sent; awaiting replant confirmation')
self.state = self.wait_place self.state = self.wait_place
def wait_place(self): def wait_place(self):
@@ -506,6 +546,7 @@ class GatherCropStates:
self.target_seed = None self.target_seed = None
self.seed_deadline = None self.seed_deadline = None
self.seed_wait_logged = False self.seed_wait_logged = False
self.seed_hand_wait_logged = False
self.active_section_anchor = None self.active_section_anchor = None
self.active_section_maturity = (0, 0) self.active_section_maturity = (0, 0)
self.active_section_positions = None self.active_section_positions = None
+28 -5
View File
@@ -1,4 +1,5 @@
import io import io
import time
import unittest import unittest
from contextlib import redirect_stdout from contextlib import redirect_stdout
from unittest.mock import patch from unittest.mock import patch
@@ -7,6 +8,7 @@ from munch import Munch
from mosfet.info import blocks, items from mosfet.info import blocks, items
from mosfet.jobs.gather_crop import GatherCropStates from mosfet.jobs.gather_crop import GatherCropStates
from mosfet.protocol.types import Slot
class CropBatchTests(unittest.TestCase): class CropBatchTests(unittest.TestCase):
@@ -116,32 +118,52 @@ class CropBatchTests(unittest.TestCase):
self.assertEqual(game.break_calls, []) self.assertEqual(game.break_calls, [])
self.assertIs(state.state.__func__, state.find_new_crop.__func__) self.assertIs(state.state.__func__, state.find_new_crop.__func__)
def test_already_held_seed_skips_selection_and_places_from_held_hand(self): def test_seed_selection_forces_slot_and_checks_inventory_hand(self):
position = (1, 64, 1) position = (1, 64, 1)
seed = items.WHEAT_SEEDS_ID seed = items.WHEAT_SEEDS_ID
class FakeGame: class FakeGame:
def __init__(self): def __init__(self):
self.placements = [] self.placements = []
self.selections = []
def select_item(self, _item_ids): def select_item(self, item_ids, force=False):
raise AssertionError('already-held seed should not be selected') self.selections.append((set(item_ids), force))
return True
def place_block(self, location, face, hand=0): def place_block(self, location, face, hand=0):
self.placements.append((location, face, hand)) self.placements.append((location, face, hand))
game = FakeGame() game = FakeGame()
state = GatherCropStates(Munch( state = GatherCropStates(Munch(
game=game, holding=seed, holding_hand=1)) game=game, holding=seed, holding_hand=1,
inv={45: Slot(True, seed, 1)}))
state.crop = position state.crop = position
state.type_id = blocks.MCD_BLOCKS['wheat']['minStateId'] state.type_id = blocks.MCD_BLOCKS['wheat']['minStateId']
state.select_seed() state.select_seed()
self.assertEqual(game.selections, [({seed}, True)])
self.assertIs(state.state.__func__, state.wait_select.__func__)
state.wait_select()
self.assertIs(state.state.__func__, state.place_crop.__func__) self.assertIs(state.state.__func__, state.place_crop.__func__)
state.place_crop() state.place_crop()
self.assertEqual(game.placements, [((1, 63, 1), 1, 1)]) self.assertEqual(game.placements, [((1, 63, 1), 1, 1)])
def test_wait_select_rejects_stale_local_held_item(self):
seed = items.WHEAT_SEEDS_ID
other_item = next(iter(items.AXE_IDS))
state = GatherCropStates(Munch(
holding=seed, holding_hand=0, holding_slot=0,
inv={36: Slot(True, other_item, 1)}))
state.target_seed = seed
state.seed_deadline = time.monotonic() + 5.0
state.state = state.wait_select
state.wait_select()
self.assertIs(state.state.__func__, state.wait_select.__func__)
def test_seed_selection_uses_crop_species_for_any_age_state(self): def test_seed_selection_uses_crop_species_for_any_age_state(self):
unripe_wheat = blocks.MCD_BLOCKS['wheat']['minStateId'] unripe_wheat = blocks.MCD_BLOCKS['wheat']['minStateId']
@@ -149,8 +171,9 @@ class CropBatchTests(unittest.TestCase):
def __init__(self): def __init__(self):
self.selected = None self.selected = None
def select_item(self, item_ids): def select_item(self, item_ids, force=False):
self.selected = set(item_ids) self.selected = set(item_ids)
self.force = force
return True return True
game = FakeGame() game = FakeGame()
+20 -1
View File
@@ -1,7 +1,7 @@
import unittest import unittest
from munch import Munch from munch import Munch
from minecraft.networking.modern import ClientTickEnd from minecraft.networking.modern import ClientTickEnd, SetCarriedItemPacket
from minecraft.networking.packets import PacketBuffer from minecraft.networking.packets import PacketBuffer
from mosfet.game import Game from mosfet.game import Game
@@ -46,6 +46,25 @@ class TestEatFood(unittest.TestCase):
self.assertEqual(buffer.get_writable(), bytes.fromhex( self.assertEqual(buffer.get_writable(), bytes.fromhex(
'01ac0242b40000c1f00000')) '01ac0242b40000c1f00000'))
def test_force_select_item_resends_current_hotbar_slot(self):
connection = FakeConnection()
game = Game.__new__(Game)
seed = items.WHEAT_SEEDS_ID
game.g = Munch(
connection=connection,
inv={36: Slot(True, seed, 1)},
holding=seed,
holding_slot=0,
holding_hand=0,
inventory_state_ids={},
)
self.assertTrue(game.select_item([seed], force=True))
self.assertIsInstance(connection.packets[0][0], SetCarriedItemPacket)
self.assertEqual(connection.packets[0][0].slot, 0)
self.assertIsInstance(connection.packets[1][0], ClientTickEnd)
def test_game_use_item_sends_packet_and_tick_end(self): def test_game_use_item_sends_packet_and_tick_end(self):
connection = FakeConnection() connection = FakeConnection()
game = Game.__new__(Game) game = Game.__new__(Game)