8

While @r alone can only target player entities, adding the type parameter allows it to target other entity types:

/say @r[type=ArmorStand]

But because of this requirement, you'd only be able to target all except for a specific entity:

/say @r[type=!Player]

Attempting to use an invalid ID will throw an error, stating "Entity type [name] is invalid", rather than targeting all except that invalid type.

/say @r[type=!InvalidID]

How would I cause @r to target all possible entities without excluding any?

3 Answers 3

3

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.

7

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.

3

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+!

2
  • That doesn't answer my question. I'm asking for any type, not any one from a set of armor stands.
    – Skylinerw
    Commented Mar 11, 2018 at 16:09
  • 1
    Edited my answer ...
    – Sutsuj
    Commented Mar 12, 2018 at 17:05

Not the answer you're looking for? Browse other questions tagged or ask your own question.