Add public distance chat command

This commit is contained in:
Tanner committed 2026-11-14 22:02:14 +00:00
1 parent 492238aa22
commit d62e745fa6
3 files changed
+39 -1

No files matched your search

+3 -1
View File
@@ -77,7 +77,7 @@ The exception are the below public commands, they can optionally be prefixed wit
and all bots will run the command. and all bots will run the command.
The current 26.3 command path handles public chat and direct messages, replying 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`, `!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 `1respawn`, `1sleep`, `1loiter`, addressed path movement such as `1goto x y z`, and
`1farm crops`. The command catalog below is retained from `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 `!pos` - replies with position and dimension
`!dist` - replies with your distance from the bot
`!afk` - goes AFK with /afk `!afk` - goes AFK with /afk
`!unafk` - goes not AFK with /afk `!unafk` - goes not AFK with /afk
+21
View File
@@ -116,6 +116,27 @@ class Commands:
if command == 'pos': if command == 'pos':
reply = str(utils.pint(self.g.pos))[1:-1] + ', ' + self.g.dimension 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 ## !health - replies with health, hunger, and saturation
if command == 'health': if command == 'health':
if not self.g.get('health_update_received'): if not self.g.get('health_update_received'):
+15
View File
@@ -208,6 +208,21 @@ class TestModernChat(unittest.TestCase):
('DM', 'tanner', 'tanner -> me: !ping', 'tanner'), ('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): def test_dm_command_replies_to_named_sender(self):
chat = FakeChat() chat = FakeChat()
command = Commands.__new__(Commands) command = Commands.__new__(Commands)