Cancelling AttackEntityEvent

I’m trying to cancelling the explosion event to prevent paintings and other “block entities” from being destroyed by it. So i wrote this listener to do that

@Listener
	public void onAttackEntity(AttackEntityEvent event, @First Explosive entity) {
		if(FlagUtils.isBlockEntity(event.getTargetEntity().getType()) || FlagUtils.isVehicle(event.getTargetEntity().getType())) {
			Region region = RegionUtils.getRegion(event.getTargetEntity().getLocation());
			if (region != null) {
				EnumRegionExplosion explosion = EnumRegionExplosion.OTHER_EXPLOSIONS;		
				if (FlagUtils.isExplosion(entity.getType())) // Check if a type of explosion handled
					explosion = FlagUtils.getExplosion(entity.getType());
				event.setCancelled(!region.getExplosionDestroy(explosion)); //It cancel the event but spams the console :/
			}
		}
	}

The event itself is cancelled, however the console gets spammed by tons of messages before the explosion “occur” (the explosive entity despawn but no actual explosion occur, meaning that the event has been cancelled). I’ve also noticed that if an EnderCrystal explodes, the listener doesn’t event get called :confused: This is what i get in console when the event is being cancelled

[20:30:25] [Server thread/ERROR] [Sponge]: /*****************************************************************************************************************************/
[20:30:25] [Server thread/ERROR] [Sponge]: /*                                          Exception occurred during a PhaseState                                           */
[20:30:25] [Server thread/ERROR] [Sponge]: /*****************************************************************************************************************************/
[20:30:25] [Server thread/ERROR] [Sponge]: /* Sponge's tracking system is very dependent on NOT throwing exceptions                                                     */
[20:30:25] [Server thread/ERROR] [Sponge]: /* randomly,sometimes it is inevitable. Unfortunately, an exception being printed                                            */
[20:30:25] [Server thread/ERROR] [Sponge]: /* now canbe very spammy, and as a result, cause logs to reach several hundred                                               */
[20:30:25] [Server thread/ERROR] [Sponge]: /* megabytesin size. Since there is an exception being thrown, it is advisable to                                            */
[20:30:25] [Server thread/ERROR] [Sponge]: /* report thislog to Sponge on GitHub.                                                                                       */
[20:30:25] [Server thread/ERROR] [Sponge]: /*****************************************************************************************************************************/
[20:30:25] [Server thread/ERROR] [Sponge]: /* The PhaseState having an exception: EntityTickPhase                                                                       */
[20:30:25] [Server thread/ERROR] [Sponge]: /* The PhaseContext:                                                                                                         */
[20:30:25] [Server thread/ERROR] [Sponge]: /*     - Owner: EntityPlayerMP['Minehendrix'/457, l='world', x=1113,43, y=75,53, z=3358,12]                                  */
[20:30:25] [Server thread/ERROR] [Sponge]: /*     - Notifier: EntityPlayerMP['Minehendrix'/457, l='world', x=1113,43, y=75,53, z=3358,12]                               */
[20:30:25] [Server thread/ERROR] [Sponge]: /*     - Source: EntityCreeper['Creeper'/6326, l='world', x=1112,70, y=71,00, z=3359,70]                                     */
[20:30:25] [Server thread/ERROR] [Sponge]: /*     - CapturedBlocks: CapturedBlocksSupplier{Captured=0}                                                                  */
[20:30:25] [Server thread/ERROR] [Sponge]: /*     - BlockItemDrops: BlockItemDropsSupplier{Captured=0}                                                                  */
[20:30:25] [Server thread/ERROR] [Sponge]: /*     - BlockItemEntityDrops: BlockItemEntityDropsSupplier{Captured=0}                                                      */
[20:30:25] [Server thread/ERROR] [Sponge]: /*     - CapturedItems: CapturedItemsSupplier{Captured=0}                                                                    */
[20:30:25] [Server thread/ERROR] [Sponge]: /*     - CapturedEntities: CapturedEntitiesSupplier{Captured=0}                                                              */
[20:30:25] [Server thread/ERROR] [Sponge]: /*     - CapturedItemStack: CapturedItemStackSupplier{Captured=0}                                                            */
[20:30:25] [Server thread/ERROR] [Sponge]: /*     - EntityItemDrops: null                                                                                               */
[20:30:25] [Server thread/ERROR] [Sponge]: /*     - EntityItemEntityDrops: null                                                                                         */
[20:30:25] [Server thread/ERROR] [Sponge]: /*     - BlockEntitySpawns: CapturedBlockEntitySpawnSupplier{Captured=0}                                                     */
[20:30:25] [Server thread/ERROR] [Sponge]: /*     - CapturedBlockPosition: null                                                                                         */
[20:30:25] [Server thread/ERROR] [Sponge]: /* org.spongepowered.asm.util.PrettyPrinter@40ea0944                                                                         */
[20:30:25] [Server thread/ERROR] [Sponge]: /*****************************************************************************************************************************/
[20:30:25] [Server thread/ERROR] [Sponge]: /* StackTrace:                                                                                                               */
[20:30:25] [Server thread/ERROR] [Sponge]: /* java.lang.RuntimeException: Unknown Exception During Phase                                                                */
[20:30:25] [Server thread/ERROR] [Sponge]: /*     org.spongepowered.common.event.tracking.PhaseTracker.printExceptionFromPhase(PhaseTracker.java:421)                   */
[20:30:25] [Server thread/ERROR] [Sponge]: /*     org.spongepowered.common.event.tracking.TrackingUtil.tickEntity(TrackingUtil.java:168)                                */
[20:30:25] [Server thread/ERROR] [Sponge]: /*     net.minecraft.world.WorldServer.redirect$onCallEntityUpdate$zlg000(WorldServer.java:2855)                             */
[20:30:25] [Server thread/ERROR] [Sponge]: /*     net.minecraft.world.World.func_72866_a(World.java:4138)                                                               */
[20:30:25] [Server thread/ERROR] [Sponge]: /*     net.minecraft.world.WorldServer.func_72866_a(WorldServer.java:831)                                                    */
[20:30:25] [Server thread/ERROR] [Sponge]: /*     net.minecraft.world.World.func_72870_g(World.java:1952)                                                               */
[20:30:25] [Server thread/ERROR] [Sponge]: /*     net.minecraft.world.World.func_72939_s(World.java:6557)                                                               */
[20:30:25] [Server thread/ERROR] [Sponge]: /*     net.minecraft.world.WorldServer.func_72939_s(WorldServer.java:2219)                                                   */
[20:30:25] [Server thread/ERROR] [Sponge]: /*     net.minecraft.server.MinecraftServer.func_71190_q(MinecraftServer.java:765)                                           */
[20:30:25] [Server thread/ERROR] [Sponge]: /*     net.minecraft.server.dedicated.DedicatedServer.func_71190_q(DedicatedServer.java:396)                                 */
[20:30:25] [Server thread/ERROR] [Sponge]: /*     net.minecraft.server.MinecraftServer.func_71217_p(MinecraftServer.java:666)                                           */
[20:30:25] [Server thread/ERROR] [Sponge]: /*     net.minecraft.server.MinecraftServer.run(MinecraftServer.java:524)                                                    */
[20:30:25] [Server thread/ERROR] [Sponge]: /*     java.lang.Thread.run(Unknown Source)                                                                                  */
[20:30:25] [Server thread/ERROR] [Sponge]: /* org.spongepowered.asm.mixin.injection.callback.CancellationException: The call func_70097_a is not cancellable.           */
[20:30:25] [Server thread/ERROR] [Sponge]: /*     org.spongepowered.asm.mixin.injection.callback.CallbackInfo.cancel(CallbackInfo.java:101)                             */
[20:30:25] [Server thread/ERROR] [Sponge]: /*     org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable.setReturnValue(CallbackInfoReturnable.java:106) */
[20:30:25] [Server thread/ERROR] [Sponge]: /*     net.minecraft.entity.EntityHanging.handler$onAttackEntityFrom$zdp000(EntityHanging.java:600)                          */
[20:30:25] [Server thread/ERROR] [Sponge]: /*     net.minecraft.entity.EntityHanging.func_70097_a(EntityHanging.java:186)                                               */
[20:30:25] [Server thread/ERROR] [Sponge]: /*     net.minecraft.world.Explosion.func_77278_a(Explosion.java:786)                                                        */
[20:30:25] [Server thread/ERROR] [Sponge]: /*     net.minecraft.world.WorldServer.func_72885_a(WorldServer.java:3048)                                                   */
[20:30:25] [Server thread/ERROR] [Sponge]: /*     net.minecraft.world.WorldServer.triggerInternalExplosion(WorldServer.java:2661)                                       */
[20:30:25] [Server thread/ERROR] [Sponge]: /*     org.spongepowered.common.interfaces.entity.explosive.IMixinExplosive.detonate(IMixinExplosive.java:49)                */
[20:30:25] [Server thread/ERROR] [Sponge]: /*     net.minecraft.entity.monster.EntityCreeper.redirect$onExplode$zgb003(SourceFile:685)                                  */
[20:30:25] [Server thread/ERROR] [Sponge]: /*     net.minecraft.entity.monster.EntityCreeper.func_146077_cc(SourceFile:248)                                             */
[20:30:25] [Server thread/ERROR] [Sponge]: /*     net.minecraft.entity.monster.EntityCreeper.func_70071_h_(SourceFile:158)                                              */
[20:30:25] [Server thread/ERROR] [Sponge]: /*     org.spongepowered.common.event.tracking.TrackingUtil.tickEntity(TrackingUtil.java:166)                                */
[20:30:25] [Server thread/ERROR] [Sponge]: /*     net.minecraft.world.WorldServer.redirect$onCallEntityUpdate$zlg000(WorldServer.java:2855)                             */
[20:30:25] [Server thread/ERROR] [Sponge]: /*     net.minecraft.world.World.func_72866_a(World.java:4138)                                                               */
[20:30:25] [Server thread/ERROR] [Sponge]: /*     net.minecraft.world.WorldServer.func_72866_a(WorldServer.java:831)                                                    */
[20:30:25] [Server thread/ERROR] [Sponge]: /*     net.minecraft.world.World.func_72870_g(World.java:1952)                                                               */
[20:30:25] [Server thread/ERROR] [Sponge]: /*     net.minecraft.world.World.func_72939_s(World.java:6557)                                                               */
[20:30:25] [Server thread/ERROR] [Sponge]: /*     net.minecraft.world.WorldServer.func_72939_s(WorldServer.java:2219)                                                   */
[20:30:25] [Server thread/ERROR] [Sponge]: /*     net.minecraft.server.MinecraftServer.func_71190_q(MinecraftServer.java:765)                                           */
[20:30:25] [Server thread/ERROR] [Sponge]: /*     net.minecraft.server.dedicated.DedicatedServer.func_71190_q(DedicatedServer.java:396)                                 */
[20:30:25] [Server thread/ERROR] [Sponge]: /*     net.minecraft.server.MinecraftServer.func_71217_p(MinecraftServer.java:666)                                           */
[20:30:25] [Server thread/ERROR] [Sponge]: /*     net.minecraft.server.MinecraftServer.run(MinecraftServer.java:524)                                                    */
[20:30:25] [Server thread/ERROR] [Sponge]: /*     java.lang.Thread.run(Unknown Source)                                                                                  */
[20:30:25] [Server thread/ERROR] [Sponge]: /*  Phases Remaining:                                                                                                        */
[20:30:25] [Server thread/ERROR] [Sponge]: /*                                                                                                                           */
[20:30:25] [Server thread/ERROR] [Sponge]: /* Minecraft : 1.12.2                                                                                                        */
[20:30:25] [Server thread/ERROR] [Sponge]: /* SpongeAPI : 7.1.0-SNAPSHOT-b163786                                                                                        */
[20:30:25] [Server thread/ERROR] [Sponge]: /* SpongeForge : 1.12.2-2555-7.1.0-BETA-2851                                                                                 */
[20:30:25] [Server thread/ERROR] [Sponge]: /* Minecraft Forge : 14.23.1.2589  

How can i fix this? :confused:

You’re running Forge 2588 and SpongeForge 2851, both of which are outdated. You should be using recommended (2931 iirc) and for the purposes of issue testing latest.

That being said, it looks like you may not be able to cancel that event.

org.spongepowered.asm.mixin.injection.callback.CancellationException: The call func_70097_a is not cancellable.

At a guess, this is probably an issue, but you should test on current versions and look into the javadocs for the event.

I also recommend you include the code of these utility methods you’re referencing as I have no idea what you’re doing in them.

I tried now with a new server installed using Forge-1.12.2-14.23.2.2611 and SpongeForge-1.12.2-2611-7.1.0-BETA-2931, wich should be the latest recommended versions for Forge and Sponge, but i sitll got that console spam :confused: The javadoc explains how the event works and how damage is calculated, so maybe should i set the final damage to 0 instead?
As for the events, this is what they do. They just check the given entity type

/**
	 * Check if an EntityType is a Vehicle
	 * @param type The EntityType
	 * @return true if the EntityType is a Vehicle, false otherwise
	 */
	public static boolean isVehicle(EntityType type) {
		return type.equals(EntityTypes.CHESTED_MINECART) || type.equals(EntityTypes.COMMANDBLOCK_MINECART)
				|| type.equals(EntityTypes.FURNACE_MINECART) || type.equals(EntityTypes.HOPPER_MINECART) 
				|| type.equals(EntityTypes.MOB_SPAWNER_MINECART) || type.equals(EntityTypes.RIDEABLE_MINECART)
				|| type.equals(EntityTypes.TNT_MINECART) || type.equals(EntityTypes.BOAT);
	}
	
	/**
	 * Check if an EntityType is an Explosion
	 * @param type The EntityType
	 * @return true if the EntityType is an Explosion, false otherwise
	 */
	public static boolean isExplosion(EntityType type) {
		return type.equals(EntityTypes.TNT_MINECART) || type.equals(EntityTypes.PRIMED_TNT)
				|| type.equals(EntityTypes.CREEPER) || type.equals(EntityTypes.ENDER_CRYSTAL) ||
				type.equals(EntityTypes.FIREBALL) || type.equals(EntityTypes.ENDER_DRAGON);
	}

EDIT: i set the base damage to -99999, the final damage was the same. However the explosion still “damaged” the painting, so i guess the event occurs regardless of damage level :confused:

Actually, I think that’s a Sponge issue. I’ll take a look into it.

Fixed.

Thanks for the fix, will update to the laster version asap :slight_smile: