| 1 | package net.minecraft.world.level.block;
|
| 2 |
|
| 3 | import java.util.List;
|
| 4 | import net.minecraft.core.BlockPos;
|
| 5 | import net.minecraft.core.Direction;
|
| 6 | import net.minecraft.core.particles.ParticleTypes;
|
| 7 | import net.minecraft.resources.Identifier;
|
| 8 | import net.minecraft.server.level.ServerLevel;
|
| 9 | import net.minecraft.sounds.SoundEvents;
|
| 10 | import net.minecraft.sounds.SoundSource;
|
| 11 | import net.minecraft.stats.Stats;
|
| 12 | import net.minecraft.tags.EnchantmentTags;
|
| 13 | import net.minecraft.tags.ItemTags;
|
| 14 | import net.minecraft.util.RandomSource;
|
| 15 | import net.minecraft.world.Containers;
|
| 16 | import net.minecraft.world.InteractionHand;
|
| 17 | import net.minecraft.world.InteractionResult;
|
| 18 | import net.minecraft.world.entity.player.Player;
|
| 19 | import net.minecraft.world.entity.projectile.Projectile;
|
| 20 | import net.minecraft.world.inventory.AbstractContainerMenu;
|
| 21 | import net.minecraft.world.item.ItemStack;
|
| 22 | import net.minecraft.world.item.context.BlockPlaceContext;
|
| 23 | import net.minecraft.world.item.enchantment.EnchantmentHelper;
|
| 24 | import net.minecraft.world.level.BlockGetter;
|
| 25 | import net.minecraft.world.level.Level;
|
| 26 | import net.minecraft.world.level.LevelReader;
|
| 27 | import net.minecraft.world.level.ScheduledTickAccess;
|
| 28 | import net.minecraft.world.level.block.entity.BlockEntity;
|
| 29 | import net.minecraft.world.level.block.entity.DecoratedPotBlockEntity;
|
| 30 | import net.minecraft.world.level.block.entity.PotDecorations;
|
| 31 | import net.minecraft.world.level.block.state.BlockBehaviour;
|
| 32 | import net.minecraft.world.level.block.state.BlockState;
|
| 33 | import net.minecraft.world.level.block.state.StateDefinition;
|
| 34 | import net.minecraft.world.level.block.state.properties.BlockStateProperties;
|
| 35 | import net.minecraft.world.level.block.state.properties.BooleanProperty;
|
| 36 | import net.minecraft.world.level.block.state.properties.EnumProperty;
|
| 37 | import net.minecraft.world.level.gameevent.GameEvent;
|
| 38 | import net.minecraft.world.level.material.FluidState;
|
| 39 | import net.minecraft.world.level.material.Fluids;
|
| 40 | import net.minecraft.world.level.pathfinder.PathComputationType;
|
| 41 | import net.minecraft.world.level.storage.loot.LootParams;
|
| 42 | import net.minecraft.world.level.storage.loot.parameters.LootContextParams;
|
| 43 | import net.minecraft.world.phys.BlockHitResult;
|
| 44 | import net.minecraft.world.phys.shapes.CollisionContext;
|
| 45 | import net.minecraft.world.phys.shapes.VoxelShape;
|
| 46 | import org.jspecify.annotations.Nullable;
|
| 47 |
|
| 48 | /**
|
| 49 | * Access widened by fabric-transitive-access-wideners-v1 to accessible
|
| 50 | */
|
| 51 | public class DecoratedPotBlock extends BaseEntityBlock implements SimpleWaterloggedBlock {
|
| 52 | public static final Identifier SHERDS_DYNAMIC_DROP_ID = Identifier.withDefaultNamespace("sherds");
|
| 53 | public static final EnumProperty<Direction> HORIZONTAL_FACING = BlockStateProperties.HORIZONTAL_FACING;
|
| 54 | public static final BooleanProperty CRACKED = BlockStateProperties.CRACKED;
|
| 55 | public static final BooleanProperty WATERLOGGED = BlockStateProperties.WATERLOGGED;
|
| 56 | private static final VoxelShape SHAPE = Block.column(14.0, 0.0, 16.0);
|
| 57 |
|
| 58 | /**
|
| 59 | * Access widened by fabric-transitive-access-wideners-v1 to accessible
|
| 60 | */
|
| 61 | public DecoratedPotBlock(final BlockBehaviour.Properties properties) {
|
| 62 | super(properties);
|
| 63 | this.registerDefaultState(this.stateDefinition.any().setValue(HORIZONTAL_FACING, Direction.NORTH).setValue(WATERLOGGED, false).setValue(CRACKED, false));
|
| 64 | }
|
| 65 |
|
| 66 | @Override
|
| 67 | protected BlockState updateShape(
|
| 68 | final BlockState state,
|
| 69 | final LevelReader level,
|
| 70 | final ScheduledTickAccess ticks,
|
| 71 | final BlockPos pos,
|
| 72 | final Direction directionToNeighbour,
|
| 73 | final BlockPos neighbourPos,
|
| 74 | final BlockState neighbourState,
|
| 75 | final RandomSource random
|
| 76 | ) {
|
| 77 | if (state.getValue(WATERLOGGED)) {
|
| 78 | ticks.scheduleTick(pos, Fluids.WATER, Fluids.WATER.getTickDelay(level));
|
| 79 | }
|
| 80 |
|
| 81 | return super.updateShape(state, level, ticks, pos, directionToNeighbour, neighbourPos, neighbourState, random);
|
| 82 | }
|
| 83 |
|
| 84 | @Override
|
| 85 | public BlockState getStateForPlacement(final BlockPlaceContext context) {
|
| 86 | FluidState replacedFluidState = context.getLevel().getFluidState(context.getClickedPos());
|
| 87 | return this.defaultBlockState()
|
| 88 | .setValue(HORIZONTAL_FACING, context.getHorizontalDirection())
|
| 89 | .setValue(WATERLOGGED, replacedFluidState.is(Fluids.WATER))
|
| 90 | .setValue(CRACKED, false);
|
| 91 | }
|
| 92 |
|
| 93 | @Override
|
| 94 | protected InteractionResult useItemOn(
|
| 95 | final ItemStack itemStack,
|
| 96 | final BlockState state,
|
| 97 | final Level level,
|
| 98 | final BlockPos pos,
|
| 99 | final Player player,
|
| 100 | final InteractionHand hand,
|
| 101 | final BlockHitResult hitResult
|
| 102 | ) {
|
| 103 | if (level.getBlockEntity(pos) instanceof DecoratedPotBlockEntity decoratedPot) {
|
| 104 | if (level.isClientSide()) {
|
| 105 | return InteractionResult.SUCCESS;
|
| 106 | }
|
| 107 |
|
| 108 | ItemStack potItem = decoratedPot.getTheItem();
|
| 109 | if (!itemStack.isEmpty() && (potItem.isEmpty() || ItemStack.isSameItemSameComponents(potItem, itemStack) && potItem.getCount() < potItem.getMaxStackSize())) {
|
| 110 | decoratedPot.wobble(DecoratedPotBlockEntity.WobbleStyle.POSITIVE);
|
| 111 | player.awardStat(Stats.ITEM_USED.get(itemStack.getItem()));
|
| 112 | ItemStack awardedItem = itemStack.consumeAndReturn(1, player);
|
| 113 | float pitchBend;
|
| 114 | if (decoratedPot.isEmpty()) {
|
| 115 | decoratedPot.setTheItem(awardedItem);
|
| 116 | pitchBend = (float)awardedItem.getCount() / awardedItem.getMaxStackSize();
|
| 117 | } else {
|
| 118 | potItem.grow(1);
|
| 119 | pitchBend = (float)potItem.getCount() / potItem.getMaxStackSize();
|
| 120 | }
|
| 121 |
|
| 122 | level.playSound(null, pos, SoundEvents.DECORATED_POT_INSERT, SoundSource.BLOCKS, 1.0F, 0.7F + 0.5F * pitchBend);
|
| 123 | if (level instanceof ServerLevel serverLevel) {
|
| 124 | 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);
|
| 125 | }
|
| 126 |
|
| 127 | decoratedPot.setChanged();
|
| 128 | level.gameEvent(player, GameEvent.BLOCK_CHANGE, pos);
|
| 129 | return InteractionResult.SUCCESS;
|
| 130 | } else {
|
| 131 | return InteractionResult.TRY_WITH_EMPTY_HAND;
|
| 132 | }
|
| 133 | } else {
|
| 134 | return InteractionResult.PASS;
|
| 135 | }
|
| 136 | }
|
| 137 |
|
| 138 | @Override
|
| 139 | protected InteractionResult useWithoutItem(final BlockState state, final Level level, final BlockPos pos, final Player player, final BlockHitResult hitResult) {
|
| 140 | if (level.getBlockEntity(pos) instanceof DecoratedPotBlockEntity decoratedPot) {
|
| 141 | level.playSound(null, pos, SoundEvents.DECORATED_POT_INSERT_FAIL, SoundSource.BLOCKS, 1.0F, 1.0F);
|
| 142 | decoratedPot.wobble(DecoratedPotBlockEntity.WobbleStyle.NEGATIVE);
|
| 143 | level.gameEvent(player, GameEvent.BLOCK_CHANGE, pos);
|
| 144 | return InteractionResult.SUCCESS;
|
| 145 | } else {
|
| 146 | return InteractionResult.PASS;
|
| 147 | }
|
| 148 | }
|
| 149 |
|
| 150 | @Override
|
| 151 | protected boolean isPathfindable(final BlockState state, final PathComputationType type) {
|
| 152 | return false;
|
| 153 | }
|
| 154 |
|
| 155 | @Override
|
| 156 | protected VoxelShape getShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context) {
|
| 157 | return SHAPE;
|
| 158 | }
|
| 159 |
|
| 160 | @Override
|
| 161 | protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder) {
|
| 162 | builder.add(HORIZONTAL_FACING, WATERLOGGED, CRACKED);
|
| 163 | }
|
| 164 |
|
| 165 | @Override
|
| 166 | public @Nullable BlockEntity newBlockEntity(final BlockPos worldPosition, final BlockState blockState) {
|
| 167 | return new DecoratedPotBlockEntity(worldPosition, blockState);
|
| 168 | }
|
| 169 |
|
| 170 | @Override
|
| 171 | protected void affectNeighborsAfterRemoval(final BlockState state, final ServerLevel level, final BlockPos pos, final boolean movedByPiston) {
|
| 172 | Containers.updateNeighboursAfterDestroy(state, level, pos);
|
| 173 | }
|
| 174 |
|
| 175 | @Override
|
| 176 | protected List<ItemStack> getDrops(final BlockState state, final LootParams.Builder params) {
|
| 177 | BlockEntity maybeEntity = params.getOptionalParameter(LootContextParams.BLOCK_ENTITY);
|
| 178 | if (maybeEntity instanceof DecoratedPotBlockEntity entity) {
|
| 179 | params.withDynamicDrop(SHERDS_DYNAMIC_DROP_ID, output -> {
|
| 180 | entity.getDecorations().left().ifPresent(item -> output.accept(item.create()));
|
| 181 | entity.getDecorations().back().ifPresent(item -> output.accept(item.create()));
|
| 182 | entity.getDecorations().front().ifPresent(item -> output.accept(item.create()));
|
| 183 | entity.getDecorations().right().ifPresent(item -> output.accept(item.create()));
|
| 184 | });
|
| 185 | }
|
| 186 |
|
| 187 | return super.getDrops(state, params);
|
| 188 | }
|
| 189 |
|
| 190 | @Override
|
| 191 | public BlockState playerWillDestroy(final Level level, final BlockPos pos, final BlockState state, final Player player) {
|
| 192 | ItemStack destroyedWith = player.getMainHandItem();
|
| 193 | BlockState nextState = state;
|
| 194 | if (destroyedWith.is(ItemTags.BREAKS_DECORATED_POTS) && !EnchantmentHelper.hasTag(destroyedWith, EnchantmentTags.PREVENTS_DECORATED_POT_SHATTERING)) {
|
| 195 | nextState = state.setValue(CRACKED, true);
|
| 196 | level.setBlock(pos, nextState, 260);
|
| 197 | }
|
| 198 |
|
| 199 | return super.playerWillDestroy(level, pos, nextState, player);
|
| 200 | }
|
| 201 |
|
| 202 | @Override
|
| 203 | protected FluidState getFluidState(final BlockState state) {
|
| 204 | return state.getValue(WATERLOGGED) ? Fluids.WATER.getSource(false) : super.getFluidState(state);
|
| 205 | }
|
| 206 |
|
| 207 | @Override
|
| 208 | protected SoundType getSoundType(final BlockState state) {
|
| 209 | return state.getValue(CRACKED) ? SoundType.DECORATED_POT_CRACKED : SoundType.DECORATED_POT;
|
| 210 | }
|
| 211 |
|
| 212 | @Override
|
| 213 | protected void onProjectileHit(final Level level, final BlockState state, final BlockHitResult blockHit, final Projectile projectile) {
|
| 214 | BlockPos pos = blockHit.getBlockPos();
|
| 215 | if (level instanceof ServerLevel serverLevel && projectile.mayInteract(serverLevel, pos) && projectile.mayBreak(serverLevel, pos)) {
|
| 216 | level.setBlock(pos, state.setValue(CRACKED, true), 260);
|
| 217 | level.destroyBlock(pos, true, projectile);
|
| 218 | }
|
| 219 | }
|
| 220 |
|
| 221 | @Override
|
| 222 | protected ItemStack getCloneItemStack(final LevelReader level, final BlockPos pos, final BlockState state, final boolean includeData) {
|
| 223 | if (level.getBlockEntity(pos) instanceof DecoratedPotBlockEntity decoratedPotBlockEntity) {
|
| 224 | PotDecorations decorations = decoratedPotBlockEntity.getDecorations();
|
| 225 | return DecoratedPotBlockEntity.createDecoratedPotInstance(decorations);
|
| 226 | } else {
|
| 227 | return super.getCloneItemStack(level, pos, state, includeData);
|
| 228 | }
|
| 229 | }
|
| 230 |
|
| 231 | @Override
|
| 232 | protected boolean hasAnalogOutputSignal(final BlockState state) {
|
| 233 | return true;
|
| 234 | }
|
| 235 |
|
| 236 | @Override
|
| 237 | protected int getAnalogOutputSignal(final BlockState state, final Level level, final BlockPos pos, final Direction direction) {
|
| 238 | return AbstractContainerMenu.getRedstoneSignalFromBlockEntity(level.getBlockEntity(pos));
|
| 239 | }
|
| 240 |
|
| 241 | @Override
|
| 242 | protected BlockState rotate(final BlockState state, final Rotation rotation) {
|
| 243 | return state.setValue(HORIZONTAL_FACING, rotation.rotate(state.getValue(HORIZONTAL_FACING)));
|
| 244 | }
|
| 245 |
|
| 246 | @Override
|
| 247 | protected BlockState mirror(final BlockState state, final Mirror mirror) {
|
| 248 | return state.rotate(mirror.getRotation(state.getValue(HORIZONTAL_FACING)));
|
| 249 | }
|
| 250 | }
|
| 251 |
|