package net.minecraft.world.level.block; import com.google.common.collect.Maps; import com.mojang.serialization.MapCodec; import com.mojang.serialization.codecs.RecordCodecBuilder; import java.util.Map; import net.minecraft.core.BlockPos; import net.minecraft.core.Direction; import net.minecraft.core.registries.BuiltInRegistries; import net.minecraft.stats.Stats; import net.minecraft.util.RandomSource; import net.minecraft.world.InteractionHand; import net.minecraft.world.InteractionResult; import net.minecraft.world.entity.player.Player; import net.minecraft.world.item.BlockItem; import net.minecraft.world.item.ItemStack; import net.minecraft.world.level.BlockGetter; import net.minecraft.world.level.Level; import net.minecraft.world.level.LevelReader; import net.minecraft.world.level.ScheduledTickAccess; import net.minecraft.world.level.block.state.BlockBehaviour; import net.minecraft.world.level.block.state.BlockState; 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 FlowerPotBlock extends Block { public static final MapCodec CODEC = RecordCodecBuilder.mapCodec( instance -> instance.group(BuiltInRegistries.BLOCK.byNameCodec().fieldOf("potted").forGetter(flowerPotBlock -> flowerPotBlock.potted), propertiesCodec()) .apply(instance, FlowerPotBlock::new) ); private static final Map POTTED_BY_CONTENT = Maps.newHashMap(); public static final float AABB_SIZE = 3.0F; protected static final VoxelShape SHAPE = Block.box(5.0, 0.0, 5.0, 11.0, 6.0, 11.0); private final Block potted; @Override public MapCodec codec() { return CODEC; } public FlowerPotBlock(Block potted, BlockBehaviour.Properties properties) { super(properties); this.potted = potted; POTTED_BY_CONTENT.put(potted, this); } @Override protected VoxelShape getShape(BlockState state, BlockGetter level, BlockPos pos, CollisionContext context) { return SHAPE; } @Override protected InteractionResult useItemOn( ItemStack stack, BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hitResult ) { BlockState blockState = (stack.getItem() instanceof BlockItem blockItem ? (Block)POTTED_BY_CONTENT.getOrDefault(blockItem.getBlock(), Blocks.AIR) : Blocks.AIR) .defaultBlockState(); if (blockState.isAir()) { return InteractionResult.TRY_WITH_EMPTY_HAND; } else if (!this.isEmpty()) { return InteractionResult.CONSUME; } else { level.setBlock(pos, blockState, 3); level.gameEvent(player, GameEvent.BLOCK_CHANGE, pos); player.awardStat(Stats.POT_FLOWER); stack.consume(1, player); return InteractionResult.SUCCESS; } } @Override protected InteractionResult useWithoutItem(BlockState state, Level level, BlockPos pos, Player player, BlockHitResult hitResult) { if (this.isEmpty()) { return InteractionResult.CONSUME; } else { ItemStack itemStack = new ItemStack(this.potted); if (!player.addItem(itemStack)) { player.drop(itemStack, false); } level.setBlock(pos, Blocks.FLOWER_POT.defaultBlockState(), 3); level.gameEvent(player, GameEvent.BLOCK_CHANGE, pos); return InteractionResult.SUCCESS; } } @Override public ItemStack getCloneItemStack(LevelReader level, BlockPos pos, BlockState state) { return this.isEmpty() ? super.getCloneItemStack(level, pos, state) : new ItemStack(this.potted); } private boolean isEmpty() { return this.potted == Blocks.AIR; } @Override protected BlockState updateShape( BlockState state, LevelReader level, ScheduledTickAccess scheduledTickAccess, BlockPos pos, Direction direction, BlockPos neighborPos, BlockState neighborState, RandomSource random ) { return direction == Direction.DOWN && !state.canSurvive(level, pos) ? Blocks.AIR.defaultBlockState() : super.updateShape(state, level, scheduledTickAccess, pos, direction, neighborPos, neighborState, random); } public Block getPotted() { return this.potted; } @Override protected boolean isPathfindable(BlockState state, PathComputationType pathComputationType) { return false; } }