Serversigns permissions fix, adapted help page to user permissions, implemented help pages

This commit is contained in:
Dico200
2016-06-05 17:46:35 +02:00
parent 2c94c40c77
commit 8e2ff33dbb

View File

@@ -4,6 +4,7 @@ import org.bukkit.Material as Material
import java.util.UUID as UUID import java.util.UUID as UUID
import org.bukkit.Material as Material import org.bukkit.Material as Material
import org.bukkit.block.BlockFace as BlockFace import org.bukkit.block.BlockFace as BlockFace
from math import ceil
commands_whitelist = ( commands_whitelist = (
"mail", "email", "memo", "mail", "email", "memo",
@@ -22,6 +23,7 @@ commands_whitelist = (
max_line_length = 256 max_line_length = 256
max_lines = 20 max_lines = 20
help_page_size = 12
def load_signs(): def load_signs():
signs_obj = open_json_file("serversigns", []) signs_obj = open_json_file("serversigns", [])
@@ -103,6 +105,46 @@ def signsMsg(msg, colour = '4'):
return "&c[Signs] &" + colour + msg return "&c[Signs] &" + colour + msg
# /svs command
subcommand_info = (
(("claim",), (
"Claims the sign so that you can add messages to it",
), (("[owner]", "claims.other"),)),
(("info", "lines"), (
"Displays information about the (claimed) sign",
)),
(("add",), (
"Adds the message to the sign. Use ++ at the end",
"to add the message to your buffer. You can then use",
"the same command again to create a longer message",
), (("<message>[++]", None),)),
(("remove", "rem", "del", "delete"), (
"Removes the message with the given ID from the sign.",
"The ID is given before each message by &b/svs info&a.",
), (("<message ID>", None),)),
(("switch", "reverse"), (
"Reverses the order in which the given messages are shown.",
), (("<message ID 1> <message ID 2>", None),)),
(("clear",), (
"Removes all messages from the sign.",
)),
(("reset", "unclaim"), (
"Resets the sign, removing all messages and its owner.",
)),
(("whitelist", "commands", "wl"), (
"Displays a list of whitelisted commands",
)),
(("help",), (
"Displays this help page",
), (("[page]", None),)),
)
@simplecommand(cmd = "serversigns", aliases = ["svs", "signmsg"], @simplecommand(cmd = "serversigns", aliases = ["svs", "signmsg"],
description = "Makes something happen when you right click signs. \nUse /svs help for more details.", description = "Makes something happen when you right click signs. \nUse /svs help for more details.",
usage = "<claim|reset|add <msg>[++]|remove <ID>|clear|info|help>", usage = "<claim|reset|add <msg>[++]|remove <ID>|clear|info|help>",
@@ -110,40 +152,55 @@ def signsMsg(msg, colour = '4'):
senderLimit = 0) senderLimit = 0)
def svs_command(sender, command, label, args): def svs_command(sender, command, label, args):
arg1 = args[0].lower() arg1 = args[0].lower()
Validate.isTrue(arg1 in ("claim", "reset", "add", "remove", "rem", "del", "delete", "info", "lines",
"clear", "help", "switch", "reverse", "unclaim", "commands", "whitelist", "wl"), cmd_info = None
signsMsg("That argument could not be recognized, use &o/svs help &4for expected arguments")) for cmd in subcommand_info:
Validate.isAuthorized(sender, "utils.serversigns." + arg1) if arg1 in cmd[0]:
cmd_info = cmd
break
Validate.notNone(cmd_info, signsMsg("That command could not be recognized, use &o/svs help &4for expected arguments"))
cmd = cmd_info[0][0]
Validate.isAuthorized(sender, "utils.serversigns." + cmd)
#-------------------- Sub commands that don't require any conditions ----------------------- #-------------------- Sub commands that don't require any conditions -----------------------
if arg1 == "help": if cmd == "help":
admin = sender.hasPermission("utils.serversigns.admin") all_lines = [
msg = signsMsg("Server signs lets you add messages to a sign.", 'a') "&aServer signs let's you add messages to a sign.",
msg += "\nRight clicking the sign will display all the messages. Commands" "Right clicking the sign will display all the messages. Commands",
msg += "\ncan also be added, by prefixing the message with a '/'." "can also be added, by prefixing the message with a '/'.",
msg += "\nHow to use &b/serversigns&a:" "How to use &b/serversigns&a:",
msg += "\n&b/svs claim" + ("" if not sender.hasPermission("utils.serversigns.admin") else " [owner]") ]
msg += "\n&a- Claims the sign so that you can add messages to it"
msg += "\n&b/svs info|lines"
msg += "\n&a- Displays information about the (claimed) sign"
msg += "\n&b/svs add <message>[++]"
msg += "\n&a- Adds the message to the sign. Use ++ at the end"
msg += "\n&a- to add the message to your buffer. You can then use"
msg += "\n&a- the same command again to create a longer message."
msg += "\n&b/svs remove <message ID>"
msg += "\n&a- Removes the message with the given ID from the sign."
msg += "\n&a- The ID is given before each message by &b/svs info&a."
msg += "\n&b/svs switch|reverse <message ID 1> <message ID 2>"
msg += "\n&a- Reverses the order in which the given messages are shown."
msg += "\n&b/svs clear"
msg += "\n&a- Removes all messages from the sign."
msg += "\n&b/svs reset|unclaim"
msg += "\n&a- Resets the sign, removing all messages and its owner."
msg += "\n&b/svs commands|whitelist|wl"
msg += "\n&a- Shows a list of whitelisted commands"
return msg
if arg1 in ("commands", "whitelist", "wl"): for cmd in subcommand_info:
if sender.hasPermission("utils.serversigns." + cmd[0][0]):
params = ""
if len(cmd) == 3:
for param, perm in cmd[2]:
if perm is None or sender.hasPermission("utils.serversigns." + perm):
params += param + " "
all_lines.append("&b/svs %s %s" % ("|".join(cmd[0]), params))
for info_line in cmd[1]:
all_lines.append("&a- " + info_line)
last_page = int(ceil(len(all_lines) / help_page_size))
info("last page: %d" % last_page)
page = 1
if len(args) > 1 and args[1].isdigit():
page = int(args[1])
if page <= 0:
page = 1
elif page > last_page:
page = last_page
first_line = signsMsg("Serversigns help page %d" % page, '6')
if page < last_page:
first_line += ", &e/svs help %d&6 for the next" % (page + 1)
page_lines = [first_line] + all_lines[(page - 1) * help_page_size : min(page * help_page_size, len(all_lines))]
return "\n".join(page_lines)
if cmd == "whitelist":
return signsMsg("Whitelisted commands: &3" + ", ".join(commands_whitelist), 'a') return signsMsg("Whitelisted commands: &3" + ", ".join(commands_whitelist), 'a')
#------------------------------------------------------------------------------------------- #-------------------------------------------------------------------------------------------
@@ -156,7 +213,7 @@ def svs_command(sender, command, label, args):
arg2 = args[1].lower() if len(args) > 1 else None arg2 = args[1].lower() if len(args) > 1 else None
#------------------------ Sub commands that require the block to be a sign ------------------------------- #------------------------ Sub commands that require the block to be a sign -------------------------------
if arg1 == "claim": if cmd == "claim":
Validate.isTrue(not sign, signsMsg("The %s was already claimed" % signName)) Validate.isTrue(not sign, signsMsg("The %s was already claimed" % signName))
Validate.isTrue(can_build2(sender, block), signsMsg("You are not permitted to claim signs here")) Validate.isTrue(can_build2(sender, block), signsMsg("You are not permitted to claim signs here"))
target = sender target = sender
@@ -180,7 +237,7 @@ def svs_command(sender, command, label, args):
Validate.notNone(sign, signsMsg("The %s has not been claimed" % signName)) Validate.notNone(sign, signsMsg("The %s has not been claimed" % signName))
#----------------------Sub commands that require the sign to be claimed as well------------------------------------ #----------------------Sub commands that require the sign to be claimed as well------------------------------------
if arg1 in ("info", "lines"): if cmd == "info":
sign_lines = "" sign_lines = ""
for id, line in enumerate(sign[1:]): for id, line in enumerate(sign[1:]):
sign_lines += ("\n &a%s: \"&f%s&a\"" % (id + 1, line)) sign_lines += ("\n &a%s: \"&f%s&a\"" % (id + 1, line))
@@ -190,7 +247,7 @@ def svs_command(sender, command, label, args):
Validate.isTrue(canEdit(sign, sender), signsMsg("You do not own the %s!" % signName)) Validate.isTrue(canEdit(sign, sender), signsMsg("You do not own the %s!" % signName))
#---------------------- Sub commands that require you to own targeted sign as well ------------------------- #---------------------- Sub commands that require you to own targeted sign as well -------------------------
if arg1 == "add": if cmd == "add":
Validate.isTrue(len(sign) - 1 <= max_lines, signsMsg("This sign already has the maximum amount of lines, you cannot add more")) Validate.isTrue(len(sign) - 1 <= max_lines, signsMsg("This sign already has the maximum amount of lines, you cannot add more"))
line = " ".join(args[1:]) line = " ".join(args[1:])
@@ -227,7 +284,7 @@ def svs_command(sender, command, label, args):
return signsMsg("Added line \"&f%s&a\" to the %s" % (line, signName), 'a') return signsMsg("Added line \"&f%s&a\" to the %s" % (line, signName), 'a')
if arg1 in ("remove", "rem", "del", "delete"): if cmd == "remove":
Validate.notNone(arg2, signsMsg("You have to enter the ID of the message to remove!")) Validate.notNone(arg2, signsMsg("You have to enter the ID of the message to remove!"))
try: try:
id = int(arg2) id = int(arg2)
@@ -238,7 +295,7 @@ def svs_command(sender, command, label, args):
return signsMsg("Removed message with id %s from the %s" % (id, signName), 'a') return signsMsg("Removed message with id %s from the %s" % (id, signName), 'a')
if arg1 in ("switch", "reverse"): if cmd == "switch":
Validate.isTrue(len(args) == 3, signsMsg("You have to enter the 2 IDs of the messages to reverse")) Validate.isTrue(len(args) == 3, signsMsg("You have to enter the 2 IDs of the messages to reverse"))
try: try:
id1 = int(args[1]) id1 = int(args[1])
@@ -252,13 +309,13 @@ def svs_command(sender, command, label, args):
return signsMsg("Reversed the messages with IDs %s and %s of the %s" % (id1, id2, signName), 'a') return signsMsg("Reversed the messages with IDs %s and %s of the %s" % (id1, id2, signName), 'a')
if arg1 == "clear": if cmd == "clear":
signs[loc] = [sign[0]] signs[loc] = [sign[0]]
save_signs() save_signs()
return signsMsg("Removed all messages from the %s" % signName, 'a') return signsMsg("Removed all messages from the %s" % signName, 'a')
if arg1 in ("reset", "unclaim"): if cmd == "reset":
del signs[loc] del signs[loc]
save_signs() save_signs()
return signsMsg("Removed all messages and the owner from the %s, it can now be claimed" % signName, 'a') return signsMsg("Removed all messages and the owner from the %s, it can now be claimed" % signName, 'a')