| 1 | package net.minecraft.world.level.block;
|
| 2 |
|
| 3 | import net.minecraft.core.BlockPos;
|
| 4 | import net.minecraft.core.Direction;
|
| 5 | import net.minecraft.sounds.SoundEvents;
|
| 6 | import net.minecraft.sounds.SoundSource;
|
| 7 | import net.minecraft.stats.Stats;
|
| 8 | import net.minecraft.tags.ItemTags;
|
| 9 | import net.minecraft.util.RandomSource;
|
| 10 | import net.minecraft.world.InteractionHand;
|
| 11 | import net.minecraft.world.InteractionResult;
|
| 12 | import net.minecraft.world.entity.player.Player;
|
| 13 | import net.minecraft.world.item.Item;
|
| 14 | import net.minecraft.world.item.ItemStack;
|
| 15 | import net.minecraft.world.level.BlockGetter;
|
| 16 | import net.minecraft.world.level.Level;
|
| 17 | import net.minecraft.world.level.LevelAccessor;
|
| 18 | import net.minecraft.world.level.LevelReader;
|
| 19 | import net.minecraft.world.level.ScheduledTickAccess;
|
| 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.BlockStateProperties;
|
| 24 | import net.minecraft.world.level.block.state.properties.IntegerProperty;
|
| 25 | import net.minecraft.world.level.gameevent.GameEvent;
|
| 26 | import net.minecraft.world.level.pathfinder.PathComputationType;
|
| 27 | import net.minecraft.world.phys.BlockHitResult;
|
| 28 | import net.minecraft.world.phys.shapes.CollisionContext;
|
| 29 | import net.minecraft.world.phys.shapes.VoxelShape;
|
| 30 |
|
| 31 | /**
|
| 32 | * Access widened by fabric-transitive-access-wideners-v1 to accessible
|
| 33 | */
|
| 34 | public class CakeBlock extends Block {
|
| 35 | public static final int MAX_BITES = 6;
|
| 36 | public static final IntegerProperty BITES = BlockStateProperties.BITES;
|
| 37 | public static final int FULL_CAKE_SIGNAL = getOutputSignal(0);
|
| 38 | private static final VoxelShape[] SHAPES = Block.boxes(6, bite -> Block.box(1 + bite * 2, 0.0, 1.0, 15.0, 8.0, 15.0));
|
| 39 |
|
| 40 | /**
|
| 41 | * Access widened by fabric-transitive-access-wideners-v1 to accessible
|
| 42 | */
|
| 43 | public CakeBlock(final BlockBehaviour.Properties properties) {
|
| 44 | super(properties);
|
| 45 | this.registerDefaultState(this.stateDefinition.any().setValue(BITES, 0));
|
| 46 | }
|
| 47 |
|
| 48 | @Override
|
| 49 | protected VoxelShape getShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context) {
|
| 50 | return SHAPES[state.getValue(BITES)];
|
| 51 | }
|
| 52 |
|
| 53 | @Override
|
| 54 | protected InteractionResult useItemOn(
|
| 55 | final ItemStack itemStack,
|
| 56 | final BlockState state,
|
| 57 | final Level level,
|
| 58 | final BlockPos pos,
|
| 59 | final Player player,
|
| 60 | final InteractionHand hand,
|
| 61 | final BlockHitResult hitResult
|
| 62 | ) {
|
| 63 | Item item = itemStack.getItem();
|
| 64 | if (itemStack.is(ItemTags.CANDLES) && state.getValue(BITES) == 0 && Block.byItem(item) instanceof CandleBlock candleBlock) {
|
| 65 | itemStack.consume(1, player);
|
| 66 | level.playSound(null, pos, SoundEvents.CAKE_ADD_CANDLE, SoundSource.BLOCKS, 1.0F, 1.0F);
|
| 67 | level.setBlockAndUpdate(pos, CandleCakeBlock.byCandle(candleBlock));
|
| 68 | level.gameEvent(player, GameEvent.BLOCK_CHANGE, pos);
|
| 69 | player.awardStat(Stats.ITEM_USED.get(item));
|
| 70 | return InteractionResult.SUCCESS;
|
| 71 | } else {
|
| 72 | return InteractionResult.TRY_WITH_EMPTY_HAND;
|
| 73 | }
|
| 74 | }
|
| 75 |
|
| 76 | @Override
|
| 77 | protected InteractionResult useWithoutItem(final BlockState state, final Level level, final BlockPos pos, final Player player, final BlockHitResult hitResult) {
|
| 78 | if (level.isClientSide()) {
|
| 79 | if (eat(level, pos, state, player).consumesAction()) {
|
| 80 | return InteractionResult.SUCCESS;
|
| 81 | }
|
| 82 |
|
| 83 | if (player.getItemInHand(InteractionHand.MAIN_HAND).isEmpty()) {
|
| 84 | return InteractionResult.CONSUME;
|
| 85 | }
|
| 86 | }
|
| 87 |
|
| 88 | return eat(level, pos, state, player);
|
| 89 | }
|
| 90 |
|
| 91 | protected static InteractionResult eat(final LevelAccessor level, final BlockPos pos, final BlockState state, final Player player) {
|
| 92 | if (!player.canEat(false)) {
|
| 93 | return InteractionResult.PASS;
|
| 94 | }
|
| 95 |
|
| 96 | player.awardStat(Stats.EAT_CAKE_SLICE);
|
| 97 | player.getFoodData().eat(2, 0.1F);
|
| 98 | int bites = state.getValue(BITES);
|
| 99 | level.gameEvent(player, GameEvent.EAT, pos);
|
| 100 | if (bites < 6) {
|
| 101 | level.setBlockAndUpdate(pos, state.setValue(BITES, bites + 1));
|
| 102 | } else {
|
| 103 | level.removeBlock(pos, false);
|
| 104 | level.gameEvent(player, GameEvent.BLOCK_DESTROY, pos);
|
| 105 | }
|
| 106 |
|
| 107 | return InteractionResult.SUCCESS;
|
| 108 | }
|
| 109 |
|
| 110 | @Override
|
| 111 | protected BlockState updateShape(
|
| 112 | final BlockState state,
|
| 113 | final LevelReader level,
|
| 114 | final ScheduledTickAccess ticks,
|
| 115 | final BlockPos pos,
|
| 116 | final Direction directionToNeighbour,
|
| 117 | final BlockPos neighbourPos,
|
| 118 | final BlockState neighbourState,
|
| 119 | final RandomSource random
|
| 120 | ) {
|
| 121 | return directionToNeighbour == Direction.DOWN && !state.canSurvive(level, pos)
|
| 122 | ? Blocks.AIR.defaultBlockState()
|
| 123 | : super.updateShape(state, level, ticks, pos, directionToNeighbour, neighbourPos, neighbourState, random);
|
| 124 | }
|
| 125 |
|
| 126 | @Override
|
| 127 | protected boolean canSurvive(final BlockState state, final LevelReader level, final BlockPos pos) {
|
| 128 | return level.getBlockState(pos.below()).isSolid();
|
| 129 | }
|
| 130 |
|
| 131 | @Override
|
| 132 | protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder) {
|
| 133 | builder.add(BITES);
|
| 134 | }
|
| 135 |
|
| 136 | @Override
|
| 137 | protected int getAnalogOutputSignal(final BlockState state, final Level level, final BlockPos pos, final Direction direction) {
|
| 138 | return getOutputSignal(state.getValue(BITES));
|
| 139 | }
|
| 140 |
|
| 141 | public static int getOutputSignal(final int bitesTaken) {
|
| 142 | return (7 - bitesTaken) * 2;
|
| 143 | }
|
| 144 |
|
| 145 | @Override
|
| 146 | protected boolean hasAnalogOutputSignal(final BlockState state) {
|
| 147 | return true;
|
| 148 | }
|
| 149 |
|
| 150 | @Override
|
| 151 | protected boolean isPathfindable(final BlockState state, final PathComputationType type) {
|
| 152 | return false;
|
| 153 | }
|
| 154 | }
|
| 155 |
|