Zude Onim
unread,Nov 23, 2019, 7:31:38 AM11/23/19Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to eve...@googlegroups.com
Your container object needs a command set, which contains commands.
Think of it like this :
object
--> has "command set"
command set
--> contains "commands"
----------
Containers in my game work like this :
(You may not need all of the code below, and feel free to change it.)
(Also, other wizards reading this, feel free to improve my code, too, please !)
#########################################
# BAGS / CONTAINERS COMMANDS
#########################################
class CmdPutContainerA(Command):
"""
|nTo put an item into a container use commands|x:|n
|gputcontainer |x<|gitem|x>|n or |gputbag |x<|gitem|x>|n
|nExamples|x:
|gputcontainer pistol|n
|gputcontainer 1-pistol |x(|nto put the first |x'|npistol|x'
|nfrom your inventory into the container|x)|n
|gputbag 1-armor|n
"""
key = "putcontainer"
aliases = ["putbag"]
locks = "cmd:all()"
help_category = "containers"
def func(self):
caller = self.caller
if not self.args:
caller.msg("\nUse command |gputcontainer |x<|gitem|x>")
return
to_put = caller.search(self.args.strip(), location=caller)
targetbag = self.obj
if not (to_put and targetbag):
return
if not to_put.location == caller:
caller.msg("|nYou are not holding %s|n." % to_put.key)
return
if to_put.db.is_container == True:
caller.msg("\n|nYou cannot put a container in a container.")
return
if to_put.db.equipped == True:
caller.msg("\n|nYou cannot put an equipped object into a
container.")
return
caller.msg("\n|nYou put %s |ninto the %s|n." % (to_put.key,
targetbag.key))
to_put.move_to(targetbag, quiet=True)
class CmdGetContainerA(Command):
"""
|nTo get an item from a container use commands|x:|n
|ggetcontainer |x<|gitem|x>|n or |ggetbag |x<|gitem|x>|n
|nExamples|x:
|ggetcontainer pistol|n
|ggetcontainer 1-pistol |x(|nto get the first |x'|npistol|x'
|nfrom the container|x)|n
|ggetbag 1-armor|n
"""
key = "getcontainer"
aliases = ["getbag"]
locks = "cmd:all()"
help_category = "containers"
def func(self):
caller = self.caller
if not self.args:
caller.msg("\nUse command |ggetcontainer |x<|gitem|x>")
return
to_get = caller.search(self.args.strip(), location=self.obj)
if not to_get:
return
caller.msg("\n|nYou get %s |nfrom the %s|n." % (to_get.key,
self.obj.key))
to_get.move_to(self.caller, quiet=True)
###########################
# BAGS / CONTAINERS COMMAND SET
###########################
class CmdSetContainerA(CmdSet):
def at_cmdset_creation(self):
self.add(CmdPutContainerA())
self.add(CmdGetContainerA())
#########################################
# BAGS / CONTAINERS
#########################################
class ContainerA(DefaultObject):
def at_object_creation(self):
self.cmdset.add_default(CmdSetContainerA, permanent=True)
# self.locks.add("call: holds()")
self.locks.add("get:false()")
self.aliases.add(["container"])
self.db.is_container = True
self.db.desc = ""
def return_appearance(self, looker, **kwargs):
if not looker:
return ""
string = "\n%s|n\n" % self.get_display_name(looker)
desc = self.db.desc
if desc:
string += desc
visible = (con for con in self.contents if con != looker and
con.access(looker, "view"))
things = defaultdict(list)
for con in visible:
key = con.get_display_name(looker)
things[key].append(con)
if things:
thing_strings = []
for key, itemlist in sorted(things.items()):
nitem = len(itemlist)
if nitem == 1:
key, _ = itemlist[0].get_numbered_name(nitem,
looker, key=key)
else:
key = [item.get_numbered_name(nitem, looker,
key=key)[1] for item in itemlist][0]
thing_strings.append(key)
string += "\n\n|nContains:\n|n" + list_to_string(thing_strings)
else:
string += "\n\n|nContainer is empty !"
return string