remove try/except. fix code if it doesn't work ;)

This commit is contained in:
jomo
2014-07-27 23:55:15 +02:00
parent 180680422a
commit 08cae3213e
4 changed files with 137 additions and 155 deletions

View File

@@ -10,7 +10,7 @@ def adminchat(sender, msg):
name = "&7{unknown}" name = "&7{unknown}"
try: try:
name = sender.getDisplayName() name = sender.getDisplayName()
except: except AttributeError:
name = sender.getName() name = sender.getName()
broadcast(ac_permission, "%s &9%s&8: &b%s" % (ac_prefix, name, msg)) broadcast(ac_permission, "%s &9%s&8: &b%s" % (ac_prefix, name, msg))

View File

@@ -10,43 +10,40 @@ cg_toggle_list = []
@hook.command("chatgroup") @hook.command("chatgroup")
def on_chatgroup_command(sender, args): def on_chatgroup_command(sender, args):
try: plugin_header(sender, "ChatGroups")
plugin_header(sender, "ChatGroups") sender_id = uid(sender)
sender_id = uid(sender) if len(args) == 1 and args[0] == "leave":
if len(args) == 1 and args[0] == "leave": if sender_id in groups.keys():
if sender_id in groups.keys(): groupchat(sender, "left the group", True)
groupchat(sender, "left the group", True) group = groups[sender_id]
group = groups[sender_id] del(groups[sender_id])
del(groups[sender_id])
save_groups()
else:
msg(sender, "&aYou can't leave no group, derp!")
elif len(args) == 1 and args[0] == "info":
if sender_id in groups.keys():
group = groups[sender_id]
msg(sender, "&aCurrent chatgroup: %s" % group)
users = []
for uuid, ugroup in groups.iteritems():
if ugroup == group:
usr = server.getPlayer(juuid(uuid))
if usr:
users.append(usr.getDisplayName())
msg(sender, "&aUsers in this group:")
msg(sender, "&a%s" % ", ".join(users))
else:
msg(sender, "&aYou're in no chatgroup.")
elif len(args) == 2 and args[0] == "join":
groups[sender_id] = args[1]
groupchat(sender, "joined the group", True)
save_groups() save_groups()
msg(sender, "&aYour chatgroup is set to '%s'" % args[1])
msg(sender, "&aUse chat like '&e%s<message>' to send messages to this group." % cg_key)
else: else:
msg(sender, "&e/chatgroup join <name>") msg(sender, "&aYou can't leave no group, derp!")
msg(sender, "&e/chatgroup leave") elif len(args) == 1 and args[0] == "info":
msg(sender, "&e/chatgroup info") if sender_id in groups.keys():
except Exception, e: group = groups[sender_id]
error(e) msg(sender, "&aCurrent chatgroup: %s" % group)
users = []
for uuid, ugroup in groups.iteritems():
if ugroup == group:
usr = server.getPlayer(juuid(uuid))
if usr:
users.append(usr.getDisplayName())
msg(sender, "&aUsers in this group:")
msg(sender, "&a%s" % ", ".join(users))
else:
msg(sender, "&aYou're in no chatgroup.")
elif len(args) == 2 and args[0] == "join":
groups[sender_id] = args[1]
groupchat(sender, "joined the group", True)
save_groups()
msg(sender, "&aYour chatgroup is set to '%s'" % args[1])
msg(sender, "&aUse chat like '&e%s<message>' to send messages to this group." % cg_key)
else:
msg(sender, "&e/chatgroup join <name>")
msg(sender, "&e/chatgroup leave")
msg(sender, "&e/chatgroup info")
@hook.command("cgt") @hook.command("cgt")
@@ -62,7 +59,6 @@ def on_cgt_command(sender, args):
def groupchat(sender, message, ann = False): def groupchat(sender, message, ann = False):
#try:
group = groups.get(uid(sender)) group = groups.get(uid(sender))
if group == None: if group == None:
msg(sender, "&cYou are not in a group!") msg(sender, "&cYou are not in a group!")
@@ -75,8 +71,7 @@ def groupchat(sender, message, ann = False):
info("[ChatGroups] %s (%s): %s" % (sender.getDisplayName(), group, message)) info("[ChatGroups] %s (%s): %s" % (sender.getDisplayName(), group, message))
for receiver in server.getOnlinePlayers(): for receiver in server.getOnlinePlayers():
groups.get(uid(receiver)) == group and msg(receiver, mesg) groups.get(uid(receiver)) == group and msg(receiver, mesg)
#except Exception, e:
# error(e)
def save_groups(): def save_groups():

View File

