Merge branch 'dev' of bitbucket.org:redstonesheep/redstoner-utils into dev

This commit is contained in:
jomo
2014-07-25 00:05:51 +02:00

View File

@@ -4,10 +4,10 @@ from re import compile as reg_compile
from traceback import format_exc as print_traceback from traceback import format_exc as print_traceback
mentio_filename = "plugins/redstoner-utils.py.dir/files/mentio.json" mentio_filename = "plugins/redstoner-utils.py.dir/files/mentio.json"
mentions = {} mentions = {}
max_amount = 3 max_amount = 3
arrow = colorify(u"&r&7\u2192&r") arrow = colorify(u"&r&7\u2192&r")
regex = reg_compile(u"\u00A7[\\da-fk-or]") regex = reg_compile(u"\u00A7[\\da-fk-or]")
try: try:
mentions = json.loads(open(mentio_filename).read()) mentions = json.loads(open(mentio_filename).read())
@@ -16,21 +16,30 @@ except Exception, e:
@hook.event("player.AsyncPlayerChatEvent", "high") @hook.event("player.AsyncPlayerChatEvent", "high")
def on_chat(event): def onChat(event):
try: try:
if not event.isCancelled(): if not event.isCancelled():
sender = event.getPlayer() sender = event.getPlayer()
words = event.getMessage().split(" ") words = event.getMessage().split(" ")
recipients = event.getRecipients() recipients = event.getRecipients()
listeners = mentions[sender.getUniqueId()]
for recipient in list(recipients): for recipient in list(recipients):
rec_words = words[:] # copy rec_words = words[:] # copy
for i in range(len(rec_words)): for i in range(len(rec_words)):
word = rec_words[i] word = rec_words[i]
if recipient.getName().lower() in word.lower(): isMentioned = False
if recipient.getName().lower() in word.lower(): # is the player's full ign in the list
isMentioned = True
if word.lower() in [i.lower() for i in listeners]: # is the word in the listeners list
isMentioned = True
if isMentioned:
colors = "".join(regex.findall("".join(words[:i+1]))) # join all color codes used upto this word colors = "".join(regex.findall("".join(words[:i+1]))) # join all color codes used upto this word
rec_words[i] = colorify("&r&a<&6") + stripcolors(word) + colorify("&r&a>&r") + colors # extra fancy highlight rec_words[i] = colorify("&r&a<&6") + stripcolors(word) + colorify("&r&a>&r") + colors # extra fancy highlight
# player was mentioned # player was mentioned
if rec_words != words: if rec_words != words:
try: try:
@@ -45,29 +54,30 @@ def on_chat(event):
error("Failed to handle PlayerChatEvent:") error("Failed to handle PlayerChatEvent:")
error(print_traceback()) error(print_traceback())
@hook.command("listen") @hook.command("listen")
def on_listen_command(sender, args): def onListenCommand(sender, args):
try: try:
currWords = [] currWords = []
if uid(sender) in mentions.keys(): if str(sender.getUniqueId()) in mentions.keys():
currWords = mentions[uid(sender)] currWords = mentions[str(sender.getUniqueId())]
# /listen add <word> # /listen add <word>
if len(args) == 2 and args[0].lower() == "add": if len(args) == 2 and args[0].lower() == "add":
if len(currWords) >= max_amount: if len(currWords) >= max_amount:
msg(sender, "&cYou are already listening for %s words! Try &6/listen del <word>" % max_amount) msg(sender, "&cYou are already listening for %s words! Try &6/listen del <word>" % max_amount)
return True return True
if len(args[1].lower()) > 16:
msg(sender, "&cThis word is longer than 16 characters. Pick a shorter one!")
if args[1].lower() in currWords: if args[1].lower() in currWords:
msg(sender, "&cYou are already listening for this word! Try &6/listen list") msg(sender, "&cYou are already listening for this word! Try &6/listen list")
return True return True
if args[1].lower() is sender.getName(): if args[1].lower() is sender.getName():
msg(sender, "&cYou are always listening for your full ingame name by default") msg(sender, "&cYou are always listening for your full ingame name by default")
currWords.append(args[1].lower()) currWords.append(args[1].lower())
mentions[uid(sender)] = currWords mentions[str(sender.getUniqueId())] = currWords
msg(sender, "&aYou are now listening for '&2"+args[1].lower()+"'!") msg(sender, "&aYou are now listening for '&2"+args[1].lower()+"'!")
save_mentions() saveMentions()
return True return True
# /listen del <word> # /listen del <word>
elif len(args) == 2 and args[0].lower() == "del": elif len(args) == 2 and args[0].lower() == "del":
@@ -78,10 +88,10 @@ def on_listen_command(sender, args):
for word in currWords[:]: for word in currWords[:]:
if word.lower() == args[1].lower(): if word.lower() == args[1].lower():
currWords.remove(word.lower()) currWords.remove(word.lower())
mentions[uid(sender)] = currWords mentions[str(sender.getUniqueId())] = currWords
success = True success = True
if success == True: if success == True:
save_mentions() saveMentions()
msg(sender, "&eYou are no longer listening for '&2"+args[1].lower()+"&e'!") msg(sender, "&eYou are no longer listening for '&2"+args[1].lower()+"&e'!")
else: else:
msg(sender, "&cWe can't remove something that doesn't exist! Try &6/listen list") msg(sender, "&cWe can't remove something that doesn't exist! Try &6/listen list")
@@ -93,14 +103,14 @@ def on_listen_command(sender, args):
for word in currWords: for word in currWords:
msg(sender, "&c- &3"+word) msg(sender, "&c- &3"+word)
else: else:
msg(sender, "&eNobody calls you %s &efor some particular reason? Too long? Add some aliases!\n\n" % sender.getDisplayName())
msg(sender, "&6/listen add <word>") msg(sender, "&6/listen add <word>")
msg(sender, "&6/listen del <word>") msg(sender, "&6/listen del <word>")
msg(sender, "&6/listen list") msg(sender, "&6/listen list")
except Exception, e: except Exception, e:
error(e) error(e)
def saveMentions():
def save_mentions():
try: try:
mentio_file = open(mentio_filename, "w") mentio_file = open(mentio_filename, "w")
mentio_file.write(json.dumps(mentions)) mentio_file.write(json.dumps(mentions))