Skip to content

The EntitySelector class

Shiny Bunny edited this page Jun 19, 2019 · 4 revisions

The EntitySelector class is used to easily and automatically create entity selectors. An entity selector may look in commands like this: @e[type=pig,tag=cool,scores={foo=5,bar=1..},team=!]

translation: select all pigs with a tag "cool", score of objective 'foo' is exactly 5, 'bar' is 1 or greater, and be a member of any team.

With MCFunctionAPI and the EntitySelector class, it can be represented like so:

EntitySelector selector = new EntitySelector("@e")
{
    Type = "pig",
    Tags = "cool",
    Scores = new ScoreSet().Where("foo", 5).Where("bar", "1.."),
    Team = Team.Any
}

Yes, it is a little longer, but the API helps you write it easily and you don't have to remember all selector arguments by yourself!

In-Depth look at the class

An entity selector can be built in 4 ways:

EntitySelector selector = EntitySelector.AllEntities.Is(EntityType.Creeper).InDistance("5..10").LimitTo(5).Score("myObjective", 5);

EntitySelector selector2 = new EntitySelector(Target.AllEntities);
selector2.Is(EntityType.Creeper);
selector2.Distance = "5..10";
selector2.Limit = 5;
selectors2.Scores.Where("myObjective", 5);

EntitySelector selector3 = new EntitySelector(Target.AllEntities)
{
  Type = "creeper"
  Distance = "5..10",
  Limit = 5,
  Scores = "myObjective=5"
};

EntitySelector selector4 = "@e[type=creeper,distance=5..10,limit=5,scores={myObjective=5}]";

Choose whatever is the easiest one and most readable for you! all of the above examples should will produce the same selector!

Filter Types

DoubleRange

A range of double values. Can be represented implicitly by a double number or a string representing the range.

// currently unused

IntRange

A range of whole numbers.

Examples:

Distance = "10..20"; // The entity must be between 10 and 20 blocks away from the executor's location.

Level = 30; // The player's xp level must be exactly 30.

double: X, Y, and Z

Tests for the entity's absolute coordinates.

double: DX, DY, DZ

Tests whether the entity is within the specified range of distance in a volume the size of those axises.

IntRange: Distance

Tests whether the entity is withing that distance of blocks

IntRange: X_Rotation, Y_Rotation

Tests for the entity's yaw and pitch, respectively.

IntRange: Level

Test for the player's xp level (only works for players)

ScoreSet: Scores

Tests for the entity's scores in the scoreboard. Contains key-value pairs of Objectives and IntRanges.

Scores = new ScoreSet().Where("test",5).Where(Objective.Of("hello"),"5..10");

Scores.Where("bar","..3");

TagSet: Tags

Tests for the entity's tag list.

Tags = new TagSet().Has("cool").Not("boring");

TeamArgument: Team

Tests for the scoreboard team the entity belongs to.

Team = "theCoolMobs";
Team = new Team("myTeam");
Team.IsNot("stupidEntities");

GamemodeArgument: Gamemode

Test for the gamemode of the entity. Works only for players.

Gamemode = "survival";
Gamemode = 1;
Gamemode.IsNot("spectator").IsNot(Gamemode.Adventure);

NameArgument: Name

Tests for the CustomName of the entity or the player username.

Name = "bob";
Name = "!tom";
Name.Is("Alex")

Clone this wiki locally