64 lines
2.3 KiB
Java
64 lines
2.3 KiB
Java
package net.minecraft.network.chat.contents;
|
|
|
|
import com.mojang.brigadier.StringReader;
|
|
import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
|
import com.mojang.serialization.Codec;
|
|
import com.mojang.serialization.MapCodec;
|
|
import com.mojang.serialization.codecs.RecordCodecBuilder;
|
|
import java.util.List;
|
|
import java.util.stream.Stream;
|
|
import net.minecraft.advancements.critereon.NbtPredicate;
|
|
import net.minecraft.commands.CommandSourceStack;
|
|
import net.minecraft.commands.arguments.selector.EntitySelector;
|
|
import net.minecraft.commands.arguments.selector.EntitySelectorParser;
|
|
import net.minecraft.nbt.CompoundTag;
|
|
import net.minecraft.world.entity.Entity;
|
|
import org.jetbrains.annotations.Nullable;
|
|
|
|
public record EntityDataSource(String selectorPattern, @Nullable EntitySelector compiledSelector) implements DataSource {
|
|
public static final MapCodec<EntityDataSource> SUB_CODEC = RecordCodecBuilder.mapCodec(
|
|
instance -> instance.group(Codec.STRING.fieldOf("entity").forGetter(EntityDataSource::selectorPattern)).apply(instance, EntityDataSource::new)
|
|
);
|
|
public static final DataSource.Type<EntityDataSource> TYPE = new DataSource.Type<>(SUB_CODEC, "entity");
|
|
|
|
public EntityDataSource(String selectorPattern) {
|
|
this(selectorPattern, compileSelector(selectorPattern));
|
|
}
|
|
|
|
@Nullable
|
|
private static EntitySelector compileSelector(String selectorPattern) {
|
|
try {
|
|
EntitySelectorParser entitySelectorParser = new EntitySelectorParser(new StringReader(selectorPattern), true);
|
|
return entitySelectorParser.parse();
|
|
} catch (CommandSyntaxException var2) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public Stream<CompoundTag> getData(CommandSourceStack source) throws CommandSyntaxException {
|
|
if (this.compiledSelector != null) {
|
|
List<? extends Entity> list = this.compiledSelector.findEntities(source);
|
|
return list.stream().map(NbtPredicate::getEntityTagToCompare);
|
|
} else {
|
|
return Stream.empty();
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public DataSource.Type<?> type() {
|
|
return TYPE;
|
|
}
|
|
|
|
public String toString() {
|
|
return "entity=" + this.selectorPattern;
|
|
}
|
|
|
|
public boolean equals(Object object) {
|
|
return this == object ? true : object instanceof EntityDataSource entityDataSource && this.selectorPattern.equals(entityDataSource.selectorPattern);
|
|
}
|
|
|
|
public int hashCode() {
|
|
return this.selectorPattern.hashCode();
|
|
}
|
|
}
|