rewrite big parts of mentio

This commit is contained in:
jomo
2014-08-07 00:59:13 +02:00
parent 28d0182496
commit a73b1da7ca

160
mentio.py
View File

@@ -3,10 +3,14 @@ from re import compile as reg_compile
from traceback import format_exc as print_traceback
mentions = open_json_file("mentio", {})
mentions = open_json_file("mentio", {}) # contains a list of keywords for each player (uuid)
max_amount = 3
arrow = colorify(u"&r&7\u2192&r")
colors_reg = reg_compile(u"\u00A7[\\da-fk-or]")
colors_reg = reg_compile(u"\u00A7[\\da-fk-or]") # finds color codes
def saveMentions():
save_json_file("mentio", mentions)
@hook.event("player.AsyncPlayerChatEvent", "high")
@@ -14,26 +18,31 @@ def onChat(event):
if not event.isCancelled():
sender = event.getPlayer()
words = event.getMessage().split(" ")
recipients = event.getRecipients()
recipients = event.getRecipients() # set of <Player>, may be a lazy or unmodifiable collection
for recipient in list(recipients):
listeners = mentions[str(recipient.getUniqueId())]
recuid = uid(recipient)
if recuid in mentions:
keywords = mentions[uid(recipient)]
else:
# player
keywords = [recipient.getName(), stripcolors(recipient.getDisplayName())]
rec_words = words[:] # copy
for i in range(len(rec_words)):
word = rec_words[i]
for index, word in enumerate(rec_words):
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
if word.lower() in keywords: # is the word in the keywords list
isMentioned = True
if isMentioned:
# join all color codes used upto this word
colors = "".join(colors_reg.findall("".join(words[:i+1])))
# highlight word containing mention, then apply all previous color codes
rec_words[i] = colorify("&r&a&n") + stripcolors(word) + colorify("&r") + colors
colors = "".join(colors_reg.findall("".join(words[:index+1])))
# highlight the word containing mention, then apply all previous color codes
rec_words[index] = colorify("&r&a&n") + stripcolors(word) + colorify("&r") + colors
# No need to
break
# player was mentioned
if rec_words != words:
@@ -47,59 +56,78 @@ def onChat(event):
recipient.playSound(recipient.getLocation(), "liquid.lavapop", 1, 2)
@hook.command("listen")
def onListenCommand(sender, args):
currWords = []
if str(sender.getUniqueId()) in mentions.keys():
currWords = mentions[str(sender.getUniqueId())]
# /listen add <word>
if len(args) == 2 and args[0].lower() == "add":
if len(currWords) >= max_amount:
msg(sender, "&cYou are already listening for %s words! Try &6/listen del <word>" % max_amount)
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:
msg(sender, "&cYou are already listening for this word! Try &6/listen list")
return True
if args[1].lower() is sender.getName():
msg(sender, "&cYou are always listening for your full ingame name by default")
currWords.append(args[1].lower())
mentions[str(sender.getUniqueId())] = currWords
msg(sender, "&aYou are now listening for '&2%s'!" % args[1].lower())
saveMentions()
return True
# /listen del <word>
elif len(args) == 2 and args[0].lower() == "del":
if len(currWords) <= 0:
msg(sender, "&cYou are currently listening for no words! Try &6/listen add <word>")
return True
success = False
for word in currWords[:]:
if word.lower() == args[1].lower():
currWords.remove(word.lower())
mentions[str(sender.getUniqueId())] = currWords
success = True
if success == True:
saveMentions()
msg(sender, "&eYou are no longer listening for '&2%s&e'!" % args[1].lower())
else:
msg(sender, "&cWe can't remove something that doesn't exist! Try &6/listen list")
return True
# /listen list
elif len(args) == 1 and args[0].lower() == "list":
msg(sender, "&6Words you're listening for:")
for word in currWords:
msg(sender, "&c- &3"+word)
def get_keywords(player):
sender_id = uid(player)
if sender_id in mentions.keys():
keywords = mentions[sender_id]
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 del <word>")
msg(sender, "&6/listen list")
keywords = []
return keywords
def saveMentions():
save_json_file("mentio", mentions)
def add_keyword(sender, args):
keywords = get_keywords(sender)
new_word = stripcolors(args[1].lower())
if len(keywords) >= max_amount:
msg(sender, "&cYou are already listening for %s words! Try &6/mentio del <word>" % max_amount)
return True
if len(new_word) > 20:
msg(sender, "&cThis word is longer than 20 characters. Pick a shorter one!")
return True
if new_word in keywords:
msg(sender, "&cYou are already listening for this word! Try &6/mentio list")
return True
keywords.append(new_word)
if keywords:
mentions[uid(sender)] = keywords
msg(sender, "&aYou are now listening for '&2%s'!" % new_word)
saveMentions()
return True
def del_keyword(sender, args):
keywords = get_keywords(sender)
del_word = stripcolors(args[1].lower())
if not keywords:
msg(sender, "&cYou are currently listening for no words! Try &6/mentio add <word>")
return
if del_word in keywords:
keywords.remove(del_word)
mentions[uid(sender)] = keywords
saveMentions()
msg(sender, "&aYou are no longer listening for '&2%s&e'!" % del_word)
else:
msg(sender, "&cWe can't remove something that doesn't exist! Try &6/mentio list")
@hook.command("mentio")
def onListenCommand(sender, args):
plugin_header(sender, "Mentio")
argnum = len(args)
if argnum:
cmd = args[0].lower()
# /mentio add <word>
if argnum == 2 and cmd == "add":
add_keyword(sender, args)
# /mentio del <word>
elif argnum == 2 and cmd == "del":
del_keyword(sender, args)
# /mentio list
elif argnum == 1 and cmd == "list":
msg(sender, "&aWords you're listening for:")
for word in get_keywords(sender):
msg(sender, "&c- &3%s" % word)
else:
msg(sender, "&a/mentio add <word>")
msg(sender, "&a/mentio del <word>")
msg(sender, "&a/mentio list")