minecraft-source/src/net/minecraft/world/level/block/AnvilBlock.java

137 lines
6.2 KiB
Java

package net.minecraft.world.level.block;
import net.minecraft.world.level.block.state.AbstractStateHolder;
import net.minecraft.world.phys.shapes.Shapes;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.level.pathfinder.PathComputationType;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.entity.item.FallingBlockEntity;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.level.BlockGetter;
import javax.annotation.Nullable;
import net.minecraft.network.chat.Component;
import net.minecraft.world.SimpleMenuProvider;
import net.minecraft.world.inventory.AnvilMenu;
import net.minecraft.world.inventory.ContainerLevelAccess;
import net.minecraft.world.MenuProvider;
import net.minecraft.stats.Stats;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.entity.player.Player;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.Level;
import net.minecraft.world.item.BlockPlaceContext;
import net.minecraft.world.level.block.state.properties.Property;
import net.minecraft.core.Direction;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.network.chat.TranslatableComponent;
import net.minecraft.world.phys.shapes.VoxelShape;
import net.minecraft.world.level.block.state.properties.DirectionProperty;
public class AnvilBlock extends FallingBlock {
public static final DirectionProperty FACING;
private static final VoxelShape BASE;
private static final VoxelShape X_LEG1;
private static final VoxelShape X_LEG2;
private static final VoxelShape X_TOP;
private static final VoxelShape Z_LEG1;
private static final VoxelShape Z_LEG2;
private static final VoxelShape Z_TOP;
private static final VoxelShape X_AXIS_AABB;
private static final VoxelShape Z_AXIS_AABB;
private static final TranslatableComponent CONTAINER_TITLE;
public AnvilBlock(final Properties c) {
super(c);
this.registerDefaultState(((AbstractStateHolder<O, BlockState>)this.stateDefinition.any()).<Comparable, Direction>setValue((Property<Comparable>)AnvilBlock.FACING, Direction.NORTH));
}
@Override
public BlockState getStateForPlacement(final BlockPlaceContext bcn) {
return ((AbstractStateHolder<O, BlockState>)this.defaultBlockState()).<Comparable, Direction>setValue((Property<Comparable>)AnvilBlock.FACING, bcn.getHorizontalDirection().getClockWise());
}
@Override
public InteractionResult use(final BlockState byg, final Level bjt, final BlockPos fk, final Player ayg, final InteractionHand ajh, final BlockHitResult cvd) {
if (bjt.isClientSide) {
return InteractionResult.SUCCESS;
}
ayg.openMenu(byg.getMenuProvider(bjt, fk));
ayg.awardStat(Stats.INTERACT_WITH_ANVIL);
return InteractionResult.SUCCESS;
}
@Nullable
@Override
public MenuProvider getMenuProvider(final BlockState byg, final Level bjt, final BlockPos fk) {
return new SimpleMenuProvider((integer, ayf, ayg) -> new AnvilMenu(integer, ayf, ContainerLevelAccess.create(bjt, fk)), AnvilBlock.CONTAINER_TITLE);
}
@Override
public VoxelShape getShape(final BlockState byg, final BlockGetter bjd, final BlockPos fk, final CollisionContext cvn) {
final Direction fp6 = byg.<Direction>getValue((Property<Direction>)AnvilBlock.FACING);
if (fp6.getAxis() == Direction.Axis.X) {
return AnvilBlock.X_AXIS_AABB;
}
return AnvilBlock.Z_AXIS_AABB;
}
@Override
protected void falling(final FallingBlockEntity avx) {
avx.setHurtsEntities(true);
}
@Override
public void onLand(final Level bjt, final BlockPos fk, final BlockState byg3, final BlockState byg4) {
bjt.levelEvent(1031, fk, 0);
}
@Override
public void onBroken(final Level bjt, final BlockPos fk) {
bjt.levelEvent(1029, fk, 0);
}
@Nullable
public static BlockState damage(final BlockState byg) {
final Block bpe2 = byg.getBlock();
if (bpe2 == Blocks.ANVIL) {
return ((AbstractStateHolder<O, BlockState>)Blocks.CHIPPED_ANVIL.defaultBlockState()).<Comparable, Comparable>setValue((Property<Comparable>)AnvilBlock.FACING, (Comparable)byg.<V>getValue((Property<V>)AnvilBlock.FACING));
}
if (bpe2 == Blocks.CHIPPED_ANVIL) {
return ((AbstractStateHolder<O, BlockState>)Blocks.DAMAGED_ANVIL.defaultBlockState()).<Comparable, Comparable>setValue((Property<Comparable>)AnvilBlock.FACING, (Comparable)byg.<V>getValue((Property<V>)AnvilBlock.FACING));
}
return null;
}
@Override
public BlockState rotate(final BlockState byg, final Rotation btr) {
return ((AbstractStateHolder<O, BlockState>)byg).<Comparable, Direction>setValue((Property<Comparable>)AnvilBlock.FACING, btr.rotate(byg.<Direction>getValue((Property<Direction>)AnvilBlock.FACING)));
}
@Override
protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> a) {
a.add(AnvilBlock.FACING);
}
@Override
public boolean isPathfindable(final BlockState byg, final BlockGetter bjd, final BlockPos fk, final PathComputationType cqo) {
return false;
}
static {
FACING = HorizontalDirectionalBlock.FACING;
BASE = Block.box(2.0, 0.0, 2.0, 14.0, 4.0, 14.0);
X_LEG1 = Block.box(3.0, 4.0, 4.0, 13.0, 5.0, 12.0);
X_LEG2 = Block.box(4.0, 5.0, 6.0, 12.0, 10.0, 10.0);
X_TOP = Block.box(0.0, 10.0, 3.0, 16.0, 16.0, 13.0);
Z_LEG1 = Block.box(4.0, 4.0, 3.0, 12.0, 5.0, 13.0);
Z_LEG2 = Block.box(6.0, 5.0, 4.0, 10.0, 10.0, 12.0);
Z_TOP = Block.box(3.0, 10.0, 0.0, 13.0, 16.0, 16.0);
X_AXIS_AABB = Shapes.or(AnvilBlock.BASE, AnvilBlock.X_LEG1, AnvilBlock.X_LEG2, AnvilBlock.X_TOP);
Z_AXIS_AABB = Shapes.or(AnvilBlock.BASE, AnvilBlock.Z_LEG1, AnvilBlock.Z_LEG2, AnvilBlock.Z_TOP);
CONTAINER_TITLE = new TranslatableComponent("container.repair", new Object[0]);
}
}