| 1 | package net.minecraft.world.level.block.entity;
|
| 2 |
|
| 3 | import net.minecraft.core.BlockPos;
|
| 4 | import net.minecraft.core.HolderLookup;
|
| 5 | import net.minecraft.core.component.DataComponentGetter;
|
| 6 | import net.minecraft.core.component.DataComponentMap;
|
| 7 | import net.minecraft.core.component.DataComponents;
|
| 8 | import net.minecraft.nbt.CompoundTag;
|
| 9 | import net.minecraft.network.chat.Component;
|
| 10 | import net.minecraft.network.chat.ComponentSerialization;
|
| 11 | import net.minecraft.network.protocol.game.ClientboundBlockEntityDataPacket;
|
| 12 | import net.minecraft.world.Nameable;
|
| 13 | import net.minecraft.world.item.DyeColor;
|
| 14 | import net.minecraft.world.item.ItemStack;
|
| 15 | import net.minecraft.world.level.block.AbstractBannerBlock;
|
| 16 | import net.minecraft.world.level.block.state.BlockState;
|
| 17 | import net.minecraft.world.level.storage.ValueInput;
|
| 18 | import net.minecraft.world.level.storage.ValueOutput;
|
| 19 | import org.jspecify.annotations.Nullable;
|
| 20 |
|
| 21 | public class BannerBlockEntity extends BlockEntity implements Nameable {
|
| 22 | public static final int MAX_PATTERNS = 6;
|
| 23 | private static final String TAG_PATTERNS = "patterns";
|
| 24 | private static final Component DEFAULT_NAME = Component.translatable("block.minecraft.banner");
|
| 25 | private @Nullable Component name;
|
| 26 | private final DyeColor baseColor;
|
| 27 | private BannerPatternLayers patterns = BannerPatternLayers.EMPTY;
|
| 28 |
|
| 29 | public BannerBlockEntity(final BlockPos worldPosition, final BlockState blockState) {
|
| 30 | this(worldPosition, blockState, ((AbstractBannerBlock)blockState.getBlock()).getColor());
|
| 31 | }
|
| 32 |
|
| 33 | public BannerBlockEntity(final BlockPos worldPosition, final BlockState blockState, final DyeColor color) {
|
| 34 | super(BlockEntityTypes.BANNER, worldPosition, blockState);
|
| 35 | this.baseColor = color;
|
| 36 | }
|
| 37 |
|
| 38 | @Override
|
| 39 | public Component getName() {
|
| 40 | return this.name != null ? this.name : DEFAULT_NAME;
|
| 41 | }
|
| 42 |
|
| 43 | @Override
|
| 44 | public @Nullable Component getCustomName() {
|
| 45 | return this.name;
|
| 46 | }
|
| 47 |
|
| 48 | @Override
|
| 49 | protected void saveAdditional(final ValueOutput output) {
|
| 50 | super.saveAdditional(output);
|
| 51 | if (!this.patterns.equals(BannerPatternLayers.EMPTY)) {
|
| 52 | output.store("patterns", BannerPatternLayers.CODEC, this.patterns);
|
| 53 | }
|
| 54 |
|
| 55 | output.storeNullable("CustomName", ComponentSerialization.CODEC, this.name);
|
| 56 | }
|
| 57 |
|
| 58 | @Override
|
| 59 | protected void loadAdditional(final ValueInput input) {
|
| 60 | super.loadAdditional(input);
|
| 61 | this.name = parseCustomNameSafe(input, "CustomName");
|
| 62 | this.patterns = input.read("patterns", BannerPatternLayers.CODEC).orElse(BannerPatternLayers.EMPTY);
|
| 63 | }
|
| 64 |
|
| 65 | public ClientboundBlockEntityDataPacket getUpdatePacket() {
|
| 66 | return ClientboundBlockEntityDataPacket.create(this);
|
| 67 | }
|
| 68 |
|
| 69 | @Override
|
| 70 | public CompoundTag getUpdateTag(final HolderLookup.Provider registries) {
|
| 71 | return this.saveWithoutMetadata(registries);
|
| 72 | }
|
| 73 |
|
| 74 | public BannerPatternLayers getPatterns() {
|
| 75 | return this.patterns;
|
| 76 | }
|
| 77 |
|
| 78 | public ItemStack getItem() {
|
| 79 | ItemStack itemStack = new ItemStack(this.getBlockState().getBlock());
|
| 80 | itemStack.applyComponents(this.collectComponents());
|
| 81 | return itemStack;
|
| 82 | }
|
| 83 |
|
| 84 | public DyeColor getBaseColor() {
|
| 85 | return this.baseColor;
|
| 86 | }
|
| 87 |
|
| 88 | @Override
|
| 89 | protected void applyImplicitComponents(final DataComponentGetter components) {
|
| 90 | super.applyImplicitComponents(components);
|
| 91 | this.patterns = components.getOrDefault(DataComponents.BANNER_PATTERNS, BannerPatternLayers.EMPTY);
|
| 92 | this.name = components.get(DataComponents.CUSTOM_NAME);
|
| 93 | }
|
| 94 |
|
| 95 | @Override
|
| 96 | protected void collectImplicitComponents(final DataComponentMap.Builder components) {
|
| 97 | super.collectImplicitComponents(components);
|
| 98 | components.set(DataComponents.BANNER_PATTERNS, this.patterns);
|
| 99 | components.set(DataComponents.CUSTOM_NAME, this.name);
|
| 100 | }
|
| 101 |
|
| 102 | @Override
|
| 103 | public void removeComponentsFromTag(final ValueOutput output) {
|
| 104 | output.discard("patterns");
|
| 105 | output.discard("CustomName");
|
| 106 | }
|
| 107 | }
|
| 108 |
|