So when I break an oak_log block I summon an area_effect_cloud with the tag B.Spawn at the block I just broke.

Then I use this command to summon a 3x3x3 area_effect_cloud cube around the [tag=B.Spawn] entity

execute at @e[tag=B.Spawn] positioned ~-1 ~-1 ~-1 unless entity @e[tag=B.Cell,distance=...5] run summon area_effect_cloud ~ ~ ~ {Tags:['B.Cell'],Duration:2147483647}

Now every [tag=B.Spawn] gets killed and every [tag=B.Cell] gets the tag of B.Spawn and summons again a 3x3x3 cube around it.

This brings a lot of entities who overlap each other causing 20000 entities in just a 5x5x5 cube.

I need a command which kills all entities within its block so there is only one left per block.

I tried:

execute at @e[tag=B.Cell] if entity @e[tag=B.Cell,distance=...99] run kill @e[tag=B.Cell,distance=...99]

but this kills all entities.


Solution 1:

I will use armour stands in this answer, because I am more familiar with them. It should be pretty easy to switch everything to AECs, if you want that.

There are multiple ways to kill everything except for one entity in a given space, the easiest one is to just tag an arbitrary one (limit=1) and then kill all without that tag.
But in your case this constant summoning and killing is actually not required and causes way more lag than necessary. Instead you could check whether an entity already exists in that space before summoning a new armour stand or AEC there. So a VeinMiner/TreeCapitator-like system could for example work like this:

Summon an armour stand at the manually broken block (you seem to have already figured out that part):

/summon armor_stand <coordinates> {NoGravity:1,NoAI:1,Invisible:1,Marker:1,Tags:["vein"]}

I will use the tag "vein" for all armour stands that are used in this VeinMiner-like system.

Then repeatedly execute a function as/at all VeinMiner armour stands that have not summoned new armour stands around them yet:

/execute as @e[tag=vein,tag=!done] at @s run function vein:summon

The function looks like this:

