Add alias/delete switch

This commit is contained in:
Chiizujin 2024-04-01 18:20:10 +11:00
parent a9e8042bbe
commit 5427817112
2 changed files with 40 additions and 5 deletions

View file

@ -218,10 +218,13 @@ class CmdSetObjAlias(COMMAND_DEFAULT_CLASS):
alias <obj> [= [alias[,alias,alias,...]]]
alias <obj> =
alias/category <obj> = [alias[,alias,...]:<category>
alias/delete <obj> = <alias>
Switches:
category - requires ending input with :category, to store the
given aliases with the given category.
delete - deletes all occurrences of the given alias, regardless
of category
Assigns aliases to an object so it can be referenced by more
than one name. Assign empty to remove all aliases from object. If
@ -235,7 +238,7 @@ class CmdSetObjAlias(COMMAND_DEFAULT_CLASS):
key = "@alias"
aliases = "setobjalias"
switch_options = ("category",)
switch_options = ("category", "delete")
locks = "cmd:perm(setobjalias) or perm(Builder)"
help_category = "Building"
@ -252,12 +255,12 @@ class CmdSetObjAlias(COMMAND_DEFAULT_CLASS):
return
objname = self.lhs
# Find the object to receive aliases
# Find the object to receive/delete aliases
obj = caller.search(objname)
if not obj:
return
if self.rhs is None:
# no =, so we just list aliases on object.
if self.rhs is None and 'delete' not in self.switches:
# no =, and not deleting, so we just list aliases on object.
aliases = obj.aliases.all(return_key_and_category=True)
if aliases:
caller.msg(
@ -280,7 +283,9 @@ class CmdSetObjAlias(COMMAND_DEFAULT_CLASS):
return
if not self.rhs:
# we have given an empty =, so delete aliases
# we have given an empty =, so delete aliases.
# as a side-effect, 'alias/delete obj' and 'alias/delete obj='
# will also be caught here, which is fine
old_aliases = obj.aliases.all()
if old_aliases:
caller.msg(
@ -292,6 +297,19 @@ class CmdSetObjAlias(COMMAND_DEFAULT_CLASS):
caller.msg("No aliases to clear.")
return
if "delete" in self.switches:
# delete all matching keys, regardless of category
existed = False
for key, category in obj.aliases.all(return_key_and_category=True):
if key == self.rhs:
obj.aliases.remove(key=self.rhs, category=category)
existed = True
if existed:
caller.msg("Alias '%s' deleted from %s." % (self.rhs, obj.get_display_name(caller)))
else:
caller.msg("%s has no alias '%s'." % (obj.get_display_name(caller), self.rhs))
return
category = None
if "category" in self.switches:
if ":" in self.rhs:

View file

@ -790,6 +790,23 @@ class TestBuilding(BaseEvenniaCommandTest):
self.call(building.CmdSetObjAlias(), "Obj2 =", "Cleared aliases from Obj2")
self.call(building.CmdSetObjAlias(), "Obj2 =", "No aliases to clear.")
self.call(building.CmdSetObjAlias(), "Obj =", "Cleared aliases from Obj: testobj1b")
self.call(building.CmdSetObjAlias(),
"/category Obj = testobj1b:category1",
"Alias(es) for 'Obj' set to 'testobj1b' (category: 'category1')."
)
self.call(
building.CmdSetObjAlias(),
"/category Obj = testobj1b:category2",
"Alias(es) for 'Obj' set to 'testobj1b,testobj1b' (category: 'category2')."
)
self.call(
building.CmdSetObjAlias(), # delete both occurences of alias 'testobj1b'
"/delete Obj = testobj1b",
"Alias 'testobj1b' deleted from Obj."
)
self.call(building.CmdSetObjAlias(), "Obj =", "No aliases to clear.")
def test_copy(self):
self.call(
building.CmdCopy(),