| 1 | package net.minecraft.world.level.block;
|
| 2 |
|
| 3 | import com.google.common.collect.Maps;
|
| 4 | import java.util.List;
|
| 5 | import java.util.Map;
|
| 6 | import net.minecraft.core.BlockPos;
|
| 7 | import net.minecraft.core.Direction;
|
| 8 | import net.minecraft.tags.BlockTags;
|
| 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.ItemStack;
|
| 14 | import net.minecraft.world.item.Items;
|
| 15 | import net.minecraft.world.level.BlockGetter;
|
| 16 | import net.minecraft.world.level.Level;
|
| 17 | import net.minecraft.world.level.LevelReader;
|
| 18 | import net.minecraft.world.level.ScheduledTickAccess;
|
| 19 | import net.minecraft.world.level.block.state.BlockBehaviour;
|
| 20 | import net.minecraft.world.level.block.state.BlockState;
|
| 21 | import net.minecraft.world.level.block.state.StateDefinition;
|
| 22 | import net.minecraft.world.level.block.state.properties.BooleanProperty;
|
| 23 | import net.minecraft.world.level.pathfinder.PathComputationType;
|
| 24 | import net.minecraft.world.phys.BlockHitResult;
|
| 25 | import net.minecraft.world.phys.Vec3;
|
| 26 | import net.minecraft.world.phys.shapes.CollisionContext;
|
| 27 | import net.minecraft.world.phys.shapes.Shapes;
|
| 28 | import net.minecraft.world.phys.shapes.VoxelShape;
|
| 29 |
|
| 30 | /**
|
| 31 | * Access widened by fabric-transitive-access-wideners-v1 to accessible
|
| 32 | */
|
| 33 | public class CandleCakeBlock extends AbstractCandleBlock {
|
| 34 | public static final BooleanProperty LIT = AbstractCandleBlock.LIT;
|
| 35 | private static final VoxelShape SHAPE = Shapes.or(Block.column(2.0, 8.0, 14.0), Block.column(14.0, 0.0, 8.0));
|
| 36 | private static final Map<CandleBlock, CandleCakeBlock> BY_CANDLE = Maps.newHashMap();
|
| 37 | private static final Iterable<Vec3> PARTICLE_OFFSETS = List.of(new Vec3(8.0, 16.0, 8.0).scale(0.0625));
|
| 38 | private final CandleBlock candleBlock;
|
| 39 |
|
| 40 | /**
|
| 41 | * Access widened by fabric-transitive-access-wideners-v1 to accessible
|
| 42 | */
|
| 43 | public CandleCakeBlock(final Block block, final BlockBehaviour.Properties properties) {
|
| 44 | super(properties);
|
| 45 | this.registerDefaultState(this.stateDefinition.any().setValue(LIT, false));
|
| 46 | if (block instanceof CandleBlock matchingCandleBlock) {
|
| 47 | BY_CANDLE.put(matchingCandleBlock, this);
|
| 48 | this.candleBlock = matchingCandleBlock;
|
| 49 | } else {
|
| 50 | throw new IllegalArgumentException("Expected block to be of " + CandleBlock.class + " was " + block.getClass());
|
| 51 | }
|
| 52 | }
|
| 53 |
|
| 54 | @Override
|
| 55 | protected Iterable<Vec3> getParticleOffsets(final BlockState state) {
|
| 56 | return PARTICLE_OFFSETS;
|
| 57 | }
|
| 58 |
|
| 59 | @Override
|
| 60 | protected VoxelShape getShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context) {
|
| 61 | return SHAPE;
|
| 62 | }
|
| 63 |
|
| 64 | @Override
|
| 65 | protected InteractionResult useItemOn(
|
| 66 | final ItemStack itemStack,
|
| 67 | final BlockState state,
|
| 68 | final Level level,
|
| 69 | final BlockPos pos,
|
| 70 | final Player player,
|
| 71 | final InteractionHand hand,
|
| 72 | final BlockHitResult hitResult
|
| 73 | ) {
|
| 74 | if (itemStack.is(Items.FLINT_AND_STEEL) || itemStack.is(Items.FIRE_CHARGE)) {
|
| 75 | return InteractionResult.PASS;
|
| 76 | } else if (candleHit(hitResult) && itemStack.isEmpty() && state.getValue(LIT)) {
|
| 77 | extinguish(player, state, level, pos);
|
| 78 | return InteractionResult.SUCCESS;
|
| 79 | } else {
|
| 80 | return super.useItemOn(itemStack, state, level, pos, player, hand, hitResult);
|
| 81 | }
|
| 82 | }
|
| 83 |
|
| 84 | @Override
|
| 85 | protected InteractionResult useWithoutItem(final BlockState state, final Level level, final BlockPos pos, final Player player, final BlockHitResult hitResult) {
|
| 86 | InteractionResult eatResult = CakeBlock.eat(level, pos, Blocks.CAKE.defaultBlockState(), player);
|
| 87 | if (eatResult.consumesAction()) {
|
| 88 | dropResources(state, level, pos);
|
| 89 | }
|
| 90 |
|
| 91 | return eatResult;
|
| 92 | }
|
| 93 |
|
| 94 | private static boolean candleHit(final BlockHitResult hitResult) {
|
| 95 | return hitResult.getLocation().y - hitResult.getBlockPos().getY() > 0.5;
|
| 96 | }
|
| 97 |
|
| 98 | @Override
|
| 99 | protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder) {
|
| 100 | builder.add(LIT);
|
| 101 | }
|
| 102 |
|
| 103 | @Override
|
| 104 | protected ItemStack getCloneItemStack(final LevelReader level, final BlockPos pos, final BlockState state, final boolean includeData) {
|
| 105 | return new ItemStack(Blocks.CAKE);
|
| 106 | }
|
| 107 |
|
| 108 | @Override
|
| 109 | protected BlockState updateShape(
|
| 110 | final BlockState state,
|
| 111 | final LevelReader level,
|
| 112 | final ScheduledTickAccess ticks,
|
| 113 | final BlockPos pos,
|
| 114 | final Direction directionToNeighbour,
|
| 115 | final BlockPos neighbourPos,
|
| 116 | final BlockState neighbourState,
|
| 117 | final RandomSource random
|
| 118 | ) {
|
| 119 | return directionToNeighbour == Direction.DOWN && !state.canSurvive(level, pos)
|
| 120 | ? Blocks.AIR.defaultBlockState()
|
| 121 | : super.updateShape(state, level, ticks, pos, directionToNeighbour, neighbourPos, neighbourState, random);
|
| 122 | }
|
| 123 |
|
| 124 | @Override
|
| 125 | protected boolean canSurvive(final BlockState state, final LevelReader level, final BlockPos pos) {
|
| 126 | return level.getBlockState(pos.below()).isSolid();
|
| 127 | }
|
| 128 |
|
| 129 | @Override
|
| 130 | protected int getAnalogOutputSignal(final BlockState state, final Level level, final BlockPos pos, final Direction direction) {
|
| 131 | return CakeBlock.FULL_CAKE_SIGNAL;
|
| 132 | }
|
| 133 |
|
| 134 | @Override
|
| 135 | protected boolean hasAnalogOutputSignal(final BlockState state) {
|
| 136 | return true;
|
| 137 | }
|
| 138 |
|
| 139 | @Override
|
| 140 | protected boolean isPathfindable(final BlockState state, final PathComputationType type) {
|
| 141 | return false;
|
| 142 | }
|
| 143 |
|
| 144 | public static BlockState byCandle(final CandleBlock block) {
|
| 145 | return BY_CANDLE.get(block).defaultBlockState();
|
| 146 | }
|
| 147 |
|
| 148 | public static boolean canLight(final BlockState state) {
|
| 149 | return state.is(BlockTags.CANDLE_CAKES, s -> s.hasProperty(LIT) && !state.getValue(LIT));
|
| 150 | }
|
| 151 | }
|
| 152 |
|