| 1 | package net.minecraft.world.level.block;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableMap;
|
| 4 | import com.google.common.collect.Maps;
|
| 5 | import java.util.Map;
|
| 6 | import java.util.Map.Entry;
|
| 7 | import java.util.function.BooleanSupplier;
|
| 8 | import java.util.function.Function;
|
| 9 | import net.minecraft.core.BlockPos;
|
| 10 | import net.minecraft.core.Direction;
|
| 11 | import net.minecraft.server.level.ServerLevel;
|
| 12 | import net.minecraft.util.RandomSource;
|
| 13 | import net.minecraft.world.entity.LivingEntity;
|
| 14 | import net.minecraft.world.item.ItemStack;
|
| 15 | import net.minecraft.world.item.context.BlockPlaceContext;
|
| 16 | import net.minecraft.world.level.BlockGetter;
|
| 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.block.state.BlockBehaviour;
|
| 22 | import net.minecraft.world.level.block.state.BlockState;
|
| 23 | import net.minecraft.world.level.block.state.StateDefinition;
|
| 24 | import net.minecraft.world.level.block.state.properties.BlockStateProperties;
|
| 25 | import net.minecraft.world.level.block.state.properties.BooleanProperty;
|
| 26 | import net.minecraft.world.level.block.state.properties.EnumProperty;
|
| 27 | import net.minecraft.world.level.block.state.properties.WallSide;
|
| 28 | import net.minecraft.world.phys.shapes.CollisionContext;
|
| 29 | import net.minecraft.world.phys.shapes.Shapes;
|
| 30 | import net.minecraft.world.phys.shapes.VoxelShape;
|
| 31 | import org.jspecify.annotations.Nullable;
|
| 32 |
|
| 33 | public class MossyCarpetBlock extends Block implements BonemealableBlock {
|
| 34 | public static final BooleanProperty BASE = BlockStateProperties.BOTTOM;
|
| 35 | public static final EnumProperty<WallSide> NORTH = BlockStateProperties.NORTH_WALL;
|
| 36 | public static final EnumProperty<WallSide> EAST = BlockStateProperties.EAST_WALL;
|
| 37 | public static final EnumProperty<WallSide> SOUTH = BlockStateProperties.SOUTH_WALL;
|
| 38 | public static final EnumProperty<WallSide> WEST = BlockStateProperties.WEST_WALL;
|
| 39 | public static final Map<Direction, EnumProperty<WallSide>> PROPERTY_BY_DIRECTION = ImmutableMap.copyOf(
|
| 40 | Maps.newEnumMap(Map.of(Direction.NORTH, NORTH, Direction.EAST, EAST, Direction.SOUTH, SOUTH, Direction.WEST, WEST))
|
| 41 | );
|
| 42 | private final Function<BlockState, VoxelShape> shapes;
|
| 43 |
|
| 44 | public MossyCarpetBlock(final BlockBehaviour.Properties properties) {
|
| 45 | super(properties);
|
| 46 | this.registerDefaultState(
|
| 47 | this.stateDefinition
|
| 48 | .any()
|
| 49 | .setValue(BASE, true)
|
| 50 | .setValue(NORTH, WallSide.NONE)
|
| 51 | .setValue(EAST, WallSide.NONE)
|
| 52 | .setValue(SOUTH, WallSide.NONE)
|
| 53 | .setValue(WEST, WallSide.NONE)
|
| 54 | );
|
| 55 | this.shapes = this.makeShapes();
|
| 56 | }
|
| 57 |
|
| 58 | public Function<BlockState, VoxelShape> makeShapes() {
|
| 59 | Map<Direction, VoxelShape> low = Shapes.rotateHorizontal(Block.boxZ(16.0, 0.0, 10.0, 0.0, 1.0));
|
| 60 | Map<Direction, VoxelShape> tall = Shapes.rotateAll(Block.boxZ(16.0, 0.0, 1.0));
|
| 61 | return this.getShapeForEachState(state -> {
|
| 62 | VoxelShape shape = state.getValue(BASE) ? tall.get(Direction.DOWN) : Shapes.empty();
|
| 63 |
|
| 64 | for (Entry<Direction, EnumProperty<WallSide>> entry : PROPERTY_BY_DIRECTION.entrySet()) {
|
| 65 | switch ((WallSide)state.getValue(entry.getValue())) {
|
| 66 | case NONE:
|
| 67 | default:
|
| 68 | break;
|
| 69 | case LOW:
|
| 70 | shape = Shapes.or(shape, low.get(entry.getKey()));
|
| 71 | break;
|
| 72 | case TALL:
|
| 73 | shape = Shapes.or(shape, tall.get(entry.getKey()));
|
| 74 | }
|
| 75 | }
|
| 76 |
|
| 77 | return shape.isEmpty() ? Shapes.block() : shape;
|
| 78 | });
|
| 79 | }
|
| 80 |
|
| 81 | @Override
|
| 82 | protected VoxelShape getShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context) {
|
| 83 | return this.shapes.apply(state);
|
| 84 | }
|
| 85 |
|
| 86 | @Override
|
| 87 | protected VoxelShape getCollisionShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context) {
|
| 88 | return state.getValue(BASE) ? this.shapes.apply(this.defaultBlockState()) : Shapes.empty();
|
| 89 | }
|
| 90 |
|
| 91 | @Override
|
| 92 | protected boolean propagatesSkylightDown(final BlockState state) {
|
| 93 | return true;
|
| 94 | }
|
| 95 |
|
| 96 | @Override
|
| 97 | protected boolean canSurvive(final BlockState state, final LevelReader level, final BlockPos pos) {
|
| 98 | BlockState belowState = level.getBlockState(pos.below());
|
| 99 | return state.getValue(BASE) ? !belowState.isAir() : belowState.is(this) && belowState.getValue(BASE);
|
| 100 | }
|
| 101 |
|
| 102 | private static boolean hasFaces(final BlockState blockState) {
|
| 103 | if (blockState.getValue(BASE)) {
|
| 104 | return true;
|
| 105 | }
|
| 106 |
|
| 107 | for (EnumProperty<WallSide> property : PROPERTY_BY_DIRECTION.values()) {
|
| 108 | if (blockState.getValue(property) != WallSide.NONE) {
|
| 109 | return true;
|
| 110 | }
|
| 111 | }
|
| 112 |
|
| 113 | return false;
|
| 114 | }
|
| 115 |
|
| 116 | private static boolean canSupportAtFace(final BlockGetter level, final BlockPos pos, final Direction direction) {
|
| 117 | return direction == Direction.UP ? false : MultifaceBlock.canAttachTo(level, pos, direction);
|
| 118 | }
|
| 119 |
|
| 120 | private static BlockState getUpdatedState(BlockState state, final BlockGetter level, final BlockPos pos, boolean createSides) {
|
| 121 | BlockState aboveState = null;
|
| 122 | BlockState belowState = null;
|
| 123 | createSides |= state.getValue(BASE);
|
| 124 |
|
| 125 | for (Direction direction : Direction.Plane.HORIZONTAL) {
|
| 126 | EnumProperty<WallSide> property = getPropertyForFace(direction);
|
| 127 | WallSide side = canSupportAtFace(level, pos, direction) ? (createSides ? WallSide.LOW : state.getValue(property)) : WallSide.NONE;
|
| 128 | if (side == WallSide.LOW) {
|
| 129 | if (aboveState == null) {
|
| 130 | aboveState = level.getBlockState(pos.above());
|
| 131 | }
|
| 132 |
|
| 133 | if (aboveState.is(Blocks.PALE_MOSS_CARPET) && aboveState.getValue(property) != WallSide.NONE && !aboveState.getValue(BASE)) {
|
| 134 | side = WallSide.TALL;
|
| 135 | }
|
| 136 |
|
| 137 | if (!state.getValue(BASE)) {
|
| 138 | if (belowState == null) {
|
| 139 | belowState = level.getBlockState(pos.below());
|
| 140 | }
|
| 141 |
|
| 142 | if (belowState.is(Blocks.PALE_MOSS_CARPET) && belowState.getValue(property) == WallSide.NONE) {
|
| 143 | side = WallSide.NONE;
|
| 144 | }
|
| 145 | }
|
| 146 | }
|
| 147 |
|
| 148 | state = state.setValue(property, side);
|
| 149 | }
|
| 150 |
|
| 151 | return state;
|
| 152 | }
|
| 153 |
|
| 154 | @Override
|
| 155 | public @Nullable BlockState getStateForPlacement(final BlockPlaceContext context) {
|
| 156 | return getUpdatedState(this.defaultBlockState(), context.getLevel(), context.getClickedPos(), true);
|
| 157 | }
|
| 158 |
|
| 159 | public static void placeAt(final LevelAccessor level, final BlockPos pos, final RandomSource random, final @Block.UpdateFlags int updateType) {
|
| 160 | BlockState simpleCarpetLayer = Blocks.PALE_MOSS_CARPET.defaultBlockState();
|
| 161 | BlockState adjustedCarpetLayer = getUpdatedState(simpleCarpetLayer, level, pos, true);
|
| 162 | level.setBlock(pos, adjustedCarpetLayer, updateType);
|
| 163 | BlockState state = createTopperWithSideChance(level, pos, random::nextBoolean);
|
| 164 | if (!state.isAir()) {
|
| 165 | level.setBlock(pos.above(), state, updateType);
|
| 166 | BlockState updateBottomCarpet = getUpdatedState(adjustedCarpetLayer, level, pos, true);
|
| 167 | level.setBlock(pos, updateBottomCarpet, updateType);
|
| 168 | }
|
| 169 | }
|
| 170 |
|
| 171 | @Override
|
| 172 | public void setPlacedBy(final Level level, final BlockPos pos, final BlockState state, final @Nullable LivingEntity by, final ItemStack itemStack) {
|
| 173 | if (!level.isClientSide()) {
|
| 174 | RandomSource random = level.getRandom();
|
| 175 | BlockState topper = createTopperWithSideChance(level, pos, random::nextBoolean);
|
| 176 | if (!topper.isAir()) {
|
| 177 | level.setBlockAndUpdate(pos.above(), topper);
|
| 178 | }
|
| 179 | }
|
| 180 | }
|
| 181 |
|
| 182 | private static BlockState createTopperWithSideChance(final BlockGetter level, final BlockPos pos, final BooleanSupplier sideSurvivalTest) {
|
| 183 | BlockPos above = pos.above();
|
| 184 | BlockState abovePreviousState = level.getBlockState(above);
|
| 185 | boolean isMossyCarpetAbove = abovePreviousState.is(Blocks.PALE_MOSS_CARPET);
|
| 186 | if ((!isMossyCarpetAbove || !abovePreviousState.getValue(BASE)) && (isMossyCarpetAbove || abovePreviousState.canBeReplaced())) {
|
| 187 | BlockState noCarpetBaseState = Blocks.PALE_MOSS_CARPET.defaultBlockState().setValue(BASE, false);
|
| 188 | BlockState aboveState = getUpdatedState(noCarpetBaseState, level, pos.above(), true);
|
| 189 |
|
| 190 | for (Direction direction : Direction.Plane.HORIZONTAL) {
|
| 191 | EnumProperty<WallSide> property = getPropertyForFace(direction);
|
| 192 | if (aboveState.getValue(property) != WallSide.NONE && !sideSurvivalTest.getAsBoolean()) {
|
| 193 | aboveState = aboveState.setValue(property, WallSide.NONE);
|
| 194 | }
|
| 195 | }
|
| 196 |
|
| 197 | return hasFaces(aboveState) && aboveState != abovePreviousState ? aboveState : Blocks.AIR.defaultBlockState();
|
| 198 | } else {
|
| 199 | return Blocks.AIR.defaultBlockState();
|
| 200 | }
|
| 201 | }
|
| 202 |
|
| 203 | @Override
|
| 204 | protected BlockState updateShape(
|
| 205 | final BlockState state,
|
| 206 | final LevelReader level,
|
| 207 | final ScheduledTickAccess ticks,
|
| 208 | final BlockPos pos,
|
| 209 | final Direction directionToNeighbour,
|
| 210 | final BlockPos neighbourPos,
|
| 211 | final BlockState neighbourState,
|
| 212 | final RandomSource random
|
| 213 | ) {
|
| 214 | if (!state.canSurvive(level, pos)) {
|
| 215 | return Blocks.AIR.defaultBlockState();
|
| 216 | }
|
| 217 |
|
| 218 | BlockState blockState = getUpdatedState(state, level, pos, false);
|
| 219 | return !hasFaces(blockState) ? Blocks.AIR.defaultBlockState() : blockState;
|
| 220 | }
|
| 221 |
|
| 222 | @Override
|
| 223 | protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder) {
|
| 224 | builder.add(BASE, NORTH, EAST, SOUTH, WEST);
|
| 225 | }
|
| 226 |
|
| 227 | @Override
|
| 228 | protected BlockState rotate(final BlockState state, final Rotation rotation) {
|
| 229 | return switch (rotation) {
|
| 230 | case CLOCKWISE_180 -> (BlockState)state.setValue(NORTH, state.getValue(SOUTH))
|
| 231 | .setValue(EAST, state.getValue(WEST))
|
| 232 | .setValue(SOUTH, state.getValue(NORTH))
|
| 233 | .setValue(WEST, state.getValue(EAST));
|
| 234 | case COUNTERCLOCKWISE_90 -> (BlockState)state.setValue(NORTH, state.getValue(EAST))
|
| 235 | .setValue(EAST, state.getValue(SOUTH))
|
| 236 | .setValue(SOUTH, state.getValue(WEST))
|
| 237 | .setValue(WEST, state.getValue(NORTH));
|
| 238 | case CLOCKWISE_90 -> (BlockState)state.setValue(NORTH, state.getValue(WEST))
|
| 239 | .setValue(EAST, state.getValue(NORTH))
|
| 240 | .setValue(SOUTH, state.getValue(EAST))
|
| 241 | .setValue(WEST, state.getValue(SOUTH));
|
| 242 | default -> state;
|
| 243 | };
|
| 244 | }
|
| 245 |
|
| 246 | @Override
|
| 247 | protected BlockState mirror(final BlockState state, final Mirror mirror) {
|
| 248 | return switch (mirror) {
|
| 249 | case LEFT_RIGHT -> (BlockState)state.setValue(NORTH, state.getValue(SOUTH)).setValue(SOUTH, state.getValue(NORTH));
|
| 250 | case FRONT_BACK -> (BlockState)state.setValue(EAST, state.getValue(WEST)).setValue(WEST, state.getValue(EAST));
|
| 251 | default -> super.mirror(state, mirror);
|
| 252 | };
|
| 253 | }
|
| 254 |
|
| 255 | public static @Nullable EnumProperty<WallSide> getPropertyForFace(final Direction direction) {
|
| 256 | return PROPERTY_BY_DIRECTION.get(direction);
|
| 257 | }
|
| 258 |
|
| 259 | @Override
|
| 260 | public boolean isValidBonemealTarget(final LevelReader level, final BlockPos pos, final BlockState state, final BonemealSource source) {
|
| 261 | return state.getValue(BASE) && !createTopperWithSideChance(level, pos, () -> true).isAir();
|
| 262 | }
|
| 263 |
|
| 264 | @Override
|
| 265 | public boolean isBonemealSuccess(final Level level, final RandomSource random, final BlockPos pos, final BlockState state, final BonemealSource source) {
|
| 266 | return true;
|
| 267 | }
|
| 268 |
|
| 269 | @Override
|
| 270 | public void performBonemeal(final ServerLevel level, final RandomSource random, final BlockPos pos, final BlockState state, final BonemealSource source) {
|
| 271 | BlockState topper = createTopperWithSideChance(level, pos, () -> true);
|
| 272 | if (!topper.isAir()) {
|
| 273 | level.setBlockAndUpdate(pos.above(), topper);
|
| 274 | }
|
| 275 | }
|
| 276 | }
|
| 277 |
|