Yes Quick question for my fellow developers. I’m trying to code an event that’ll happen during a zombie death…can someone get me started ? i believe it uses the Death code. But other than that i’m Kind of stumped thank you
Just like any other event, you create a listener for it
@Listener
public void onEntityDeath(DestructEntityEvent.Death event) {
// Some logic here
}
Use event.getTargetEntity()
to get the entity that’s just died
AMAZING!!! THANK YOU LOL mine is way tooo big that simplifies things thanks
how would i grab a player that’s the cause?
event.getCause right? but what else?
if(!event.getCause().first(Player.class).isPresent()){
return;
}
Player player = event.getCause().first(Player.class).get();
Like in other events you can query the cause for certain types.
To see if a player caused the death, you do:
@Listener
public void onEntityDeath(DestructEntityEvent.Death event) {
Optional<Player> optPlayer = event.getCause().first(Player.class);
if (optPlayer.isPresent()) {
Player player = optPlayer.get();
// The player caused the entity to die
} else {
// The entity died from a non-player cause
}
}
You can also use the new event filtering
@Listener
public void onEntityDeath(DestructEntityEvent.Death event, @First Player player) {
// Only gets called when the player was the cause
}
these…don’t seem to work
I’m going to take a guess you need to update Sponge but without a full log it’s hard to tell
Then again it looks like your trying to initialize an object that doesn’t exist. Let’s see your code
ok so
@Listener public void onEntityDeath(DestructEntityEvent.Death event, @First Player player) { player.sendMessage(Texts.of("Ha you Dirty Man")); }
Should send the player responsible a message yes?
cause right now on version 951 it does absolutely nothing
I think the killer appears as a DamageSource, more precisely an EntityDamageSource
So you’d have to get the entity from the EntityDamageSource
and check if it is a Player
Yes that’s it @Saladoc is correct, it’s an EntityDamageSource
It is often useful to print out the event.getCause()
when debugging to see what’s actually happening
Cause[{Name=Source, Object={EntityDamageSource{Name=player, Type=attack, Source=EntityPlayerMP['Player740'/398, l='Test1', x=223.66, y=65.79, z=78.30]}}}, {Name=Victim, Object={EntityChicken['Chicken'/702, l='Test1', x=224.23, y=66.26, z=75.37]}}
]
ok so slight issue got the main part fixed…and is working now…but when i try to use
if (event.getTargetEntity().equals(EntityTypes.Zombie)){
//this code doesn’t work
}
Thoughts?
you want to use event.getTargetEntity().getType().equals(EntityTypes.ZOMBIE)
Remember, Entity
is not the same as EntityType