diff --git a/helpers.py b/helpers.py index 13e90bf..bb810ec 100644 --- a/helpers.py +++ b/helpers.py @@ -31,6 +31,9 @@ def broadcast(perm, text): def colorify(text): return sub("&(?=[?\\da-fk-or])", u"\u00A7", "%s" % text) +def stripcolors(text): + return sub(u"\u00A7[\\da-fk-or]", "", "%s" % text) + def safetp(player, world, x, y, z, yaw = 0, pitch = 0): tpblock = Location(world, x, y, z).getBlock() if (tpblock.isEmpty() and tpblock.getRelative(bblock.BlockFace.UP).isEmpty()) or y > 255: diff --git a/main.py b/main.py index 4f1ddd3..9876a1b 100644 --- a/main.py +++ b/main.py @@ -30,7 +30,7 @@ def onDisable(): log("Loading RedstonerUtils...") # Import all modules -modules = ["misc", "adminchat", "lagchunks", "reports", "chatgroups", "webtoken", "saylol", "skullclick"] +modules = ["misc", "adminchat", "lagchunks", "reports", "chatgroups", "webtoken", "saylol", "skullclick", "mentio"] mod = {} for module in modules: try: diff --git a/mentio.py b/mentio.py index 4ccaa9c..525b6d6 100644 --- a/mentio.py +++ b/mentio.py @@ -1,18 +1,32 @@ #pylint: disable=F0401 from helpers import * +from re import compile as reg_compile + +arrow = colorify(u"&r&7\u2192&r") +regex = reg_compile(u"\u00A7[\\da-fk-or]") -mio_permission = "utils.mio" - -@hook.event("player.PlayerChatEvent", "normal") +@hook.event("player.AsyncPlayerChatEvent", "normal") def onChat(event): - symbol = u"\u272a" - sender = event.getPlayer() - messages = event.getMessage() - messagesList = messages.split(" ") - for message in messagesList: - for recipient in server.getOnlinePlayers().tolist(): - if message[:3].lower() in recipient.getName().lower() and len(message) > 2: - msg(recipient, "&6" + symbol + " &f%s &6mentioned you" % sender.getDisplayName()) - # Couldn't figure out how to do this - # recipient.playSound(recipient.getLocation(), Sound.CHICKEN_EGG_POP, 1, 1) + if not event.isCancelled(): + sender = event.getPlayer() + words = event.getMessage().split(" ") + recipients = event.getRecipients() + + for recipient in recipients: + rec_words = words[:] # copy + for i in range(len(rec_words)): + word = rec_words[i] + if recipient.getName().lower() in word.lower(): + 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 + + # player was mentioned + if rec_words != words: + try: # list might not be mutable + recipients.remove(recipient) # don't send original message + except: + pass + message = " ".join([sender.getDisplayName(), arrow] + rec_words) + msg(recipient, message, usecolor = False) + recipient.playSound(recipient.getLocation(), "mob.chicken.plop", 1, 0)