Updated event variable

This commit is contained in:
PixelSergey
2015-10-16 17:37:52 +03:00
parent ed27cdf561
commit b9293d7be2
+100 -100
View File
@@ -1,100 +1,100 @@
# Chat Aliasing plugin by Curs3d # # Chat Aliasing plugin by Curs3d #
################################## ##################################
# Allows users to alias words, # Allows users to alias words,
# so that when they send a # so that when they send a
# message in chat, it gets # message in chat, it gets
# replaced by their specified # replaced by their specified
# word. Configuration of this # word. Configuration of this
# plugin is in the "gnl" # plugin is in the "gnl"
# (general) tag of the JSON # (general) tag of the JSON
# file named "aliases". The # file named "aliases". The
# file is generated if not # file is generated if not
# present. Set values to -1 # present. Set values to -1
# for unlimited values. # for unlimited values.
from helpers import * from helpers import *
def safe_open_json(): def safe_open_json():
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 = {"gnl":{"max_len":"35","max_entries":"10"}}
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 False return False
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, "This is a plugin that allows you to type in chat and have words replaced by other ones automatically!")
msg(sender, "\nCommands:") msg(sender, "\nCommands:")
msg(sender, "/alias <word>: removes <word> from aliased words. Use * to remove all aliased words.") msg(sender, "/alias <word>: removes <word> from aliased words. Use * to remove all aliased words.")
msg(sender, "/alias <word> <others...>: Will change <word> to <others...> in chat") msg(sender, "/alias <word> <others...>: Will change <word> to <others...> in chat")
msg(sender, "\nYour Aliases:") msg(sender, "\nYour Aliases:")
data = safe_open_json() data = safe_open_json()
try: try:
for alias, value in data[sender.getName()].items(): for alias, value in data[sender.getName()].items():
msg(sender, "%s ==> %s" % (alias, value)) msg(sender, "%s ==> %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] == "*":
data[sender.getName()].clear() data[sender.getName()].clear()
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, "ALL alias data successfuly removed!")
return True return True
if data[sender.getName()].pop(args[0], None) is None: if data[sender.getName()].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, "Could not remove: alias not present!")
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, "Alias for %s successfuly 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 data["gnl"]["max_len"] > 0: if len(alias) > int(data["gnl"]["max_len"]) and data["gnl"]["max_len"] > 0:
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, "Please do not alias long words/sentences.")
return True return True
if len(data[sender.getName()]) >= int(data["gnl"]["max_entries"]) and data["gnl"]["max_entries"] > 0: if len(data[sender.getName()]) >= int(data["gnl"]["max_entries"]) and data["gnl"]["max_entries"] > 0:
plugin_header(recipient = sender, name = "Chat Alias") plugin_header(recipient = sender, name = "Chat Alias")
msg(sender, "You have reached the maximum amount of alias entries! Sorry!") msg(sender, "You have reached the maximum amount of alias entries! Sorry!")
return True return True
except KeyError: except KeyError:
data[sender.getName()] = {} data[sender.getName()] = {}
data[sender.getName()][args[0]] = alias data[sender.getName()][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, "Chat Alias %s ==> %s successfully created!" % (args[0], alias))
return True return True
else: else:
return False return False
@hook.event("player.AsyncPlayerChatEvent", "High") @hook.event("player.AsyncPlayerChatEvent", "High")
def on_player_chat(e): def on_player_chat(event):
if e.isCancelled(): if event.isCancelled():
return return
data = safe_open_json() data = safe_open_json()
for alias, value in data[e.getPlayer().getName()].items(): for alias, value in data[event.getPlayer().getName()].items():
e.setMessage(e.getMessage().replace(alias, value)) event.setMessage(event.getMessage().replace(alias, value))