Added colorsupport, fixed perms and limits into file, added length limitations, made file PEP8 conform
This commit is contained in:
97
chatalias.py
97
chatalias.py
@@ -17,95 +17,102 @@ from traceback import format_exc as trace
|
|||||||
|
|
||||||
data = None
|
data = None
|
||||||
|
|
||||||
|
max_entries = 10
|
||||||
|
max_alias_length = 35
|
||||||
|
# minecraft message limit is 100 so I decided to give a little tolerance
|
||||||
|
max_overall_length = 115
|
||||||
|
|
||||||
|
alias_perm = "utils.alias.allowed"
|
||||||
|
exceed_length = "utils.alias.exceedlimit"
|
||||||
|
exceed_entries = "utils.alias.exceedlimit"
|
||||||
|
exceed_overall_length = "utils.alias.exceedlimit"
|
||||||
|
|
||||||
|
|
||||||
def safe_open_json():
|
def safe_open_json():
|
||||||
global data
|
global data
|
||||||
if data is not None:
|
if data is not None:
|
||||||
return data
|
return data
|
||||||
data = open_json_file("aliases")
|
data = open_json_file("aliases")
|
||||||
if data is None:
|
if data is None:
|
||||||
data = {"gnl":{"max_len":"35","max_entries":"10"}}
|
data = {}
|
||||||
save_json_file("aliases", data)
|
save_json_file("aliases", data)
|
||||||
return data
|
return data
|
||||||
|
|
||||||
|
|
||||||
@hook.command("alias", usage = "/<command> [to_alias] [alias...]", desc = "Aliases words in chat")
|
@hook.command("alias",
|
||||||
|
usage="/<command> [to_alias] [alias...]",
|
||||||
|
desc="Aliases words in chat")
|
||||||
def on_alias_command(sender, cmd, label, args):
|
def on_alias_command(sender, cmd, label, args):
|
||||||
|
|
||||||
if not is_player(sender):
|
if not is_player(sender):
|
||||||
msg(sender, "Sorry, Console cannot alias words")
|
msg(sender, "Sorry, Console cannot alias words")
|
||||||
return True
|
return True
|
||||||
|
if not sender.hasPermission(alias_perm):
|
||||||
if not sender.hasPermission("utils.alias.allowed"):
|
plugin_header(recipient=sender, name="Chat Alias")
|
||||||
plugin_header(recipient = sender, name = "Chat Alias")
|
|
||||||
noperm(sender)
|
noperm(sender)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
if len(args) == 0:
|
if len(args) == 0:
|
||||||
plugin_header(recipient = sender, name = "Chat Alias")
|
plugin_header(recipient=sender, name="Chat Alias")
|
||||||
msg(sender, "This is a plugin that allows you to type in chat and have words replaced by other ones automatically!")
|
msg(sender, "&7This is a plugin that allows you to get words" +
|
||||||
msg(sender, "\nCommands:")
|
"replaced by other ones automatically!")
|
||||||
msg(sender, "/alias <word>: removes <word> from aliased words. Use * to remove all aliased words.")
|
msg(sender, "&7\nCommands:")
|
||||||
msg(sender, "/alias <word> <others...>: Will change <word> to <others...> in chat")
|
msg(sender, "&e/alias <word> &7removes <word> from your aliases. " +
|
||||||
msg(sender, "\nYour Aliases:")
|
"Use &e/alias * &7to remove all aliases.")
|
||||||
|
msg(sender, "&e/alias <word> <replacement> &7will change &e<word> " +
|
||||||
|
"&7to &e<replacement> &7in chat")
|
||||||
|
msg(sender, "&7\nYour Aliases:")
|
||||||
data = safe_open_json()
|
data = safe_open_json()
|
||||||
try:
|
try:
|
||||||
for alias, value in data[str(sender.getUniqueId())].items():
|
for alias, value in data[str(sender.getUniqueId())].items():
|
||||||
msg(sender, "%s ==> %s" % (alias, value))
|
msg(sender, "&7%s &7==> %s" % (alias, value))
|
||||||
except KeyError:
|
except KeyError:
|
||||||
pass
|
pass
|
||||||
return True
|
return True
|
||||||
|
|
||||||
elif len(args) == 1:
|
elif len(args) == 1:
|
||||||
data = safe_open_json()
|
data = safe_open_json()
|
||||||
if args[0] == "*":
|
if args[0] == "*":
|
||||||
try:
|
try:
|
||||||
del data[str(sender.getUniqueId())]
|
del data[str(sender.getUniqueId())]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
plugin_header(recipient = sender, name = "Chat Alias")
|
plugin_header(recipient=sender, name="Chat Alias")
|
||||||
msg(sender, "No alias data to remove!")
|
msg(sender, "&7No alias data to remove!")
|
||||||
return True
|
return True
|
||||||
save_json_file("aliases", data)
|
save_json_file("aliases", data)
|
||||||
plugin_header(recipient = sender, name = "Chat Alias")
|
plugin_header(recipient=sender, name="Chat Alias")
|
||||||
msg(sender, "ALL alias data successfuly removed!")
|
msg(sender, "&cALL &7alias data successfuly removed!")
|
||||||
return True
|
return True
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if data[str(sender.getUniqueId())].pop(args[0], None) is None:
|
if data[str(sender.getUniqueId())].pop(args[0], None) is None:
|
||||||
plugin_header(recipient = sender, name = "Chat Alias")
|
plugin_header(recipient=sender, name="Chat Alias")
|
||||||
msg(sender, "Could not remove: alias not present!")
|
msg(sender, "&7Could not remove: alias not present!")
|
||||||
return True
|
return True
|
||||||
except KeyError:
|
except KeyError:
|
||||||
plugin_header(recipient = sender, name = "Chat Alias")
|
plugin_header(recipient=sender, name="Chat Alias")
|
||||||
msg(sender, "Could not remove: you do not have any aliases!")
|
msg(sender, "&7Could not remove: you do not have any aliases!")
|
||||||
return True
|
return True
|
||||||
|
|
||||||
save_json_file("aliases", data)
|
save_json_file("aliases", data)
|
||||||
plugin_header(recipient = sender, name = "Chat Alias")
|
plugin_header(recipient=sender, name="Chat Alias")
|
||||||
msg(sender, "Alias for %s successfuly removed" % args[0])
|
msg(sender, "&7Alias for %s &7successfuly removed" % args[0])
|
||||||
return True
|
return True
|
||||||
|
|
||||||
elif len(args) >= 2:
|
elif len(args) >= 2:
|
||||||
data = safe_open_json()
|
data = safe_open_json()
|
||||||
alias = " ".join(args[1:])
|
alias = " ".join(args[1:])
|
||||||
try:
|
try:
|
||||||
if len(alias) > int(data["gnl"]["max_len"]) and int(data["gnl"]["max_len"]) >= 0:
|
if (len(alias) > max_alias_length) and (max_alias_length >= 0) and (not sender.hasPermission(exceed_length)):
|
||||||
plugin_header(recipient = sender, name = "Chat Alias")
|
plugin_header(recipient=sender, name="Chat Alias")
|
||||||
msg(sender, "Please do not alias long words/sentences.")
|
msg(sender, "&7Please do not alias long words/sentences.")
|
||||||
return True
|
return True
|
||||||
|
if (len(data[str(sender.getUniqueId())]) >= max_entries) and (max_entries >= 0) and (not sender.hasPermission(exceed_entries)):
|
||||||
if len(data[str(sender.getUniqueId())]) >= int(data["gnl"]["max_entries"]) and int(data["gnl"]["max_entries"]) >= 0:
|
plugin_header(recipient=sender, name="Chat Alias")
|
||||||
plugin_header(recipient = sender, name = "Chat Alias")
|
msg(sender, "&7You have reached your alias limit!")
|
||||||
msg(sender, "You have reached the maximum amount of alias entries! Sorry!")
|
|
||||||
return True
|
return True
|
||||||
except KeyError:
|
except KeyError:
|
||||||
data[str(sender.getUniqueId())] = {}
|
data[str(sender.getUniqueId())] = {}
|
||||||
|
|
||||||
data[str(sender.getUniqueId())][args[0]] = alias
|
data[str(sender.getUniqueId())][args[0]] = alias
|
||||||
save_json_file("aliases", data)
|
save_json_file("aliases", data)
|
||||||
plugin_header(recipient = sender, name = "Chat Alias")
|
plugin_header(recipient=sender, name="Chat Alias")
|
||||||
msg(sender, "Chat Alias %s ==> %s successfully created!" % (args[0], alias))
|
msg(sender, "&7Chat Alias %s &7==> %s &7successfully created!" % (args[0], alias))
|
||||||
return True
|
return True
|
||||||
|
|
||||||
else:
|
else:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
@@ -114,14 +121,18 @@ def on_alias_command(sender, cmd, label, args):
|
|||||||
def on_player_chat(event):
|
def on_player_chat(event):
|
||||||
playerid = str(event.getPlayer().getUniqueId())
|
playerid = str(event.getPlayer().getUniqueId())
|
||||||
data = safe_open_json()
|
data = safe_open_json()
|
||||||
|
|
||||||
if event.isCancelled():
|
if event.isCancelled():
|
||||||
return
|
return
|
||||||
|
|
||||||
try:
|
try:
|
||||||
crashtest = data[playerid].items()
|
crashtest = data[playerid].items()
|
||||||
except KeyError:
|
except KeyError:
|
||||||
return
|
return
|
||||||
|
|
||||||
for alias, value in data[playerid].items():
|
for alias, value in data[playerid].items():
|
||||||
event.setMessage(event.getMessage().replace(alias, value))
|
event.setMessage(event.getMessage().replace(alias, value))
|
||||||
|
if (event.getPlayer().hasPermission('essentials.chat.color')):
|
||||||
|
event.setMessage(colorify(event.getMessage()))
|
||||||
|
if (len(event.getMessage()) > max_overall_length) and (not sender.hasPermission(exceed_overall_length)):
|
||||||
|
event.setCancelled(True)
|
||||||
|
msg(sender, "&7The message generated was too long and was not sent " +
|
||||||
|
"but it would've looked like that:")
|
||||||
|
msg(sender, event.getMessage())
|
||||||
|
|||||||
Reference in New Issue
Block a user