Collect and equip armor supplies
This commit is contained in:
1 parent
c97e836380
commit
9c0db4e4b0
4 files changed
+111
-6
No files matched your search
@@ -1502,6 +1502,19 @@ class Game(LegacyGame):
|
|||||||
self.g.connection.write_packet(ClientTickEnd(), force=True)
|
self.g.connection.write_packet(ClientTickEnd(), force=True)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
def quick_move_inventory_slot(self, slot):
|
||||||
|
"""Shift-click a player inventory slot with the inventory window open."""
|
||||||
|
if self.g.get('window') or not 9 <= slot <= 44:
|
||||||
|
return False
|
||||||
|
packet = ContainerClickQuickMovePacket(
|
||||||
|
container_id=0,
|
||||||
|
state_id=self.g.get('inventory_state_ids', {}).get(0, 0),
|
||||||
|
slot=slot,
|
||||||
|
)
|
||||||
|
self.g.connection.write_packet(packet, force=True)
|
||||||
|
self.g.connection.write_packet(ClientTickEnd(), force=True)
|
||||||
|
return True
|
||||||
|
|
||||||
def swap_container_slot_with_hotbar(self, container_slot, inventory_slot):
|
def swap_container_slot_with_hotbar(self, container_slot, inventory_slot):
|
||||||
window = self.g.get('window')
|
window = self.g.get('window')
|
||||||
if not window or not 36 <= inventory_slot <= 44:
|
if not window or not 36 <= inventory_slot <= 44:
|
||||||
|
|||||||
+19
-1
@@ -50,6 +50,24 @@ SWORD_IDS = {
|
|||||||
item['protocol_id'] for name, item in ITEMS.items()
|
item['protocol_id'] for name, item in ITEMS.items()
|
||||||
if name.endswith('_sword')
|
if name.endswith('_sword')
|
||||||
}
|
}
|
||||||
|
ARMOR_SLOT_CATEGORIES = {
|
||||||
|
5: 'head_armor',
|
||||||
|
6: 'chest_armor',
|
||||||
|
7: 'leg_armor',
|
||||||
|
8: 'foot_armor',
|
||||||
|
}
|
||||||
|
ARMOR_SLOT_IDS = {
|
||||||
|
slot: {
|
||||||
|
item['protocol_id'] for item in ITEMS.values()
|
||||||
|
if category in item.get('enchantCategories', ())
|
||||||
|
}
|
||||||
|
for slot, category in ARMOR_SLOT_CATEGORIES.items()
|
||||||
|
}
|
||||||
|
ARMOR_SLOT_BY_ITEM = {
|
||||||
|
item_id: slot for slot, item_ids in ARMOR_SLOT_IDS.items()
|
||||||
|
for item_id in item_ids
|
||||||
|
}
|
||||||
|
ARMOR_IDS = set(ARMOR_SLOT_BY_ITEM)
|
||||||
SAPLING_IDS = get_set(SAPLINGS)
|
SAPLING_IDS = get_set(SAPLINGS)
|
||||||
LOG_IDS = get_set(LOGS)
|
LOG_IDS = get_set(LOGS)
|
||||||
_LOG_FAMILY_ITEM_IDS = {}
|
_LOG_FAMILY_ITEM_IDS = {}
|
||||||
@@ -124,7 +142,7 @@ def storage_item_ids_for_label(label):
|
|||||||
return set(_LABEL_ITEM_IDS.get(normalized, ()))
|
return set(_LABEL_ITEM_IDS.get(normalized, ()))
|
||||||
|
|
||||||
|
|
||||||
INIT_NEEDED_ITEMS = BED_IDS | FOOD_IDS
|
INIT_NEEDED_ITEMS = BED_IDS | FOOD_IDS | SWORD_IDS | ARMOR_IDS
|
||||||
INIT_NEEDED_ITEMS.add(CHEST_ID)
|
INIT_NEEDED_ITEMS.add(CHEST_ID)
|
||||||
NEEDED_ITEMS = INIT_NEEDED_ITEMS
|
NEEDED_ITEMS = INIT_NEEDED_ITEMS
|
||||||
INIT_WANTED_ITEMS = set()
|
INIT_WANTED_ITEMS = set()
|
||||||
|
|||||||
@@ -21,6 +21,9 @@ class GrabSuppliesStates(WorkBoundaryReturnMixin):
|
|||||||
tuple(items.BED_IDS): (1, 1),
|
tuple(items.BED_IDS): (1, 1),
|
||||||
(items.CHEST_ID,): (1, 1),
|
(items.CHEST_ID,): (1, 1),
|
||||||
tuple(items.FOOD_IDS): (1, 1),
|
tuple(items.FOOD_IDS): (1, 1),
|
||||||
|
tuple(items.SWORD_IDS): (1, 1),
|
||||||
|
**{tuple(item_ids): (1, 1)
|
||||||
|
for item_ids in items.ARMOR_SLOT_IDS.values()},
|
||||||
}
|
}
|
||||||
|
|
||||||
def default_supplies(self):
|
def default_supplies(self):
|
||||||
@@ -64,11 +67,69 @@ class GrabSuppliesStates(WorkBoundaryReturnMixin):
|
|||||||
self.ground_entity_id = None
|
self.ground_entity_id = None
|
||||||
self.ground_position = None
|
self.ground_position = None
|
||||||
if self.supplies:
|
if self.supplies:
|
||||||
self.state = self.check_supplies
|
self._begin_armor_equip(self.check_supplies)
|
||||||
else:
|
else:
|
||||||
print('Aborting getting supplies, none specified')
|
print('Aborting getting supplies, none specified')
|
||||||
self.state = self.cleanup
|
self.state = self.cleanup
|
||||||
|
|
||||||
|
def _begin_armor_equip(self, next_state):
|
||||||
|
self.armor_equip_next_state = next_state
|
||||||
|
self.pending_armor_equip = None
|
||||||
|
self.failed_armor_equip_slots = set()
|
||||||
|
self.state = self.equip_available_armor
|
||||||
|
|
||||||
|
def equip_available_armor(self):
|
||||||
|
if self.g.get('window'):
|
||||||
|
return
|
||||||
|
|
||||||
|
pending = self.pending_armor_equip
|
||||||
|
if pending:
|
||||||
|
source_slot, armor_slot, item_id, deadline = pending
|
||||||
|
worn = self.g.inv.get(armor_slot)
|
||||||
|
if worn and worn.present and worn.item_id == item_id:
|
||||||
|
print('Equipped', self._item_label(item_id),
|
||||||
|
'in armor slot', armor_slot)
|
||||||
|
self.pending_armor_equip = None
|
||||||
|
elif time.monotonic() < deadline:
|
||||||
|
return
|
||||||
|
else:
|
||||||
|
print('Armor equip was not confirmed for',
|
||||||
|
self._item_label(item_id))
|
||||||
|
self.failed_armor_equip_slots.add(source_slot)
|
||||||
|
self.pending_armor_equip = None
|
||||||
|
self.g.item_lock = False
|
||||||
|
|
||||||
|
if self.g.get('item_lock'):
|
||||||
|
return
|
||||||
|
|
||||||
|
requested_armor_ids = {
|
||||||
|
item_id for group in self.supplies for item_id in group
|
||||||
|
if item_id in items.ARMOR_SLOT_BY_ITEM
|
||||||
|
}
|
||||||
|
for source_slot in range(9, 45):
|
||||||
|
stack = self.g.inv.get(source_slot)
|
||||||
|
if (not stack or not stack.present or
|
||||||
|
stack.item_id not in requested_armor_ids or
|
||||||
|
source_slot in self.failed_armor_equip_slots):
|
||||||
|
continue
|
||||||
|
|
||||||
|
armor_slot = items.ARMOR_SLOT_BY_ITEM[stack.item_id]
|
||||||
|
worn = self.g.inv.get(armor_slot)
|
||||||
|
if worn and worn.present:
|
||||||
|
continue
|
||||||
|
|
||||||
|
print('Equipping', self._stack_label(stack))
|
||||||
|
self.g.item_lock = True
|
||||||
|
if self.g.game.quick_move_inventory_slot(source_slot):
|
||||||
|
self.pending_armor_equip = (
|
||||||
|
source_slot, armor_slot, stack.item_id,
|
||||||
|
time.monotonic() + 2.0)
|
||||||
|
return
|
||||||
|
self.g.item_lock = False
|
||||||
|
self.failed_armor_equip_slots.add(source_slot)
|
||||||
|
|
||||||
|
self.state = self.armor_equip_next_state
|
||||||
|
|
||||||
def _ground_bed_reserved_by_nearby_bot(self, obj):
|
def _ground_bed_reserved_by_nearby_bot(self, obj):
|
||||||
if obj.item_id not in items.BED_IDS:
|
if obj.item_id not in items.BED_IDS:
|
||||||
return False
|
return False
|
||||||
@@ -199,6 +260,7 @@ class GrabSuppliesStates(WorkBoundaryReturnMixin):
|
|||||||
self.g.path = []
|
self.g.path = []
|
||||||
self.ground_entity_id = None
|
self.ground_entity_id = None
|
||||||
self.ground_position = None
|
self.ground_position = None
|
||||||
|
self._begin_armor_equip(self.check_supplies)
|
||||||
|
|
||||||
def check_supplies(self):
|
def check_supplies(self):
|
||||||
# Search every configured item group on the ground before looking in
|
# Search every configured item group on the ground before looking in
|
||||||
@@ -296,7 +358,6 @@ class GrabSuppliesStates(WorkBoundaryReturnMixin):
|
|||||||
if have_enough:
|
if have_enough:
|
||||||
print('Picked up required ground supplies')
|
print('Picked up required ground supplies')
|
||||||
self._record_ground_pickup()
|
self._record_ground_pickup()
|
||||||
self.state = self.check_supplies
|
|
||||||
elif (obj is None or
|
elif (obj is None or
|
||||||
utils.pint(self.g.pos) == self.ground_position or
|
utils.pint(self.g.pos) == self.ground_position or
|
||||||
utils.phyp(self.g.pos, (obj.x, obj.y, obj.z)) <= 1.25):
|
utils.phyp(self.g.pos, (obj.x, obj.y, obj.z)) <= 1.25):
|
||||||
@@ -319,7 +380,6 @@ class GrabSuppliesStates(WorkBoundaryReturnMixin):
|
|||||||
print('Picked up required ground supplies after freeing inventory space')
|
print('Picked up required ground supplies after freeing inventory space')
|
||||||
self._record_ground_pickup()
|
self._record_ground_pickup()
|
||||||
self.pending_drop_slot = None
|
self.pending_drop_slot = None
|
||||||
self.state = self.check_supplies
|
|
||||||
return
|
return
|
||||||
|
|
||||||
if self.g.get('drop_waiting'):
|
if self.g.get('drop_waiting'):
|
||||||
@@ -354,7 +414,6 @@ class GrabSuppliesStates(WorkBoundaryReturnMixin):
|
|||||||
self.ground_required_count):
|
self.ground_required_count):
|
||||||
print('Picked up required ground supplies')
|
print('Picked up required ground supplies')
|
||||||
self._record_ground_pickup()
|
self._record_ground_pickup()
|
||||||
self.state = self.check_supplies
|
|
||||||
return
|
return
|
||||||
|
|
||||||
if self.wait_time > 0:
|
if self.wait_time > 0:
|
||||||
@@ -635,7 +694,7 @@ class GrabSuppliesStates(WorkBoundaryReturnMixin):
|
|||||||
print('Closing supply chest')
|
print('Closing supply chest')
|
||||||
self.g.game.close_window()
|
self.g.game.close_window()
|
||||||
self.g.look_at = None
|
self.g.look_at = None
|
||||||
self.state = self.choose_supply_chest
|
self._begin_armor_equip(self.choose_supply_chest)
|
||||||
|
|
||||||
def cleanup(self):
|
def cleanup(self):
|
||||||
if self._maybe_return_to_work_boundary(self.cleanup):
|
if self._maybe_return_to_work_boundary(self.cleanup):
|
||||||
|
|||||||
@@ -137,6 +137,21 @@ class TestInventoryDrain(unittest.TestCase):
|
|||||||
self.assertEqual(payload.get_writable(), bytes.fromhex(
|
self.assertEqual(payload.get_writable(), bytes.fromhex(
|
||||||
'0307002a00010000'))
|
'0307002a00010000'))
|
||||||
|
|
||||||
|
def test_player_inventory_quick_move_uses_window_zero(self):
|
||||||
|
connection = FakeConnection()
|
||||||
|
game = Game.__new__(Game)
|
||||||
|
game.g = Munch(
|
||||||
|
connection=connection, window=None,
|
||||||
|
inventory_state_ids={0: 12},
|
||||||
|
)
|
||||||
|
|
||||||
|
self.assertTrue(game.quick_move_inventory_slot(10))
|
||||||
|
packet = connection.packets[0][0]
|
||||||
|
self.assertIsInstance(packet, ContainerClickQuickMovePacket)
|
||||||
|
self.assertEqual((packet.container_id, packet.state_id, packet.slot),
|
||||||
|
(0, 12, 10))
|
||||||
|
self.assertIsInstance(connection.packets[1][0], ClientTickEnd)
|
||||||
|
|
||||||
def test_open_window_packet_decodes_container_metadata(self):
|
def test_open_window_packet_decodes_container_metadata(self):
|
||||||
# window id, menu type, empty anonymous-NBT title
|
# window id, menu type, empty anonymous-NBT title
|
||||||
packet = ContainerOpenPacket()
|
packet = ContainerOpenPacket()
|
||||||
|
|||||||
Reference in new issue
Block a user