Improve labeled chest caching interactions
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
import unittest
|
||||
from munch import Munch
|
||||
|
||||
from minecraft.networking.types import BlockFace
|
||||
|
||||
from mosfet.info import items
|
||||
from mosfet.jobs.cache_items import CacheItemsStates
|
||||
|
||||
|
||||
class CacheItemsTests(unittest.TestCase):
|
||||
def test_successful_labeled_transfer_skips_placing_another_cache_chest(self):
|
||||
item_id = items.get_id('stone')
|
||||
stack = Munch(present=True, item_id=item_id, item_count=16)
|
||||
window = Munch(container_slots=27, contents={
|
||||
**dict.fromkeys(range(27)), 27: stack,
|
||||
})
|
||||
clicks = []
|
||||
|
||||
class FakeGame:
|
||||
@staticmethod
|
||||
def count_window_slots():
|
||||
return 0
|
||||
|
||||
@staticmethod
|
||||
def click_window(*args):
|
||||
clicks.append(args)
|
||||
|
||||
class FakeWorld:
|
||||
@staticmethod
|
||||
def find_blocks_indexed(*_args):
|
||||
return []
|
||||
|
||||
@staticmethod
|
||||
def find_labeled_chests(*_args):
|
||||
return []
|
||||
|
||||
g = Munch(
|
||||
game=FakeGame(), world=FakeWorld(), pos=(0, 64, 0),
|
||||
inv={}, window=window, item_lock=False)
|
||||
state = CacheItemsStates(g)
|
||||
state.is_labeled_container = True
|
||||
state.container_item_ids = {item_id}
|
||||
|
||||
state.move_items()
|
||||
self.assertEqual(len(clicks), 1)
|
||||
self.assertFalse(state.transferred_to_labeled_chest)
|
||||
|
||||
window.contents[27] = Munch(present=False, item_id=0, item_count=0)
|
||||
g.item_lock = False
|
||||
state.pending_transfer['confirm_after'] = 0
|
||||
state.move_items()
|
||||
self.assertTrue(state.transferred_to_labeled_chest)
|
||||
|
||||
state.labeled_chests = []
|
||||
state.trapped_chests = []
|
||||
state.choose_trapped_chest()
|
||||
self.assertIs(state.state.__func__, state.cleanup.__func__)
|
||||
|
||||
def test_cache_chest_placement_sneaks_to_avoid_opening_support_container(self):
|
||||
calls = []
|
||||
|
||||
class FakeGame:
|
||||
@staticmethod
|
||||
def place_block(*args, **kwargs):
|
||||
calls.append((args, kwargs))
|
||||
|
||||
state = CacheItemsStates(Munch(game=FakeGame()))
|
||||
state.area = (3, 65, 7)
|
||||
|
||||
state.place_chest()
|
||||
|
||||
self.assertEqual(calls, [(
|
||||
((3, 64, 7), BlockFace.TOP), {'sneaking': True},
|
||||
)])
|
||||
self.assertIs(state.state.__func__, state.open_chest.__func__)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
@@ -27,8 +27,8 @@ class ChestDetectionTests(unittest.TestCase):
|
||||
self.manager = ChestOpenManager(self.g)
|
||||
self.position = (12, 64, -7)
|
||||
|
||||
def event(self, block_id=2, action=1, viewers=1):
|
||||
x, y, z = self.position
|
||||
def event(self, block_id=2, action=1, viewers=1, position=None):
|
||||
x, y, z = position or self.position
|
||||
return Munch(
|
||||
location=Munch(x=x, y=y, z=z),
|
||||
block_id=block_id, action=action, parameter=viewers,
|
||||
@@ -54,12 +54,27 @@ class ChestDetectionTests(unittest.TestCase):
|
||||
self.manager.handle_block_event(self.event(viewers=1))
|
||||
|
||||
self.assertIsNone(self.g.last_chest_opened_by_other)
|
||||
self.assertIsNone(self.g.pending_chest_open)
|
||||
self.assertIsNotNone(self.g.pending_chest_open)
|
||||
|
||||
self.manager.handle_block_event(self.event(viewers=2))
|
||||
self.assertEqual(self.g.last_chest_opened_by_other['viewers'], 2)
|
||||
self.assertEqual(self.g.last_chest_opened_by_other['previous_viewers'], 1)
|
||||
|
||||
def test_bot_open_of_either_double_chest_half_is_not_misreported(self):
|
||||
other_half = (13, 64, -7)
|
||||
self.g.pending_chest_open = {
|
||||
'position': self.position,
|
||||
'positions': (self.position, other_half),
|
||||
'requested_at': time.monotonic(),
|
||||
}
|
||||
|
||||
self.manager.handle_block_event(
|
||||
self.event(position=other_half, viewers=1))
|
||||
|
||||
self.assertIsNone(self.g.last_chest_opened_by_other)
|
||||
self.assertIsNotNone(self.g.pending_chest_open)
|
||||
self.assertEqual(self.g.chest_viewers[other_half], 1)
|
||||
|
||||
def test_ignores_non_chests_and_tracks_close(self):
|
||||
self.manager.handle_block_event(self.event(block_id=1, viewers=1))
|
||||
self.assertIsNone(self.g.last_chest_opened_by_other)
|
||||
@@ -71,11 +86,13 @@ class ChestDetectionTests(unittest.TestCase):
|
||||
self.assertEqual(len(self.g.chest_open_events), 1)
|
||||
|
||||
def test_open_container_marks_bot_initiated_chest_open(self):
|
||||
chest_state = 5552
|
||||
sent = []
|
||||
game = Game.__new__(Game)
|
||||
game.g = Munch(chunks=Munch(
|
||||
get_block_at=lambda *_position: chest_state))
|
||||
other_half = (12, 64, -8)
|
||||
game.g = Munch(
|
||||
chunks=Munch(get_block_at=lambda *_position: None),
|
||||
world=Munch(_double_chest_positions=lambda _position:
|
||||
(self.position,)))
|
||||
game.place_block = lambda location, face: sent.append(
|
||||
(location, face))
|
||||
|
||||
@@ -83,6 +100,8 @@ class ChestDetectionTests(unittest.TestCase):
|
||||
|
||||
self.assertEqual(sent[0][0], self.position)
|
||||
self.assertEqual(game.g.pending_chest_open['position'], self.position)
|
||||
self.assertIn(self.position, game.g.pending_chest_open['positions'])
|
||||
self.assertIn(other_half, game.g.pending_chest_open['positions'])
|
||||
self.assertIsInstance(game.g.pending_chest_open['requested_at'], float)
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
import unittest
|
||||
from munch import Munch
|
||||
|
||||
from minecraft.networking.modern import ClientTickEnd, UseItemOnBlockPacket
|
||||
from mosfet.game import Game
|
||||
from mosfet.protocol.modern import PlayerInputPacket
|
||||
|
||||
|
||||
class FakeConnection:
|
||||
def __init__(self):
|
||||
self.packets = []
|
||||
|
||||
def write_packet(self, packet, force=False):
|
||||
self.packets.append((packet, force))
|
||||
|
||||
|
||||
class SneakPlacementTests(unittest.TestCase):
|
||||
def test_sneaking_wraps_block_placement_and_is_released(self):
|
||||
connection = FakeConnection()
|
||||
game = Game.__new__(Game)
|
||||
game.g = Munch(connection=connection)
|
||||
game.sequence = 0
|
||||
|
||||
game.place_block((3, 64, 7), 1, sneaking=True)
|
||||
|
||||
packets = [packet for packet, _force in connection.packets]
|
||||
self.assertEqual(len(packets), 6)
|
||||
self.assertIsInstance(packets[0], PlayerInputPacket)
|
||||
self.assertEqual(packets[0].flags, 0x20)
|
||||
self.assertIsInstance(packets[1], ClientTickEnd)
|
||||
self.assertIsInstance(packets[2], UseItemOnBlockPacket)
|
||||
self.assertEqual(packets[2].location, (3, 64, 7))
|
||||
self.assertIsInstance(packets[3], ClientTickEnd)
|
||||
self.assertIsInstance(packets[4], PlayerInputPacket)
|
||||
self.assertEqual(packets[4].flags, 0)
|
||||
self.assertIsInstance(packets[5], ClientTickEnd)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user