Wait for landing before block breaking

This commit is contained in:
Tanner committed 2026-11-16 23:45:14 +00:00
1 parent 3e8b95c895
commit 5a3b299aa9
5 files changed
+281 -85

No files matched your search

+82 -36
View File
@@ -681,8 +681,8 @@ class LegacyGame:
# Active game facade. The constructor registers no legacy packet listeners;
# migrated actions use the 26.3 packet classes imported above.
class Game(LegacyGame):
ARM_SWING_INTERVAL = 0.1
BREAK_FINISH_GRACE = 0.2
ARM_SWING_INTERVAL = 0.35
PENDING_GROUND_TIMEOUT = 1.0
DOOR_AUTO_OPEN_GRACE = 0.25
DOOR_TURN_DURATION = 0.3
@@ -691,6 +691,7 @@ class Game(LegacyGame):
self.sequence = 0
self.break_deadline = None
self.breaking_state = None
self.break_pending_since = None
self.next_swing_at = 0.0
self.door_navigation = None
self.g.yaw_target_override = None
@@ -1012,43 +1013,68 @@ class Game(LegacyGame):
block_id = self.g.chunks.get_block_at(*location)
if block_id is None or block_id == blocks.AIR:
return False
if self.g.get('breaking'):
return tuple(self.g.breaking) == location
if not self.g.get('on_ground', True):
now = time.monotonic()
self.g.breaking = location
self.breaking_state = block_id
self.break_pending_since = now
self.break_deadline = None
return True
return self._start_block_break(location, block_id)
def _start_block_break(self, location, block_id):
holding = self.g.get('holding', 0)
held_slot = (45 if self.g.get('holding_hand', 0) == 1 else
36 + int(self.g.get('holding_slot', 0) or 0))
held_stack = self.g.get('inv', {}).get(held_slot)
enchantments = (getattr(held_stack, 'components', {}) or {}).get(
enchantments = dict((getattr(held_stack, 'components', {}) or {}).get(
'enchantments', {}) or {})
helmet = self.g.get('inv', {}).get(5)
helmet_enchantments = (getattr(helmet, 'components', {}) or {}).get(
'enchantments', {})
if isinstance(helmet_enchantments, dict):
enchantments.update(helmet_enchantments)
in_water = False
on_ground = True
on_ground = self.g.get('on_ground', True)
position = self.g.get('pos')
chunks = self.g.get('chunks')
if position is not None and chunks is not None:
x, y, z = (floor(position.x), floor(position.y),
floor(position.z))
support = chunks.get_block_at(x, floor(position.y - 0.2), z)
if support is not None:
on_ground = support not in blocks.NON_SOLID_IDS
feet = chunks.get_block_at(x, y, z)
eyes = chunks.get_block_at(x, floor(position.y + 1.62), z)
in_water = feet in blocks.WATER_IDS or eyes in blocks.WATER_IDS
# Mineflayer applies the water penalty only when the eyes are
# submerged, and aqua affinity on the helmet cancels that penalty.
in_water = eyes in blocks.WATER_IDS
if 'on_ground' not in self.g:
support = chunks.get_block_at(
x, floor(position.y - 0.2), z)
if support is not None:
on_ground = support not in blocks.NON_SOLID_IDS
game_mode = self.g.get('game_mode')
duration = utils.break_time(
block_id, holding, in_water=in_water, on_ground=on_ground,
enchantments=enchantments, effects=self.g.get('effects', {}))
enchantments=enchantments, effects=self.g.get('effects', {}),
creative=(game_mode == 1 or game_mode == 'creative'))
if not isfinite(duration):
self.g.breaking = None
self.breaking_state = None
self.break_pending_since = None
return False
sequence = self._next_sequence()
packet = PlayerActionPacket(
action=0, location=location, face=1,
sequence=self._next_sequence())
sequence=sequence)
self._send_action(packet)
self.g.breaking = location
self.breaking_state = block_id
now = time.monotonic()
# A server tick and packet delay can otherwise make the client finish
# just before the server has accumulated the full mining progress.
self.break_deadline = now + duration + self.BREAK_FINISH_GRACE
self.break_pending_since = None
self.break_deadline = now + duration
self.animate()
self.next_swing_at = now + self.ARM_SWING_INTERVAL
return True
@@ -1251,39 +1277,59 @@ class Game(LegacyGame):
self._tick_dump()
if self.g.get('draining'):
self._tick_drain()
if not self.g.get('breaking') or self.break_deadline is None:
if not self.g.get('breaking'):
return
now = time.monotonic()
if now < self.break_deadline:
if now >= self.next_swing_at:
self.animate()
self.next_swing_at = now + self.ARM_SWING_INTERVAL
return
packet = PlayerActionPacket(
action=3, location=self.g.breaking, face=1,
sequence=self._next_sequence())
self._send_action(packet)
location = self.g.breaking
if self.g.chunks.get_block_at(*location) == self.breaking_state:
# Predict the normal client-side block removal. The server's later
# block update is authoritative and will correct the cache if the
# action was denied or the block changed differently.
current_block = self.g.chunks.get_block_at(*location)
if current_block != self.breaking_state:
self.g.breaking = None
self.break_deadline = None
self.breaking_state = None
self.break_pending_since = None
self.next_swing_at = 0.0
return
if self.break_deadline is None:
pending_since = getattr(self, 'break_pending_since', None)
if pending_since is None:
pending_since = now
self.break_pending_since = now
if (not self.g.get('on_ground', True) and
now - pending_since < self.PENDING_GROUND_TIMEOUT):
return
self._start_block_break(location, current_block)
return
if now >= self.break_deadline:
sequence = self._next_sequence()
packet = PlayerActionPacket(
action=3, location=location, face=1,
sequence=sequence)
self._send_action(packet)
# Match Mineflayer's local prediction. The server's block update
# remains authoritative and will restore the cache if rejected.
self.g.chunks.set_block_at(*location, blocks.AIR)
self.g.breaking = None
self.break_deadline = None
self.breaking_state = None
self.next_swing_at = 0.0
self.g.breaking = None
self.break_deadline = None
self.breaking_state = None
self.break_pending_since = None
self.next_swing_at = 0.0
return
if now >= self.next_swing_at:
self.animate()
self.next_swing_at = now + self.ARM_SWING_INTERVAL
def cancel_break(self):
if not self.g.get('breaking'):
return
packet = PlayerActionPacket(
action=2, location=self.g.breaking, face=1,
sequence=self._next_sequence())
self._send_action(packet)
if self.break_deadline is not None:
packet = PlayerActionPacket(
action=2, location=self.g.breaking, face=1,
sequence=self._next_sequence())
self._send_action(packet)
self.g.breaking = None
self.break_deadline = None
self.breaking_state = None
self.break_pending_since = None
self.next_swing_at = 0.0
def select_item(self, item_ids, force=False):