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

280 lines
12 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.level.block.state.properties.BlockStateProperties;
import net.minecraft.world.level.pathfinder.PathComputationType;
import net.minecraft.stats.Stats;
import net.minecraft.world.MenuProvider;
import net.minecraft.tags.ItemTags;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.item.ItemEntity;
import java.util.Random;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.entity.player.Player;
import net.minecraft.sounds.SoundSource;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import javax.annotation.Nullable;
import net.minecraft.world.level.block.entity.LecternBlockEntity;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.item.BlockPlaceContext;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.BlockGetter;
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.world.phys.shapes.VoxelShape;
import net.minecraft.world.level.block.state.properties.BooleanProperty;
import net.minecraft.world.level.block.state.properties.DirectionProperty;
public class LecternBlock extends BaseEntityBlock {
public static final DirectionProperty FACING;
public static final BooleanProperty POWERED;
public static final BooleanProperty HAS_BOOK;
public static final VoxelShape SHAPE_BASE;
public static final VoxelShape SHAPE_POST;
public static final VoxelShape SHAPE_COMMON;
public static final VoxelShape SHAPE_TOP_PLATE;
public static final VoxelShape SHAPE_COLLISION;
public static final VoxelShape SHAPE_WEST;
public static final VoxelShape SHAPE_NORTH;
public static final VoxelShape SHAPE_EAST;
public static final VoxelShape SHAPE_SOUTH;
protected LecternBlock(final Properties c) {
super(c);
this.registerDefaultState(((((AbstractStateHolder<O, BlockState>)this.stateDefinition.any()).setValue((Property<Comparable>)LecternBlock.FACING, Direction.NORTH)).setValue((Property<Comparable>)LecternBlock.POWERED, false)).<Comparable, Boolean>setValue((Property<Comparable>)LecternBlock.HAS_BOOK, false));
}
@Override
public RenderShape getRenderShape(final BlockState byg) {
return RenderShape.MODEL;
}
@Override
public VoxelShape getOcclusionShape(final BlockState byg, final BlockGetter bjd, final BlockPos fk) {
return LecternBlock.SHAPE_COMMON;
}
@Override
public boolean useShapeForLightOcclusion(final BlockState byg) {
return true;
}
@Override
public BlockState getStateForPlacement(final BlockPlaceContext bcn) {
return ((AbstractStateHolder<O, BlockState>)this.defaultBlockState()).<Comparable, Direction>setValue((Property<Comparable>)LecternBlock.FACING, bcn.getHorizontalDirection().getOpposite());
}
@Override
public VoxelShape getCollisionShape(final BlockState byg, final BlockGetter bjd, final BlockPos fk, final CollisionContext cvn) {
return LecternBlock.SHAPE_COLLISION;
}
@Override
public VoxelShape getShape(final BlockState byg, final BlockGetter bjd, final BlockPos fk, final CollisionContext cvn) {
switch (byg.<Direction>getValue((Property<Direction>)LecternBlock.FACING)) {
case NORTH: {
return LecternBlock.SHAPE_NORTH;
}
case SOUTH: {
return LecternBlock.SHAPE_SOUTH;
}
case EAST: {
return LecternBlock.SHAPE_EAST;
}
case WEST: {
return LecternBlock.SHAPE_WEST;
}
default: {
return LecternBlock.SHAPE_COMMON;
}
}
}
@Override
public BlockState rotate(final BlockState byg, final Rotation btr) {
return ((AbstractStateHolder<O, BlockState>)byg).<Comparable, Direction>setValue((Property<Comparable>)LecternBlock.FACING, btr.rotate(byg.<Direction>getValue((Property<Direction>)LecternBlock.FACING)));
}
@Override
public BlockState mirror(final BlockState byg, final Mirror bsr) {
return byg.rotate(bsr.getRotation(byg.<Direction>getValue((Property<Direction>)LecternBlock.FACING)));
}
@Override
protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> a) {
a.add(LecternBlock.FACING, LecternBlock.POWERED, LecternBlock.HAS_BOOK);
}
@Nullable
@Override
public BlockEntity newBlockEntity(final BlockGetter bjd) {
return new LecternBlockEntity();
}
public static boolean tryPlaceBook(final Level bjt, final BlockPos fk, final BlockState byg, final ItemStack bek) {
if (!byg.<Boolean>getValue((Property<Boolean>)LecternBlock.HAS_BOOK)) {
if (!bjt.isClientSide) {
placeBook(bjt, fk, byg, bek);
}
return true;
}
return false;
}
private static void placeBook(final Level bjt, final BlockPos fk, final BlockState byg, final ItemStack bek) {
final BlockEntity bwi5 = bjt.getBlockEntity(fk);
if (bwi5 instanceof LecternBlockEntity) {
final LecternBlockEntity bxa6 = (LecternBlockEntity)bwi5;
bxa6.setBook(bek.split(1));
resetBookState(bjt, fk, byg, true);
bjt.playSound(null, fk, SoundEvents.BOOK_PUT, SoundSource.BLOCKS, 1.0f, 1.0f);
}
}
public static void resetBookState(final Level bjt, final BlockPos fk, final BlockState byg, final boolean boolean4) {
bjt.setBlock(fk, (((AbstractStateHolder<O, BlockState>)byg).setValue((Property<Comparable>)LecternBlock.POWERED, false)).<Comparable, Boolean>setValue((Property<Comparable>)LecternBlock.HAS_BOOK, boolean4), 3);
updateBelow(bjt, fk, byg);
}
public static void signalPageChange(final Level bjt, final BlockPos fk, final BlockState byg) {
changePowered(bjt, fk, byg, true);
bjt.getBlockTicks().scheduleTick(fk, byg.getBlock(), 2);
bjt.levelEvent(1043, fk, 0);
}
private static void changePowered(final Level bjt, final BlockPos fk, final BlockState byg, final boolean boolean4) {
bjt.setBlock(fk, ((AbstractStateHolder<O, BlockState>)byg).<Comparable, Boolean>setValue((Property<Comparable>)LecternBlock.POWERED, boolean4), 3);
updateBelow(bjt, fk, byg);
}
private static void updateBelow(final Level bjt, final BlockPos fk, final BlockState byg) {
bjt.updateNeighborsAt(fk.below(), byg.getBlock());
}
@Override
public void tick(final BlockState byg, final ServerLevel xd, final BlockPos fk, final Random random) {
changePowered(xd, fk, byg, false);
}
@Override
public void onRemove(final BlockState byg1, final Level bjt, final BlockPos fk, final BlockState byg4, final boolean boolean5) {
if (byg1.getBlock() == byg4.getBlock()) {
return;
}
if (byg1.<Boolean>getValue((Property<Boolean>)LecternBlock.HAS_BOOK)) {
this.popBook(byg1, bjt, fk);
}
if (byg1.<Boolean>getValue((Property<Boolean>)LecternBlock.POWERED)) {
bjt.updateNeighborsAt(fk.below(), this);
}
super.onRemove(byg1, bjt, fk, byg4, boolean5);
}
private void popBook(final BlockState byg, final Level bjt, final BlockPos fk) {
final BlockEntity bwi5 = bjt.getBlockEntity(fk);
if (bwi5 instanceof LecternBlockEntity) {
final LecternBlockEntity bxa6 = (LecternBlockEntity)bwi5;
final Direction fp7 = byg.<Direction>getValue((Property<Direction>)LecternBlock.FACING);
final ItemStack bek8 = bxa6.getBook().copy();
final float float9 = 0.25f * fp7.getStepX();
final float float10 = 0.25f * fp7.getStepZ();
final ItemEntity avy11 = new ItemEntity(bjt, fk.getX() + 0.5 + float9, fk.getY() + 1, fk.getZ() + 0.5 + float10, bek8);
avy11.setDefaultPickUpDelay();
bjt.addFreshEntity(avy11);
bxa6.clearContent();
}
}
@Override
public boolean isSignalSource(final BlockState byg) {
return true;
}
@Override
public int getSignal(final BlockState byg, final BlockGetter bjd, final BlockPos fk, final Direction fp) {
return byg.<Boolean>getValue((Property<Boolean>)LecternBlock.POWERED) ? 15 : 0;
}
@Override
public int getDirectSignal(final BlockState byg, final BlockGetter bjd, final BlockPos fk, final Direction fp) {
return (fp == Direction.UP && byg.<Boolean>getValue((Property<Boolean>)LecternBlock.POWERED)) ? 15 : 0;
}
@Override
public boolean hasAnalogOutputSignal(final BlockState byg) {
return true;
}
@Override
public int getAnalogOutputSignal(final BlockState byg, final Level bjt, final BlockPos fk) {
if (byg.<Boolean>getValue((Property<Boolean>)LecternBlock.HAS_BOOK)) {
final BlockEntity bwi5 = bjt.getBlockEntity(fk);
if (bwi5 instanceof LecternBlockEntity) {
return ((LecternBlockEntity)bwi5).getRedstoneSignal();
}
}
return 0;
}
@Override
public InteractionResult use(final BlockState byg, final Level bjt, final BlockPos fk, final Player ayg, final InteractionHand ajh, final BlockHitResult cvd) {
if (byg.<Boolean>getValue((Property<Boolean>)LecternBlock.HAS_BOOK)) {
if (!bjt.isClientSide) {
this.openScreen(bjt, fk, ayg);
}
return InteractionResult.SUCCESS;
}
final ItemStack bek8 = ayg.getItemInHand(ajh);
if (bek8.isEmpty() || bek8.getItem().is(ItemTags.LECTERN_BOOKS)) {
return InteractionResult.PASS;
}
return InteractionResult.CONSUME;
}
@Nullable
@Override
public MenuProvider getMenuProvider(final BlockState byg, final Level bjt, final BlockPos fk) {
if (!byg.<Boolean>getValue((Property<Boolean>)LecternBlock.HAS_BOOK)) {
return null;
}
return super.getMenuProvider(byg, bjt, fk);
}
private void openScreen(final Level bjt, final BlockPos fk, final Player ayg) {
final BlockEntity bwi5 = bjt.getBlockEntity(fk);
if (bwi5 instanceof LecternBlockEntity) {
ayg.openMenu((MenuProvider)bwi5);
ayg.awardStat(Stats.INTERACT_WITH_LECTERN);
}
}
@Override
public boolean isPathfindable(final BlockState byg, final BlockGetter bjd, final BlockPos fk, final PathComputationType cqo) {
return false;
}
static {
FACING = HorizontalDirectionalBlock.FACING;
POWERED = BlockStateProperties.POWERED;
HAS_BOOK = BlockStateProperties.HAS_BOOK;
SHAPE_BASE = Block.box(0.0, 0.0, 0.0, 16.0, 2.0, 16.0);
SHAPE_POST = Block.box(4.0, 2.0, 4.0, 12.0, 14.0, 12.0);
SHAPE_COMMON = Shapes.or(LecternBlock.SHAPE_BASE, LecternBlock.SHAPE_POST);
SHAPE_TOP_PLATE = Block.box(0.0, 15.0, 0.0, 16.0, 15.0, 16.0);
SHAPE_COLLISION = Shapes.or(LecternBlock.SHAPE_COMMON, LecternBlock.SHAPE_TOP_PLATE);
SHAPE_WEST = Shapes.or(Block.box(1.0, 10.0, 0.0, 5.333333, 14.0, 16.0), Block.box(5.333333, 12.0, 0.0, 9.666667, 16.0, 16.0), Block.box(9.666667, 14.0, 0.0, 14.0, 18.0, 16.0), LecternBlock.SHAPE_COMMON);
SHAPE_NORTH = Shapes.or(Block.box(0.0, 10.0, 1.0, 16.0, 14.0, 5.333333), Block.box(0.0, 12.0, 5.333333, 16.0, 16.0, 9.666667), Block.box(0.0, 14.0, 9.666667, 16.0, 18.0, 14.0), LecternBlock.SHAPE_COMMON);
SHAPE_EAST = Shapes.or(Block.box(15.0, 10.0, 0.0, 10.666667, 14.0, 16.0), Block.box(10.666667, 12.0, 0.0, 6.333333, 16.0, 16.0), Block.box(6.333333, 14.0, 0.0, 2.0, 18.0, 16.0), LecternBlock.SHAPE_COMMON);
SHAPE_SOUTH = Shapes.or(Block.box(0.0, 10.0, 15.0, 16.0, 14.0, 10.666667), Block.box(0.0, 12.0, 10.666667, 16.0, 16.0, 6.333333), Block.box(0.0, 14.0, 6.333333, 16.0, 18.0, 2.0), LecternBlock.SHAPE_COMMON);
}
}