Wait for landing before block breaking
This commit is contained in:
1 parent
3e8b95c895
commit
5a3b299aa9
5 files changed
+281
-85
No files matched your search
@@ -1,11 +1,12 @@
|
||||
import io
|
||||
import time
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
from munch import Munch
|
||||
|
||||
from minecraft.networking.modern import (
|
||||
ClientTickEnd, EntityAnimationPacket, EntitySwingAnimationPacket,
|
||||
PunchPacket,
|
||||
PlayerActionPacket, PunchPacket,
|
||||
)
|
||||
from mosfet.game import Game
|
||||
|
||||
@@ -22,7 +23,18 @@ class BlockBreakAnimationTests(unittest.TestCase):
|
||||
def make_game(self):
|
||||
connection = RecordingConnection()
|
||||
game = object.__new__(Game)
|
||||
game.g = Munch(connection=connection, inv={}, inventory_state_ids={})
|
||||
class FakeChunks:
|
||||
block = 10
|
||||
|
||||
def get_block_at(self, *_position):
|
||||
return self.block
|
||||
|
||||
def set_block_at(self, *_position):
|
||||
self.block = 0
|
||||
|
||||
game.g = Munch(
|
||||
connection=connection, inv={}, inventory_state_ids={},
|
||||
chunks=FakeChunks())
|
||||
game.sequence = 0
|
||||
game.break_deadline = None
|
||||
game.breaking_state = None
|
||||
@@ -54,24 +66,33 @@ class BlockBreakAnimationTests(unittest.TestCase):
|
||||
[PunchPacket, ClientTickEnd])
|
||||
self.assertTrue(all(force for _, force in connection.packets))
|
||||
|
||||
def test_breaking_sends_initial_and_periodic_punches(self):
|
||||
def test_breaking_sends_finish_and_predicts_block_air(self):
|
||||
game, connection = self.make_game()
|
||||
game._advance_world_time = lambda: None
|
||||
game._tick_single_drop = lambda: None
|
||||
game.g.breaking = (1, 2, 3)
|
||||
game.break_deadline = time.monotonic() + 5
|
||||
game.break_deadline = 101.0
|
||||
game.breaking_state = 10
|
||||
game.next_swing_at = 0
|
||||
|
||||
game.tick()
|
||||
self.assertEqual(sum(isinstance(packet, PunchPacket)
|
||||
for packet, _ in connection.packets), 1)
|
||||
sent = len(connection.packets)
|
||||
game.tick()
|
||||
self.assertEqual(len(connection.packets), sent)
|
||||
with patch('mosfet.game.time.monotonic', return_value=100.0):
|
||||
game.tick()
|
||||
self.assertEqual(sum(isinstance(packet, PunchPacket)
|
||||
for packet, _ in connection.packets), 1)
|
||||
sent = len(connection.packets)
|
||||
game.tick()
|
||||
self.assertEqual(len(connection.packets), sent)
|
||||
|
||||
game.next_swing_at = 0
|
||||
game.tick()
|
||||
self.assertEqual(sum(isinstance(packet, PunchPacket)
|
||||
for packet, _ in connection.packets), 2)
|
||||
game.break_deadline = 99.0
|
||||
with patch('mosfet.game.time.monotonic', return_value=100.0):
|
||||
game.tick()
|
||||
actions = [packet.action for packet, _ in connection.packets
|
||||
if isinstance(packet, PlayerActionPacket)]
|
||||
self.assertEqual(actions, [3])
|
||||
self.assertIsNone(game.g.breaking)
|
||||
sent = len(connection.packets)
|
||||
game.tick()
|
||||
self.assertEqual(len(connection.packets), sent)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
@@ -2,10 +2,12 @@ import math
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
from munch import Munch
|
||||
from minecraft.networking.modern import PlayerActionPacket
|
||||
|
||||
from mosfet import utils
|
||||
from mosfet.game import Game
|
||||
from mosfet.info import blocks, items
|
||||
from mosfet.vector import Point3D
|
||||
|
||||
|
||||
class BlockBreakTimingTests(unittest.TestCase):
|
||||
@@ -21,6 +23,17 @@ class BlockBreakTimingTests(unittest.TestCase):
|
||||
self.assertAlmostEqual(
|
||||
utils.break_time(oak_leaves, oak_log, on_ground=False), 1.5)
|
||||
|
||||
def test_dig_time_uses_eye_submersion_and_aqua_affinity(self):
|
||||
sand = blocks.MCD_BLOCKS['sand']['minStateId']
|
||||
wooden_shovel = items.get_id('wooden_shovel')
|
||||
|
||||
self.assertEqual(utils.break_time(sand, wooden_shovel), 0.4)
|
||||
self.assertAlmostEqual(
|
||||
utils.break_time(sand, wooden_shovel, in_water=True), 1.9)
|
||||
self.assertEqual(utils.break_time(
|
||||
sand, wooden_shovel, in_water=True,
|
||||
enchantments={'minecraft:aqua_affinity': 1}), 0.4)
|
||||
|
||||
def test_efficiency_haste_and_fatigue_affect_break_ticks(self):
|
||||
oak_leaves = blocks.MCD_BLOCKS['oak_leaves']['minStateId']
|
||||
wooden_hoe = items.get_id('wooden_hoe')
|
||||
@@ -36,7 +49,7 @@ class BlockBreakTimingTests(unittest.TestCase):
|
||||
bedrock = blocks.MCD_BLOCKS['bedrock']['minStateId']
|
||||
self.assertTrue(math.isinf(utils.break_time(bedrock)))
|
||||
|
||||
def test_game_sends_finish_after_calculated_tick_duration(self):
|
||||
def test_dig_request_waits_until_grounded_before_starting(self):
|
||||
class RecordingConnection:
|
||||
def __init__(self):
|
||||
self.packets = []
|
||||
@@ -45,9 +58,62 @@ class BlockBreakTimingTests(unittest.TestCase):
|
||||
self.packets.append(packet)
|
||||
|
||||
class FakeChunks:
|
||||
@staticmethod
|
||||
def get_block_at(*_position):
|
||||
return blocks.MCD_BLOCKS['oak_leaves']['minStateId']
|
||||
state = blocks.MCD_BLOCKS['sand']['minStateId']
|
||||
|
||||
def get_block_at(self, *_position):
|
||||
return self.state
|
||||
|
||||
def set_block_at(self, *_position):
|
||||
self.state = blocks.AIR
|
||||
|
||||
connection = RecordingConnection()
|
||||
game = object.__new__(Game)
|
||||
game.g = Munch(
|
||||
connection=connection, chunks=FakeChunks(), inv={},
|
||||
holding=items.get_id('wooden_shovel'), holding_hand=0,
|
||||
holding_slot=0, pos=Point3D((0.0, 64.0, 0.0)),
|
||||
on_ground=False, breaking=None, game_mode=0)
|
||||
game.sequence = 0
|
||||
game.break_deadline = None
|
||||
game.breaking_state = None
|
||||
game.break_pending_since = None
|
||||
game.next_swing_at = 0.0
|
||||
game._advance_world_time = lambda: None
|
||||
game._tick_single_drop = lambda: None
|
||||
|
||||
with patch('mosfet.game.time.monotonic', return_value=100.0):
|
||||
self.assertTrue(game.break_block((0, 63, 0)))
|
||||
self.assertEqual(connection.packets, [])
|
||||
with patch('mosfet.game.time.monotonic', return_value=100.5):
|
||||
game.tick()
|
||||
self.assertEqual(connection.packets, [])
|
||||
|
||||
game.g.on_ground = True
|
||||
with patch('mosfet.game.time.monotonic', return_value=100.6):
|
||||
game.tick()
|
||||
|
||||
self.assertEqual(sum(isinstance(packet, PlayerActionPacket)
|
||||
for packet in connection.packets), 1)
|
||||
self.assertEqual(connection.packets[0].action, 0)
|
||||
self.assertAlmostEqual(game.break_deadline, 101.0)
|
||||
|
||||
def test_game_finishes_at_dig_time_and_predicts_block_air(self):
|
||||
class RecordingConnection:
|
||||
def __init__(self):
|
||||
self.packets = []
|
||||
|
||||
def write_packet(self, packet, force=False):
|
||||
self.packets.append(packet)
|
||||
|
||||
class FakeChunks:
|
||||
def __init__(self):
|
||||
self.block = blocks.MCD_BLOCKS['oak_leaves']['minStateId']
|
||||
|
||||
def get_block_at(self, *_position):
|
||||
return self.block
|
||||
|
||||
def set_block_at(self, *_position):
|
||||
self.block = blocks.AIR
|
||||
|
||||
connection = RecordingConnection()
|
||||
game = object.__new__(Game)
|
||||
@@ -62,9 +128,19 @@ class BlockBreakTimingTests(unittest.TestCase):
|
||||
|
||||
with patch('mosfet.game.time.monotonic', return_value=100.0):
|
||||
self.assertTrue(game.break_block((0, 64, 0)))
|
||||
self.assertAlmostEqual(
|
||||
game.break_deadline,
|
||||
100.3 + game.BREAK_FINISH_GRACE)
|
||||
self.assertAlmostEqual(game.break_deadline, 100.3)
|
||||
|
||||
game._advance_world_time = lambda: None
|
||||
game._tick_single_drop = lambda: None
|
||||
with patch('mosfet.game.time.monotonic',
|
||||
return_value=game.break_deadline):
|
||||
game.tick()
|
||||
|
||||
actions = [packet.action for packet in connection.packets
|
||||
if isinstance(packet, PlayerActionPacket)]
|
||||
self.assertEqual(actions, [0, 3])
|
||||
self.assertIsNone(game.g.breaking)
|
||||
self.assertEqual(game.g.chunks.block, blocks.AIR)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
Reference in new issue
Block a user