| 1 | package net.minecraft.world.scores;
|
| 2 |
|
| 3 | import com.mojang.serialization.Codec;
|
| 4 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 5 | import java.util.Objects;
|
| 6 | import java.util.Optional;
|
| 7 | import net.minecraft.network.chat.Component;
|
| 8 | import net.minecraft.network.chat.ComponentSerialization;
|
| 9 | import net.minecraft.network.chat.ComponentUtils;
|
| 10 | import net.minecraft.network.chat.HoverEvent;
|
| 11 | import net.minecraft.network.chat.numbers.NumberFormat;
|
| 12 | import net.minecraft.network.chat.numbers.NumberFormatTypes;
|
| 13 | import net.minecraft.world.scores.criteria.ObjectiveCriteria;
|
| 14 | import org.jspecify.annotations.Nullable;
|
| 15 |
|
| 16 | public class Objective {
|
| 17 | private final Scoreboard scoreboard;
|
| 18 | private final String name;
|
| 19 | private final ObjectiveCriteria criteria;
|
| 20 | private Component displayName;
|
| 21 | private Component formattedDisplayName;
|
| 22 | private ObjectiveCriteria.RenderType renderType;
|
| 23 | private boolean displayAutoUpdate;
|
| 24 | private @Nullable NumberFormat numberFormat;
|
| 25 |
|
| 26 | public Objective(
|
| 27 | final Scoreboard scoreboard,
|
| 28 | final String name,
|
| 29 | final ObjectiveCriteria criteria,
|
| 30 | final Component displayName,
|
| 31 | final ObjectiveCriteria.RenderType renderType,
|
| 32 | final boolean displayAutoUpdate,
|
| 33 | final @Nullable NumberFormat numberFormat
|
| 34 | ) {
|
| 35 | this.scoreboard = scoreboard;
|
| 36 | this.name = name;
|
| 37 | this.criteria = criteria;
|
| 38 | this.displayName = displayName;
|
| 39 | this.formattedDisplayName = this.createFormattedDisplayName();
|
| 40 | this.renderType = renderType;
|
| 41 | this.displayAutoUpdate = displayAutoUpdate;
|
| 42 | this.numberFormat = numberFormat;
|
| 43 | }
|
| 44 |
|
| 45 | public Objective.Packed pack() {
|
| 46 | return new Objective.Packed(this.name, this.criteria, this.displayName, this.renderType, this.displayAutoUpdate, Optional.ofNullable(this.numberFormat));
|
| 47 | }
|
| 48 |
|
| 49 | public Scoreboard getScoreboard() {
|
| 50 | return this.scoreboard;
|
| 51 | }
|
| 52 |
|
| 53 | public String getName() {
|
| 54 | return this.name;
|
| 55 | }
|
| 56 |
|
| 57 | public ObjectiveCriteria getCriteria() {
|
| 58 | return this.criteria;
|
| 59 | }
|
| 60 |
|
| 61 | public Component getDisplayName() {
|
| 62 | return this.displayName;
|
| 63 | }
|
| 64 |
|
| 65 | public boolean displayAutoUpdate() {
|
| 66 | return this.displayAutoUpdate;
|
| 67 | }
|
| 68 |
|
| 69 | public @Nullable NumberFormat numberFormat() {
|
| 70 | return this.numberFormat;
|
| 71 | }
|
| 72 |
|
| 73 | public NumberFormat numberFormatOrDefault(final NumberFormat _default) {
|
| 74 | return Objects.requireNonNullElse(this.numberFormat, _default);
|
| 75 | }
|
| 76 |
|
| 77 | private Component createFormattedDisplayName() {
|
| 78 | return ComponentUtils.wrapInSquareBrackets(this.displayName.copy().withStyle(s -> s.withHoverEvent(new HoverEvent.ShowText(Component.literal(this.name)))));
|
| 79 | }
|
| 80 |
|
| 81 | public Component getFormattedDisplayName() {
|
| 82 | return this.formattedDisplayName;
|
| 83 | }
|
| 84 |
|
| 85 | public void setDisplayName(final Component name) {
|
| 86 | this.displayName = name;
|
| 87 | this.formattedDisplayName = this.createFormattedDisplayName();
|
| 88 | this.scoreboard.onObjectiveChanged(this);
|
| 89 | }
|
| 90 |
|
| 91 | public ObjectiveCriteria.RenderType getRenderType() {
|
| 92 | return this.renderType;
|
| 93 | }
|
| 94 |
|
| 95 | public void setRenderType(final ObjectiveCriteria.RenderType renderType) {
|
| 96 | this.renderType = renderType;
|
| 97 | this.scoreboard.onObjectiveChanged(this);
|
| 98 | }
|
| 99 |
|
| 100 | public void setDisplayAutoUpdate(final boolean displayAutoUpdate) {
|
| 101 | this.displayAutoUpdate = displayAutoUpdate;
|
| 102 | this.scoreboard.onObjectiveChanged(this);
|
| 103 | }
|
| 104 |
|
| 105 | public void setNumberFormat(final @Nullable NumberFormat numberFormat) {
|
| 106 | this.numberFormat = numberFormat;
|
| 107 | this.scoreboard.onObjectiveChanged(this);
|
| 108 | }
|
| 109 |
|
| 110 | public record Packed(
|
| 111 | String name,
|
| 112 | ObjectiveCriteria criteria,
|
| 113 | Component displayName,
|
| 114 | ObjectiveCriteria.RenderType renderType,
|
| 115 | boolean displayAutoUpdate,
|
| 116 | Optional<NumberFormat> numberFormat
|
| 117 | ) {
|
| 118 | public static final Codec<Objective.Packed> CODEC = RecordCodecBuilder.create(
|
| 119 | i -> i.group(
|
| 120 | Codec.STRING.fieldOf("Name").forGetter(Objective.Packed::name),
|
| 121 | ObjectiveCriteria.CODEC.optionalFieldOf("CriteriaName", ObjectiveCriteria.DUMMY).forGetter(Objective.Packed::criteria),
|
| 122 | ComponentSerialization.CODEC.fieldOf("DisplayName").forGetter(Objective.Packed::displayName),
|
| 123 | ObjectiveCriteria.RenderType.CODEC.optionalFieldOf("RenderType", ObjectiveCriteria.RenderType.INTEGER).forGetter(Objective.Packed::renderType),
|
| 124 | Codec.BOOL.optionalFieldOf("display_auto_update", false).forGetter(Objective.Packed::displayAutoUpdate),
|
| 125 | NumberFormatTypes.CODEC.optionalFieldOf("format").forGetter(Objective.Packed::numberFormat)
|
| 126 | )
|
| 127 | .apply(i, Objective.Packed::new)
|
| 128 | );
|
| 129 | }
|
| 130 | }
|
| 131 |
|