| 1 | package net.minecraft.world.level.block;
|
| 2 |
|
| 3 | import java.util.Map;
|
| 4 | import net.minecraft.core.BlockPos;
|
| 5 | import net.minecraft.core.Direction;
|
| 6 | import net.minecraft.network.chat.Component;
|
| 7 | import net.minecraft.stats.Stats;
|
| 8 | import net.minecraft.world.InteractionResult;
|
| 9 | import net.minecraft.world.MenuProvider;
|
| 10 | import net.minecraft.world.SimpleMenuProvider;
|
| 11 | import net.minecraft.world.damagesource.DamageSource;
|
| 12 | import net.minecraft.world.entity.Entity;
|
| 13 | import net.minecraft.world.entity.item.FallingBlockEntity;
|
| 14 | import net.minecraft.world.entity.player.Player;
|
| 15 | import net.minecraft.world.inventory.AnvilMenu;
|
| 16 | import net.minecraft.world.inventory.ContainerLevelAccess;
|
| 17 | import net.minecraft.world.item.context.BlockPlaceContext;
|
| 18 | import net.minecraft.world.level.BlockGetter;
|
| 19 | import net.minecraft.world.level.Level;
|
| 20 | import net.minecraft.world.level.block.state.BlockBehaviour;
|
| 21 | import net.minecraft.world.level.block.state.BlockState;
|
| 22 | import net.minecraft.world.level.block.state.StateDefinition;
|
| 23 | import net.minecraft.world.level.block.state.properties.EnumProperty;
|
| 24 | import net.minecraft.world.level.pathfinder.PathComputationType;
|
| 25 | import net.minecraft.world.phys.BlockHitResult;
|
| 26 | import net.minecraft.world.phys.shapes.CollisionContext;
|
| 27 | import net.minecraft.world.phys.shapes.Shapes;
|
| 28 | import net.minecraft.world.phys.shapes.VoxelShape;
|
| 29 | import org.jspecify.annotations.Nullable;
|
| 30 |
|
| 31 | public class AnvilBlock extends FallingBlock {
|
| 32 | public static final EnumProperty<Direction> FACING = HorizontalDirectionalBlock.FACING;
|
| 33 | private static final Map<Direction.Axis, VoxelShape> SHAPES = Shapes.rotateHorizontalAxis(
|
| 34 | Shapes.or(Block.column(12.0, 0.0, 4.0), Block.column(8.0, 10.0, 4.0, 5.0), Block.column(4.0, 8.0, 5.0, 10.0), Block.column(10.0, 16.0, 10.0, 16.0))
|
| 35 | );
|
| 36 | private static final Component CONTAINER_TITLE = Component.translatable("container.repair");
|
| 37 | private static final float FALL_DAMAGE_PER_DISTANCE = 2.0F;
|
| 38 | private static final int FALL_DAMAGE_MAX = 40;
|
| 39 |
|
| 40 | public AnvilBlock(final BlockBehaviour.Properties properties) {
|
| 41 | super(properties);
|
| 42 | this.registerDefaultState(this.stateDefinition.any().setValue(FACING, Direction.NORTH));
|
| 43 | }
|
| 44 |
|
| 45 | @Override
|
| 46 | public BlockState getStateForPlacement(final BlockPlaceContext context) {
|
| 47 | return this.defaultBlockState().setValue(FACING, context.getHorizontalDirection().getClockWise());
|
| 48 | }
|
| 49 |
|
| 50 | @Override
|
| 51 | protected InteractionResult useWithoutItem(final BlockState state, final Level level, final BlockPos pos, final Player player, final BlockHitResult hitResult) {
|
| 52 | if (!level.isClientSide()) {
|
| 53 | player.openMenu(state.getMenuProvider(level, pos));
|
| 54 | player.awardStat(Stats.INTERACT_WITH_ANVIL);
|
| 55 | }
|
| 56 |
|
| 57 | return InteractionResult.SUCCESS;
|
| 58 | }
|
| 59 |
|
| 60 | @Override
|
| 61 | protected @Nullable MenuProvider getMenuProvider(final BlockState state, final Level level, final BlockPos pos) {
|
| 62 | return new SimpleMenuProvider(
|
| 63 | (containerId, inventory, player) -> new AnvilMenu(containerId, inventory, ContainerLevelAccess.create(level, pos)), CONTAINER_TITLE
|
| 64 | );
|
| 65 | }
|
| 66 |
|
| 67 | @Override
|
| 68 | protected VoxelShape getShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context) {
|
| 69 | return SHAPES.get(state.getValue(FACING).getAxis());
|
| 70 | }
|
| 71 |
|
| 72 | @Override
|
| 73 | protected void falling(final FallingBlockEntity entity) {
|
| 74 | entity.setHurtsEntities(2.0F, 40);
|
| 75 | }
|
| 76 |
|
| 77 | @Override
|
| 78 | public void onLand(final Level level, final BlockPos pos, final BlockState state, final BlockState replacedBlock, final FallingBlockEntity entity) {
|
| 79 | if (!entity.isSilent()) {
|
| 80 | level.levelEvent(1031, pos, 0);
|
| 81 | }
|
| 82 | }
|
| 83 |
|
| 84 | @Override
|
| 85 | public void onBrokenAfterFall(final Level level, final BlockPos pos, final FallingBlockEntity entity) {
|
| 86 | if (!entity.isSilent()) {
|
| 87 | level.levelEvent(1029, pos, 0);
|
| 88 | }
|
| 89 | }
|
| 90 |
|
| 91 | @Override
|
| 92 | public DamageSource getFallDamageSource(final Entity entity) {
|
| 93 | return entity.damageSources().anvil(entity);
|
| 94 | }
|
| 95 |
|
| 96 | public static @Nullable BlockState damage(final BlockState blockState) {
|
| 97 | if (blockState.is(Blocks.ANVIL)) {
|
| 98 | return Blocks.CHIPPED_ANVIL.defaultBlockState().setValue(FACING, blockState.getValue(FACING));
|
| 99 | } else {
|
| 100 | return blockState.is(Blocks.CHIPPED_ANVIL) ? Blocks.DAMAGED_ANVIL.defaultBlockState().setValue(FACING, blockState.getValue(FACING)) : null;
|
| 101 | }
|
| 102 | }
|
| 103 |
|
| 104 | @Override
|
| 105 | protected BlockState rotate(final BlockState state, final Rotation rotation) {
|
| 106 | return state.setValue(FACING, rotation.rotate(state.getValue(FACING)));
|
| 107 | }
|
| 108 |
|
| 109 | @Override
|
| 110 | protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder) {
|
| 111 | builder.add(FACING);
|
| 112 | }
|
| 113 |
|
| 114 | @Override
|
| 115 | protected boolean isPathfindable(final BlockState state, final PathComputationType type) {
|
| 116 | return false;
|
| 117 | }
|
| 118 |
|
| 119 | @Override
|
| 120 | public int getDustColor(final BlockState blockState, final BlockGetter level, final BlockPos pos) {
|
| 121 | return blockState.getMapColor(level, pos).col;
|
| 122 | }
|
| 123 | }
|
| 124 |
|