package net.minecraft.world.level.block; import com.mojang.serialization.MapCodec; import java.util.List; import net.minecraft.core.BlockPos; import net.minecraft.core.Direction; import net.minecraft.core.particles.ParticleTypes; import net.minecraft.resources.Identifier; import net.minecraft.server.level.ServerLevel; import net.minecraft.sounds.SoundEvents; import net.minecraft.sounds.SoundSource; import net.minecraft.stats.Stats; import net.minecraft.tags.EnchantmentTags; import net.minecraft.tags.ItemTags; import net.minecraft.util.RandomSource; import net.minecraft.world.Containers; import net.minecraft.world.InteractionHand; import net.minecraft.world.InteractionResult; import net.minecraft.world.entity.player.Player; import net.minecraft.world.entity.projectile.Projectile; import net.minecraft.world.inventory.AbstractContainerMenu; import net.minecraft.world.item.Item; import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.context.BlockPlaceContext; import net.minecraft.world.item.enchantment.EnchantmentHelper; 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.entity.BlockEntity; import net.minecraft.world.level.block.entity.DecoratedPotBlockEntity; import net.minecraft.world.level.block.entity.PotDecorations; 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.BooleanProperty; import net.minecraft.world.level.block.state.properties.EnumProperty; import net.minecraft.world.level.gameevent.GameEvent; import net.minecraft.world.level.material.FluidState; import net.minecraft.world.level.material.Fluids; import net.minecraft.world.level.pathfinder.PathComputationType; import net.minecraft.world.level.storage.loot.LootParams; import net.minecraft.world.level.storage.loot.parameters.LootContextParams; import net.minecraft.world.phys.BlockHitResult; import net.minecraft.world.phys.shapes.CollisionContext; import net.minecraft.world.phys.shapes.VoxelShape; import org.jspecify.annotations.Nullable; /** * Access widened by fabric-transitive-access-wideners-v1 to accessible */ public class DecoratedPotBlock extends BaseEntityBlock implements SimpleWaterloggedBlock { public static final MapCodec CODEC = simpleCodec(DecoratedPotBlock::new); public static final Identifier SHERDS_DYNAMIC_DROP_ID = Identifier.withDefaultNamespace("sherds"); public static final EnumProperty HORIZONTAL_FACING = BlockStateProperties.HORIZONTAL_FACING; public static final BooleanProperty CRACKED = BlockStateProperties.CRACKED; public static final BooleanProperty WATERLOGGED = BlockStateProperties.WATERLOGGED; private static final VoxelShape SHAPE = Block.column(14.0, 0.0, 16.0); @Override public MapCodec codec() { return CODEC; } /** * Access widened by fabric-transitive-access-wideners-v1 to accessible */ public DecoratedPotBlock(final BlockBehaviour.Properties properties) { super(properties); this.registerDefaultState(this.stateDefinition.any().setValue(HORIZONTAL_FACING, Direction.NORTH).setValue(WATERLOGGED, false).setValue(CRACKED, false)); } @Override protected BlockState updateShape( final BlockState state, final LevelReader level, final ScheduledTickAccess ticks, final BlockPos pos, final Direction directionToNeighbour, final BlockPos neighbourPos, final BlockState neighbourState, final RandomSource random ) { if ((Boolean)state.getValue(WATERLOGGED)) { ticks.scheduleTick(pos, Fluids.WATER, Fluids.WATER.getTickDelay(level)); } return super.updateShape(state, level, ticks, pos, directionToNeighbour, neighbourPos, neighbourState, random); } @Override public BlockState getStateForPlacement(final BlockPlaceContext context) { FluidState replacedFluidState = context.getLevel().getFluidState(context.getClickedPos()); return this.defaultBlockState() .setValue(HORIZONTAL_FACING, context.getHorizontalDirection()) .setValue(WATERLOGGED, replacedFluidState.is(Fluids.WATER)) .setValue(CRACKED, false); } @Override protected InteractionResult useItemOn( final ItemStack itemStack, final BlockState state, final Level level, final BlockPos pos, final Player player, final InteractionHand hand, final BlockHitResult hitResult ) { if (level.getBlockEntity(pos) instanceof DecoratedPotBlockEntity decoratedPot) { if (level.isClientSide()) { return InteractionResult.SUCCESS; } else { ItemStack potItem = decoratedPot.getTheItem(); if (!itemStack.isEmpty() && (potItem.isEmpty() || ItemStack.isSameItemSameComponents(potItem, itemStack) && potItem.getCount() < potItem.getMaxStackSize()) ) { decoratedPot.wobble(DecoratedPotBlockEntity.WobbleStyle.POSITIVE); player.awardStat(Stats.ITEM_USED.get(itemStack.getItem())); ItemStack awardedItem = itemStack.consumeAndReturn(1, player); float pitchBend; if (decoratedPot.isEmpty()) { decoratedPot.setTheItem(awardedItem); pitchBend = (float)awardedItem.getCount() / awardedItem.getMaxStackSize(); } else { potItem.grow(1); pitchBend = (float)potItem.getCount() / potItem.getMaxStackSize(); } level.playSound(null, pos, SoundEvents.DECORATED_POT_INSERT, SoundSource.BLOCKS, 1.0F, 0.7F + 0.5F * pitchBend); if (level instanceof ServerLevel serverLevel) { serverLevel.sendParticles(ParticleTypes.DUST_PLUME, pos.getX() + 0.5, pos.getY() + 1.2, pos.getZ() + 0.5, 7, 0.0, 0.0, 0.0, 0.0); } decoratedPot.setChanged(); level.gameEvent(player, GameEvent.BLOCK_CHANGE, pos); return InteractionResult.SUCCESS; } else { return InteractionResult.TRY_WITH_EMPTY_HAND; } } } else { return InteractionResult.PASS; } } @Override protected InteractionResult useWithoutItem(final BlockState state, final Level level, final BlockPos pos, final Player player, final BlockHitResult hitResult) { if (level.getBlockEntity(pos) instanceof DecoratedPotBlockEntity decoratedPot) { level.playSound(null, pos, SoundEvents.DECORATED_POT_INSERT_FAIL, SoundSource.BLOCKS, 1.0F, 1.0F); decoratedPot.wobble(DecoratedPotBlockEntity.WobbleStyle.NEGATIVE); level.gameEvent(player, GameEvent.BLOCK_CHANGE, pos); return InteractionResult.SUCCESS; } else { return InteractionResult.PASS; } } @Override protected boolean isPathfindable(final BlockState state, final PathComputationType type) { return false; } @Override protected VoxelShape getShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context) { return SHAPE; } @Override protected void createBlockStateDefinition(final StateDefinition.Builder builder) { builder.add(HORIZONTAL_FACING, WATERLOGGED, CRACKED); } @Nullable @Override public BlockEntity newBlockEntity(final BlockPos worldPosition, final BlockState blockState) { return new DecoratedPotBlockEntity(worldPosition, blockState); } @Override protected void affectNeighborsAfterRemoval(final BlockState state, final ServerLevel level, final BlockPos pos, final boolean movedByPiston) { Containers.updateNeighboursAfterDestroy(state, level, pos); } @Override protected List getDrops(final BlockState state, final LootParams.Builder params) { BlockEntity maybeEntity = params.getOptionalParameter(LootContextParams.BLOCK_ENTITY); if (maybeEntity instanceof DecoratedPotBlockEntity entity) { params.withDynamicDrop(SHERDS_DYNAMIC_DROP_ID, output -> { for (Item item : entity.getDecorations().ordered()) { output.accept(item.getDefaultInstance()); } }); } return super.getDrops(state, params); } @Override public BlockState playerWillDestroy(final Level level, final BlockPos pos, final BlockState state, final Player player) { ItemStack destroyedWith = player.getMainHandItem(); BlockState nextState = state; if (destroyedWith.is(ItemTags.BREAKS_DECORATED_POTS) && !EnchantmentHelper.hasTag(destroyedWith, EnchantmentTags.PREVENTS_DECORATED_POT_SHATTERING)) { nextState = state.setValue(CRACKED, true); level.setBlock(pos, nextState, 260); } return super.playerWillDestroy(level, pos, nextState, player); } @Override protected FluidState getFluidState(final BlockState state) { return state.getValue(WATERLOGGED) ? Fluids.WATER.getSource(false) : super.getFluidState(state); } @Override protected SoundType getSoundType(final BlockState state) { return state.getValue(CRACKED) ? SoundType.DECORATED_POT_CRACKED : SoundType.DECORATED_POT; } @Override protected void onProjectileHit(final Level level, final BlockState state, final BlockHitResult blockHit, final Projectile projectile) { BlockPos pos = blockHit.getBlockPos(); if (level instanceof ServerLevel serverLevel && projectile.mayInteract(serverLevel, pos) && projectile.mayBreak(serverLevel)) { level.setBlock(pos, state.setValue(CRACKED, true), 260); level.destroyBlock(pos, true, projectile); } } @Override protected ItemStack getCloneItemStack(final LevelReader level, final BlockPos pos, final BlockState state, final boolean includeData) { if (level.getBlockEntity(pos) instanceof DecoratedPotBlockEntity decoratedPotBlockEntity) { PotDecorations decorations = decoratedPotBlockEntity.getDecorations(); return DecoratedPotBlockEntity.createDecoratedPotInstance(decorations); } else { return super.getCloneItemStack(level, pos, state, includeData); } } @Override protected boolean hasAnalogOutputSignal(final BlockState state) { return true; } @Override protected int getAnalogOutputSignal(final BlockState state, final Level level, final BlockPos pos, final Direction direction) { return AbstractContainerMenu.getRedstoneSignalFromBlockEntity(level.getBlockEntity(pos)); } @Override protected BlockState rotate(final BlockState state, final Rotation rotation) { return state.setValue(HORIZONTAL_FACING, rotation.rotate(state.getValue(HORIZONTAL_FACING))); } @Override protected BlockState mirror(final BlockState state, final Mirror mirror) { return state.rotate(mirror.getRotation(state.getValue(HORIZONTAL_FACING))); } }