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.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)
matches = [(slot, item) for slot, item in self.g.inv.items()
if item.present and item.item_id in item_ids]
@@ -1317,7 +1318,7 @@ class Game(LegacyGame):
else:
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(
slot=hotbar_slot), force=True)
self.g.holding_slot = hotbar_slot
+54 -13
View File
@@ -372,6 +372,7 @@ class GatherCropStates:
self.crop_profile['seed_started_at'] = now
self.seed_deadline = now + 8.0
self.seed_wait_logged = False
self.seed_hand_wait_logged = False
self.state = self.select_seed
elif time.time() > self.break_deadline:
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.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):
self._finish_crop_travel()
crop_seeds = {
@@ -397,15 +412,13 @@ class GatherCropStates:
self.state = self.find_new_crop
return
if self.target_seed == self.g.get('holding'):
if self.crop_profile:
self.crop_profile['seed'] = (
time.monotonic() -
self.crop_profile.pop('seed_started_at', time.monotonic()))
self.seed_deadline = None
self.state = self.place_crop
elif self.g.game.select_item([self.target_seed]):
self.seed_deadline = None
hand, slot, slot_item_id = self._held_item_snapshot()
print('Selecting crop seed: target={}, local held={}, hand={}, '
'slot={}, slot item={}'.format(
self.target_seed, self.g.get('holding'), hand, slot,
slot_item_id))
if self.g.game.select_item([self.target_seed], force=True):
print('Crop seed selection sent; awaiting matching held-slot state')
self.state = self.wait_select
else:
if (self.seed_deadline is not None and
@@ -422,8 +435,24 @@ class GatherCropStates:
def wait_select(self):
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
self.seed_deadline = None
self.seed_hand_wait_logged = False
if self.crop_profile:
self.crop_profile['seed'] = (
time.monotonic() -
@@ -432,6 +461,13 @@ class GatherCropStates:
def place_crop(self):
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')
if position is not None and utils.pint(position) != self.crop:
return
@@ -443,12 +479,16 @@ class GatherCropStates:
# Keep the existing path moving until the support face is
# within reach and visible.
return
hand, slot, slot_item_id = self._held_item_snapshot()
if self.crop_profile:
self.crop_profile['plant_started_at'] = time.monotonic()
self.place_deadline = time.time() + 3.0
self.g.game.place_block(
p, BlockFace.TOP, hand=self.g.get('holding_hand', 0))
print('Placed crop')
print('Sending crop placement: crop={}, held={}, hand={}, '
'slot={}, slot item={}, position={}'.format(
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
def wait_place(self):
@@ -506,6 +546,7 @@ class GatherCropStates:
self.target_seed = None
self.seed_deadline = None
self.seed_wait_logged = False
self.seed_hand_wait_logged = False
self.active_section_anchor = None
self.active_section_maturity = (0, 0)
self.active_section_positions = None
+28 -5
View File
@@ -1,4 +1,5 @@
import io
import time
import unittest
from contextlib import redirect_stdout
from unittest.mock import patch
@@ -7,6 +8,7 @@ from munch import Munch
from mosfet.info import blocks, items
from mosfet.jobs.gather_crop import GatherCropStates
from mosfet.protocol.types import Slot
class CropBatchTests(unittest.TestCase):
@@ -116,32 +118,52 @@ class CropBatchTests(unittest.TestCase):
self.assertEqual(game.break_calls, [])
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)
seed = items.WHEAT_SEEDS_ID
class FakeGame:
def __init__(self):
self.placements = []
self.selections = []
def select_item(self, _item_ids):
raise AssertionError('already-held seed should not be selected')
def select_item(self, item_ids, force=False):
self.selections.append((set(item_ids), force))
return True
def place_block(self, location, face, hand=0):
self.placements.append((location, face, hand))
game = FakeGame()
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.type_id = blocks.MCD_BLOCKS['wheat']['minStateId']
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__)
state.place_crop()
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):
unripe_wheat = blocks.MCD_BLOCKS['wheat']['minStateId']
@@ -149,8 +171,9 @@ class CropBatchTests(unittest.TestCase):
def __init__(self):
self.selected = None
def select_item(self, item_ids):
def select_item(self, item_ids, force=False):
self.selected = set(item_ids)
self.force = force
return True
game = FakeGame()
+20 -1
View File
@@ -1,7 +1,7 @@
import unittest
from munch import Munch
from minecraft.networking.modern import ClientTickEnd
from minecraft.networking.modern import ClientTickEnd, SetCarriedItemPacket
from minecraft.networking.packets import PacketBuffer
from mosfet.game import Game
@@ -46,6 +46,25 @@ class TestEatFood(unittest.TestCase):
self.assertEqual(buffer.get_writable(), bytes.fromhex(
'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):
connection = FakeConnection()
game = Game.__new__(Game)