Added basecommands.py
Contains @simplecommand and @basecommand Slightly alterred simplecommand, made both display the command's required permission when displaying help if the sender has the permission "utils.showpermission". Also a small tweak in misc.py's join listener.
This commit is contained in:
210
basecommands.py
Normal file
210
basecommands.py
Normal file
@@ -0,0 +1,210 @@
|
|||||||
|
from helpers import *
|
||||||
|
|
||||||
|
to_see_permission = "utils.showpermission" # See cmd permission in help
|
||||||
|
|
||||||
|
def simplecommand(cmd,
|
||||||
|
aliases = [],
|
||||||
|
usage = "[args...]",
|
||||||
|
description = None,
|
||||||
|
senderLimit = -1,
|
||||||
|
amin = 0,
|
||||||
|
amax = -1,
|
||||||
|
helpNoargs = False,
|
||||||
|
helpSubcmd = False):
|
||||||
|
cmd = cmd.lower()
|
||||||
|
permission = "utils." + cmd
|
||||||
|
if not description:
|
||||||
|
description = "Handles " + cmd
|
||||||
|
|
||||||
|
def getHelp(sender):
|
||||||
|
return helpMsg(sender, cmd, description, usage, aliases, permission)
|
||||||
|
|
||||||
|
def decorator(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 "&cThis command can only be run from the console" if isplayer else "&cThis command can only be run by players"
|
||||||
|
if not sender.hasPermission(permission):
|
||||||
|
return "&cYou do not have permission to use this 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 function(sender, command, label, args)
|
||||||
|
return call
|
||||||
|
return decorator
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def advancedcommand(cmd,
|
||||||
|
aliases = [],
|
||||||
|
description = None,
|
||||||
|
senderLimit = -1,
|
||||||
|
subCommands = []):
|
||||||
|
cmd = cmd.lower()
|
||||||
|
|
||||||
|
def isSenderValid(isPlayer):
|
||||||
|
return True if senderLimit == -1 else senderLimit != isPlayer
|
||||||
|
|
||||||
|
def getHelp(sender):
|
||||||
|
return helpMsg(sender, cmd, description, usage, aliases)
|
||||||
|
|
||||||
|
def getSubCmd(alias):
|
||||||
|
called = None
|
||||||
|
for sub in subCommands:
|
||||||
|
if sub.isCalled(alias):
|
||||||
|
called = sub
|
||||||
|
|
||||||
|
@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 not args:
|
||||||
|
return getHelp()
|
||||||
|
subcmd = args[0].lower()
|
||||||
|
if subcmd == "help":
|
||||||
|
if len(args) == 2:
|
||||||
|
called = getSubCmd(args[1].lower())
|
||||||
|
if called:
|
||||||
|
return called.getHelp(sender)
|
||||||
|
else:
|
||||||
|
return getHelp()
|
||||||
|
called = getSubCmd(subcmd)
|
||||||
|
if not called:
|
||||||
|
return getHelp()
|
||||||
|
if not isSenderValid(called.senderLimit, isPlayer):
|
||||||
|
return invalidSenderMsg(isPlayer)
|
||||||
|
if not sender.hasPermission(called.permission):
|
||||||
|
return "&cYou do not have permission to use this command"
|
||||||
|
if not checkargs(sender, args[1:], called.amin, called.amax):
|
||||||
|
return None
|
||||||
|
return called.call(sender, command, label, args)
|
||||||
|
|
||||||
|
def decorator(function):
|
||||||
|
|
||||||
|
functions = [func for func in function.__dict__.itervalues() if inspect.isfunction(func)]
|
||||||
|
|
||||||
|
for sub in subCommands:
|
||||||
|
sub.setParent(cmd)
|
||||||
|
for func in functions:
|
||||||
|
if sub.cmd == func.__name__.lower():
|
||||||
|
sub.setCalledFunction(func)
|
||||||
|
if not sub.call:
|
||||||
|
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(parent):
|
||||||
|
self.parent = parent
|
||||||
|
self.permission = "utils.%s.%s" % (parent, cmd)
|
||||||
|
self.description = description if description else "Handles /" + parent
|
||||||
|
|
||||||
|
def setCalledFunction(function):
|
||||||
|
self.call = function
|
||||||
|
|
||||||
|
def isCalled(subcmd):
|
||||||
|
alias = cmd
|
||||||
|
i = 0
|
||||||
|
while (i <= len(aliases)):
|
||||||
|
if alias == subcmd:
|
||||||
|
return True
|
||||||
|
alias = 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...))
|
||||||
|
|
||||||
|
It will take care of the following things:
|
||||||
|
- Basic permission check. The default permission is utils.<cmd_name>
|
||||||
|
- Sender type check. You can pass whether the sender must be a player, a consolesender, or both. (see sender_limit below)
|
||||||
|
- Minimum and maximum args. Works with checkargs from helpers.py.
|
||||||
|
- A '/<cmd> help' message containing the description, usage and aliases of the command.
|
||||||
|
|
||||||
|
Information about some arguments:
|
||||||
|
- cmd: The command you type in chat.
|
||||||
|
- usage: For example "<player> <other_player" and is part of the help message.
|
||||||
|
- description: A brief description of what the command does. (also part of the help message)
|
||||||
|
- aliases: A list of aliases for the command
|
||||||
|
- permission: The basic permission for the command. Defaults to utils.<cmd>
|
||||||
|
- sender_limit: Defines what sendertype can use the command. Leave blank for console & player, 0 for player only, 1 for console only.
|
||||||
|
- min_args: The least arguments for the command.
|
||||||
|
- max_args: You guessed it right. Defaults to infinity (-1).
|
||||||
|
- help_noargs: Whether to send help if no arguments are given
|
||||||
|
- help_subcmd: Whether to send help upon '/<cmd> help'
|
||||||
|
|
||||||
|
*** DISCLAIMER ***
|
||||||
|
|
||||||
|
Your command function must take four arguments: sender, command, label, args and help_msg.
|
||||||
|
help_msg is a function which can be called like 'return help_msg(sender)' to send them information about the command.
|
||||||
|
|
||||||
|
Your function must also not return a boolean like before, but a String instead. The string returned will be sent to the player (&-codes supported).
|
||||||
|
Use None or "" for no message.
|
||||||
|
|
||||||
|
Feel free to edit or give suggestions (but don't break existing commands)
|
||||||
|
|
||||||
|
See below for an example command:
|
||||||
|
|
||||||
|
@simplecommand("test", usage = "<player>", description = "Kicks a player", aliases = ["kickp", "tek"], permission = "utils.kickuser", sender_limit = 0, min_args = 1, max_args = 2)
|
||||||
|
def on_test(sender, command, label, args):
|
||||||
|
target = server.getPlayer(args[0]);
|
||||||
|
if target:
|
||||||
|
target.kickPlayer("Kicked from the server")
|
||||||
|
return None
|
||||||
|
return "&cThat player could not be found"
|
||||||
|
"""
|
||||||
103
helpers.py
103
helpers.py
@@ -217,106 +217,3 @@ def toggle(player, ls, name = "Toggle", add = None):
|
|||||||
msg(player, "&a%s turned on!" % name)
|
msg(player, "&a%s turned on!" % name)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
"""
|
|
||||||
Use this instead of @hook.command for your command methods. (@simplecommand(things...))
|
|
||||||
|
|
||||||
It will take care of the following things:
|
|
||||||
- Basic permission check. The default permission is utils.<cmd_name>
|
|
||||||
- Sender type check. You can pass whether the sender must be a player, a consolesender, or both. (see sender_limit below)
|
|
||||||
- Minimum and maximum args. Works with checkargs from helpers.py.
|
|
||||||
- A '/<cmd> help' message containing the description, usage and aliases of the command.
|
|
||||||
|
|
||||||
Information about some arguments:
|
|
||||||
- cmd_name: The command you type in chat.
|
|
||||||
- usage: For example "<player> <other_player" and is part of the help message.
|
|
||||||
- description: A brief description of what the command does. (also part of the help message)
|
|
||||||
- aliases: A list of aliases for the command
|
|
||||||
- permission: The basic permission for the command. Defaults to utils.<cmd>
|
|
||||||
- sender_limit: Defines what sendertype can use the command. Leave blank for console & player, 0 for player only, 1 for console only.
|
|
||||||
- min_args: The least arguments for the command.
|
|
||||||
- max_args: You guessed it right. Defaults to infinity (-1).
|
|
||||||
- help_noargs: Whether to send help if no arguments are given
|
|
||||||
- help_subcmd: Whether to send help upon '/<cmd> help'
|
|
||||||
|
|
||||||
*** DISCLAIMER ***
|
|
||||||
|
|
||||||
Your command function must take four arguments: sender, command, label, args and help_msg.
|
|
||||||
help_msg is a function which can be called like 'return help_msg(sender)' to send them information about the command.
|
|
||||||
|
|
||||||
Your function must also not return a boolean like before, but a String instead. The string returned will be sent to the player (&-codes supported).
|
|
||||||
Use None or "" for no message.
|
|
||||||
|
|
||||||
Feel free to edit or give suggestions (but don't break existing commands)
|
|
||||||
|
|
||||||
See below for an example command.
|
|
||||||
"""
|
|
||||||
|
|
||||||
def simplecommand(cmd_name, aliases = [], permission = None, usage = "[args...]", description = None, sender_limit = -1, min_args = 0, max_args = -1, help_noargs = False, help_subcmd = False):
|
|
||||||
cmd_name = cmd_name.lower()
|
|
||||||
if not permission:
|
|
||||||
permission = "utils." + cmd_name
|
|
||||||
if not description:
|
|
||||||
description = "Handles " + cmd_name
|
|
||||||
|
|
||||||
def actualDecorator(function):
|
|
||||||
|
|
||||||
@hook.command(cmd_name, aliases = aliases)
|
|
||||||
def __call_func__(sender, command, label, args):
|
|
||||||
try:
|
|
||||||
message = __run__(sender, command, label, args)
|
|
||||||
if message:
|
|
||||||
msg(sender, message)
|
|
||||||
except:
|
|
||||||
error(trace())
|
|
||||||
|
|
||||||
def __run__(sender, command, label, args):
|
|
||||||
isplayer = is_player(sender)
|
|
||||||
if not is_sender_valid(isplayer):
|
|
||||||
return "&cThis command can only be run from the console" if isplayer else "&cThis command can only be run by players"
|
|
||||||
if not sender.hasPermission(permission):
|
|
||||||
return "&cYou do not have permission to use this command"
|
|
||||||
if ((not args) and help_noargs) or (help_subcmd and args and args[0].lower() == "help"):
|
|
||||||
return help_message(sender)
|
|
||||||
if not checkargs(sender, args, min_args, max_args):
|
|
||||||
return None
|
|
||||||
return function(sender, command, label, args, help_message)
|
|
||||||
|
|
||||||
return __call_func__
|
|
||||||
|
|
||||||
def is_sender_valid(isplayer):
|
|
||||||
return True if sender_limit == -1 else sender_limit != isplayer
|
|
||||||
|
|
||||||
def help_message(sender):
|
|
||||||
"""
|
|
||||||
Information about command /<cmd>: #Light green
|
|
||||||
description... #Blue
|
|
||||||
description... #Blue
|
|
||||||
|
|
||||||
Syntax: /<cmd> <usage> #Light green
|
|
||||||
Aliases: alias1, alias2, alias3 #Gold
|
|
||||||
"""
|
|
||||||
try:
|
|
||||||
help_msg = "&aInformation about command /%s:\n &9%s" % (cmd_name, description.replace("\n", "\n "))
|
|
||||||
help_msg += "\n \n&aSyntax: /%s %s" % (cmd_name, usage)
|
|
||||||
if aliases:
|
|
||||||
help_msg += ("\n&6Aliases: " + "".join([(alias + ", ") for alias in aliases]))[:-2]
|
|
||||||
return help_msg
|
|
||||||
except:
|
|
||||||
error(trace())
|
|
||||||
|
|
||||||
return actualDecorator
|
|
||||||
|
|
||||||
|
|
||||||
"""
|
|
||||||
@simplecommand("test", usage = "<player>", description = "Kicks a player", aliases = ["kickp", "tek"], permission = "utils.kickuser", sender_limit = 0, min_args = 1, max_args = 2)
|
|
||||||
def on_test(sender, command, label, args):
|
|
||||||
target = server.getPlayer(args[0]);
|
|
||||||
if target:
|
|
||||||
target.kickPlayer("Kicked from the server")
|
|
||||||
return None
|
|
||||||
return "&cThat player could not be found"
|
|
||||||
"""
|
|
||||||
|
|||||||
38
misc.py
38
misc.py
@@ -6,6 +6,7 @@ import thread
|
|||||||
import org.bukkit.inventory.ItemStack as ItemStack
|
import org.bukkit.inventory.ItemStack as ItemStack
|
||||||
import org.bukkit.Bukkit as Bukkit
|
import org.bukkit.Bukkit as Bukkit
|
||||||
import org.bukkit.event.player.PlayerChatEvent as PlayerChatEvent
|
import org.bukkit.event.player.PlayerChatEvent as PlayerChatEvent
|
||||||
|
from basecommands import simplecommand
|
||||||
|
|
||||||
|
|
||||||
@hook.event("player.PlayerJoinEvent", "monitor")
|
@hook.event("player.PlayerJoinEvent", "monitor")
|
||||||
@@ -32,19 +33,18 @@ def on_join(event):
|
|||||||
# teleport to spawn when spawning inside portal
|
# teleport to spawn when spawning inside portal
|
||||||
loginloc = player.getLocation().getBlock().getType()
|
loginloc = player.getLocation().getBlock().getType()
|
||||||
headloc = player.getEyeLocation().getBlock().getType()
|
headloc = player.getEyeLocation().getBlock().getType()
|
||||||
if str(loginloc) == "PORTAL" or str(headloc) == "PORTAL":
|
if "PORTAL" in [str(headloc), str(loginloc)]:
|
||||||
msg(player, "&4Looks like you spawned in a portal... Let me help you out")
|
msg(player, "&4Looks like you spawned in a portal... Let me help you out")
|
||||||
msg(player, "&6You can use /back if you &nreally&6 want to go back")
|
msg(player, "&6You can use /back if you &nreally&6 want to go back")
|
||||||
player.teleport(player.getWorld().getSpawnLocation())
|
player.teleport(player.getWorld().getSpawnLocation())
|
||||||
|
|
||||||
|
|
||||||
@simplecommand("sudo",
|
@simplecommand("sudo",
|
||||||
permission = "utils.sudo",
|
|
||||||
usage = "<player> [cmd..]",
|
usage = "<player> [cmd..]",
|
||||||
description = "Makes <player> write [cmd..] in chat",
|
description = "Makes <player> write [cmd..] in chat",
|
||||||
min_args = 2,
|
amin = 2,
|
||||||
help_noargs = True)
|
helpNoargs = True)
|
||||||
def on_sudo_command(sender, command, label, args, help_msg):
|
def on_sudo_command(sender, command, label, args):
|
||||||
target = args[0]
|
target = args[0]
|
||||||
cmd = " ".join(args[1:])
|
cmd = " ".join(args[1:])
|
||||||
msg(sender, "&2[SUDO] &rRunning '&e%s&r' as &3%s" % (cmd, target))
|
msg(sender, "&2[SUDO] &rRunning '&e%s&r' as &3%s" % (cmd, target))
|
||||||
@@ -61,11 +61,10 @@ def on_sudo_command(sender, command, label, args, help_msg):
|
|||||||
|
|
||||||
|
|
||||||
@simplecommand("me",
|
@simplecommand("me",
|
||||||
permission = "utils.me",
|
|
||||||
usage = "[message..]",
|
usage = "[message..]",
|
||||||
description = "Sends a message in third person",
|
description = "Sends a message in third person",
|
||||||
help_noargs = True)
|
helpNoargs = True)
|
||||||
def on_me_command(sender, command, label, args, help_msg):
|
def on_me_command(sender, command, label, args):
|
||||||
broadcast("utils.me", "&7- %s &7%s %s" % (sender.getDisplayName() if isinstance(sender, Player) else "&9CONSOLE", u"\u21E6", " ".join(args)))
|
broadcast("utils.me", "&7- %s &7%s %s" % (sender.getDisplayName() if isinstance(sender, Player) else "&9CONSOLE", u"\u21E6", " ".join(args)))
|
||||||
return None
|
return None
|
||||||
|
|
||||||
@@ -158,30 +157,11 @@ def eval_thread(sender, code):
|
|||||||
msg(sender, ">>> %s: %s" % (eclass.__name__, e) + "\n ", False, "c")
|
msg(sender, ">>> %s: %s" % (eclass.__name__, e) + "\n ", False, "c")
|
||||||
thread.exit()
|
thread.exit()
|
||||||
|
|
||||||
|
|
||||||
"""
|
|
||||||
@hook.command("pyeval")
|
|
||||||
def on_pyeval_command(sender, command, label, args):
|
|
||||||
"
|
|
||||||
/pyeval
|
|
||||||
run python code ingame
|
|
||||||
"
|
|
||||||
if sender.hasPermission("utils.pyeval"):
|
|
||||||
if not checkargs(sender, args, 1, -1):
|
|
||||||
return True
|
|
||||||
msg(sender, "%s" % " ".join(args), False, "e")
|
|
||||||
thread.start_new_thread(eval_thread, (sender, " ".join(args)))
|
|
||||||
else:
|
|
||||||
noperm(sender)
|
|
||||||
return True
|
|
||||||
"""
|
|
||||||
|
|
||||||
@simplecommand("pyeval",
|
@simplecommand("pyeval",
|
||||||
permission = "utils.pyeval",
|
|
||||||
usage = "[code..]",
|
usage = "[code..]",
|
||||||
description = "Runs python [code..] and returns the result",
|
description = "Runs python [code..] and returns the result",
|
||||||
help_noargs = True)
|
helpNoargs = True)
|
||||||
def on_pyeval_command(sender, command, label, args, help_msg):
|
def on_pyeval_command(sender, command, label, args):
|
||||||
msg(sender, " ".join(args), False, "e")
|
msg(sender, " ".join(args), False, "e")
|
||||||
thread.start_new_thread(eval_thread, (sender, " ".join(args)))
|
thread.start_new_thread(eval_thread, (sender, " ".join(args)))
|
||||||
return None
|
return None
|
||||||
|
|||||||
Reference in New Issue
Block a user