Preventing SOME NOT ALL entity destruction during an explosion

I’ve been struggling with this one a while: I want to prevent the destruction of some entities (ie, particular item frames) but not interfere with destruction/damage of others by the explosion.

The ExplosionEvent.Detonate provides a series of block-snapshot transactions, as well as a list of entities in the explosion, but also a list of entity-snapshots so i think thats the key but im not seeing it.

I can invalidate particular block damage during that event, by invalidating the transaction - but i see no way to invalidate an entity inclusion.

I can make a set/list of the entities from that explosion that i dont want destroyed, but there is no downstream event I can see that triggers the destruction of the frames (and frames dont throw entity damage events, they just destruct) until the DestructEntity event - at which point, its too late, because that event is not cancellable - if it was, i could use the set/list of entities i fished out and nix that event if the destructed entity is in the set…

I dont want to cancel the detonation event, just stop the destruction of particular entities in it. The AffectEntity generic method returns the list of entities, not individuals, and cancelling the event prevents all entity destruction in the detonation…

What is the role of the entitysnapshot in the detonate event - its a second way to get the same information as the entity list, but, there has to be more to it. Is there a way to use that entitysnapshot information to prevent the destruction.

======
A possible workaround I see may be to tag a list of entities in the detonation other than the one i want to be unaffected, and cancel the event in the affectentities if ANY entity is a frame, then process and destruct that list of entities i made. However, not all entities are ‘destroyed’ in an explosion, a player may take damage without dying, but is still listed as an impacted entity in the detonate - so default destructing the player would be wrong (or other mobs that take some damage without dying) So its not really a workable workaround yet.

Does anyone have ideas on how to spare specific entities from the list only during a detonation, or use that entitysnapshot to recreate them as if they were not destroyed etc (I assume the snapshot would contain custom data assigned to the entity??)

AGGH!

Its the filterEntities() method - and it works kinda oppositely to what the suggestion would be.

getEntities() list will be destroyed when the event finishes.
filterEntities() is used not to filter out entities to exclude, but to filter for the entities to KEEP destroying/impacting.

So making a list of the entities meeting critiera to spare, then filter the entities where they are not in the list of ones to spare :: The ones satisfying the filter condition are the ones that REMAIN, not the ones removed-out (filtered-out)

Instead of calling filterEntities you can write:

public void onEvent(MyEntityEvent e) {
Iterator<Entity> it = e.getEntities().iterator();
while (it.hasNext()) {
   // Do something ?
   // Do nothing
   // Or remove the entity from being affected by the event using
  it.remove();
}
}

The filterEntities method makes sense because it filters entities that will be affected by the event, it work like that for every EntityEvents.
The ExplosionEvent is a bit confusing because every entities that are kept in the event will be destroyed in the game, and vice versa

Just thinking out loud here but couldn’t you listen to the DestructEntityEvent and check if cause is some kind of explosion?

Yes, yes he couldn’t…

It shouldn’t be a problem that filter filters entities to include instead of exclude in the event, just invert whatever predicate you would otherwise pass.

Thats what I did… I just posted my solution afterwards for posterity sake of searches.

1 Like