| 1 | package net.minecraft.world.scores;
|
| 2 |
|
| 3 | import com.mojang.serialization.Codec;
|
| 4 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 5 | import java.util.List;
|
| 6 | import java.util.Map;
|
| 7 | import net.minecraft.resources.Identifier;
|
| 8 | import net.minecraft.util.datafix.DataFixTypes;
|
| 9 | import net.minecraft.world.level.saveddata.SavedData;
|
| 10 | import net.minecraft.world.level.saveddata.SavedDataType;
|
| 11 |
|
| 12 | public class ScoreboardSaveData extends SavedData {
|
| 13 | public static final SavedDataType<ScoreboardSaveData> TYPE = new SavedDataType<>(
|
| 14 | Identifier.withDefaultNamespace("scoreboard"),
|
| 15 | ScoreboardSaveData::new,
|
| 16 | ScoreboardSaveData.Packed.CODEC.xmap(ScoreboardSaveData::new, ScoreboardSaveData::getData),
|
| 17 | DataFixTypes.SAVED_DATA_SCOREBOARD
|
| 18 | );
|
| 19 | private ScoreboardSaveData.Packed data;
|
| 20 |
|
| 21 | private ScoreboardSaveData() {
|
| 22 | this(ScoreboardSaveData.Packed.EMPTY);
|
| 23 | }
|
| 24 |
|
| 25 | public ScoreboardSaveData(final ScoreboardSaveData.Packed data) {
|
| 26 | this.data = data;
|
| 27 | }
|
| 28 |
|
| 29 | public ScoreboardSaveData.Packed getData() {
|
| 30 | return this.data;
|
| 31 | }
|
| 32 |
|
| 33 | public void setData(final ScoreboardSaveData.Packed data) {
|
| 34 | if (!data.equals(this.data)) {
|
| 35 | this.data = data;
|
| 36 | this.setDirty();
|
| 37 | }
|
| 38 | }
|
| 39 |
|
| 40 | public record Packed(
|
| 41 | List<Objective.Packed> objectives, List<Scoreboard.PackedScore> scores, Map<DisplaySlot, String> displaySlots, List<PlayerTeam.Packed> teams
|
| 42 | ) {
|
| 43 | public static final ScoreboardSaveData.Packed EMPTY = new ScoreboardSaveData.Packed(List.of(), List.of(), Map.of(), List.of());
|
| 44 | public static final Codec<ScoreboardSaveData.Packed> CODEC = RecordCodecBuilder.create(
|
| 45 | i -> i.group(
|
| 46 | Objective.Packed.CODEC.listOf().optionalFieldOf("Objectives", List.of()).forGetter(ScoreboardSaveData.Packed::objectives),
|
| 47 | Scoreboard.PackedScore.CODEC.listOf().optionalFieldOf("PlayerScores", List.of()).forGetter(ScoreboardSaveData.Packed::scores),
|
| 48 | Codec.unboundedMap(DisplaySlot.CODEC, Codec.STRING).optionalFieldOf("DisplaySlots", Map.of()).forGetter(ScoreboardSaveData.Packed::displaySlots),
|
| 49 | PlayerTeam.Packed.CODEC.listOf().optionalFieldOf("Teams", List.of()).forGetter(ScoreboardSaveData.Packed::teams)
|
| 50 | )
|
| 51 | .apply(i, ScoreboardSaveData.Packed::new)
|
| 52 | );
|
| 53 | }
|
| 54 | }
|
| 55 |
|