@@ -12,19 +12,16 @@ def print_help(sender):
def scan_chunks(amount): def scan_chunks(amount):
try: global lagchunks
global lagchunks chunks = []
chunks = [] for world in bukkit.Bukkit.getServer().getWorlds():
for world in bukkit.Bukkit.getServer().getWorlds(): for chunk in world.getLoadedChunks():
for chunk in world.getLoadedChunks(): if len(chunk.getEntities()) >= amount:
if len(chunk.getEntities()) >= amount: ents = chunk.getEntities()
ents = chunk.getEntities() # [0]world [1]X [2]Y [3]Z [4]amount
# [0]world [1]X [2]Y [3]Z [4]amount chunks.append([chunk.getWorld(), int(ents[-1].getLocation().getX()), int(ents[0].getLocation().getY()), int(ents[0].getLocation().getZ()), len(ents)])
chunks.append([chunk.getWorld(), int(ents[-1].getLocation().getX()), int(ents[0].getLocation().getY()), int(ents[0].getLocation().getZ()), len(ents)]) chunks.sort(key = lambda entry: entry[4], reverse = True)
chunks.sort(key = lambda entry: entry[4], reverse = True) lagchunks = chunks
lagchunks = chunks
except Exception, e:
error(e)
def list_chunks(sender): def list_chunks(sender):
@@ -41,26 +38,23 @@ def tp_chunk(sender, id):
@hook.command("lagchunks") @hook.command("lagchunks")
def on_lagchunks_command(sender, args): def on_lagchunks_command(sender, args):
try: if sender.hasPermission("utils.lagchunks"):
if sender.hasPermission("utils.lagchunks"): plugin_header(sender, "Lagchunks")
plugin_header(sender, "Lagchunks") global lagchunks
global lagchunks if len(args) == 1 and args[0].isdigit() and int(args[0]) > 0:
if len(args) == 1 and args[0].isdigit() and int(args[0]) > 0: amount = args[0]
amount = args[0] msg(sender, "&aChunks with at least &b%s &aentities:" % amount, )
msg(sender, "&aChunks with at least &b%s &aentities:" % amount, ) scan_chunks(int(amount))
scan_chunks(int(amount)) list_chunks(sender)
list_chunks(sender) elif len(args) == 1 and args[0].lower() == "list":
elif len(args) == 1 and args[0].lower() == "list": list_chunks(sender)
list_chunks(sender) elif len(args) == 2 and args[0].lower() == "tp" and args[1].isdigit() and int(args[1]) <= len(lagchunks)-1:
elif len(args) == 2 and args[0].lower() == "tp" and args[1].isdigit() and int(args[1]) <= len(lagchunks)-1: if isinstance(sender, Player):
if isinstance(sender, Player): tp_chunk(sender, int(args[1]))
tp_chunk(sender, int(args[1]))
else:
msg(sender, "&cOnly players can do this!")
else: else:
print_help(sender) msg(sender, "&cOnly players can do this!")
else: else:
noperm(sender) print_help(sender)
return True else:
except Exception, e: noperm(sender)
error(e) return True

157
mentio.py
View File

@@ -11,101 +11,94 @@ colors_reg = reg_compile(u"\u00A7[\\da-fk-or]")
@hook.event("player.AsyncPlayerChatEvent", "high") @hook.event("player.AsyncPlayerChatEvent", "high")
def onChat(event): def onChat(event):
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()
for recipient in list(recipients): for recipient in list(recipients):
listeners = mentions[str(recipient.getUniqueId())] listeners = mentions[str(recipient.getUniqueId())]
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]
isMentioned = False isMentioned = False
if recipient.getName().lower() in word.lower(): # is the player's full ign in the list if recipient.getName().lower() in word.lower(): # is the player's full ign in the list
isMentioned = True isMentioned = True
if word.lower() in [i.lower() for i in listeners]: # is the word in the listeners list if word.lower() in [i.lower() for i in listeners]: # is the word in the listeners list
isMentioned = True isMentioned = True
if isMentioned: if isMentioned:
# join all color codes used upto this word # join all color codes used upto this word
colors = "".join(colors_reg.findall("".join(words[:i+1]))) colors = "".join(colors_reg.findall("".join(words[:i+1])))
# highlight word containing mention, then apply all previous color codes # highlight word containing mention, then apply all previous color codes
rec_words[i] = colorify("&r&a&n") + stripcolors(word) + colorify("&r") + colors rec_words[i] = colorify("&r&a&n") + stripcolors(word) + colorify("&r") + colors
# player was mentioned # player was mentioned
if rec_words != words: if rec_words != words:
try: try:
recipients.remove(recipient) # don't send original message recipients.remove(recipient) # don't send original message
except: except:
# list might not be mutable, ignoring. Receiver will get the message twice # list might not be mutable, ignoring. Receiver will get the message twice
pass pass
message = " ".join([sender.getDisplayName(), arrow] + rec_words) message = " ".join([sender.getDisplayName(), arrow] + rec_words)
msg(recipient, message, usecolor = False) msg(recipient, message, usecolor = False)
recipient.playSound(recipient.getLocation(), "liquid.lavapop", 1, 2) recipient.playSound(recipient.getLocation(), "liquid.lavapop", 1, 2)
except:
error("Failed to handle PlayerChatEvent:")
error(print_traceback())
@hook.command("listen") @hook.command("listen")
def onListenCommand(sender, args): def onListenCommand(sender, args):
try: currWords = []
currWords = [] if str(sender.getUniqueId()) in mentions.keys():
if str(sender.getUniqueId()) in mentions.keys(): currWords = mentions[str(sender.getUniqueId())]
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: if len(args[1].lower()) > 16:
msg(sender, "&cThis word is longer than 16 characters. Pick a shorter one!") 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[str(sender.getUniqueId())] = 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()+"'!")
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() saveMentions()
return True msg(sender, "&eYou are no longer listening for '&2"+args[1].lower()+"&e'!")
# /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"+args[1].lower()+"&e'!")
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)
else: else:
msg(sender, "&eNobody calls you %s &efor some particular reason? Too long? Add some aliases!\n\n" % sender.getDisplayName()) msg(sender, "&cWe can't remove something that doesn't exist! Try &6/listen list")
msg(sender, "&6/listen add <word>") return True
msg(sender, "&6/listen del <word>")
msg(sender, "&6/listen list") # /listen list
except Exception, e: elif len(args) == 1 and args[0].lower() == "list":
error(e) msg(sender, "&6Words you're listening for:")
for word in currWords:
msg(sender, "&c- &3"+word)
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")
def saveMentions(): def saveMentions():