diff --git a/README.md b/README.md index 3060383..21c248b 100644 --- a/README.md +++ b/README.md @@ -77,7 +77,7 @@ The exception are the below public commands, they can optionally be prefixed wit and all bots will run the command. The current 26.3 command path handles public chat and direct messages, replying -in the same channel. Verified commands are `!ping`, `!pos`, `!loaded`, +in the same channel. Verified commands are `!ping`, `!pos`, `!dist`, `!loaded`, `!info x y z`, `!inv` (basic slot/count data), `1cache`, `1drain`, `1eat`, `1respawn`, `1sleep`, `1loiter`, addressed path movement such as `1goto x y z`, and `1farm crops`. The command catalog below is retained from @@ -101,6 +101,8 @@ These can be ran by anyone, all bots will reply. `!pos` - replies with position and dimension +`!dist` - replies with your distance from the bot + `!afk` - goes AFK with /afk `!unafk` - goes not AFK with /afk diff --git a/mosfet/commands.py b/mosfet/commands.py index f26dd41..de42a23 100644 --- a/mosfet/commands.py +++ b/mosfet/commands.py @@ -116,6 +116,27 @@ class Commands: if command == 'pos': reply = str(utils.pint(self.g.pos))[1:-1] + ', ' + self.g.dimension + ## !dist - replies with the distance to the requesting player + if command == 'dist': + sender_ids = {str(sender), str(reply_target)} + player_names = self.g.get('player_names') or {} + for alias in tuple(sender_ids): + linked_id = player_names.get(alias) + if linked_id is None: + linked_id = player_names.get(alias.lower()) + if linked_id is not None: + sender_ids.add(str(linked_id)) + player = next((candidate + for candidate in (self.g.get('players') or {}).values() + if str(candidate.get('player_uuid', '')) in sender_ids), + None) + if player is None or self.g.get('pos') is None: + reply = 'cannot find your position; you may be out of range' + else: + player_position = (player.x, player.y, player.z) + distance = utils.phyp(self.g.pos, player_position) + reply = 'you are {:.1f} blocks away'.format(distance) + ## !health - replies with health, hunger, and saturation if command == 'health': if not self.g.get('health_update_received'): diff --git a/tests/test_modern_chat.py b/tests/test_modern_chat.py index 9ae2874..ddd45a0 100644 --- a/tests/test_modern_chat.py +++ b/tests/test_modern_chat.py @@ -208,6 +208,21 @@ class TestModernChat(unittest.TestCase): ('DM', 'tanner', 'tanner -> me: !ping', 'tanner'), ]) + def test_public_distance_command_uses_requesting_player_position(self): + player_uuid = 'f6126022-2c68-37f7-be93-aaa551374663' + chat = FakeChat() + command = Commands.__new__(Commands) + command.g = Munch( + name='mosfet1', pos=(0, 64, 0), chat=chat, + players={12: Munch( + player_uuid=player_uuid, x=3.0, y=68.0, z=0.0)}, + player_names={player_uuid: 'tanner'}, + ) + + command.handle_chat(('CHAT', player_uuid, 'tanner: !dist')) + + self.assertEqual(chat.messages, ['you are 5.0 blocks away']) + def test_dm_command_replies_to_named_sender(self): chat = FakeChat() command = Commands.__new__(Commands)