| 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.resources.Identifier;
|
| 13 | import net.minecraft.world.item.component.ResolvableProfile;
|
| 14 | import net.minecraft.world.level.Level;
|
| 15 | import net.minecraft.world.level.block.SkullBlock;
|
| 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 SkullBlockEntity extends BlockEntity {
|
| 22 | private static final String TAG_PROFILE = "profile";
|
| 23 | private static final String TAG_NOTE_BLOCK_SOUND = "note_block_sound";
|
| 24 | private static final String TAG_CUSTOM_NAME = "custom_name";
|
| 25 | private @Nullable ResolvableProfile owner;
|
| 26 | private @Nullable Identifier noteBlockSound;
|
| 27 | private int animationTickCount;
|
| 28 | private boolean isAnimating;
|
| 29 | private @Nullable Component customName;
|
| 30 |
|
| 31 | public SkullBlockEntity(final BlockPos worldPosition, final BlockState blockState) {
|
| 32 | super(BlockEntityTypes.SKULL, worldPosition, blockState);
|
| 33 | }
|
| 34 |
|
| 35 | @Override
|
| 36 | protected void saveAdditional(final ValueOutput output) {
|
| 37 | super.saveAdditional(output);
|
| 38 | output.storeNullable("profile", ResolvableProfile.CODEC, this.owner);
|
| 39 | output.storeNullable("note_block_sound", Identifier.CODEC, this.noteBlockSound);
|
| 40 | output.storeNullable("custom_name", ComponentSerialization.CODEC, this.customName);
|
| 41 | }
|
| 42 |
|
| 43 | @Override
|
| 44 | protected void loadAdditional(final ValueInput input) {
|
| 45 | super.loadAdditional(input);
|
| 46 | this.owner = input.read("profile", ResolvableProfile.CODEC).orElse(null);
|
| 47 | this.noteBlockSound = input.read("note_block_sound", Identifier.CODEC).orElse(null);
|
| 48 | this.customName = parseCustomNameSafe(input, "custom_name");
|
| 49 | }
|
| 50 |
|
| 51 | public static void animation(final Level level, final BlockPos pos, final BlockState state, final SkullBlockEntity entity) {
|
| 52 | if (state.hasProperty(SkullBlock.POWERED) && state.getValue(SkullBlock.POWERED)) {
|
| 53 | entity.isAnimating = true;
|
| 54 | entity.animationTickCount++;
|
| 55 | } else {
|
| 56 | entity.isAnimating = false;
|
| 57 | }
|
| 58 | }
|
| 59 |
|
| 60 | public float getAnimation(final float a) {
|
| 61 | return this.isAnimating ? this.animationTickCount + a : this.animationTickCount;
|
| 62 | }
|
| 63 |
|
| 64 | public @Nullable ResolvableProfile getOwnerProfile() {
|
| 65 | return this.owner;
|
| 66 | }
|
| 67 |
|
| 68 | public @Nullable Identifier getNoteBlockSound() {
|
| 69 | return this.noteBlockSound;
|
| 70 | }
|
| 71 |
|
| 72 | public ClientboundBlockEntityDataPacket getUpdatePacket() {
|
| 73 | return ClientboundBlockEntityDataPacket.create(this);
|
| 74 | }
|
| 75 |
|
| 76 | @Override
|
| 77 | public CompoundTag getUpdateTag(final HolderLookup.Provider registries) {
|
| 78 | return this.saveCustomOnly(registries);
|
| 79 | }
|
| 80 |
|
| 81 | @Override
|
| 82 | protected void applyImplicitComponents(final DataComponentGetter components) {
|
| 83 | super.applyImplicitComponents(components);
|
| 84 | this.owner = components.get(DataComponents.PROFILE);
|
| 85 | this.noteBlockSound = components.get(DataComponents.NOTE_BLOCK_SOUND);
|
| 86 | this.customName = components.get(DataComponents.CUSTOM_NAME);
|
| 87 | }
|
| 88 |
|
| 89 | @Override
|
| 90 | protected void collectImplicitComponents(final DataComponentMap.Builder components) {
|
| 91 | super.collectImplicitComponents(components);
|
| 92 | components.set(DataComponents.PROFILE, this.owner);
|
| 93 | components.set(DataComponents.NOTE_BLOCK_SOUND, this.noteBlockSound);
|
| 94 | components.set(DataComponents.CUSTOM_NAME, this.customName);
|
| 95 | }
|
| 96 |
|
| 97 | @Override
|
| 98 | public void removeComponentsFromTag(final ValueOutput output) {
|
| 99 | super.removeComponentsFromTag(output);
|
| 100 | output.discard("profile");
|
| 101 | output.discard("note_block_sound");
|
| 102 | output.discard("custom_name");
|
| 103 | }
|
| 104 | }
|
| 105 |
|