MapBanner .java

net.minecraft.world.level.saveddata.maps.MapBanner (record, 60 lines): members: color, getDecoration, fromWorld, getId, name, pos, CODEC. strings: "color".

net.minecraft.world.level.saveddata.mapsrecord60 linescommon2.3 KB

API Surface (3)

record MapBanner(BlockPos pos, DyeColor color, Optional<Component> name)
Holder<MapDecorationType> getDecoration()
String getId()

Fields & Constants (1)

CODEC

String literals (2)

"color"
"banner-"

Full Source

MapBanner.java60 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.level.saveddata.maps;
2
3
import com.mojang.serialization.Codec;
4
import com.mojang.serialization.codecs.RecordCodecBuilder;
5
import java.util.Optional;
6
import net.minecraft.core.BlockPos;
7
import net.minecraft.core.Holder;
8
import net.minecraft.network.chat.Component;
9
import net.minecraft.network.chat.ComponentSerialization;
10
import net.minecraft.world.item.DyeColor;
11
import net.minecraft.world.level.BlockGetter;
12
import net.minecraft.world.level.block.entity.BannerBlockEntity;
13
import org.jspecify.annotations.Nullable;
14
15
public record MapBanner(BlockPos pos, DyeColor color, Optional<Component> name) {
16
	public static final Codec<MapBanner> CODEC = RecordCodecBuilder.create(
17
		i -> i.group(
18
				BlockPos.CODEC.fieldOf("pos").forGetter(MapBanner::pos),
19
				DyeColor.CODEC.lenientOptionalFieldOf("color", DyeColor.WHITE).forGetter(MapBanner::color),
20
				ComponentSerialization.CODEC.lenientOptionalFieldOf("name").forGetter(MapBanner::name)
21
			)
22
			.apply(i, MapBanner::new)
23
	);
24
25
	public static @Nullable MapBanner fromWorld(final BlockGetter level, final BlockPos pos) {
26
		if (level.getBlockEntity(pos) instanceof BannerBlockEntity banner) {
27
			DyeColor color = banner.getBaseColor();
28
			Optional<Component> name = Optional.ofNullable(banner.getCustomName());
29
			return new MapBanner(pos, color, name);
30
		} else {
31
			return null;
32
		}
33
	}
34
35
	public Holder<MapDecorationType> getDecoration() {
36
		return switch (this.color) {
37
			case WHITE -> MapDecorationTypes.WHITE_BANNER;
38
			case ORANGE -> MapDecorationTypes.ORANGE_BANNER;
39
			case MAGENTA -> MapDecorationTypes.MAGENTA_BANNER;
40
			case LIGHT_BLUE -> MapDecorationTypes.LIGHT_BLUE_BANNER;
41
			case YELLOW -> MapDecorationTypes.YELLOW_BANNER;
42
			case LIME -> MapDecorationTypes.LIME_BANNER;
43
			case PINK -> MapDecorationTypes.PINK_BANNER;
44
			case GRAY -> MapDecorationTypes.GRAY_BANNER;
45
			case LIGHT_GRAY -> MapDecorationTypes.LIGHT_GRAY_BANNER;
46
			case CYAN -> MapDecorationTypes.CYAN_BANNER;
47
			case PURPLE -> MapDecorationTypes.PURPLE_BANNER;
48
			case BLUE -> MapDecorationTypes.BLUE_BANNER;
49
			case BROWN -> MapDecorationTypes.BROWN_BANNER;
50
			case GREEN -> MapDecorationTypes.GREEN_BANNER;
51
			case RED -> MapDecorationTypes.RED_BANNER;
52
			case BLACK -> MapDecorationTypes.BLACK_BANNER;
53
		};
54
	}
55
56
	public String getId() {
57
		return "banner-" + this.pos.getX() + "," + this.pos.getY() + "," + this.pos.getZ();
58
	}
59
}
60

Decompiled Minecraft 26.3 source of MapBanner (net/minecraft/world/level/saveddata/maps/MapBanner.java) with Mojang official mappings, stored verbatim on the site.