diff --git a/evennia/commands/cmdset.py b/evennia/commands/cmdset.py index 1a0aa952b0..9d8877a679 100644 --- a/evennia/commands/cmdset.py +++ b/evennia/commands/cmdset.py @@ -546,10 +546,7 @@ class CmdSet(object, metaclass=_CmdSetMeta): commands[ic] = cmd # replace except ValueError: commands.append(cmd) - self.commands = commands - if not allow_duplicates: - # extra run to make sure to avoid doublets - self.commands = list(set(self.commands)) + # add system_command to separate list as well, # for quick look-up if cmd.key.startswith("__"): @@ -559,6 +556,11 @@ class CmdSet(object, metaclass=_CmdSetMeta): except ValueError: system_commands.append(cmd) + self.commands = commands + if not allow_duplicates: + # extra run to make sure to avoid doublets + self.commands = list(set(self.commands)) + def remove(self, cmd): """ Remove a command instance from the cmdset. @@ -568,6 +570,11 @@ class CmdSet(object, metaclass=_CmdSetMeta): or the key of such a command. """ + if isinstance(cmd, str): + cmd = next((_cmd for _cmd in self.commands if _cmd.key == cmd), None) + if cmd is None: + return None + cmd = self._instantiate(cmd) if cmd.key.startswith("__"): try: @@ -591,6 +598,11 @@ class CmdSet(object, metaclass=_CmdSetMeta): cmd (Command): The first matching Command in the set. """ + if isinstance(cmd, str): + cmd = next((_cmd for _cmd in self.commands if _cmd.key == cmd), None) + if cmd is None: + return None + cmd = self._instantiate(cmd) for thiscmd in self.commands: if thiscmd == cmd: diff --git a/evennia/commands/tests.py b/evennia/commands/tests.py index c06962da84..c422a69a81 100644 --- a/evennia/commands/tests.py +++ b/evennia/commands/tests.py @@ -1199,3 +1199,21 @@ class TestCmdSetNesting(BaseEvenniaTest): cmd = self.char1.cmdset.cmdset_stack[-1].commands[0] self.assertEqual(cmd.obj, self.char1) + + +class TestCmdSet(BaseEvenniaTest): + """ + General tests for cmdsets + """ + + def test_cmdset_remove_by_key(self): + test_cmd_set = _CmdSetTest() + test_cmd_set.remove("another command") + + self.assertNotIn(_CmdTest2, test_cmd_set.commands) + + def test_cmdset_gets_by_key(self): + test_cmd_set = _CmdSetTest() + result = test_cmd_set.get("another command") + + self.assertIsInstance(result, _CmdTest2)