| 1 | package net.minecraft.world.item;
|
| 2 |
|
| 3 | import java.util.Map;
|
| 4 | import net.minecraft.core.BlockPos;
|
| 5 | import net.minecraft.core.Direction;
|
| 6 | import net.minecraft.world.item.context.BlockPlaceContext;
|
| 7 | import net.minecraft.world.level.LevelReader;
|
| 8 | import net.minecraft.world.level.block.Block;
|
| 9 | import net.minecraft.world.level.block.state.BlockState;
|
| 10 | import net.minecraft.world.phys.shapes.CollisionContext;
|
| 11 | import org.jspecify.annotations.Nullable;
|
| 12 |
|
| 13 | public class StandingAndWallBlockItem extends BlockItem {
|
| 14 | protected final Block wallBlock;
|
| 15 | private final Direction attachmentDirection;
|
| 16 |
|
| 17 | public StandingAndWallBlockItem(final Block block, final Block wallBlock, final Direction attachmentDirection, final Item.Properties properties) {
|
| 18 | super(block, properties);
|
| 19 | this.wallBlock = wallBlock;
|
| 20 | this.attachmentDirection = attachmentDirection;
|
| 21 | }
|
| 22 |
|
| 23 | protected boolean canPlace(final LevelReader level, final BlockState possibleState, final BlockPos pos) {
|
| 24 | return possibleState.canSurvive(level, pos);
|
| 25 | }
|
| 26 |
|
| 27 | @Override
|
| 28 | protected @Nullable BlockState getPlacementState(final BlockPlaceContext context) {
|
| 29 | BlockState wallState = this.wallBlock.getStateForPlacement(context);
|
| 30 | BlockState stateForPlacement = null;
|
| 31 | LevelReader level = context.getLevel();
|
| 32 | BlockPos pos = context.getClickedPos();
|
| 33 |
|
| 34 | for (Direction direction : context.getNearestLookingDirections()) {
|
| 35 | if (direction != this.attachmentDirection.getOpposite()) {
|
| 36 | BlockState possibleState = direction == this.attachmentDirection ? this.getBlock().getStateForPlacement(context) : wallState;
|
| 37 | if (possibleState != null && this.canPlace(level, possibleState, pos)) {
|
| 38 | stateForPlacement = possibleState;
|
| 39 | break;
|
| 40 | }
|
| 41 | }
|
| 42 | }
|
| 43 |
|
| 44 | return stateForPlacement != null && level.isUnobstructed(stateForPlacement, pos, CollisionContext.empty()) ? stateForPlacement : null;
|
| 45 | }
|
| 46 |
|
| 47 | @Override
|
| 48 | public void registerBlocks(final Map<Block, Item> map, final Item item) {
|
| 49 | super.registerBlocks(map, item);
|
| 50 | map.put(this.wallBlock, item);
|
| 51 | }
|
| 52 | }
|
| 53 |
|