summaryrefslogtreecommitdiff
path: root/basecommands.py
diff options
context:
space:
mode:
authorDico200 <dico.karssiens@gmail.com>2015-04-03 02:01:57 +0200
committerDico200 <dico.karssiens@gmail.com>2015-04-03 02:01:57 +0200
commitde43b0d0db6ede685d8c3f6579a20eb268262cb2 (patch)
treee8976a27ce13214a8094898ecfd5957499eac2aa /basecommands.py
parent661e25dc675cb1aade3b7038520d0f6371981a88 (diff)
Added (sub/main)command, removed abot error check
@command is untested.
Diffstat (limited to 'basecommands.py')
-rw-r--r--basecommands.py246
1 files changed, 189 insertions, 57 deletions
diff --git a/basecommands.py b/basecommands.py
index 311ee9b..6452064 100644
--- a/basecommands.py
+++ b/basecommands.py
@@ -1,8 +1,178 @@
from helpers import *
import inspect, new
+current_subs = []
+current_main = None
+
to_see_permission = "utils.showpermission" # See cmd permission in help
+def maincommand(function):
+ global current_main
+ current_main = function
+ return function
+
+class subcommand():
+
+ def __init__(self, cmd,
+ aliases = [],
+ amin = 0,
+ amax = -1,
+ description = None,
+ usage = "[args...]",
+ senderLimit = -1,
+ helpNoargs = False,
+ helpSubcmd = False):
+ cmd = cmd.lower()
+ self.description = description
+ self.cmd = cmd
+ self.usage = usage
+ self.aliases = aliases
+ self.amin = amin
+ self.amax = amax
+ self.senderLimit = senderLimit
+ self.helpNoargs = helpNoargs
+ self.helpSubcmd = helpSubcmd
+ self.permission = None
+ self.parent = None
+ self.call = None
+
+ def __call__(self, f):
+ global current_subs
+ def call_sub(sender, command, label, args):
+ isPlayer = is_player(sender)
+ if not isSenderValid(senderLimit, isPlayer):
+ return invalidSenderMsg(isPlayer)
+ if not sender.hasPermission(permission):
+ return "&cYou do not have permission to use that command"
+ if ((not args) and helpNoargs) or (helpSubcmd and args and args[0].lower() == "help"):
+ return getHelp(sender)
+ if not checkargs(sender, args, amin, amax):
+ return None
+ return f(sender, command, label, args)
+
+ self.call = call_sub
+ current_subs.append(self)
+ return call_sub
+
+ def getHelp(sender):
+ return helpMsg(sender, "%s %s" % (parent, cmd), description, usage, aliases, permission)
+
+ def setParent(self, parent):
+ self.parent = parent
+ self.permission = "utils.%s.%s" % (parent, self.cmd)
+ self.description = self.description if self.description else "Handles /" + parent
+
+ def isCalled(self, subcmd):
+ alias = self.cmd
+ i = 0
+ while i <= len(self.aliases):
+ if alias == subcmd:
+ return True
+ alias = self.aliases[i]
+ i += 1
+ return False
+
+
+def command(cmd,
+ aliases = [],
+ usage = "[args...]",
+ description = None,
+ senderLimit = -1,
+ amin = 0,
+ amax = -1,
+ helpNoargs = False,
+ helpSubcmd = False):
+
+ cmd = cmd.lower()
+
+ global curent_subs
+ subcommands = []
+ for subcmd in current_subs:
+ subcmd.setParent(cmd)
+ subcommands.append(subcmd)
+ info(subcommands)
+ current_subs = []
+
+ global current_main
+ main_function = current_main
+ current_main = None
+ if main_function == None and len(subcommands) != 0:
+ error("No main function found for " + cmd)
+
+ permission = "utils." + cmd
+ if not description:
+ description = "Handles " + cmd
+ if not usage:
+ usage = "/%s <subcmd>" % cmd
+
+ def getHelp(sender):
+ return helpMsg(sender, cmd, description, usage, aliases, permission)
+
+ def getCalledCmd(cmdName):
+ for subcmd in subcommands:
+ if subcmd.isCalled(cmdName):
+ return subcmd
+ return None
+
+ def decorator(function):
+
+ if len(subcommands) == 0:
+ main_function = function
+
+ @hook.command(cmd, aliases = aliases)
+ def call(sender, command, label, args):
+ message = run(sender, command, label, args)
+ if message:
+ if message == "HELP":
+ message = getHelp(sender)
+ msg(sender, message)
+ return True
+
+ def run(sender, command, label, args):
+ isPlayer = is_player(sender)
+ if not isSenderValid(senderLimit, isPlayer):
+ return invalidSenderMsg(isPlayer)
+
+ if args:
+ arg1 = args[0].lower()
+ if (not args) and helpNoargs:
+ return getHelp(sender)
+ if helpSubcmd and arg1 == "help":
+ if len(args) > 1:
+ sub = getCalledCmd(args[1].lower())
+ return sub.getHelp(sender) if sub and sub.helpSubcmd else "&c%s is not a command!" % args[1]
+ return getHelp(sender)
+ sub = getCalledCmd(args[0].lower())
+ if sub:
+ return sub.call(sender, command, label, args[1:])
+ if not sender.hasPermission(permission):
+ return "&cYou do not have permission to use that command"
+ if not checkargs(sender, args, amin, amax):
+ return None
+ return main_function(sender, command, label, args)
+ return call
+ return decorator
+
+
+
+
+def isSenderValid(senderLimit, isPlayer):
+ return True if senderLimit == -1 else senderLimit != isPlayer
+
+def invalidSenderMsg(isPlayer):
+ return "&cThat command can only be run from the console" if isPlayer else "&cThat command can only be run by players"
+
+def helpMsg(sender, cmd, description, usage, aliases, permission):
+ help_msg = "&aInformation about command /%s:\n &9%s" % (cmd, description.replace("\n", "\n "))
+ help_msg += "\n \n&aSyntax: /%s %s" % (cmd, usage)
+ if aliases:
+ help_msg += ("\n&6Aliases: " + "".join([(alias + ", ") for alias in aliases]))[:-2]
+ if sender.hasPermission(to_see_permission):
+ help_msg += "\n&6Required permission: " + permission
+ return help_msg
+
+
+
def simplecommand(cmd,
aliases = [],
usage = "[args...]",
@@ -115,64 +285,8 @@ def advancedcommand(cmd,
error("No function found for /%s %s" % (cmd, sub.cmd))
return call
return decorator
-
-
-class subcommand():
-
- def __init__(self, cmd,
- aliases = [],
- amin = 0,
- amax = -1,
- description = None,
- usage = "[args...]",
- senderLimit = -1):
- cmd = cmd.lower()
- self.description = description
- self.cmd = cmd
- self.usage = usage
- self.aliases = aliases
- self.amin = amin
- self.amax = amax
- self.senderLimit = senderLimit
- self.call = None
-
- def getHelp(sender):
- return helpMsg(sender, "%s %s" % (parent, cmd), description, usage, aliases, permission)
-
- def setParent(self, parent):
- self.parent = parent
- self.permission = "utils.%s.%s" % (parent, self.cmd)
- self.description = self.description if self.description else "Handles /" + parent
-
- def setCalledFunction(self, function):
- self.call = function
-
- def isCalled(self, subcmd):
- alias = self.cmd
- i = 0
- while i <= len(self.aliases):
- if alias == subcmd:
- return True
- alias = self.aliases[i]
- i += 1
- return False
"""
-def isSenderValid(senderLimit, isPlayer):
- return True if senderLimit == -1 else senderLimit != isPlayer
-
-def invalidSenderMsg(isPlayer):
- return "&cThat command can only be run from the console" if isPlayer else "&cThat command can only be run by players"
-
-def helpMsg(sender, cmd, description, usage, aliases, permission):
- help_msg = "&aInformation about command /%s:\n &9%s" % (cmd, description.replace("\n", "\n "))
- help_msg += "\n \n&aSyntax: /%s %s" % (cmd, usage)
- if aliases:
- help_msg += ("\n&6Aliases: " + "".join([(alias + ", ") for alias in aliases]))[:-2]
- if sender.hasPermission(to_see_permission):
- help_msg += "\n&6Required permission: " + permission
- return help_msg
-
"""
Use this instead of @hook.command for your command methods. (@simplecommand(things...))
@@ -214,4 +328,22 @@ def on_test(sender, command, label, args):
target.kickPlayer("Kicked from the server")
return None
return "&cThat player could not be found"
-""" \ No newline at end of file
+"""
+
+"""
+Example @command:
+
+@command("forcefield")
+def forcefield_command():
+
+ @subcommand("add")
+ def func(sender, command, label, args):
+ ...add to whitelist
+
+ @maincommand
+ def other(sender, command, label, args):
+ ...Execute command if no sub commands were found
+
+@command("simplestuff")
+def simplestuff_command(sender, command, label, args):
+ msg(sender, "hi, I do not use subcommands!")