| 1 | package net.minecraft.world.level.block;
|
| 2 |
|
| 3 | import java.util.function.BiConsumer;
|
| 4 | import net.minecraft.core.BlockPos;
|
| 5 | import net.minecraft.core.Direction;
|
| 6 | import net.minecraft.server.level.ServerLevel;
|
| 7 | import net.minecraft.sounds.SoundEvents;
|
| 8 | import net.minecraft.sounds.SoundSource;
|
| 9 | import net.minecraft.tags.BlockTags;
|
| 10 | import net.minecraft.util.RandomSource;
|
| 11 | import net.minecraft.world.Containers;
|
| 12 | import net.minecraft.world.attribute.EnvironmentAttributes;
|
| 13 | import net.minecraft.world.entity.player.Player;
|
| 14 | import net.minecraft.world.item.ItemStack;
|
| 15 | import net.minecraft.world.item.context.BlockPlaceContext;
|
| 16 | import net.minecraft.world.level.Explosion;
|
| 17 | import net.minecraft.world.level.Level;
|
| 18 | import net.minecraft.world.level.LevelAccessor;
|
| 19 | import net.minecraft.world.level.LevelReader;
|
| 20 | import net.minecraft.world.level.ScheduledTickAccess;
|
| 21 | import net.minecraft.world.level.ServerExplosion;
|
| 22 | import net.minecraft.world.level.block.entity.BlockEntity;
|
| 23 | import net.minecraft.world.level.block.entity.BlockEntityTicker;
|
| 24 | import net.minecraft.world.level.block.entity.BlockEntityType;
|
| 25 | import net.minecraft.world.level.block.entity.BlockEntityTypes;
|
| 26 | import net.minecraft.world.level.block.entity.CreakingHeartBlockEntity;
|
| 27 | import net.minecraft.world.level.block.state.BlockBehaviour;
|
| 28 | import net.minecraft.world.level.block.state.BlockState;
|
| 29 | import net.minecraft.world.level.block.state.StateDefinition;
|
| 30 | import net.minecraft.world.level.block.state.properties.BlockStateProperties;
|
| 31 | import net.minecraft.world.level.block.state.properties.BooleanProperty;
|
| 32 | import net.minecraft.world.level.block.state.properties.CreakingHeartState;
|
| 33 | import net.minecraft.world.level.block.state.properties.EnumProperty;
|
| 34 | import org.jspecify.annotations.Nullable;
|
| 35 |
|
| 36 | /**
|
| 37 | * Access widened by fabric-transitive-access-wideners-v1 to accessible
|
| 38 | */
|
| 39 | public class CreakingHeartBlock extends BaseEntityBlock {
|
| 40 | public static final EnumProperty<Direction.Axis> AXIS = BlockStateProperties.AXIS;
|
| 41 | public static final EnumProperty<CreakingHeartState> STATE = BlockStateProperties.CREAKING_HEART_STATE;
|
| 42 | public static final BooleanProperty NATURAL = BlockStateProperties.NATURAL;
|
| 43 |
|
| 44 | /**
|
| 45 | * Access widened by fabric-transitive-access-wideners-v1 to accessible
|
| 46 | */
|
| 47 | public CreakingHeartBlock(final BlockBehaviour.Properties properties) {
|
| 48 | super(properties);
|
| 49 | this.registerDefaultState(this.defaultBlockState().setValue(AXIS, Direction.Axis.Y).setValue(STATE, CreakingHeartState.UPROOTED).setValue(NATURAL, false));
|
| 50 | }
|
| 51 |
|
| 52 | @Override
|
| 53 | public BlockEntity newBlockEntity(final BlockPos worldPosition, final BlockState blockState) {
|
| 54 | return new CreakingHeartBlockEntity(worldPosition, blockState);
|
| 55 | }
|
| 56 |
|
| 57 | @Override
|
| 58 | public <T extends BlockEntity> @Nullable BlockEntityTicker<T> getTicker(final Level level, final BlockState blockState, final BlockEntityType<T> type) {
|
| 59 | if (level.isClientSide()) {
|
| 60 | return null;
|
| 61 | } else {
|
| 62 | return blockState.getValue(STATE) != CreakingHeartState.UPROOTED
|
| 63 | ? createTickerHelper(type, BlockEntityTypes.CREAKING_HEART, CreakingHeartBlockEntity::serverTick)
|
| 64 | : null;
|
| 65 | }
|
| 66 | }
|
| 67 |
|
| 68 | @Override
|
| 69 | public void animateTick(final BlockState state, final Level level, final BlockPos pos, final RandomSource random) {
|
| 70 | if (level.environmentAttributes().getValue(EnvironmentAttributes.CREAKING_ACTIVE, pos)) {
|
| 71 | if (state.getValue(STATE) != CreakingHeartState.UPROOTED) {
|
| 72 | if (random.nextInt(16) == 0 && isSurroundedByLogs(level, pos)) {
|
| 73 | level.playLocalSound(pos.getX(), pos.getY(), pos.getZ(), SoundEvents.CREAKING_HEART_IDLE, SoundSource.BLOCKS, 1.0F, 1.0F, false);
|
| 74 | }
|
| 75 | }
|
| 76 | }
|
| 77 | }
|
| 78 |
|
| 79 | @Override
|
| 80 | protected BlockState updateShape(
|
| 81 | final BlockState state,
|
| 82 | final LevelReader level,
|
| 83 | final ScheduledTickAccess ticks,
|
| 84 | final BlockPos pos,
|
| 85 | final Direction directionToNeighbour,
|
| 86 | final BlockPos neighbourPos,
|
| 87 | final BlockState neighbourState,
|
| 88 | final RandomSource random
|
| 89 | ) {
|
| 90 | ticks.scheduleTick(pos, this, 1);
|
| 91 | return super.updateShape(state, level, ticks, pos, directionToNeighbour, neighbourPos, neighbourState, random);
|
| 92 | }
|
| 93 |
|
| 94 | @Override
|
| 95 | protected void tick(final BlockState state, final ServerLevel level, final BlockPos pos, final RandomSource random) {
|
| 96 | BlockState newState = updateState(state, level, pos);
|
| 97 | if (newState != state) {
|
| 98 | level.setBlockAndUpdate(pos, newState);
|
| 99 | }
|
| 100 | }
|
| 101 |
|
| 102 | private static BlockState updateState(final BlockState state, final Level level, final BlockPos pos) {
|
| 103 | boolean hasLogs = hasRequiredLogs(state, level, pos);
|
| 104 | boolean disabled = state.getValue(STATE) == CreakingHeartState.UPROOTED;
|
| 105 | return hasLogs && disabled
|
| 106 | ? state.setValue(
|
| 107 | STATE, level.environmentAttributes().getValue(EnvironmentAttributes.CREAKING_ACTIVE, pos) ? CreakingHeartState.AWAKE : CreakingHeartState.DORMANT
|
| 108 | )
|
| 109 | : state;
|
| 110 | }
|
| 111 |
|
| 112 | public static boolean hasRequiredLogs(final BlockState state, final LevelReader level, final BlockPos pos) {
|
| 113 | Direction.Axis axis = state.getValue(AXIS);
|
| 114 |
|
| 115 | for (Direction dir : axis.getDirections()) {
|
| 116 | BlockState neigbour = level.getBlockState(pos.relative(dir));
|
| 117 | if (!neigbour.is(BlockTags.PALE_OAK_LOGS) || neigbour.getValue(AXIS) != axis) {
|
| 118 | return false;
|
| 119 | }
|
| 120 | }
|
| 121 |
|
| 122 | return true;
|
| 123 | }
|
| 124 |
|
| 125 | private static boolean isSurroundedByLogs(final LevelAccessor level, final BlockPos pos) {
|
| 126 | for (Direction dir : Direction.values()) {
|
| 127 | BlockPos neighbourPos = pos.relative(dir);
|
| 128 | BlockState neighbourState = level.getBlockState(neighbourPos);
|
| 129 | if (!neighbourState.is(BlockTags.PALE_OAK_LOGS)) {
|
| 130 | return false;
|
| 131 | }
|
| 132 | }
|
| 133 |
|
| 134 | return true;
|
| 135 | }
|
| 136 |
|
| 137 | @Override
|
| 138 | public @Nullable BlockState getStateForPlacement(final BlockPlaceContext context) {
|
| 139 | return updateState(this.defaultBlockState().setValue(AXIS, context.getClickedFace().getAxis()), context.getLevel(), context.getClickedPos());
|
| 140 | }
|
| 141 |
|
| 142 | @Override
|
| 143 | protected BlockState rotate(final BlockState state, final Rotation rotation) {
|
| 144 | return RotatedPillarBlock.rotatePillar(state, rotation);
|
| 145 | }
|
| 146 |
|
| 147 | @Override
|
| 148 | protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder) {
|
| 149 | builder.add(AXIS, STATE, NATURAL);
|
| 150 | }
|
| 151 |
|
| 152 | @Override
|
| 153 | protected void affectNeighborsAfterRemoval(final BlockState state, final ServerLevel level, final BlockPos pos, final boolean movedByPiston) {
|
| 154 | Containers.updateNeighboursAfterDestroy(state, level, pos);
|
| 155 | }
|
| 156 |
|
| 157 | @Override
|
| 158 | protected void onExplosionHit(
|
| 159 | final BlockState state, final ServerLevel level, final BlockPos pos, final Explosion explosion, final BiConsumer<ItemStack, BlockPos> onHit
|
| 160 | ) {
|
| 161 | if (level.getBlockEntity(pos) instanceof CreakingHeartBlockEntity creakingHeartBlockEntity
|
| 162 | && explosion instanceof ServerExplosion serverExplosion
|
| 163 | && explosion.getBlockInteraction().shouldAffectBlocklikeEntities()) {
|
| 164 | creakingHeartBlockEntity.removeProtector(serverExplosion.getDamageSource());
|
| 165 | if (explosion.getIndirectSourceEntity() instanceof Player player && explosion.getBlockInteraction().shouldAffectBlocklikeEntities()) {
|
| 166 | this.tryAwardExperience(player, state, level, pos);
|
| 167 | }
|
| 168 | }
|
| 169 |
|
| 170 | super.onExplosionHit(state, level, pos, explosion, onHit);
|
| 171 | }
|
| 172 |
|
| 173 | @Override
|
| 174 | public BlockState playerWillDestroy(final Level level, final BlockPos pos, final BlockState state, final Player player) {
|
| 175 | if (level.getBlockEntity(pos) instanceof CreakingHeartBlockEntity creakingHeartBlockEntity) {
|
| 176 | creakingHeartBlockEntity.removeProtector(player.damageSources().playerAttack(player));
|
| 177 | this.tryAwardExperience(player, state, level, pos);
|
| 178 | }
|
| 179 |
|
| 180 | return super.playerWillDestroy(level, pos, state, player);
|
| 181 | }
|
| 182 |
|
| 183 | private void tryAwardExperience(final Player player, final BlockState state, final Level level, final BlockPos pos) {
|
| 184 | if (!player.preventsBlockDrops() && !player.isSpectator() && state.getValue(NATURAL) && level instanceof ServerLevel serverLevel) {
|
| 185 | this.popExperience(serverLevel, pos, level.getRandom().nextIntBetweenInclusive(20, 24));
|
| 186 | }
|
| 187 | }
|
| 188 |
|
| 189 | @Override
|
| 190 | protected boolean hasAnalogOutputSignal(final BlockState state) {
|
| 191 | return true;
|
| 192 | }
|
| 193 |
|
| 194 | @Override
|
| 195 | protected int getAnalogOutputSignal(final BlockState state, final Level level, final BlockPos pos, final Direction direction) {
|
| 196 | if (state.getValue(STATE) == CreakingHeartState.UPROOTED) {
|
| 197 | return 0;
|
| 198 | } else {
|
| 199 | return level.getBlockEntity(pos) instanceof CreakingHeartBlockEntity creakingHeartBlockEntity ? creakingHeartBlockEntity.getAnalogOutputSignal() : 0;
|
| 200 | }
|
| 201 | }
|
| 202 | }
|
| 203 |
|