| 1 | package net.minecraft.advancements;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableMap;
|
| 4 | import com.mojang.serialization.Codec;
|
| 5 | import com.mojang.serialization.DataResult;
|
| 6 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 7 | import java.util.Map;
|
| 8 | import java.util.Optional;
|
| 9 | import net.fabricmc.fabric.api.advancement.v1.FabricAdvancementBuilder;
|
| 10 | import net.minecraft.ChatFormatting;
|
| 11 | import net.minecraft.advancements.triggers.Criterion;
|
| 12 | import net.minecraft.core.ClientAsset;
|
| 13 | import net.minecraft.core.HolderGetter;
|
| 14 | import net.minecraft.data.worldgen.BootstrapContext;
|
| 15 | import net.minecraft.network.RegistryFriendlyByteBuf;
|
| 16 | import net.minecraft.network.chat.Component;
|
| 17 | import net.minecraft.network.chat.ComponentUtils;
|
| 18 | import net.minecraft.network.chat.HoverEvent;
|
| 19 | import net.minecraft.network.chat.Style;
|
| 20 | import net.minecraft.network.codec.ByteBufCodecs;
|
| 21 | import net.minecraft.network.codec.StreamCodec;
|
| 22 | import net.minecraft.resources.Identifier;
|
| 23 | import net.minecraft.util.ProblemReporter;
|
| 24 | import net.minecraft.world.item.Item;
|
| 25 | import net.minecraft.world.item.ItemStackTemplate;
|
| 26 | import net.minecraft.world.level.storage.loot.ValidationContextSource;
|
| 27 |
|
| 28 | public record Advancement(
|
| 29 | Optional<Identifier> parent,
|
| 30 | Optional<DisplayInfo> display,
|
| 31 | AdvancementRewards rewards,
|
| 32 | Map<String, Criterion<?>> criteria,
|
| 33 | AdvancementRequirements requirements,
|
| 34 | boolean sendsTelemetryEvent,
|
| 35 | Optional<Component> name
|
| 36 | ) {
|
| 37 | private static final Codec<Map<String, Criterion<?>>> CRITERIA_CODEC = Codec.unboundedMap(Codec.STRING, Criterion.CODEC)
|
| 38 | .validate(criteria -> criteria.isEmpty() ? DataResult.error(() -> "Advancement criteria cannot be empty") : DataResult.success(criteria));
|
| 39 | public static final Codec<Advancement> CODEC = RecordCodecBuilder.<Advancement>create(
|
| 40 | i -> i.group(
|
| 41 | Identifier.CODEC.optionalFieldOf("parent").forGetter(Advancement::parent),
|
| 42 | DisplayInfo.CODEC.optionalFieldOf("display").forGetter(Advancement::display),
|
| 43 | AdvancementRewards.CODEC.optionalFieldOf("rewards", AdvancementRewards.EMPTY).forGetter(Advancement::rewards),
|
| 44 | CRITERIA_CODEC.fieldOf("criteria").forGetter(Advancement::criteria),
|
| 45 | AdvancementRequirements.CODEC.optionalFieldOf("requirements").forGetter(a -> Optional.of(a.requirements())),
|
| 46 | Codec.BOOL.optionalFieldOf("sends_telemetry_event", false).forGetter(Advancement::sendsTelemetryEvent)
|
| 47 | )
|
| 48 | .apply(i, (parent, display, rewards, criteria, requirementsOpt, sendsTelemetryEvent) -> {
|
| 49 | AdvancementRequirements requirements = requirementsOpt.orElseGet(() -> AdvancementRequirements.allOf(criteria.keySet()));
|
| 50 | return new Advancement(parent, display, rewards, criteria, requirements, sendsTelemetryEvent);
|
| 51 | })
|
| 52 | )
|
| 53 | .validate(Advancement::validate);
|
| 54 | public static final StreamCodec<RegistryFriendlyByteBuf, Advancement> STREAM_CODEC = StreamCodec.composite(
|
| 55 | Identifier.STREAM_CODEC.apply(ByteBufCodecs::optional),
|
| 56 | Advancement::parent,
|
| 57 | DisplayInfo.STREAM_CODEC.apply(ByteBufCodecs::optional),
|
| 58 | Advancement::display,
|
| 59 | AdvancementRequirements.STREAM_CODEC,
|
| 60 | Advancement::requirements,
|
| 61 | ByteBufCodecs.BOOL,
|
| 62 | Advancement::sendsTelemetryEvent,
|
| 63 | (parent, display, requirements, sendsTelemetryEvent) -> new Advancement(
|
| 64 | parent, display, AdvancementRewards.EMPTY, Map.of(), requirements, sendsTelemetryEvent
|
| 65 | )
|
| 66 | );
|
| 67 |
|
| 68 | public Advancement(
|
| 69 | final Optional<Identifier> parent,
|
| 70 | final Optional<DisplayInfo> display,
|
| 71 | final AdvancementRewards rewards,
|
| 72 | final Map<String, Criterion<?>> criteria,
|
| 73 | final AdvancementRequirements requirements,
|
| 74 | final boolean sendsTelemetryEvent
|
| 75 | ) {
|
| 76 | this(parent, display, rewards, Map.copyOf(criteria), requirements, sendsTelemetryEvent, display.map(Advancement::decorateName));
|
| 77 | }
|
| 78 |
|
| 79 | private static DataResult<Advancement> validate(final Advancement advancement) {
|
| 80 | boolean isRoot = advancement.parent.isEmpty();
|
| 81 | boolean isVisible = advancement.display.isPresent();
|
| 82 | boolean hasBackground = isVisible && advancement.display.get().background().isPresent();
|
| 83 | if (isRoot && isVisible && !hasBackground) {
|
| 84 | return DataResult.error(() -> "Visible advancement roots must have background");
|
| 85 | }
|
| 86 |
|
| 87 | if (!isRoot && hasBackground) {
|
| 88 | return DataResult.error(() -> "Only advancement roots can have background");
|
| 89 | }
|
| 90 |
|
| 91 | DataResult<AdvancementRequirements> requirementValidation = advancement.requirements().validate(advancement.criteria().keySet());
|
| 92 | return requirementValidation.isError() ? requirementValidation.map(var1x -> advancement) : DataResult.success(advancement);
|
| 93 | }
|
| 94 |
|
| 95 | private static Component decorateName(final DisplayInfo display) {
|
| 96 | Component displayTitle = display.title();
|
| 97 | ChatFormatting color = display.type().getChatColor();
|
| 98 | Component tooltip = ComponentUtils.mergeStyles(displayTitle.copy(), Style.EMPTY.withColor(color)).append("\n").append(display.description());
|
| 99 | Component title = displayTitle.copy().withStyle(s -> s.withHoverEvent(new HoverEvent.ShowText(tooltip)));
|
| 100 | return ComponentUtils.wrapInSquareBrackets(title).withStyle(color);
|
| 101 | }
|
| 102 |
|
| 103 | public static Component name(final AdvancementHolder holder) {
|
| 104 | return holder.value().name().orElseGet(() -> Component.literal(holder.id().toString()));
|
| 105 | }
|
| 106 |
|
| 107 | public boolean isRoot() {
|
| 108 | return this.parent.isEmpty();
|
| 109 | }
|
| 110 |
|
| 111 | public void validate(final ProblemReporter reporter, final HolderGetter.Provider lootData) {
|
| 112 | this.criteria.forEach((name, criterion) -> {
|
| 113 | ValidationContextSource validator = new ValidationContextSource(reporter.forChild(new ProblemReporter.RootFieldPathElement(name)), lootData);
|
| 114 | criterion.triggerInstance().validate(validator);
|
| 115 | });
|
| 116 | }
|
| 117 |
|
| 118 | public static class Builder implements FabricAdvancementBuilder, net.fabricmc.fabric.api.datagen.v1.advancement.FabricAdvancementBuilder {
|
| 119 | private Optional<Identifier> parent = Optional.empty();
|
| 120 | private Optional<DisplayInfo> display = Optional.empty();
|
| 121 | private AdvancementRewards rewards = AdvancementRewards.EMPTY;
|
| 122 | private final ImmutableMap.Builder<String, Criterion<?>> criteria = ImmutableMap.builder();
|
| 123 | private Optional<AdvancementRequirements> requirements = Optional.empty();
|
| 124 | private AdvancementRequirements.Strategy requirementsStrategy = AdvancementRequirements.Strategy.AND;
|
| 125 | private boolean sendsTelemetryEvent;
|
| 126 |
|
| 127 | public static Advancement.Builder advancement() {
|
| 128 | return new Advancement.Builder().sendsTelemetryEvent();
|
| 129 | }
|
| 130 |
|
| 131 | public static Advancement.Builder recipeAdvancement() {
|
| 132 | return new Advancement.Builder();
|
| 133 | }
|
| 134 |
|
| 135 | public Advancement.Builder parent(final AdvancementHolder parent) {
|
| 136 | this.parent = Optional.of(parent.id());
|
| 137 | return this;
|
| 138 | }
|
| 139 |
|
| 140 | @Deprecated(forRemoval = true)
|
| 141 | public Advancement.Builder parent(final Identifier parent) {
|
| 142 | this.parent = Optional.of(parent);
|
| 143 | return this;
|
| 144 | }
|
| 145 |
|
| 146 | public Advancement.Builder rootDisplay(
|
| 147 | final ItemStackTemplate icon,
|
| 148 | final Component title,
|
| 149 | final Component description,
|
| 150 | final Identifier background,
|
| 151 | final AdvancementType frame,
|
| 152 | final boolean showToast,
|
| 153 | final boolean announceChat,
|
| 154 | final boolean hidden
|
| 155 | ) {
|
| 156 | return this.display(
|
| 157 | new DisplayInfo(icon, title, description, Optional.of(new ClientAsset.ResourceTexture(background)), frame, showToast, announceChat, hidden)
|
| 158 | );
|
| 159 | }
|
| 160 |
|
| 161 | public Advancement.Builder rootDisplay(
|
| 162 | final Item icon,
|
| 163 | final Component title,
|
| 164 | final Component description,
|
| 165 | final Identifier background,
|
| 166 | final AdvancementType frame,
|
| 167 | final boolean showToast,
|
| 168 | final boolean announceChat,
|
| 169 | final boolean hidden
|
| 170 | ) {
|
| 171 | return this.rootDisplay(new ItemStackTemplate(icon), title, description, background, frame, showToast, announceChat, hidden);
|
| 172 | }
|
| 173 |
|
| 174 | public Advancement.Builder display(
|
| 175 | final ItemStackTemplate icon,
|
| 176 | final Component title,
|
| 177 | final Component description,
|
| 178 | final AdvancementType frame,
|
| 179 | final boolean showToast,
|
| 180 | final boolean announceChat,
|
| 181 | final boolean hidden
|
| 182 | ) {
|
| 183 | return this.display(new DisplayInfo(icon, title, description, Optional.empty(), frame, showToast, announceChat, hidden));
|
| 184 | }
|
| 185 |
|
| 186 | public Advancement.Builder display(
|
| 187 | final Item icon,
|
| 188 | final Component title,
|
| 189 | final Component description,
|
| 190 | final AdvancementType frame,
|
| 191 | final boolean showToast,
|
| 192 | final boolean announceChat,
|
| 193 | final boolean hidden
|
| 194 | ) {
|
| 195 | return this.display(new ItemStackTemplate(icon), title, description, frame, showToast, announceChat, hidden);
|
| 196 | }
|
| 197 |
|
| 198 | public Advancement.Builder display(final DisplayInfo display) {
|
| 199 | this.display = Optional.of(display);
|
| 200 | return this;
|
| 201 | }
|
| 202 |
|
| 203 | public Advancement.Builder rewards(final AdvancementRewards.Builder rewards) {
|
| 204 | return this.rewards(rewards.build());
|
| 205 | }
|
| 206 |
|
| 207 | public Advancement.Builder rewards(final AdvancementRewards rewards) {
|
| 208 | this.rewards = rewards;
|
| 209 | return this;
|
| 210 | }
|
| 211 |
|
| 212 | public Advancement.Builder addCriterion(final String name, final Criterion<?> criterion) {
|
| 213 | this.criteria.put(name, criterion);
|
| 214 | return this;
|
| 215 | }
|
| 216 |
|
| 217 | public Advancement.Builder requirements(final AdvancementRequirements.Strategy strategy) {
|
| 218 | this.requirementsStrategy = strategy;
|
| 219 | return this;
|
| 220 | }
|
| 221 |
|
| 222 | public Advancement.Builder requirements(final AdvancementRequirements requirements) {
|
| 223 | this.requirements = Optional.of(requirements);
|
| 224 | return this;
|
| 225 | }
|
| 226 |
|
| 227 | public Advancement.Builder sendsTelemetryEvent() {
|
| 228 | this.sendsTelemetryEvent = true;
|
| 229 | return this;
|
| 230 | }
|
| 231 |
|
| 232 | public AdvancementHolder build(final Identifier id) {
|
| 233 | Map<String, Criterion<?>> criteria = this.criteria.buildOrThrow();
|
| 234 | AdvancementRequirements requirements = this.requirements.orElseGet(() -> this.requirementsStrategy.create(criteria.keySet()));
|
| 235 | return new AdvancementHolder(id, new Advancement(this.parent, this.display, this.rewards, criteria, requirements, this.sendsTelemetryEvent));
|
| 236 | }
|
| 237 |
|
| 238 | public AdvancementHolder save(final BootstrapContext<Advancement> output, final String name) {
|
| 239 | AdvancementHolder advancement = this.build(Identifier.parse(name));
|
| 240 | advancement.register(output);
|
| 241 | return advancement;
|
| 242 | }
|
| 243 | }
|
| 244 | }
|
| 245 |
|