60 lines
1.9 KiB
Python
60 lines
1.9 KiB
Python
import unittest
|
|
|
|
from munch import Munch
|
|
from minecraft.networking.modern import ClientTickEnd
|
|
from minecraft.networking.packets import PacketBuffer
|
|
|
|
from mosfet.game import ContainerClickDropPacket, Game
|
|
from mosfet.protocol.types import Slot
|
|
|
|
|
|
class FakeConnection:
|
|
def __init__(self):
|
|
self.packets = []
|
|
|
|
def write_packet(self, packet, force=False):
|
|
self.packets.append((packet, force))
|
|
|
|
|
|
class TestInventoryDrain(unittest.TestCase):
|
|
def test_drop_packet_encodes_throw_whole_stack(self):
|
|
packet = ContainerClickDropPacket(
|
|
container_id=0, state_id=7, slot=45)
|
|
payload = PacketBuffer()
|
|
packet.write_fields(payload)
|
|
|
|
self.assertEqual(packet.id, 0x12)
|
|
self.assertEqual(payload.get_writable(), bytes.fromhex(
|
|
'0007002d01040000'))
|
|
|
|
def test_drain_waits_for_authoritative_slot_update(self):
|
|
connection = FakeConnection()
|
|
game = Game.__new__(Game)
|
|
game.g = Munch(
|
|
inv={36: Slot(True, 62, 3)},
|
|
inventory_state_ids={0: 5},
|
|
connection=connection,
|
|
draining=False,
|
|
)
|
|
|
|
self.assertEqual(game.start_drain(), 1)
|
|
game._tick_drain()
|
|
self.assertTrue(game.g.draining)
|
|
self.assertEqual(game.g.drain_waiting['slot'], 36)
|
|
self.assertIsInstance(connection.packets[0][0],
|
|
ContainerClickDropPacket)
|
|
self.assertEqual(connection.packets[0][0].state_id, 5)
|
|
self.assertIsInstance(connection.packets[1][0], ClientTickEnd)
|
|
|
|
# InventoryManager updates the state id and slot from server packets.
|
|
game.g.inventory_state_ids[0] = 6
|
|
game.g.inv[36] = Slot(False)
|
|
game._tick_drain()
|
|
self.assertFalse(game.g.draining)
|
|
self.assertEqual(game.g.drain_dropped, 1)
|
|
self.assertIsNone(game.g.drain_error)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|