How can I target a random entity of any type using @r?
The type
parameter grabs a list of string IDs from the same tab-complete list that /summon
uses. If the input into the type
parameter is not among that list, the selector will throw that error. The type
parameter specifically appends "Player" to that list, which is why "Player" does not show up in /summon
's tab-complete.
Among that list is "LightningBolt", which is not actually a valid savegame entity ID. Valid IDs are those that appear within the EntityList
class mapping, which is what the tab-complete function will grab from before forcibly appending the list with "LightningBolt". It is essentially only appended for usability with /summon
, which handles "LightningBolt" directly rather than through an automated process for any other input.
As such, you can safely use type=!LightningBolt
. Lightning bolts themselves are not target-able in the first place. They are not added to the internal list of spawned entities, which is what entity selectors obtain targets from. Even if they were added to that list but remained non-savegame entities, "LightningBolt" is still not associated with them and the selector would target them regardless.
To reiterate: lightning bolts are not excluded by the selector because they are not included in the list of possible targets to begin with, nor is the ID "LightningBolt" associated with them in any way other than as a means of creation via /summon
. It is simply impossible to target lightning bolts.
But since the type
parameter believes that "LightningBolt" is a valid ID due to the /summon
tab-complete list, it can be used without throwing an error. The following will target a random entity from all entities possible with selectors:
/say @r[type=!LightningBolt]
It will also target non-savegame entities such as fishing bobbers. While fishing bobbers do not have an ID at all, they do not have the "LightningBolt" ID which satisfies the selector. Fishing bobbers, unlike lightning bolts, are added to the list of spawned entities, which is why they can be targeted by selectors while lightning bolts cannot be.
I have a solution!
First you have to write ArmorStand like this: armor_stand
and to get it random:
@e[type=armor_stand,sort=random,limit=1]
The limit = 1 limits it to only 1 entity which is exactly what you want.
Example:
/execute as @e[sort=random,type=armor_stand,limit=1] at @s run summon minecraft:lightning_bolt
I hope I could help you :D
Edit: to target all entities just remove the
type=armor_stand
Example: /execute as @e[sort=random,limit=1] at @s run summon minecraft:lightning_bolt
This only works in 1.13+!
In newer versions of Minecraft, you can set the sort mode that will choose how targets are sorted. You can then limit the number of targeted entities to 1, so that you are returned only 1 entity.
Here is a command that lets you do that:
/say @e[sort=random,limit=1]
The list of targets is sorted randomly, and then the limit being 1 means only the 1st one of that list is selected, essentially giving you a random entity selection.