| 1 | package net.minecraft.world.level.block;
|
| 2 |
|
| 3 | import net.minecraft.core.BlockPos;
|
| 4 | import net.minecraft.core.Direction;
|
| 5 | import net.minecraft.core.particles.ParticleTypes;
|
| 6 | import net.minecraft.core.particles.SimpleParticleType;
|
| 7 | import net.minecraft.server.level.ServerLevel;
|
| 8 | import net.minecraft.sounds.SoundEvents;
|
| 9 | import net.minecraft.sounds.SoundSource;
|
| 10 | import net.minecraft.stats.Stats;
|
| 11 | import net.minecraft.tags.BlockTags;
|
| 12 | import net.minecraft.tags.ItemTags;
|
| 13 | import net.minecraft.util.RandomSource;
|
| 14 | import net.minecraft.world.InteractionHand;
|
| 15 | import net.minecraft.world.InteractionResult;
|
| 16 | import net.minecraft.world.entity.Entity;
|
| 17 | import net.minecraft.world.entity.InsideBlockEffectApplier;
|
| 18 | import net.minecraft.world.entity.LivingEntity;
|
| 19 | import net.minecraft.world.entity.player.Player;
|
| 20 | import net.minecraft.world.entity.projectile.Projectile;
|
| 21 | import net.minecraft.world.item.ItemStack;
|
| 22 | import net.minecraft.world.item.context.BlockPlaceContext;
|
| 23 | import net.minecraft.world.item.crafting.CampfireCookingRecipe;
|
| 24 | import net.minecraft.world.item.crafting.RecipeManager;
|
| 25 | import net.minecraft.world.item.crafting.RecipePropertySet;
|
| 26 | import net.minecraft.world.item.crafting.RecipeType;
|
| 27 | import net.minecraft.world.item.crafting.SingleRecipeInput;
|
| 28 | import net.minecraft.world.level.BlockGetter;
|
| 29 | import net.minecraft.world.level.Level;
|
| 30 | import net.minecraft.world.level.LevelAccessor;
|
| 31 | import net.minecraft.world.level.LevelReader;
|
| 32 | import net.minecraft.world.level.ScheduledTickAccess;
|
| 33 | import net.minecraft.world.level.block.entity.BlockEntity;
|
| 34 | import net.minecraft.world.level.block.entity.BlockEntityTicker;
|
| 35 | import net.minecraft.world.level.block.entity.BlockEntityType;
|
| 36 | import net.minecraft.world.level.block.entity.BlockEntityTypes;
|
| 37 | import net.minecraft.world.level.block.entity.CampfireBlockEntity;
|
| 38 | import net.minecraft.world.level.block.state.BlockBehaviour;
|
| 39 | import net.minecraft.world.level.block.state.BlockState;
|
| 40 | import net.minecraft.world.level.block.state.StateDefinition;
|
| 41 | import net.minecraft.world.level.block.state.properties.BlockStateProperties;
|
| 42 | import net.minecraft.world.level.block.state.properties.BooleanProperty;
|
| 43 | import net.minecraft.world.level.block.state.properties.EnumProperty;
|
| 44 | import net.minecraft.world.level.gameevent.GameEvent;
|
| 45 | import net.minecraft.world.level.material.FluidState;
|
| 46 | import net.minecraft.world.level.material.Fluids;
|
| 47 | import net.minecraft.world.level.pathfinder.PathComputationType;
|
| 48 | import net.minecraft.world.phys.BlockHitResult;
|
| 49 | import net.minecraft.world.phys.shapes.BooleanOp;
|
| 50 | import net.minecraft.world.phys.shapes.CollisionContext;
|
| 51 | import net.minecraft.world.phys.shapes.Shapes;
|
| 52 | import net.minecraft.world.phys.shapes.VoxelShape;
|
| 53 | import org.jspecify.annotations.Nullable;
|
| 54 |
|
| 55 | public class CampfireBlock extends BaseEntityBlock implements SimpleWaterloggedBlock {
|
| 56 | public static final BooleanProperty LIT = BlockStateProperties.LIT;
|
| 57 | public static final BooleanProperty SIGNAL_FIRE = BlockStateProperties.SIGNAL_FIRE;
|
| 58 | public static final BooleanProperty WATERLOGGED = BlockStateProperties.WATERLOGGED;
|
| 59 | public static final EnumProperty<Direction> FACING = BlockStateProperties.HORIZONTAL_FACING;
|
| 60 | private static final VoxelShape SHAPE = Block.column(16.0, 0.0, 7.0);
|
| 61 | private static final VoxelShape SHAPE_VIRTUAL_POST = Block.column(4.0, 0.0, 16.0);
|
| 62 | private static final int SMOKE_DISTANCE = 5;
|
| 63 | private final boolean spawnParticles;
|
| 64 | private final int fireDamage;
|
| 65 |
|
| 66 | public CampfireBlock(final boolean spawnParticles, final int fireDamage, final BlockBehaviour.Properties properties) {
|
| 67 | super(properties);
|
| 68 | this.spawnParticles = spawnParticles;
|
| 69 | this.fireDamage = fireDamage;
|
| 70 | this.registerDefaultState(
|
| 71 | this.stateDefinition.any().setValue(LIT, true).setValue(SIGNAL_FIRE, false).setValue(WATERLOGGED, false).setValue(FACING, Direction.NORTH)
|
| 72 | );
|
| 73 | }
|
| 74 |
|
| 75 | @Override
|
| 76 | protected InteractionResult useItemOn(
|
| 77 | final ItemStack itemStack,
|
| 78 | final BlockState state,
|
| 79 | final Level level,
|
| 80 | final BlockPos pos,
|
| 81 | final Player player,
|
| 82 | final InteractionHand hand,
|
| 83 | final BlockHitResult hitResult
|
| 84 | ) {
|
| 85 | if (level.getBlockEntity(pos) instanceof CampfireBlockEntity campfire) {
|
| 86 | ItemStack itemInHand = player.getItemInHand(hand);
|
| 87 | if (level.recipeAccess().propertySet(RecipePropertySet.CAMPFIRE_INPUT).test(itemInHand)) {
|
| 88 | if (level instanceof ServerLevel serverLevel && campfire.placeFood(serverLevel, player, itemInHand)) {
|
| 89 | player.awardStat(Stats.INTERACT_WITH_CAMPFIRE);
|
| 90 | return InteractionResult.SUCCESS_SERVER;
|
| 91 | }
|
| 92 |
|
| 93 | return InteractionResult.CONSUME;
|
| 94 | }
|
| 95 | }
|
| 96 |
|
| 97 | if (itemStack.is(ItemTags.DOUSES_CAMPFIRES) && state.getValue(LIT)) {
|
| 98 | if (!level.isClientSide()) {
|
| 99 | level.levelEvent(null, 1009, pos, 0);
|
| 100 | }
|
| 101 |
|
| 102 | douse(player, level, pos, state);
|
| 103 | level.setBlockAndUpdate(pos, state.setValue(LIT, false));
|
| 104 | itemStack.hurtAndBreak(1, player, hand.asEquipmentSlot());
|
| 105 | return InteractionResult.SUCCESS;
|
| 106 | } else {
|
| 107 | return InteractionResult.TRY_WITH_EMPTY_HAND;
|
| 108 | }
|
| 109 | }
|
| 110 |
|
| 111 | @Override
|
| 112 | protected void entityInside(
|
| 113 | final BlockState state, final Level level, final BlockPos pos, final Entity entity, final InsideBlockEffectApplier effectApplier, final boolean isPrecise
|
| 114 | ) {
|
| 115 | if (state.getValue(LIT) && entity instanceof LivingEntity) {
|
| 116 | entity.hurt(level.damageSources().campfire(), this.fireDamage);
|
| 117 | }
|
| 118 |
|
| 119 | super.entityInside(state, level, pos, entity, effectApplier, isPrecise);
|
| 120 | }
|
| 121 |
|
| 122 | @Override
|
| 123 | public @Nullable BlockState getStateForPlacement(final BlockPlaceContext context) {
|
| 124 | LevelAccessor level = context.getLevel();
|
| 125 | BlockPos pos = context.getClickedPos();
|
| 126 | boolean replacedWater = level.getFluidState(pos).is(Fluids.WATER);
|
| 127 | return this.defaultBlockState()
|
| 128 | .setValue(WATERLOGGED, replacedWater)
|
| 129 | .setValue(SIGNAL_FIRE, this.isSmokeSource(level.getBlockState(pos.below())))
|
| 130 | .setValue(LIT, !replacedWater)
|
| 131 | .setValue(FACING, context.getHorizontalDirection());
|
| 132 | }
|
| 133 |
|
| 134 | @Override
|
| 135 | protected BlockState updateShape(
|
| 136 | final BlockState state,
|
| 137 | final LevelReader level,
|
| 138 | final ScheduledTickAccess ticks,
|
| 139 | final BlockPos pos,
|
| 140 | final Direction directionToNeighbour,
|
| 141 | final BlockPos neighbourPos,
|
| 142 | final BlockState neighbourState,
|
| 143 | final RandomSource random
|
| 144 | ) {
|
| 145 | if (state.getValue(WATERLOGGED)) {
|
| 146 | ticks.scheduleTick(pos, Fluids.WATER, Fluids.WATER.getTickDelay(level));
|
| 147 | }
|
| 148 |
|
| 149 | return directionToNeighbour == Direction.DOWN
|
| 150 | ? state.setValue(SIGNAL_FIRE, this.isSmokeSource(neighbourState))
|
| 151 | : super.updateShape(state, level, ticks, pos, directionToNeighbour, neighbourPos, neighbourState, random);
|
| 152 | }
|
| 153 |
|
| 154 | private boolean isSmokeSource(final BlockState blockState) {
|
| 155 | return blockState.is(Blocks.HAY_BLOCK);
|
| 156 | }
|
| 157 |
|
| 158 | @Override
|
| 159 | protected VoxelShape getShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context) {
|
| 160 | return SHAPE;
|
| 161 | }
|
| 162 |
|
| 163 | @Override
|
| 164 | public void animateTick(final BlockState state, final Level level, final BlockPos pos, final RandomSource random) {
|
| 165 | if (state.getValue(LIT)) {
|
| 166 | if (random.nextInt(10) == 0) {
|
| 167 | level.playLocalSound(
|
| 168 | pos.getX() + 0.5,
|
| 169 | pos.getY() + 0.5,
|
| 170 | pos.getZ() + 0.5,
|
| 171 | SoundEvents.CAMPFIRE_CRACKLE,
|
| 172 | SoundSource.BLOCKS,
|
| 173 | 0.5F + random.nextFloat(),
|
| 174 | random.nextFloat() * 0.7F + 0.6F,
|
| 175 | false
|
| 176 | );
|
| 177 | }
|
| 178 |
|
| 179 | if (this.spawnParticles && random.nextInt(5) == 0) {
|
| 180 | for (int i = 0; i < random.nextInt(1) + 1; i++) {
|
| 181 | level.addParticle(ParticleTypes.LAVA, pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5, random.nextFloat() / 2.0F, 5.0E-5, random.nextFloat() / 2.0F);
|
| 182 | }
|
| 183 | }
|
| 184 | }
|
| 185 | }
|
| 186 |
|
| 187 | public static void douse(final @Nullable Entity source, final LevelAccessor level, final BlockPos pos, final BlockState state) {
|
| 188 | if (level.isClientSide()) {
|
| 189 | for (int j = 0; j < 20; j++) {
|
| 190 | makeParticles((Level)level, pos, state.getValue(SIGNAL_FIRE), true);
|
| 191 | }
|
| 192 | }
|
| 193 |
|
| 194 | level.gameEvent(source, GameEvent.BLOCK_CHANGE, pos);
|
| 195 | }
|
| 196 |
|
| 197 | @Override
|
| 198 | public boolean placeLiquid(final LevelAccessor level, final BlockPos pos, final BlockState state, final FluidState fluidState) {
|
| 199 | if (!state.getValue(BlockStateProperties.WATERLOGGED) && fluidState.is(Fluids.WATER)) {
|
| 200 | boolean isLit = state.getValue(LIT);
|
| 201 | if (isLit) {
|
| 202 | if (!level.isClientSide()) {
|
| 203 | level.playSound(null, pos, SoundEvents.GENERIC_EXTINGUISH_FIRE, SoundSource.BLOCKS, 1.0F, 1.0F);
|
| 204 | }
|
| 205 |
|
| 206 | douse(null, level, pos, state);
|
| 207 | }
|
| 208 |
|
| 209 | level.setBlockAndUpdate(pos, state.setValue(WATERLOGGED, true).setValue(LIT, false));
|
| 210 | level.scheduleTick(pos, fluidState.getType(), fluidState.getType().getTickDelay(level));
|
| 211 | return true;
|
| 212 | } else {
|
| 213 | return false;
|
| 214 | }
|
| 215 | }
|
| 216 |
|
| 217 | @Override
|
| 218 | protected void onProjectileHit(final Level level, final BlockState state, final BlockHitResult blockHit, final Projectile projectile) {
|
| 219 | BlockPos pos = blockHit.getBlockPos();
|
| 220 | if (level instanceof ServerLevel serverLevel
|
| 221 | && projectile.isOnFire()
|
| 222 | && projectile.mayInteract(serverLevel, pos)
|
| 223 | && !state.getValue(LIT)
|
| 224 | && !state.getValue(WATERLOGGED)) {
|
| 225 | level.setBlock(pos, state.setValue(BlockStateProperties.LIT, true), 11);
|
| 226 | }
|
| 227 | }
|
| 228 |
|
| 229 | public static void makeParticles(final Level level, final BlockPos pos, final boolean isSignalFire, final boolean smoking) {
|
| 230 | RandomSource random = level.getRandom();
|
| 231 | SimpleParticleType smokeParticle = isSignalFire ? ParticleTypes.CAMPFIRE_SIGNAL_SMOKE : ParticleTypes.CAMPFIRE_COSY_SMOKE;
|
| 232 | level.addAlwaysVisibleParticle(
|
| 233 | smokeParticle,
|
| 234 | true,
|
| 235 | pos.getX() + 0.5 + random.nextDouble() / 3.0 * (random.nextBoolean() ? 1 : -1),
|
| 236 | pos.getY() + random.nextDouble() + random.nextDouble(),
|
| 237 | pos.getZ() + 0.5 + random.nextDouble() / 3.0 * (random.nextBoolean() ? 1 : -1),
|
| 238 | 0.0,
|
| 239 | 0.07,
|
| 240 | 0.0
|
| 241 | );
|
| 242 | if (smoking) {
|
| 243 | level.addParticle(
|
| 244 | ParticleTypes.SMOKE,
|
| 245 | pos.getX() + 0.5 + random.nextDouble() / 4.0 * (random.nextBoolean() ? 1 : -1),
|
| 246 | pos.getY() + 0.4,
|
| 247 | pos.getZ() + 0.5 + random.nextDouble() / 4.0 * (random.nextBoolean() ? 1 : -1),
|
| 248 | 0.0,
|
| 249 | 0.005,
|
| 250 | 0.0
|
| 251 | );
|
| 252 | }
|
| 253 | }
|
| 254 |
|
| 255 | public static boolean isSmokeyPos(final Level level, final BlockPos pos) {
|
| 256 | for (int i = 1; i <= 5; i++) {
|
| 257 | BlockPos posToCheck = pos.below(i);
|
| 258 | BlockState blockState = level.getBlockState(posToCheck);
|
| 259 | if (isLitCampfire(blockState)) {
|
| 260 | return true;
|
| 261 | }
|
| 262 |
|
| 263 | boolean smokeBlocked = Shapes.joinIsNotEmpty(SHAPE_VIRTUAL_POST, blockState.getCollisionShape(level, pos, CollisionContext.empty()), BooleanOp.AND);
|
| 264 | if (smokeBlocked) {
|
| 265 | BlockState belowState = level.getBlockState(posToCheck.below());
|
| 266 | return isLitCampfire(belowState);
|
| 267 | }
|
| 268 | }
|
| 269 |
|
| 270 | return false;
|
| 271 | }
|
| 272 |
|
| 273 | public static boolean isLitCampfire(final BlockState blockState) {
|
| 274 | return blockState.hasProperty(LIT) && blockState.is(BlockTags.CAMPFIRES) && blockState.getValue(LIT);
|
| 275 | }
|
| 276 |
|
| 277 | @Override
|
| 278 | protected FluidState getFluidState(final BlockState state) {
|
| 279 | return state.getValue(WATERLOGGED) ? Fluids.WATER.getSource(false) : super.getFluidState(state);
|
| 280 | }
|
| 281 |
|
| 282 | @Override
|
| 283 | protected BlockState rotate(final BlockState state, final Rotation rotation) {
|
| 284 | return state.setValue(FACING, rotation.rotate(state.getValue(FACING)));
|
| 285 | }
|
| 286 |
|
| 287 | @Override
|
| 288 | protected BlockState mirror(final BlockState state, final Mirror mirror) {
|
| 289 | return state.rotate(mirror.getRotation(state.getValue(FACING)));
|
| 290 | }
|
| 291 |
|
| 292 | @Override
|
| 293 | protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder) {
|
| 294 | builder.add(LIT, SIGNAL_FIRE, WATERLOGGED, FACING);
|
| 295 | }
|
| 296 |
|
| 297 | @Override
|
| 298 | public BlockEntity newBlockEntity(final BlockPos worldPosition, final BlockState blockState) {
|
| 299 | return new CampfireBlockEntity(worldPosition, blockState);
|
| 300 | }
|
| 301 |
|
| 302 | @Override
|
| 303 | public <T extends BlockEntity> @Nullable BlockEntityTicker<T> getTicker(final Level level, final BlockState blockState, final BlockEntityType<T> type) {
|
| 304 | if (level instanceof ServerLevel serverLevel) {
|
| 305 | if (blockState.getValue(LIT)) {
|
| 306 | RecipeManager.CachedCheck<SingleRecipeInput, CampfireCookingRecipe> quickCheck = RecipeManager.createCheck(RecipeType.CAMPFIRE_COOKING);
|
| 307 | return createTickerHelper(
|
| 308 | type, BlockEntityTypes.CAMPFIRE, (innerLevel, pos, state, entity) -> CampfireBlockEntity.cookTick(serverLevel, pos, state, entity, quickCheck)
|
| 309 | );
|
| 310 | } else {
|
| 311 | return createTickerHelper(type, BlockEntityTypes.CAMPFIRE, CampfireBlockEntity::cooldownTick);
|
| 312 | }
|
| 313 | } else {
|
| 314 | return blockState.getValue(LIT) ? createTickerHelper(type, BlockEntityTypes.CAMPFIRE, CampfireBlockEntity::particleTick) : null;
|
| 315 | }
|
| 316 | }
|
| 317 |
|
| 318 | @Override
|
| 319 | protected boolean isPathfindable(final BlockState state, final PathComputationType type) {
|
| 320 | return false;
|
| 321 | }
|
| 322 |
|
| 323 | public static boolean canLight(final BlockState state) {
|
| 324 | return state.is(BlockTags.CAMPFIRES, s -> s.hasProperty(WATERLOGGED) && s.hasProperty(LIT)) && !state.getValue(WATERLOGGED) && !state.getValue(LIT);
|
| 325 | }
|
| 326 | }
|
| 327 |
|