Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nested events are passed as parent event #18

Open
Dico200 opened this issue May 3, 2015 · 0 comments
Open

Nested events are passed as parent event #18

Dico200 opened this issue May 3, 2015 · 0 comments

Comments

@Dico200
Copy link

Dico200 commented May 3, 2015

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant