| 1 | package net.minecraft.world.level.block;
|
| 2 |
|
| 3 | import java.util.Collection;
|
| 4 | import net.minecraft.core.BlockPos;
|
| 5 | import net.minecraft.core.Direction;
|
| 6 | import net.minecraft.sounds.SoundEvents;
|
| 7 | import net.minecraft.sounds.SoundSource;
|
| 8 | import net.minecraft.tags.BlockTags;
|
| 9 | import net.minecraft.tags.TagKey;
|
| 10 | import net.minecraft.util.Mth;
|
| 11 | import net.minecraft.util.RandomSource;
|
| 12 | import net.minecraft.world.level.BlockGetter;
|
| 13 | import net.minecraft.world.level.LevelAccessor;
|
| 14 | import net.minecraft.world.level.block.state.BlockBehaviour;
|
| 15 | import net.minecraft.world.level.block.state.BlockState;
|
| 16 | import net.minecraft.world.level.block.state.properties.BooleanProperty;
|
| 17 | import net.minecraft.world.level.material.FluidState;
|
| 18 | import net.minecraft.world.level.material.Fluids;
|
| 19 |
|
| 20 | public class SculkVeinBlock extends MultifaceSpreadeableBlock implements SculkBehaviour {
|
| 21 | private final MultifaceSpreader veinSpreader = new MultifaceSpreader(new SculkVeinBlock.SculkVeinSpreaderConfig(MultifaceSpreader.DEFAULT_SPREAD_ORDER));
|
| 22 | private final MultifaceSpreader sameSpaceSpreader = new MultifaceSpreader(
|
| 23 | new SculkVeinBlock.SculkVeinSpreaderConfig(MultifaceSpreader.SpreadType.SAME_POSITION)
|
| 24 | );
|
| 25 |
|
| 26 | public SculkVeinBlock(final BlockBehaviour.Properties properties) {
|
| 27 | super(properties);
|
| 28 | }
|
| 29 |
|
| 30 | @Override
|
| 31 | public MultifaceSpreader getSpreader() {
|
| 32 | return this.veinSpreader;
|
| 33 | }
|
| 34 |
|
| 35 | public MultifaceSpreader getSameSpaceSpreader() {
|
| 36 | return this.sameSpaceSpreader;
|
| 37 | }
|
| 38 |
|
| 39 | public static boolean regrow(final LevelAccessor level, final BlockPos pos, final BlockState existing, final Collection<Direction> faces) {
|
| 40 | boolean hasAtLeastOneFace = false;
|
| 41 | BlockState newState = Blocks.SCULK_VEIN.defaultBlockState();
|
| 42 |
|
| 43 | for (Direction face : faces) {
|
| 44 | if (canAttachTo(level, pos, face)) {
|
| 45 | newState = newState.setValue(getFaceProperty(face), true);
|
| 46 | hasAtLeastOneFace = true;
|
| 47 | }
|
| 48 | }
|
| 49 |
|
| 50 | if (!hasAtLeastOneFace) {
|
| 51 | return false;
|
| 52 | }
|
| 53 |
|
| 54 | if (!existing.getFluidState().isEmpty()) {
|
| 55 | newState = newState.setValue(MultifaceBlock.WATERLOGGED, true);
|
| 56 | }
|
| 57 |
|
| 58 | level.setBlockAndUpdate(pos, newState);
|
| 59 | return true;
|
| 60 | }
|
| 61 |
|
| 62 | @Override
|
| 63 | public void onDischarged(final LevelAccessor level, BlockState state, final BlockPos pos, final RandomSource random) {
|
| 64 | if (state.is(this)) {
|
| 65 | for (Direction dir : DIRECTIONS) {
|
| 66 | BooleanProperty sideProperty = getFaceProperty(dir);
|
| 67 | if (state.getValue(sideProperty) && level.getBlockState(pos.relative(dir)).is(Blocks.SCULK)) {
|
| 68 | state = state.setValue(sideProperty, false);
|
| 69 | }
|
| 70 | }
|
| 71 |
|
| 72 | if (!hasAnyFace(state)) {
|
| 73 | FluidState fluidState = level.getFluidState(pos);
|
| 74 | state = (fluidState.isEmpty() ? Blocks.AIR : Blocks.WATER).defaultBlockState();
|
| 75 | }
|
| 76 |
|
| 77 | level.setBlockAndUpdate(pos, state);
|
| 78 | SculkBehaviour.super.onDischarged(level, state, pos, random);
|
| 79 | }
|
| 80 | }
|
| 81 |
|
| 82 | @Override
|
| 83 | public int attemptUseCharge(
|
| 84 | final SculkSpreader.ChargeCursor cursor,
|
| 85 | final LevelAccessor level,
|
| 86 | final BlockPos originPos,
|
| 87 | final RandomSource random,
|
| 88 | final SculkSpreader spreader,
|
| 89 | final boolean spreadVeins
|
| 90 | ) {
|
| 91 | if (spreadVeins && this.attemptPlaceSculk(spreader, level, cursor.getPos(), random)) {
|
| 92 | return cursor.getCharge() - 1;
|
| 93 | } else {
|
| 94 | return random.nextInt(spreader.chargeDecayRate()) == 0 ? Mth.floor(cursor.getCharge() * 0.5F) : cursor.getCharge();
|
| 95 | }
|
| 96 | }
|
| 97 |
|
| 98 | private boolean attemptPlaceSculk(final SculkSpreader spreader, final LevelAccessor level, final BlockPos pos, final RandomSource random) {
|
| 99 | BlockState state = level.getBlockState(pos);
|
| 100 | TagKey<Block> replaceTag = spreader.replaceableBlocks();
|
| 101 |
|
| 102 | for (Direction support : Direction.allShuffled(random)) {
|
| 103 | if (hasFace(state, support)) {
|
| 104 | BlockPos supportPos = pos.relative(support);
|
| 105 | BlockState supportState = level.getBlockState(supportPos);
|
| 106 | if (supportState.is(replaceTag)) {
|
| 107 | BlockState defaultSculk = Blocks.SCULK.defaultBlockState();
|
| 108 | level.setBlockAndUpdate(supportPos, defaultSculk);
|
| 109 | Block.pushEntitiesUp(supportState, defaultSculk, level, supportPos);
|
| 110 | level.playSound(null, supportPos, SoundEvents.SCULK_BLOCK_SPREAD, SoundSource.BLOCKS, 1.0F, 1.0F);
|
| 111 | this.veinSpreader.spreadAll(defaultSculk, level, supportPos, spreader.isWorldGeneration());
|
| 112 | Direction skip = support.getOpposite();
|
| 113 |
|
| 114 | for (Direction veinBlocks : DIRECTIONS) {
|
| 115 | if (veinBlocks != skip) {
|
| 116 | BlockPos veinPos = supportPos.relative(veinBlocks);
|
| 117 | BlockState possibleVeinBlock = level.getBlockState(veinPos);
|
| 118 | if (possibleVeinBlock.is(this)) {
|
| 119 | this.onDischarged(level, possibleVeinBlock, veinPos, random);
|
| 120 | }
|
| 121 | }
|
| 122 | }
|
| 123 |
|
| 124 | return true;
|
| 125 | }
|
| 126 | }
|
| 127 | }
|
| 128 |
|
| 129 | return false;
|
| 130 | }
|
| 131 |
|
| 132 | public static boolean hasSubstrateAccess(final LevelAccessor level, final BlockState state, final BlockPos pos) {
|
| 133 | if (!state.is(Blocks.SCULK_VEIN)) {
|
| 134 | return false;
|
| 135 | }
|
| 136 |
|
| 137 | for (Direction direction : DIRECTIONS) {
|
| 138 | if (hasFace(state, direction) && level.getBlockState(pos.relative(direction)).is(BlockTags.SCULK_REPLACEABLE)) {
|
| 139 | return true;
|
| 140 | }
|
| 141 | }
|
| 142 |
|
| 143 | return false;
|
| 144 | }
|
| 145 |
|
| 146 | private class SculkVeinSpreaderConfig extends MultifaceSpreader.DefaultSpreaderConfig {
|
| 147 | private final MultifaceSpreader.SpreadType[] spreadTypes;
|
| 148 |
|
| 149 | public SculkVeinSpreaderConfig(final MultifaceSpreader.SpreadType... spreadTypes) {
|
| 150 | super(SculkVeinBlock.this);
|
| 151 | this.spreadTypes = spreadTypes;
|
| 152 | }
|
| 153 |
|
| 154 | @Override
|
| 155 | public boolean stateCanBeReplaced(
|
| 156 | final BlockGetter level, final BlockPos sourcePos, final BlockPos placementPos, final Direction placementDirection, final BlockState existingState
|
| 157 | ) {
|
| 158 | BlockState againstState = level.getBlockState(placementPos.relative(placementDirection));
|
| 159 | if (!againstState.is(Blocks.SCULK) && !againstState.is(Blocks.SCULK_CATALYST) && !againstState.is(Blocks.MOVING_PISTON)) {
|
| 160 | if (sourcePos.distManhattan(placementPos) == 2) {
|
| 161 | BlockPos neighourPos = sourcePos.relative(placementDirection.getOpposite());
|
| 162 | if (level.getBlockState(neighourPos).isFaceSturdy(level, neighourPos, placementDirection)) {
|
| 163 | return false;
|
| 164 | }
|
| 165 | }
|
| 166 |
|
| 167 | FluidState fluidState = existingState.getFluidState();
|
| 168 | if (!fluidState.isEmpty() && !fluidState.is(Fluids.WATER)) {
|
| 169 | return false;
|
| 170 | } else {
|
| 171 | return existingState.is(BlockTags.FIRE)
|
| 172 | ? false
|
| 173 | : existingState.canBeReplaced() || super.stateCanBeReplaced(level, sourcePos, placementPos, placementDirection, existingState);
|
| 174 | }
|
| 175 | } else {
|
| 176 | return false;
|
| 177 | }
|
| 178 | }
|
| 179 |
|
| 180 | @Override
|
| 181 | public MultifaceSpreader.SpreadType[] getSpreadTypes() {
|
| 182 | return this.spreadTypes;
|
| 183 | }
|
| 184 |
|
| 185 | @Override
|
| 186 | public boolean isOtherBlockValidAsSource(final BlockState state) {
|
| 187 | return !state.is(Blocks.SCULK_VEIN);
|
| 188 | }
|
| 189 | }
|
| 190 | }
|
| 191 |
|