| 1 | package net.minecraft.world.level.block;
|
| 2 |
|
| 3 | import com.google.common.collect.Maps;
|
| 4 | import java.util.Map;
|
| 5 | import net.minecraft.core.BlockPos;
|
| 6 | import net.minecraft.core.Direction;
|
| 7 | import net.minecraft.server.level.ServerLevel;
|
| 8 | import net.minecraft.sounds.SoundSource;
|
| 9 | import net.minecraft.stats.Stats;
|
| 10 | import net.minecraft.util.Prediction;
|
| 11 | import net.minecraft.util.RandomSource;
|
| 12 | import net.minecraft.world.InteractionHand;
|
| 13 | import net.minecraft.world.InteractionResult;
|
| 14 | import net.minecraft.world.attribute.EnvironmentAttributes;
|
| 15 | import net.minecraft.world.entity.player.Player;
|
| 16 | import net.minecraft.world.item.BlockItem;
|
| 17 | import net.minecraft.world.item.ItemStack;
|
| 18 | import net.minecraft.world.level.BlockGetter;
|
| 19 | import net.minecraft.world.level.Level;
|
| 20 | import net.minecraft.world.level.LevelReader;
|
| 21 | import net.minecraft.world.level.ScheduledTickAccess;
|
| 22 | import net.minecraft.world.level.block.state.BlockBehaviour;
|
| 23 | import net.minecraft.world.level.block.state.BlockState;
|
| 24 | import net.minecraft.world.level.gameevent.GameEvent;
|
| 25 | import net.minecraft.world.level.pathfinder.PathComputationType;
|
| 26 | import net.minecraft.world.phys.BlockHitResult;
|
| 27 | import net.minecraft.world.phys.shapes.CollisionContext;
|
| 28 | import net.minecraft.world.phys.shapes.VoxelShape;
|
| 29 |
|
| 30 | public class FlowerPotBlock extends Block {
|
| 31 | private static final Map<Block, Block> POTTED_BY_CONTENT = Maps.newHashMap();
|
| 32 | private static final VoxelShape SHAPE = Block.column(6.0, 0.0, 6.0);
|
| 33 | private final Block potted;
|
| 34 |
|
| 35 | public FlowerPotBlock(final Block potted, final BlockBehaviour.Properties properties) {
|
| 36 | super(properties);
|
| 37 | this.potted = potted;
|
| 38 | POTTED_BY_CONTENT.put(potted, this);
|
| 39 | }
|
| 40 |
|
| 41 | @Override
|
| 42 | protected VoxelShape getShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context) {
|
| 43 | return SHAPE;
|
| 44 | }
|
| 45 |
|
| 46 | @Override
|
| 47 | protected InteractionResult useItemOn(
|
| 48 | final ItemStack itemStack,
|
| 49 | final BlockState state,
|
| 50 | final Level level,
|
| 51 | final BlockPos pos,
|
| 52 | final Player player,
|
| 53 | final InteractionHand hand,
|
| 54 | final BlockHitResult hitResult
|
| 55 | ) {
|
| 56 | BlockState newContents = (itemStack.getItem() instanceof BlockItem blockItem ? POTTED_BY_CONTENT.getOrDefault(blockItem.getBlock(), Blocks.AIR) : Blocks.AIR)
|
| 57 | .defaultBlockState();
|
| 58 | if (newContents.isAir()) {
|
| 59 | return InteractionResult.TRY_WITH_EMPTY_HAND;
|
| 60 | }
|
| 61 |
|
| 62 | if (!this.isEmpty()) {
|
| 63 | return InteractionResult.CONSUME;
|
| 64 | }
|
| 65 |
|
| 66 | level.setBlockAndUpdate(pos, newContents);
|
| 67 | level.gameEvent(player, GameEvent.BLOCK_CHANGE, pos);
|
| 68 | player.awardStat(Stats.POT_FLOWER);
|
| 69 | itemStack.consume(1, player);
|
| 70 | return InteractionResult.SUCCESS;
|
| 71 | }
|
| 72 |
|
| 73 | @Override
|
| 74 | protected InteractionResult useWithoutItem(final BlockState state, final Level level, final BlockPos pos, final Player player, final BlockHitResult hitResult) {
|
| 75 | if (this.isEmpty()) {
|
| 76 | return InteractionResult.CONSUME;
|
| 77 | }
|
| 78 |
|
| 79 | ItemStack plant = new ItemStack(this.potted);
|
| 80 | if (!player.addItem(plant)) {
|
| 81 | player.drop(plant, false, Prediction.PREDICTED);
|
| 82 | }
|
| 83 |
|
| 84 | level.setBlockAndUpdate(pos, Blocks.FLOWER_POT.defaultBlockState());
|
| 85 | level.gameEvent(player, GameEvent.BLOCK_CHANGE, pos);
|
| 86 | return InteractionResult.SUCCESS;
|
| 87 | }
|
| 88 |
|
| 89 | @Override
|
| 90 | protected ItemStack getCloneItemStack(final LevelReader level, final BlockPos pos, final BlockState state, final boolean includeData) {
|
| 91 | return this.isEmpty() ? super.getCloneItemStack(level, pos, state, includeData) : new ItemStack(this.potted);
|
| 92 | }
|
| 93 |
|
| 94 | private boolean isEmpty() {
|
| 95 | return this.potted == Blocks.AIR;
|
| 96 | }
|
| 97 |
|
| 98 | @Override
|
| 99 | protected BlockState updateShape(
|
| 100 | final BlockState state,
|
| 101 | final LevelReader level,
|
| 102 | final ScheduledTickAccess ticks,
|
| 103 | final BlockPos pos,
|
| 104 | final Direction directionToNeighbour,
|
| 105 | final BlockPos neighbourPos,
|
| 106 | final BlockState neighbourState,
|
| 107 | final RandomSource random
|
| 108 | ) {
|
| 109 | return directionToNeighbour == Direction.DOWN && !state.canSurvive(level, pos)
|
| 110 | ? Blocks.AIR.defaultBlockState()
|
| 111 | : super.updateShape(state, level, ticks, pos, directionToNeighbour, neighbourPos, neighbourState, random);
|
| 112 | }
|
| 113 |
|
| 114 | public Block getPotted() {
|
| 115 | return this.potted;
|
| 116 | }
|
| 117 |
|
| 118 | @Override
|
| 119 | protected boolean isPathfindable(final BlockState state, final PathComputationType type) {
|
| 120 | return false;
|
| 121 | }
|
| 122 |
|
| 123 | @Override
|
| 124 | protected boolean isRandomlyTicking(final BlockState state) {
|
| 125 | return state.is(Blocks.POTTED_OPEN_EYEBLOSSOM) || state.is(Blocks.POTTED_CLOSED_EYEBLOSSOM);
|
| 126 | }
|
| 127 |
|
| 128 | @Override
|
| 129 | protected void randomTick(final BlockState state, final ServerLevel level, final BlockPos pos, final RandomSource random) {
|
| 130 | if (this.isRandomlyTicking(state)) {
|
| 131 | boolean isOpen = this.potted == Blocks.OPEN_EYEBLOSSOM;
|
| 132 | boolean shouldBeOpen = level.environmentAttributes().getValue(EnvironmentAttributes.EYEBLOSSOM_OPEN, pos).toBoolean(isOpen);
|
| 133 | if (isOpen != shouldBeOpen) {
|
| 134 | level.setBlockAndUpdate(pos, this.opposite(state));
|
| 135 | EyeblossomBlock.Type newType = EyeblossomBlock.Type.fromBoolean(isOpen).transform();
|
| 136 | newType.spawnTransformParticle(level, pos, random);
|
| 137 | level.playSound(null, pos, newType.longSwitchSound(), SoundSource.BLOCKS, 1.0F, 1.0F);
|
| 138 | }
|
| 139 | }
|
| 140 |
|
| 141 | super.randomTick(state, level, pos, random);
|
| 142 | }
|
| 143 |
|
| 144 | public BlockState opposite(final BlockState state) {
|
| 145 | if (state.is(Blocks.POTTED_OPEN_EYEBLOSSOM)) {
|
| 146 | return Blocks.POTTED_CLOSED_EYEBLOSSOM.defaultBlockState();
|
| 147 | } else {
|
| 148 | return state.is(Blocks.POTTED_CLOSED_EYEBLOSSOM) ? Blocks.POTTED_OPEN_EYEBLOSSOM.defaultBlockState() : state;
|
| 149 | }
|
| 150 | }
|
| 151 | }
|
| 152 |
|