| 1 | package net.minecraft.world.item.component;
|
| 2 |
|
| 3 | import com.mojang.serialization.Codec;
|
| 4 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 5 | import it.unimi.dsi.fastutil.objects.ReferenceLinkedOpenHashSet;
|
| 6 | import it.unimi.dsi.fastutil.objects.ReferenceSortedSets;
|
| 7 | import java.util.List;
|
| 8 | import java.util.SequencedSet;
|
| 9 | import net.minecraft.core.component.DataComponentType;
|
| 10 | import net.minecraft.network.RegistryFriendlyByteBuf;
|
| 11 | import net.minecraft.network.codec.ByteBufCodecs;
|
| 12 | import net.minecraft.network.codec.StreamCodec;
|
| 13 |
|
| 14 | public record TooltipDisplay(boolean hideTooltip, SequencedSet<DataComponentType<?>> hiddenComponents) {
|
| 15 | private static final Codec<SequencedSet<DataComponentType<?>>> COMPONENT_SET_CODEC = DataComponentType.CODEC
|
| 16 | .listOf()
|
| 17 | .xmap(ReferenceLinkedOpenHashSet::new, List::copyOf);
|
| 18 | public static final Codec<TooltipDisplay> CODEC = RecordCodecBuilder.create(
|
| 19 | i -> i.group(
|
| 20 | Codec.BOOL.optionalFieldOf("hide_tooltip", false).forGetter(TooltipDisplay::hideTooltip),
|
| 21 | COMPONENT_SET_CODEC.optionalFieldOf("hidden_components", ReferenceSortedSets.emptySet()).forGetter(TooltipDisplay::hiddenComponents)
|
| 22 | )
|
| 23 | .apply(i, TooltipDisplay::new)
|
| 24 | );
|
| 25 | public static final StreamCodec<RegistryFriendlyByteBuf, TooltipDisplay> STREAM_CODEC = StreamCodec.composite(
|
| 26 | ByteBufCodecs.BOOL,
|
| 27 | TooltipDisplay::hideTooltip,
|
| 28 | DataComponentType.STREAM_CODEC.apply(ByteBufCodecs.collection(ReferenceLinkedOpenHashSet::new)),
|
| 29 | TooltipDisplay::hiddenComponents,
|
| 30 | TooltipDisplay::new
|
| 31 | );
|
| 32 | public static final TooltipDisplay DEFAULT = new TooltipDisplay(false, ReferenceSortedSets.emptySet());
|
| 33 |
|
| 34 | public TooltipDisplay withHidden(final DataComponentType<?> component, final boolean hidden) {
|
| 35 | if (this.hiddenComponents.contains(component) == hidden) {
|
| 36 | return this;
|
| 37 | }
|
| 38 |
|
| 39 | SequencedSet<DataComponentType<?>> newHiddenComponents = new ReferenceLinkedOpenHashSet<>(this.hiddenComponents);
|
| 40 | if (hidden) {
|
| 41 | newHiddenComponents.add(component);
|
| 42 | } else {
|
| 43 | newHiddenComponents.remove(component);
|
| 44 | }
|
| 45 |
|
| 46 | return new TooltipDisplay(this.hideTooltip, newHiddenComponents);
|
| 47 | }
|
| 48 |
|
| 49 | public boolean shows(final DataComponentType<?> component) {
|
| 50 | return !this.hideTooltip && !this.hiddenComponents.contains(component);
|
| 51 | }
|
| 52 | }
|
| 53 |
|