use of org.spongepowered.api.text.selector.SelectorType in project SpongeCommon by SpongePowered.
the class SelectorResolver method makeFilter.
private Predicate<Entity> makeFilter() {
// for easier reading
final Selector sel = this.selector;
Vector3d position = getPositionOrDefault(this.position, ArgumentTypes.POSITION);
List<Predicate<Entity>> filters = Lists.newArrayList();
addTypeFilters(filters);
addDimensionFilters(position, filters);
addRadiusFilters(position, filters);
addLevelFilters(filters);
addGamemodeFilters(filters);
addNameFilters(filters);
addRotationFilters(filters);
addTeamFilters(filters);
addScoreFilters(filters);
SelectorType selectorType = sel.getType();
Optional<Invertible<EntityType>> type = sel.getArgument(ArgumentTypes.ENTITY_TYPE);
// isn't an ALL_ENTITIES selector or it is a RANDOM selector for only players
boolean isPlayerOnlySelector = selectorType == SelectorTypes.ALL_PLAYERS || selectorType == SelectorTypes.NEAREST_PLAYER || (selectorType == SelectorTypes.RANDOM && type.isPresent() && !type.get().isInverted() && type.get().getValue() != EntityTypes.PLAYER);
if (isPlayerOnlySelector) {
// insert at the start so it applies first
filters.add(0, requireTypePredicate(Entity.class, Player.class));
}
return Functional.predicateAnd(filters);
}
use of org.spongepowered.api.text.selector.SelectorType in project SpongeCommon by SpongePowered.
the class SelectorResolver method resolve.
public Set<Entity> resolve() {
SelectorType selectorType = this.selector.getType();
int defaultCount = 1;
if (INFINITE_TYPES.contains(selectorType)) {
defaultCount = 0;
}
int maxToSelect = this.selector.get(ArgumentTypes.COUNT).orElse(defaultCount);
Set<? extends Extent> extents = getExtentSet();
int count = 0;
ImmutableSet.Builder<Entity> entities = ImmutableSet.builder();
for (Extent extent : extents) {
Collection<Entity> allEntities = extent.getEntities();
if (selectorType == SelectorTypes.RANDOM) {
List<Entity> entityList = new ArrayList<>(allEntities);
Collections.shuffle(entityList);
allEntities = entityList;
}
for (Entity e : allEntities) {
if (!this.selectorFilter.test(e)) {
continue;
}
entities.add(e);
count++;
if (maxToSelect != 0 && count > maxToSelect) {
break;
}
}
}
return entities.build();
}
use of org.spongepowered.api.text.selector.SelectorType in project LanternServer by LanternPowered.
the class SelectorResolver method resolve.
public Set<Entity> resolve() {
SelectorType selectorType = this.selector.getType();
int defaultCount = 1;
if (INFINITE_TYPES.contains(selectorType)) {
defaultCount = 0;
}
int maxToSelect = this.selector.get(ArgumentTypes.COUNT).orElse(defaultCount);
Set<? extends Extent> extents = getExtentSet();
int count = 0;
ImmutableSet.Builder<Entity> entities = ImmutableSet.builder();
for (Extent extent : extents) {
Collection<Entity> allEntities = extent.getEntities();
if (selectorType == SelectorTypes.RANDOM) {
List<Entity> entityList = new ArrayList<Entity>(allEntities);
Collections.shuffle(entityList);
allEntities = entityList;
}
for (Entity e : allEntities) {
if (!this.selectorFilter.test(e)) {
continue;
}
entities.add(e);
count++;
if (maxToSelect != 0 && count > maxToSelect) {
break;
}
}
}
return entities.build();
}
use of org.spongepowered.api.text.selector.SelectorType in project SpongeCommon by SpongePowered.
the class SpongeSelectorFactory method parseRawSelector.
@Override
public Selector parseRawSelector(String selector) {
checkArgument(selector.startsWith("@"), "Invalid selector %s", selector);
// If multi-character types are possible, this handles it
int argListIndex = selector.indexOf('[');
if (argListIndex < 0) {
argListIndex = selector.length();
} else {
int end = selector.indexOf(']');
checkArgument(end > argListIndex && selector.charAt(end - 1) != ',', "Invalid selector %s", selector);
}
String typeStr = selector.substring(1, argListIndex);
checkArgument(idToType.containsKey(typeStr), "No type known as '%s'", typeStr);
SelectorType type = idToType.get(typeStr);
try {
Map<String, String> rawMap;
if (argListIndex == selector.length()) {
rawMap = ImmutableMap.of();
} else {
rawMap = EntitySelector.getArgumentMap(selector.substring(argListIndex + 1, selector.length() - 1));
}
Map<ArgumentType<?>, Argument<?>> arguments = parseArguments(rawMap);
return new SpongeSelector(type, ImmutableMap.copyOf(arguments));
} catch (Exception e) {
throw new IllegalArgumentException("Invalid selector " + selector, e);
}
}
use of org.spongepowered.api.text.selector.SelectorType in project LanternServer by LanternPowered.
the class SelectorResolver method makeFilter.
private Predicate<Entity> makeFilter() {
// for easier reading
final Selector sel = this.selector;
final Vector3d position = getPositionOrDefault(this.position, ArgumentTypes.POSITION);
final List<Predicate<Entity>> filters = new ArrayList<>();
addTypeFilters(filters);
addDimensionFilters(position, filters);
addRadiusFilters(position, filters);
addLevelFilters(filters);
addGamemodeFilters(filters);
addNameFilters(filters);
addRotationFilters(filters);
addTeamFilters(filters);
addScoreFilters(filters);
SelectorType selectorType = sel.getType();
final Optional<Argument.Invertible<EntityType>> type = sel.getArgument(ArgumentTypes.ENTITY_TYPE);
// isn't an ALL_ENTITIES selector or it is a RANDOM selector for only players
final boolean isPlayerOnlySelector = selectorType == SelectorTypes.ALL_PLAYERS || selectorType == SelectorTypes.NEAREST_PLAYER || (selectorType == SelectorTypes.RANDOM && type.isPresent() && !type.get().isInverted() && type.get().getValue() != EntityTypes.PLAYER);
if (isPlayerOnlySelector) {
// insert at the start so it applies first
filters.add(0, requireTypePredicate(Entity.class, Player.class));
}
return Functional.predicateAnd(filters);
}
Aggregations