Trying to get the TNT that caused the damage. getNamedCauses returns Source - net.minecraft.util.DamageSource, and that’s where I’m stuck. The following code throws cast exceptions. I feel like i’m missing something really stupid and I’ve been staring at it too long to notice.
if(!event.getCause().first(DamageSource.class).isPresent()){
return;
}
DamageSource source = event.getCause().first(DamageSource.class).get();
if(!(source.isExplosive())){
return;
}
Explosion explosion = (Explosion) source;
The DamageSource
is never an Explosion
. Simply checking if isExplosive()
returns true
means that the source is “explosive”, so it can mean many things, such as a PrimedTNT
or Creeper
or even just an Explosive
, all of which are Entity
types that you can find in an EntityDamageSource.getSource()
.
That All makes sense but what I don’t understand is why checking if EntityDamageSource is present always returns false. That was the reason I was checking DamageSource because it’s the only thing I can seem to get for a cause
Seems like because Minecraft discards the TNT and only uses a raw DamageSource
with little to no other information. I’ll see about the viability of possibly making an ExplosionDamageSource
, might be a little bit though.
In short, yes, the only DamageSource
available if a Player
was not involved according to vanilla Minecraft mechanics, would be a DamageSource
. Otherwise, if a player was involved, like a Creeper
based explosion, then it would be appropriately an EntityDamageSource
of an explosion.