| 1 | package net.minecraft.advancements;
|
| 2 |
|
| 3 | import com.mojang.serialization.Codec;
|
| 4 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 5 | import java.util.Optional;
|
| 6 | import net.minecraft.core.ClientAsset;
|
| 7 | import net.minecraft.network.RegistryFriendlyByteBuf;
|
| 8 | import net.minecraft.network.chat.Component;
|
| 9 | import net.minecraft.network.chat.ComponentSerialization;
|
| 10 | import net.minecraft.network.codec.StreamCodec;
|
| 11 | import net.minecraft.world.item.ItemStackTemplate;
|
| 12 |
|
| 13 | public record DisplayInfo(
|
| 14 | ItemStackTemplate icon,
|
| 15 | Component title,
|
| 16 | Component description,
|
| 17 | Optional<ClientAsset.ResourceTexture> background,
|
| 18 | AdvancementType type,
|
| 19 | boolean showToast,
|
| 20 | boolean announceToChat,
|
| 21 | boolean hidden
|
| 22 | ) {
|
| 23 | public static final Codec<DisplayInfo> CODEC = RecordCodecBuilder.create(
|
| 24 | i -> i.group(
|
| 25 | ItemStackTemplate.CODEC.fieldOf("icon").forGetter(DisplayInfo::icon),
|
| 26 | ComponentSerialization.CODEC.fieldOf("title").forGetter(DisplayInfo::title),
|
| 27 | ComponentSerialization.CODEC.fieldOf("description").forGetter(DisplayInfo::description),
|
| 28 | ClientAsset.ResourceTexture.CODEC.optionalFieldOf("background").forGetter(DisplayInfo::background),
|
| 29 | AdvancementType.CODEC.optionalFieldOf("frame", AdvancementType.TASK).forGetter(DisplayInfo::type),
|
| 30 | Codec.BOOL.optionalFieldOf("show_toast", true).forGetter(DisplayInfo::showToast),
|
| 31 | Codec.BOOL.optionalFieldOf("announce_to_chat", true).forGetter(DisplayInfo::announceToChat),
|
| 32 | Codec.BOOL.optionalFieldOf("hidden", false).forGetter(DisplayInfo::hidden)
|
| 33 | )
|
| 34 | .apply(i, DisplayInfo::new)
|
| 35 | );
|
| 36 | public static final StreamCodec<RegistryFriendlyByteBuf, DisplayInfo> STREAM_CODEC = StreamCodec.ofMember(
|
| 37 | DisplayInfo::serializeToNetwork, DisplayInfo::fromNetwork
|
| 38 | );
|
| 39 |
|
| 40 | private void serializeToNetwork(final RegistryFriendlyByteBuf output) {
|
| 41 | ComponentSerialization.TRUSTED_STREAM_CODEC.encode(output, this.title);
|
| 42 | ComponentSerialization.TRUSTED_STREAM_CODEC.encode(output, this.description);
|
| 43 | ItemStackTemplate.STREAM_CODEC.encode(output, this.icon);
|
| 44 | AdvancementType.STREAM_CODEC.encode(output, this.type);
|
| 45 | int flags = 0;
|
| 46 | if (this.background.isPresent()) {
|
| 47 | flags |= 1;
|
| 48 | }
|
| 49 |
|
| 50 | if (this.showToast) {
|
| 51 | flags |= 2;
|
| 52 | }
|
| 53 |
|
| 54 | if (this.hidden) {
|
| 55 | flags |= 4;
|
| 56 | }
|
| 57 |
|
| 58 | output.writeInt(flags);
|
| 59 | this.background.map(ClientAsset::id).ifPresent(output::writeIdentifier);
|
| 60 | }
|
| 61 |
|
| 62 | private static DisplayInfo fromNetwork(final RegistryFriendlyByteBuf input) {
|
| 63 | Component title = ComponentSerialization.TRUSTED_STREAM_CODEC.decode(input);
|
| 64 | Component description = ComponentSerialization.TRUSTED_STREAM_CODEC.decode(input);
|
| 65 | ItemStackTemplate icon = ItemStackTemplate.STREAM_CODEC.decode(input);
|
| 66 | AdvancementType frame = AdvancementType.STREAM_CODEC.decode(input);
|
| 67 | int flags = input.readInt();
|
| 68 | Optional<ClientAsset.ResourceTexture> background = (flags & 1) != 0 ? Optional.of(new ClientAsset.ResourceTexture(input.readIdentifier())) : Optional.empty();
|
| 69 | boolean showToast = (flags & 2) != 0;
|
| 70 | boolean hidden = (flags & 4) != 0;
|
| 71 | return new DisplayInfo(icon, title, description, background, frame, showToast, false, hidden);
|
| 72 | }
|
| 73 | }
|
| 74 |
|