execute positioned ~1 ~1 ~1 if block ~ ~ ~ oak_log unless entity @e[tag=vein,distance=...1] run summon armor_stand ~ ~ ~ {Marker:1,NoGravity:1,NoAI:1,Invisible:1,Tags:["vein"]}
execute positioned ~1 ~1 ~ if block ~ ~ ~ oak_log unless entity @e[tag=vein,distance=...1] run summon armor_stand ~ ~ ~ {Marker:1,NoGravity:1,NoAI:1,Invisible:1,Tags:["vein"]}
execute positioned ~1 ~1 ~-1 if block ~ ~ ~ oak_log unless entity @e[tag=vein,distance=...1] run summon armor_stand ~ ~ ~ {Marker:1,NoGravity:1,NoAI:1,Invisible:1,Tags:["vein"]}
execute positioned ~1 ~ ~1 if block ~ ~ ~ oak_log unless entity @e[tag=vein,distance=...1] run summon armor_stand ~ ~ ~ {Marker:1,NoGravity:1,NoAI:1,Invisible:1,Tags:["vein"]}
execute positioned ~1 ~ ~ if block ~ ~ ~ oak_log unless entity @e[tag=vein,distance=...1] run summon armor_stand ~ ~ ~ {Marker:1,NoGravity:1,NoAI:1,Invisible:1,Tags:["vein"]}
execute positioned ~1 ~ ~-1 if block ~ ~ ~ oak_log unless entity @e[tag=vein,distance=...1] run summon armor_stand ~ ~ ~ {Marker:1,NoGravity:1,NoAI:1,Invisible:1,Tags:["vein"]}
execute positioned ~1 ~-1 ~1 if block ~ ~ ~ oak_log unless entity @e[tag=vein,distance=...1] run summon armor_stand ~ ~ ~ {Marker:1,NoGravity:1,NoAI:1,Invisible:1,Tags:["vein"]}
execute positioned ~1 ~-1 ~ if block ~ ~ ~ oak_log unless entity @e[tag=vein,distance=...1] run summon armor_stand ~ ~ ~ {Marker:1,NoGravity:1,NoAI:1,Invisible:1,Tags:["vein"]}
execute positioned ~1 ~-1 ~-1 if block ~ ~ ~ oak_log unless entity @e[tag=vein,distance=...1] run summon armor_stand ~ ~ ~ {Marker:1,NoGravity:1,NoAI:1,Invisible:1,Tags:["vein"]}
execute positioned ~ ~1 ~1 if block ~ ~ ~ oak_log unless entity @e[tag=vein,distance=...1] run summon armor_stand ~ ~ ~ {Marker:1,NoGravity:1,NoAI:1,Invisible:1,Tags:["vein"]}
execute positioned ~ ~1 ~ if block ~ ~ ~ oak_log unless entity @e[tag=vein,distance=...1] run summon armor_stand ~ ~ ~ {Marker:1,NoGravity:1,NoAI:1,Invisible:1,Tags:["vein"]}
execute positioned ~ ~1 ~-1 if block ~ ~ ~ oak_log unless entity @e[tag=vein,distance=...1] run summon armor_stand ~ ~ ~ {Marker:1,NoGravity:1,NoAI:1,Invisible:1,Tags:["vein"]}
execute positioned ~ ~ ~1 if block ~ ~ ~ oak_log unless entity @e[tag=vein,distance=...1] run summon armor_stand ~ ~ ~ {Marker:1,NoGravity:1,NoAI:1,Invisible:1,Tags:["vein"]}
# ~ ~ ~ check not necessary
execute positioned ~ ~ ~-1 if block ~ ~ ~ oak_log unless entity @e[tag=vein,distance=...1] run summon armor_stand ~ ~ ~ {Marker:1,NoGravity:1,NoAI:1,Invisible:1,Tags:["vein"]}
execute positioned ~ ~-1 ~1 if block ~ ~ ~ oak_log unless entity @e[tag=vein,distance=...1] run summon armor_stand ~ ~ ~ {Marker:1,NoGravity:1,NoAI:1,Invisible:1,Tags:["vein"]}
execute positioned ~ ~-1 ~ if block ~ ~ ~ oak_log unless entity @e[tag=vein,distance=...1] run summon armor_stand ~ ~ ~ {Marker:1,NoGravity:1,NoAI:1,Invisible:1,Tags:["vein"]}
execute positioned ~ ~-1 ~-1 if block ~ ~ ~ oak_log unless entity @e[tag=vein,distance=...1] run summon armor_stand ~ ~ ~ {Marker:1,NoGravity:1,NoAI:1,Invisible:1,Tags:["vein"]}
execute positioned ~-1 ~1 ~1 if block ~ ~ ~ oak_log unless entity @e[tag=vein,distance=...1] run summon armor_stand ~ ~ ~ {Marker:1,NoGravity:1,NoAI:1,Invisible:1,Tags:["vein"]}
execute positioned ~-1 ~1 ~ if block ~ ~ ~ oak_log unless entity @e[tag=vein,distance=...1] run summon armor_stand ~ ~ ~ {Marker:1,NoGravity:1,NoAI:1,Invisible:1,Tags:["vein"]}
execute positioned ~-1 ~1 ~-1 if block ~ ~ ~ oak_log unless entity @e[tag=vein,distance=...1] run summon armor_stand ~ ~ ~ {Marker:1,NoGravity:1,NoAI:1,Invisible:1,Tags:["vein"]}
execute positioned ~-1 ~ ~1 if block ~ ~ ~ oak_log unless entity @e[tag=vein,distance=...1] run summon armor_stand ~ ~ ~ {Marker:1,NoGravity:1,NoAI:1,Invisible:1,Tags:["vein"]}
execute positioned ~-1 ~ ~ if block ~ ~ ~ oak_log unless entity @e[tag=vein,distance=...1] run summon armor_stand ~ ~ ~ {Marker:1,NoGravity:1,NoAI:1,Invisible:1,Tags:["vein"]}
execute positioned ~-1 ~ ~-1 if block ~ ~ ~ oak_log unless entity @e[tag=vein,distance=...1] run summon armor_stand ~ ~ ~ {Marker:1,NoGravity:1,NoAI:1,Invisible:1,Tags:["vein"]}
execute positioned ~-1 ~-1 ~1 if block ~ ~ ~ oak_log unless entity @e[tag=vein,distance=...1] run summon armor_stand ~ ~ ~ {Marker:1,NoGravity:1,NoAI:1,Invisible:1,Tags:["vein"]}
execute positioned ~-1 ~-1 ~ if block ~ ~ ~ oak_log unless entity @e[tag=vein,distance=...1] run summon armor_stand ~ ~ ~ {Marker:1,NoGravity:1,NoAI:1,Invisible:1,Tags:["vein"]}
execute positioned ~-1 ~-1 ~-1 if block ~ ~ ~ oak_log unless entity @e[tag=vein,distance=...1] run summon armor_stand ~ ~ ~ {Marker:1,NoGravity:1,NoAI:1,Invisible:1,Tags:["vein"]}
tag @s add done

Explanation: At each of the 26 blocks around the armour stand, a new armour stand is summoned only if there is an oak log there and not another VeinMiner armour stand already. Finally, the armour stand tags itself with "done" so that it doesn't need to check around it anymore in the following iterations.
This already automatically deals with loops and never summons any more armour stand than necessary.

You can stop the iteration when no more armour stands with the tag "vein" and without the tag "done" exist anymore (or after a certain number of repetitions, in case you apply it to e.g. stone in a default world).

Using a function is actually required in this case. If you first summoned armour stands around all without the "done" tag and then tagged everything with "done", you would also tag the new ones. If you first tagged all with "done" and then summoned new armour stands around all without the tag "done", then of course nothing would happen, because there would be none without the tag left.
Alternatively you would need yet another tag for armour stands that got created in the current round.

Note that a check for distance=0 does not always work correctly due to the bug MC-88533, so I'm using a radius of 0.1 here.