Python-Plugin-Loader icon indicating copy to clipboard operation
Python-Plugin-Loader copied to clipboard

Nested events are passed as parent event

Open Dico200 opened this issue 10 years ago • 0 comments

If an event nests another event, for example PlayerDeathEvent, which extends EntityDeathEvent, an instance of EntityDeathEvent is passed to the plugin.

hooking into "entity.PlayerDeathEvent" is fine, however an error is thrown on load. I'm printing exc_info here:

[02:27:34 ERROR]: [RedstonerUtils] Traceback (most recent call last):
  File "C:\Users\Dico\Documents\Servers\Test Server\plugins\redstoner-utils.py.dir\deathmessages.py", lin
    broadcastToWorlds(normal_survival_worlds, event.getDeathMessage())
AttributeError: 'org.bukkit.event.entity.EntityDeathEvent' object has no attribute 'getDeathMessage'

Here's the code I'm using:

from helpers import *

normal_survival_worlds = ("Survival_1", "Survival_1_nether", "Survival_1_the_end", "world")
trusted_survival_worlds = ("TrustedSurvival_1", "TrustedSurvival_1_nether", "TrustedSurvival_1_the_end")

@hook.event("entity.PlayerDeathEvent", "low")
def on_death(event):
    try:
        info("someone died")
        world = event.getEntity().getWorld()
        if world.getGameRuleValue("showDeathMessages") == "true":
            name = world.getName()
            if name in normal_survival_worlds:
                broadcastToWorlds(normal_survival_worlds, event.getDeathMessage())    
            elif name in trusted_survival_worlds:
                broadcastToWorlds(trusted_survival_worlds, event.getDeathMessage())
    except:
        error(trace())

def broadcastToWorlds(worlds, message):
    info("broadcasting...")
    try:
        for player in (list(world.getPlayers()) for world in (server.getWorld(name) for name in worlds) if world != None):
            msg(player, message)
    except:
        error(trace())

You're not able to see all imported functions but you should be able to read everything.

Note that an instance of EntityDeathEvent is passed to the function while PlayerDeathEvent is listened for.

Dico200 avatar May 03 '15 00:05 Dico200