Files
minecraft-bot/tests/test_break_animation.py
T

100 lines
3.4 KiB
Python

import io
import time
import unittest
from unittest.mock import patch
from munch import Munch
from minecraft.networking.modern import (
ClientTickEnd, EntityAnimationPacket, EntitySwingAnimationPacket,
PlayerActionPacket, PunchPacket,
)
from mosfet.game import Game
class RecordingConnection:
def __init__(self):
self.packets = []
def write_packet(self, packet, force=False):
self.packets.append((packet, force))
class BlockBreakAnimationTests(unittest.TestCase):
def make_game(self):
connection = RecordingConnection()
game = object.__new__(Game)
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
game.next_swing_at = 0.0
return game, connection
def test_26_3_punch_packet_is_the_empty_serverbound_packet(self):
self.assertEqual(PunchPacket.id, 0x2E)
self.assertEqual(PunchPacket.definition, [])
def test_observer_packet_decodes_entity_id_and_swing_kind(self):
packet = EntityAnimationPacket()
packet.read(io.BytesIO(bytes((0x85, 0x01, 0))))
self.assertEqual(packet.entity_id, 133)
self.assertEqual(packet.animation, 0)
def test_26_3_observer_swing_packet_decodes_animation_details(self):
packet = EntitySwingAnimationPacket()
packet.read(io.BytesIO(bytes((0x85, 0x01, 0, 1, 6))))
self.assertEqual(EntitySwingAnimationPacket.id, 0x7B)
self.assertEqual((packet.entity_id, packet.hand,
packet.animation_type, packet.duration),
(133, 0, 1, 6))
def test_animate_sends_punch_before_client_tick_end(self):
game, connection = self.make_game()
game.animate()
self.assertEqual([type(packet) for packet, _ in connection.packets],
[PunchPacket, ClientTickEnd])
self.assertTrue(all(force for _, force in connection.packets))
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 = 101.0
game.breaking_state = 10
game.next_swing_at = 0
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.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__':
unittest.main()