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

107 lines
4.6 KiB
Java

package net.minecraft.world.level.block;
import net.minecraft.world.level.block.state.AbstractStateHolder;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import net.minecraft.world.level.pathfinder.PathComputationType;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.level.LevelReader;
import net.minecraft.core.Direction;
import net.minecraft.stats.Stats;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.LevelAccessor;
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.world.level.Level;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.block.state.properties.Property;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.shapes.VoxelShape;
import net.minecraft.world.level.block.state.properties.IntegerProperty;
public class CakeBlock extends Block {
public static final IntegerProperty BITES;
protected static final VoxelShape[] SHAPE_BY_BITE;
protected CakeBlock(final Properties c) {
super(c);
this.registerDefaultState(((AbstractStateHolder<O, BlockState>)this.stateDefinition.any()).<Comparable, Integer>setValue((Property<Comparable>)CakeBlock.BITES, 0));
}
@Override
public VoxelShape getShape(final BlockState byg, final BlockGetter bjd, final BlockPos fk, final CollisionContext cvn) {
return CakeBlock.SHAPE_BY_BITE[byg.<Integer>getValue((Property<Integer>)CakeBlock.BITES)];
}
@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) {
final ItemStack bek8 = ayg.getItemInHand(ajh);
if (this.eat(bjt, fk, byg, ayg) == InteractionResult.SUCCESS) {
return InteractionResult.SUCCESS;
}
if (bek8.isEmpty()) {
return InteractionResult.CONSUME;
}
}
return this.eat(bjt, fk, byg, ayg);
}
private InteractionResult eat(final LevelAccessor bju, final BlockPos fk, final BlockState byg, final Player ayg) {
if (!ayg.canEat(false)) {
return InteractionResult.PASS;
}
ayg.awardStat(Stats.EAT_CAKE_SLICE);
ayg.getFoodData().eat(2, 0.1f);
final int integer6 = byg.<Integer>getValue((Property<Integer>)CakeBlock.BITES);
if (integer6 < 6) {
bju.setBlock(fk, ((AbstractStateHolder<O, BlockState>)byg).<Comparable, Integer>setValue((Property<Comparable>)CakeBlock.BITES, integer6 + 1), 3);
}
else {
bju.removeBlock(fk, false);
}
return InteractionResult.SUCCESS;
}
@Override
public BlockState updateShape(final BlockState byg1, final Direction fp, final BlockState byg3, final LevelAccessor bju, final BlockPos fk5, final BlockPos fk6) {
if (fp == Direction.DOWN && !byg1.canSurvive(bju, fk5)) {
return Blocks.AIR.defaultBlockState();
}
return super.updateShape(byg1, fp, byg3, bju, fk5, fk6);
}
@Override
public boolean canSurvive(final BlockState byg, final LevelReader bjw, final BlockPos fk) {
return bjw.getBlockState(fk.below()).getMaterial().isSolid();
}
@Override
protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> a) {
a.add(CakeBlock.BITES);
}
@Override
public int getAnalogOutputSignal(final BlockState byg, final Level bjt, final BlockPos fk) {
return (7 - byg.<Integer>getValue((Property<Integer>)CakeBlock.BITES)) * 2;
}
@Override
public boolean hasAnalogOutputSignal(final BlockState byg) {
return true;
}
@Override
public boolean isPathfindable(final BlockState byg, final BlockGetter bjd, final BlockPos fk, final PathComputationType cqo) {
return false;
}
static {
BITES = BlockStateProperties.BITES;
SHAPE_BY_BITE = new VoxelShape[] { Block.box(1.0, 0.0, 1.0, 15.0, 8.0, 15.0), Block.box(3.0, 0.0, 1.0, 15.0, 8.0, 15.0), Block.box(5.0, 0.0, 1.0, 15.0, 8.0, 15.0), Block.box(7.0, 0.0, 1.0, 15.0, 8.0, 15.0), Block.box(9.0, 0.0, 1.0, 15.0, 8.0, 15.0), Block.box(11.0, 0.0, 1.0, 15.0, 8.0, 15.0), Block.box(13.0, 0.0, 1.0, 15.0, 8.0, 15.0) };
}
}