149 lines
6.0 KiB
Java
149 lines
6.0 KiB
Java
/*
|
|
* Decompiled with CFR 0.152.
|
|
*
|
|
* Could not load the following classes:
|
|
* com.mojang.serialization.MapCodec
|
|
*/
|
|
package net.minecraft.world.level.block;
|
|
|
|
import com.mojang.serialization.MapCodec;
|
|
import net.minecraft.core.BlockPos;
|
|
import net.minecraft.core.Direction;
|
|
import net.minecraft.sounds.SoundEvents;
|
|
import net.minecraft.sounds.SoundSource;
|
|
import net.minecraft.stats.Stats;
|
|
import net.minecraft.tags.ItemTags;
|
|
import net.minecraft.util.RandomSource;
|
|
import net.minecraft.world.InteractionHand;
|
|
import net.minecraft.world.InteractionResult;
|
|
import net.minecraft.world.entity.Entity;
|
|
import net.minecraft.world.entity.player.Player;
|
|
import net.minecraft.world.item.Item;
|
|
import net.minecraft.world.item.ItemStack;
|
|
import net.minecraft.world.level.BlockGetter;
|
|
import net.minecraft.world.level.Level;
|
|
import net.minecraft.world.level.LevelAccessor;
|
|
import net.minecraft.world.level.LevelReader;
|
|
import net.minecraft.world.level.ScheduledTickAccess;
|
|
import net.minecraft.world.level.block.Block;
|
|
import net.minecraft.world.level.block.Blocks;
|
|
import net.minecraft.world.level.block.CandleBlock;
|
|
import net.minecraft.world.level.block.CandleCakeBlock;
|
|
import net.minecraft.world.level.block.state.BlockBehaviour;
|
|
import net.minecraft.world.level.block.state.BlockState;
|
|
import net.minecraft.world.level.block.state.StateDefinition;
|
|
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
|
|
import net.minecraft.world.level.block.state.properties.IntegerProperty;
|
|
import net.minecraft.world.level.gameevent.GameEvent;
|
|
import net.minecraft.world.level.pathfinder.PathComputationType;
|
|
import net.minecraft.world.phys.BlockHitResult;
|
|
import net.minecraft.world.phys.shapes.CollisionContext;
|
|
import net.minecraft.world.phys.shapes.VoxelShape;
|
|
|
|
public class CakeBlock
|
|
extends Block {
|
|
public static final MapCodec<CakeBlock> CODEC = CakeBlock.simpleCodec(CakeBlock::new);
|
|
public static final int MAX_BITES = 6;
|
|
public static final IntegerProperty BITES = BlockStateProperties.BITES;
|
|
public static final int FULL_CAKE_SIGNAL = CakeBlock.getOutputSignal(0);
|
|
private static final VoxelShape[] SHAPES = Block.boxes(6, bite -> Block.box(1 + bite * 2, 0.0, 1.0, 15.0, 8.0, 15.0));
|
|
|
|
public MapCodec<CakeBlock> codec() {
|
|
return CODEC;
|
|
}
|
|
|
|
protected CakeBlock(BlockBehaviour.Properties properties) {
|
|
super(properties);
|
|
this.registerDefaultState((BlockState)((BlockState)this.stateDefinition.any()).setValue(BITES, 0));
|
|
}
|
|
|
|
@Override
|
|
protected VoxelShape getShape(BlockState state, BlockGetter level, BlockPos pos, CollisionContext context) {
|
|
return SHAPES[state.getValue(BITES)];
|
|
}
|
|
|
|
@Override
|
|
protected InteractionResult useItemOn(ItemStack itemStack, BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hitResult) {
|
|
Block block;
|
|
Item item = itemStack.getItem();
|
|
if (!itemStack.is(ItemTags.CANDLES) || state.getValue(BITES) != 0 || !((block = Block.byItem(item)) instanceof CandleBlock)) {
|
|
return InteractionResult.TRY_WITH_EMPTY_HAND;
|
|
}
|
|
CandleBlock candleBlock = (CandleBlock)block;
|
|
itemStack.consume(1, player);
|
|
level.playSound(null, pos, SoundEvents.CAKE_ADD_CANDLE, SoundSource.BLOCKS, 1.0f, 1.0f);
|
|
level.setBlockAndUpdate(pos, CandleCakeBlock.byCandle(candleBlock));
|
|
level.gameEvent((Entity)player, GameEvent.BLOCK_CHANGE, pos);
|
|
player.awardStat(Stats.ITEM_USED.get(item));
|
|
return InteractionResult.SUCCESS;
|
|
}
|
|
|
|
@Override
|
|
protected InteractionResult useWithoutItem(BlockState state, Level level, BlockPos pos, Player player, BlockHitResult hitResult) {
|
|
if (level.isClientSide()) {
|
|
if (CakeBlock.eat(level, pos, state, player).consumesAction()) {
|
|
return InteractionResult.SUCCESS;
|
|
}
|
|
if (player.getItemInHand(InteractionHand.MAIN_HAND).isEmpty()) {
|
|
return InteractionResult.CONSUME;
|
|
}
|
|
}
|
|
return CakeBlock.eat(level, pos, state, player);
|
|
}
|
|
|
|
protected static InteractionResult eat(LevelAccessor level, BlockPos pos, BlockState state, Player player) {
|
|
if (!player.canEat(false)) {
|
|
return InteractionResult.PASS;
|
|
}
|
|
player.awardStat(Stats.EAT_CAKE_SLICE);
|
|
player.getFoodData().eat(2, 0.1f);
|
|
int bites = state.getValue(BITES);
|
|
level.gameEvent((Entity)player, GameEvent.EAT, pos);
|
|
if (bites < 6) {
|
|
level.setBlock(pos, (BlockState)state.setValue(BITES, bites + 1), 3);
|
|
} else {
|
|
level.removeBlock(pos, false);
|
|
level.gameEvent((Entity)player, GameEvent.BLOCK_DESTROY, pos);
|
|
}
|
|
return InteractionResult.SUCCESS;
|
|
}
|
|
|
|
@Override
|
|
protected BlockState updateShape(BlockState state, LevelReader level, ScheduledTickAccess ticks, BlockPos pos, Direction directionToNeighbour, BlockPos neighbourPos, BlockState neighbourState, RandomSource random) {
|
|
if (directionToNeighbour == Direction.DOWN && !state.canSurvive(level, pos)) {
|
|
return Blocks.AIR.defaultBlockState();
|
|
}
|
|
return super.updateShape(state, level, ticks, pos, directionToNeighbour, neighbourPos, neighbourState, random);
|
|
}
|
|
|
|
@Override
|
|
protected boolean canSurvive(BlockState state, LevelReader level, BlockPos pos) {
|
|
return level.getBlockState(pos.below()).isSolid();
|
|
}
|
|
|
|
@Override
|
|
protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) {
|
|
builder.add(BITES);
|
|
}
|
|
|
|
@Override
|
|
protected int getAnalogOutputSignal(BlockState state, Level level, BlockPos pos, Direction direction) {
|
|
return CakeBlock.getOutputSignal(state.getValue(BITES));
|
|
}
|
|
|
|
public static int getOutputSignal(int bitesTaken) {
|
|
return (7 - bitesTaken) * 2;
|
|
}
|
|
|
|
@Override
|
|
protected boolean hasAnalogOutputSignal(BlockState state) {
|
|
return true;
|
|
}
|
|
|
|
@Override
|
|
protected boolean isPathfindable(BlockState state, PathComputationType type) {
|
|
return false;
|
|
}
|
|
}
|
|
|