| 1 | package net.minecraft.network.chat.contents;
|
| 2 |
|
| 3 | import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
| 4 | import com.mojang.serialization.MapCodec;
|
| 5 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 6 | import java.util.Optional;
|
| 7 | import net.minecraft.commands.CommandSourceStack;
|
| 8 | import net.minecraft.commands.arguments.selector.EntitySelector;
|
| 9 | import net.minecraft.network.chat.Component;
|
| 10 | import net.minecraft.network.chat.ComponentContents;
|
| 11 | import net.minecraft.network.chat.ComponentSerialization;
|
| 12 | import net.minecraft.network.chat.ComponentUtils;
|
| 13 | import net.minecraft.network.chat.FormattedText;
|
| 14 | import net.minecraft.network.chat.MutableComponent;
|
| 15 | import net.minecraft.network.chat.ResolutionContext;
|
| 16 | import net.minecraft.network.chat.Style;
|
| 17 | import net.minecraft.util.CompilableString;
|
| 18 | import net.minecraft.world.entity.Entity;
|
| 19 |
|
| 20 | public record SelectorContents(CompilableString<EntitySelector> selector, Optional<Component> separator) implements ComponentContents {
|
| 21 | public static final MapCodec<SelectorContents> MAP_CODEC = RecordCodecBuilder.mapCodec(
|
| 22 | i -> i.group(
|
| 23 | EntitySelector.COMPILABLE_CODEC.fieldOf("selector").forGetter(SelectorContents::selector),
|
| 24 | ComponentSerialization.CODEC.optionalFieldOf("separator").forGetter(SelectorContents::separator)
|
| 25 | )
|
| 26 | .apply(i, SelectorContents::new)
|
| 27 | );
|
| 28 |
|
| 29 | @Override
|
| 30 | public MapCodec<SelectorContents> codec() {
|
| 31 | return MAP_CODEC;
|
| 32 | }
|
| 33 |
|
| 34 | @Override
|
| 35 | public MutableComponent resolve(final ResolutionContext context, final int recursionDepth) throws CommandSyntaxException {
|
| 36 | CommandSourceStack source = context.source();
|
| 37 | if (source == null) {
|
| 38 | return Component.empty();
|
| 39 | }
|
| 40 |
|
| 41 | Optional<? extends Component> resolvedSeparator = ComponentUtils.resolve(context, this.separator, recursionDepth);
|
| 42 | return ComponentUtils.formatList(this.selector.compiled().findEntities(source), resolvedSeparator, Entity::getDisplayName);
|
| 43 | }
|
| 44 |
|
| 45 | @Override
|
| 46 | public <T> Optional<T> visit(final FormattedText.StyledContentConsumer<T> output, final Style currentStyle) {
|
| 47 | return output.accept(currentStyle, this.selector.source());
|
| 48 | }
|
| 49 |
|
| 50 | @Override
|
| 51 | public <T> Optional<T> visit(final FormattedText.ContentConsumer<T> output) {
|
| 52 | return output.accept(this.selector.source());
|
| 53 | }
|
| 54 |
|
| 55 | @Override
|
| 56 | public String toString() {
|
| 57 | return "pattern{" + this.selector + "}";
|
| 58 | }
|
| 59 | }
|
| 60 |
|