| 1 | package net.minecraft.world.level.block;
|
| 2 |
|
| 3 | import net.minecraft.core.BlockPos;
|
| 4 | import net.minecraft.sounds.SoundSource;
|
| 5 | import net.minecraft.tags.BlockTags;
|
| 6 | import net.minecraft.util.Mth;
|
| 7 | import net.minecraft.util.RandomSource;
|
| 8 | import net.minecraft.util.valueproviders.ConstantInt;
|
| 9 | import net.minecraft.world.level.LevelAccessor;
|
| 10 | import net.minecraft.world.level.block.state.BlockBehaviour;
|
| 11 | import net.minecraft.world.level.block.state.BlockState;
|
| 12 | import net.minecraft.world.level.block.state.properties.BlockStateProperties;
|
| 13 | import net.minecraft.world.level.material.Fluids;
|
| 14 |
|
| 15 | public class SculkBlock extends DropExperienceBlock implements SculkBehaviour {
|
| 16 | public static final int GROWTH_INHIBITOR_RANGE = 4;
|
| 17 |
|
| 18 | public SculkBlock(final BlockBehaviour.Properties properties) {
|
| 19 | super(ConstantInt.of(1), properties);
|
| 20 | }
|
| 21 |
|
| 22 | @Override
|
| 23 | public int attemptUseCharge(
|
| 24 | final SculkSpreader.ChargeCursor cursor,
|
| 25 | final LevelAccessor level,
|
| 26 | final BlockPos originPos,
|
| 27 | final RandomSource random,
|
| 28 | final SculkSpreader spreader,
|
| 29 | final boolean spreadVein
|
| 30 | ) {
|
| 31 | int charge = cursor.getCharge();
|
| 32 | if (charge != 0 && random.nextInt(spreader.chargeDecayRate()) == 0) {
|
| 33 | BlockPos chargePos = cursor.getPos();
|
| 34 | boolean isCloseToCatalyst = chargePos.closerThan(originPos, spreader.noGrowthRadius());
|
| 35 | if (!isCloseToCatalyst && canPlaceGrowth(level, chargePos)) {
|
| 36 | int xpPerGrowthSpawn = spreader.growthSpawnCost();
|
| 37 | if (random.nextInt(xpPerGrowthSpawn) < charge) {
|
| 38 | BlockPos growthPlacement = chargePos.above();
|
| 39 | BlockState growthState = this.getRandomGrowthState(level, growthPlacement, random, spreader.isWorldGeneration());
|
| 40 | level.setBlockAndUpdate(growthPlacement, growthState);
|
| 41 | level.playSound(null, chargePos, growthState.getSoundType().getPlaceSound(), SoundSource.BLOCKS, 1.0F, 1.0F);
|
| 42 | }
|
| 43 |
|
| 44 | return Math.max(0, charge - xpPerGrowthSpawn);
|
| 45 | } else {
|
| 46 | return random.nextInt(spreader.additionalDecayRate()) != 0
|
| 47 | ? charge
|
| 48 | : charge - (isCloseToCatalyst ? 1 : getDecayPenalty(spreader, chargePos, originPos, charge));
|
| 49 | }
|
| 50 | } else {
|
| 51 | return charge;
|
| 52 | }
|
| 53 | }
|
| 54 |
|
| 55 | private static int getDecayPenalty(final SculkSpreader spreader, final BlockPos pos, final BlockPos originPos, final int charge) {
|
| 56 | int noGrowthRadius = spreader.noGrowthRadius();
|
| 57 | float outerDistanceSquared = Mth.square((float)Math.sqrt(pos.distSqr(originPos)) - noGrowthRadius);
|
| 58 | int maxReachSquared = Mth.square(24 - noGrowthRadius);
|
| 59 | float distanceFactor = Math.min(1.0F, outerDistanceSquared / maxReachSquared);
|
| 60 | return Math.max(1, (int)(charge * distanceFactor * 0.5F));
|
| 61 | }
|
| 62 |
|
| 63 | private BlockState getRandomGrowthState(final LevelAccessor level, final BlockPos pos, final RandomSource random, final boolean isWorldGen) {
|
| 64 | BlockState state;
|
| 65 | if (random.nextInt(11) == 0) {
|
| 66 | state = Blocks.SCULK_SHRIEKER.defaultBlockState().setValue(SculkShriekerBlock.CAN_SUMMON, isWorldGen);
|
| 67 | } else {
|
| 68 | state = Blocks.SCULK_SENSOR.defaultBlockState();
|
| 69 | }
|
| 70 |
|
| 71 | return state.hasProperty(BlockStateProperties.WATERLOGGED) && !level.getFluidState(pos).isEmpty()
|
| 72 | ? state.setValue(BlockStateProperties.WATERLOGGED, true)
|
| 73 | : state;
|
| 74 | }
|
| 75 |
|
| 76 | private static boolean canPlaceGrowth(final LevelAccessor level, final BlockPos pos) {
|
| 77 | BlockState stateAbove = level.getBlockState(pos.above());
|
| 78 | return stateAbove.isAir() || stateAbove.is(Blocks.WATER) && stateAbove.getFluidState().is(Fluids.WATER)
|
| 79 | ? level.findBlocksIn(pos.offset(-4, 0, -4), pos.offset(4, 2, 4)).filterState(state -> state.is(BlockTags.SCULK_GROWTH_INHIBITORS)).atMostMatched(2)
|
| 80 | : false;
|
| 81 | }
|
| 82 |
|
| 83 | @Override
|
| 84 | public boolean canChangeBlockStateOnSpread() {
|
| 85 | return false;
|
| 86 | }
|
| 87 | }
|
| 88 |
|