mirror of
https://github.com/evennia/evennia.git
synced 2026-03-22 15:56:30 +01:00
Merge pull request #2755 from ChrisLR/2736-fix-cmdset-remove
[Bugfix] 2736 Allow cmdsets to remove and retrieve by key
This commit is contained in:
commit
61139fc0df
2 changed files with 34 additions and 4 deletions
|
|
@ -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:
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue