| 1 | package net.minecraft.world.level.saveddata.maps;
|
| 2 |
|
| 3 | import com.mojang.serialization.Codec;
|
| 4 | import io.netty.buffer.ByteBuf;
|
| 5 | import java.util.function.Consumer;
|
| 6 | import net.minecraft.ChatFormatting;
|
| 7 | import net.minecraft.core.component.DataComponentGetter;
|
| 8 | import net.minecraft.core.component.DataComponents;
|
| 9 | import net.minecraft.network.chat.Component;
|
| 10 | import net.minecraft.network.codec.ByteBufCodecs;
|
| 11 | import net.minecraft.network.codec.StreamCodec;
|
| 12 | import net.minecraft.world.item.Item;
|
| 13 | import net.minecraft.world.item.TooltipFlag;
|
| 14 | import net.minecraft.world.item.component.MapPostProcessing;
|
| 15 | import net.minecraft.world.item.component.TooltipProvider;
|
| 16 |
|
| 17 | public record MapId(int id) implements TooltipProvider {
|
| 18 | public static final Codec<MapId> CODEC = Codec.INT.xmap(MapId::new, MapId::id);
|
| 19 | public static final StreamCodec<ByteBuf, MapId> STREAM_CODEC = ByteBufCodecs.VAR_INT.map(MapId::new, MapId::id);
|
| 20 | private static final Component LOCKED_TEXT = Component.translatable("filled_map.locked").withStyle(ChatFormatting.GRAY);
|
| 21 |
|
| 22 | public String key() {
|
| 23 | return "maps/" + this.id;
|
| 24 | }
|
| 25 |
|
| 26 | @Override
|
| 27 | public void addToTooltip(final Item.TooltipContext context, final Consumer<Component> consumer, final TooltipFlag flag, final DataComponentGetter components) {
|
| 28 | MapItemSavedData data = context.mapData(this);
|
| 29 | if (data == null) {
|
| 30 | consumer.accept(Component.translatable("filled_map.unknown").withStyle(ChatFormatting.GRAY));
|
| 31 | } else {
|
| 32 | MapPostProcessing postProcessing = components.get(DataComponents.MAP_POST_PROCESSING);
|
| 33 | if (components.get(DataComponents.CUSTOM_NAME) == null && postProcessing == null) {
|
| 34 | consumer.accept(Component.translatable("filled_map.id", this.id).withStyle(ChatFormatting.GRAY));
|
| 35 | }
|
| 36 |
|
| 37 | if (data.locked || postProcessing == MapPostProcessing.LOCK) {
|
| 38 | consumer.accept(LOCKED_TEXT);
|
| 39 | }
|
| 40 |
|
| 41 | if (flag.isAdvanced()) {
|
| 42 | int scaleToAdd = postProcessing == MapPostProcessing.SCALE ? 1 : 0;
|
| 43 | int scale = Math.min(data.scale + scaleToAdd, 4);
|
| 44 | consumer.accept(Component.translatable("filled_map.scale", 1 << scale).withStyle(ChatFormatting.GRAY));
|
| 45 | consumer.accept(Component.translatable("filled_map.level", scale, 4).withStyle(ChatFormatting.GRAY));
|
| 46 | }
|
| 47 | }
|
| 48 | }
|
| 49 | }
|
| 50 |
|