| 1 | package net.minecraft.network.chat.contents;
|
| 2 |
|
| 3 | import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
| 4 | import com.mojang.datafixers.util.Either;
|
| 5 | import com.mojang.serialization.Codec;
|
| 6 | import com.mojang.serialization.MapCodec;
|
| 7 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 8 | import java.util.List;
|
| 9 | import java.util.Optional;
|
| 10 | import net.minecraft.commands.CommandSourceStack;
|
| 11 | import net.minecraft.commands.arguments.EntityArgument;
|
| 12 | import net.minecraft.commands.arguments.selector.EntitySelector;
|
| 13 | import net.minecraft.network.chat.Component;
|
| 14 | import net.minecraft.network.chat.ComponentContents;
|
| 15 | import net.minecraft.network.chat.MutableComponent;
|
| 16 | import net.minecraft.network.chat.ResolutionContext;
|
| 17 | import net.minecraft.network.chat.numbers.StyledFormat;
|
| 18 | import net.minecraft.server.MinecraftServer;
|
| 19 | import net.minecraft.util.CompilableString;
|
| 20 | import net.minecraft.world.entity.Entity;
|
| 21 | import net.minecraft.world.scores.Objective;
|
| 22 | import net.minecraft.world.scores.ReadOnlyScoreInfo;
|
| 23 | import net.minecraft.world.scores.ScoreHolder;
|
| 24 | import net.minecraft.world.scores.Scoreboard;
|
| 25 |
|
| 26 | public record ScoreContents(Either<CompilableString<EntitySelector>, String> name, String objective) implements ComponentContents {
|
| 27 | public static final MapCodec<ScoreContents> INNER_CODEC = RecordCodecBuilder.mapCodec(
|
| 28 | i -> i.group(
|
| 29 | Codec.either(EntitySelector.COMPILABLE_CODEC, Codec.STRING).fieldOf("name").forGetter(ScoreContents::name),
|
| 30 | Codec.STRING.fieldOf("objective").forGetter(ScoreContents::objective)
|
| 31 | )
|
| 32 | .apply(i, ScoreContents::new)
|
| 33 | );
|
| 34 | public static final MapCodec<ScoreContents> MAP_CODEC = INNER_CODEC.fieldOf("score");
|
| 35 |
|
| 36 | @Override
|
| 37 | public MapCodec<ScoreContents> codec() {
|
| 38 | return MAP_CODEC;
|
| 39 | }
|
| 40 |
|
| 41 | private ScoreHolder findTargetName(final CommandSourceStack source) throws CommandSyntaxException {
|
| 42 | Optional<CompilableString<EntitySelector>> selector = this.name.left();
|
| 43 | if (selector.isPresent()) {
|
| 44 | List<? extends Entity> entities = selector.get().compiled().findEntities(source);
|
| 45 | if (!entities.isEmpty()) {
|
| 46 | if (entities.size() != 1) {
|
| 47 | throw EntityArgument.ERROR_NOT_SINGLE_ENTITY.create();
|
| 48 | } else {
|
| 49 | return entities.getFirst();
|
| 50 | }
|
| 51 | } else {
|
| 52 | return ScoreHolder.forNameOnly(selector.get().source());
|
| 53 | }
|
| 54 | } else {
|
| 55 | return ScoreHolder.forNameOnly(this.name.right().orElseThrow());
|
| 56 | }
|
| 57 | }
|
| 58 |
|
| 59 | private MutableComponent getScore(final ScoreHolder name, final CommandSourceStack source) {
|
| 60 | MinecraftServer server = source.getServer();
|
| 61 | if (server != null) {
|
| 62 | Scoreboard scoreboard = server.getScoreboard();
|
| 63 | Objective objective = scoreboard.getObjective(this.objective);
|
| 64 | if (objective != null) {
|
| 65 | ReadOnlyScoreInfo scoreInfo = scoreboard.getPlayerScoreInfo(name, objective);
|
| 66 | if (scoreInfo != null) {
|
| 67 | return scoreInfo.formatValue(objective.numberFormatOrDefault(StyledFormat.NO_STYLE));
|
| 68 | }
|
| 69 | }
|
| 70 | }
|
| 71 |
|
| 72 | return Component.empty();
|
| 73 | }
|
| 74 |
|
| 75 | @Override
|
| 76 | public MutableComponent resolve(final ResolutionContext context, final int recursionDepth) throws CommandSyntaxException {
|
| 77 | CommandSourceStack source = context.source();
|
| 78 | if (source == null) {
|
| 79 | return Component.empty();
|
| 80 | }
|
| 81 |
|
| 82 | ScoreHolder scoreHolder = this.findTargetName(source);
|
| 83 | Entity entity = context.defaultScoreboardEntity();
|
| 84 | ScoreHolder scoreName = entity != null && scoreHolder.equals(ScoreHolder.WILDCARD) ? entity : scoreHolder;
|
| 85 | return this.getScore(scoreName, source);
|
| 86 | }
|
| 87 |
|
| 88 | @Override
|
| 89 | public String toString() {
|
| 90 | return "score{name='" + this.name + "', objective='" + this.objective + "'}";
|
| 91 | }
|
| 92 | }
|
| 93 |
|