| 1 | package net.minecraft.world.level.block;
|
| 2 |
|
| 3 | import java.util.Optional;
|
| 4 | import net.minecraft.core.BlockPos;
|
| 5 | import net.minecraft.core.Direction;
|
| 6 | import net.minecraft.core.particles.ParticleTypes;
|
| 7 | import net.minecraft.server.level.ServerPlayer;
|
| 8 | import net.minecraft.sounds.SoundEvents;
|
| 9 | import net.minecraft.sounds.SoundSource;
|
| 10 | import net.minecraft.util.RandomSource;
|
| 11 | import net.minecraft.world.entity.Entity;
|
| 12 | import net.minecraft.world.entity.InsideBlockEffectApplier;
|
| 13 | import net.minecraft.world.entity.InsideBlockEffectType;
|
| 14 | import net.minecraft.world.entity.player.Player;
|
| 15 | import net.minecraft.world.item.context.BlockPlaceContext;
|
| 16 | import net.minecraft.world.level.BlockGetter;
|
| 17 | import net.minecraft.world.level.Level;
|
| 18 | import net.minecraft.world.level.block.state.BlockBehaviour;
|
| 19 | import net.minecraft.world.level.block.state.BlockState;
|
| 20 | import net.minecraft.world.level.portal.PortalShape;
|
| 21 | import net.minecraft.world.phys.shapes.CollisionContext;
|
| 22 | import net.minecraft.world.phys.shapes.VoxelShape;
|
| 23 | import org.jspecify.annotations.Nullable;
|
| 24 |
|
| 25 | public abstract class BaseFireBlock extends Block {
|
| 26 | private static final int SECONDS_ON_FIRE = 8;
|
| 27 | private static final int MIN_FIRE_TICKS_TO_ADD = 1;
|
| 28 | private static final int MAX_FIRE_TICKS_TO_ADD = 3;
|
| 29 | private final float fireDamage;
|
| 30 | protected static final VoxelShape SHAPE = Block.column(16.0, 0.0, 1.0);
|
| 31 |
|
| 32 | public BaseFireBlock(final BlockBehaviour.Properties properties, final float fireDamage) {
|
| 33 | super(properties);
|
| 34 | this.fireDamage = fireDamage;
|
| 35 | }
|
| 36 |
|
| 37 | @Override
|
| 38 | public BlockState getStateForPlacement(final BlockPlaceContext context) {
|
| 39 | return getState(context.getLevel(), context.getClickedPos());
|
| 40 | }
|
| 41 |
|
| 42 | public static BlockState getState(final BlockGetter level, final BlockPos pos) {
|
| 43 | BlockPos below = pos.below();
|
| 44 | BlockState belowState = level.getBlockState(below);
|
| 45 | return SoulFireBlock.canSurviveOnBlock(belowState) ? Blocks.SOUL_FIRE.defaultBlockState() : ((FireBlock)Blocks.FIRE).getStateForPlacement(level, pos);
|
| 46 | }
|
| 47 |
|
| 48 | @Override
|
| 49 | protected VoxelShape getShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context) {
|
| 50 | return SHAPE;
|
| 51 | }
|
| 52 |
|
| 53 | @Override
|
| 54 | public void animateTick(final BlockState state, final Level level, final BlockPos pos, final RandomSource random) {
|
| 55 | if (random.nextInt(24) == 0) {
|
| 56 | level.playLocalSound(
|
| 57 | pos.getX() + 0.5,
|
| 58 | pos.getY() + 0.5,
|
| 59 | pos.getZ() + 0.5,
|
| 60 | SoundEvents.FIRE_AMBIENT,
|
| 61 | SoundSource.BLOCKS,
|
| 62 | 1.0F + random.nextFloat(),
|
| 63 | random.nextFloat() * 0.7F + 0.3F,
|
| 64 | false
|
| 65 | );
|
| 66 | }
|
| 67 |
|
| 68 | BlockPos below = pos.below();
|
| 69 | BlockState belowState = level.getBlockState(below);
|
| 70 | if (!this.canBurn(belowState) && !belowState.isFaceSturdy(level, below, Direction.UP)) {
|
| 71 | if (this.canBurn(level.getBlockState(pos.west()))) {
|
| 72 | for (int i = 0; i < 2; i++) {
|
| 73 | double xx = pos.getX() + random.nextDouble() * 0.1F;
|
| 74 | double yy = pos.getY() + random.nextDouble();
|
| 75 | double zz = pos.getZ() + random.nextDouble();
|
| 76 | level.addParticle(ParticleTypes.LARGE_SMOKE, xx, yy, zz, 0.0, 0.0, 0.0);
|
| 77 | }
|
| 78 | }
|
| 79 |
|
| 80 | if (this.canBurn(level.getBlockState(pos.east()))) {
|
| 81 | for (int i = 0; i < 2; i++) {
|
| 82 | double xx = pos.getX() + 1 - random.nextDouble() * 0.1F;
|
| 83 | double yy = pos.getY() + random.nextDouble();
|
| 84 | double zz = pos.getZ() + random.nextDouble();
|
| 85 | level.addParticle(ParticleTypes.LARGE_SMOKE, xx, yy, zz, 0.0, 0.0, 0.0);
|
| 86 | }
|
| 87 | }
|
| 88 |
|
| 89 | if (this.canBurn(level.getBlockState(pos.north()))) {
|
| 90 | for (int i = 0; i < 2; i++) {
|
| 91 | double xx = pos.getX() + random.nextDouble();
|
| 92 | double yy = pos.getY() + random.nextDouble();
|
| 93 | double zz = pos.getZ() + random.nextDouble() * 0.1F;
|
| 94 | level.addParticle(ParticleTypes.LARGE_SMOKE, xx, yy, zz, 0.0, 0.0, 0.0);
|
| 95 | }
|
| 96 | }
|
| 97 |
|
| 98 | if (this.canBurn(level.getBlockState(pos.south()))) {
|
| 99 | for (int i = 0; i < 2; i++) {
|
| 100 | double xx = pos.getX() + random.nextDouble();
|
| 101 | double yy = pos.getY() + random.nextDouble();
|
| 102 | double zz = pos.getZ() + 1 - random.nextDouble() * 0.1F;
|
| 103 | level.addParticle(ParticleTypes.LARGE_SMOKE, xx, yy, zz, 0.0, 0.0, 0.0);
|
| 104 | }
|
| 105 | }
|
| 106 |
|
| 107 | if (this.canBurn(level.getBlockState(pos.above()))) {
|
| 108 | for (int i = 0; i < 2; i++) {
|
| 109 | double xx = pos.getX() + random.nextDouble();
|
| 110 | double yy = pos.getY() + 1 - random.nextDouble() * 0.1F;
|
| 111 | double zz = pos.getZ() + random.nextDouble();
|
| 112 | level.addParticle(ParticleTypes.LARGE_SMOKE, xx, yy, zz, 0.0, 0.0, 0.0);
|
| 113 | }
|
| 114 | }
|
| 115 | } else {
|
| 116 | for (int i = 0; i < 3; i++) {
|
| 117 | double xx = pos.getX() + random.nextDouble();
|
| 118 | double yy = pos.getY() + random.nextDouble() * 0.5 + 0.5;
|
| 119 | double zz = pos.getZ() + random.nextDouble();
|
| 120 | level.addParticle(ParticleTypes.LARGE_SMOKE, xx, yy, zz, 0.0, 0.0, 0.0);
|
| 121 | }
|
| 122 | }
|
| 123 | }
|
| 124 |
|
| 125 | protected abstract boolean canBurn(final BlockState state);
|
| 126 |
|
| 127 | @Override
|
| 128 | protected void entityInside(
|
| 129 | final BlockState state, final Level level, final BlockPos pos, final Entity entity, final InsideBlockEffectApplier effectApplier, final boolean isPrecise
|
| 130 | ) {
|
| 131 | effectApplier.apply(InsideBlockEffectType.CLEAR_FREEZE);
|
| 132 | effectApplier.apply(InsideBlockEffectType.FIRE_IGNITE);
|
| 133 | effectApplier.runAfter(InsideBlockEffectType.FIRE_IGNITE, e -> e.hurt(e.level().damageSources().inFire(), this.fireDamage));
|
| 134 | }
|
| 135 |
|
| 136 | public static void fireIgnite(final Entity entity) {
|
| 137 | if (!entity.fireImmune()) {
|
| 138 | if (entity.getRemainingFireTicks() < 0) {
|
| 139 | entity.setRemainingFireTicks(entity.getRemainingFireTicks() + 1);
|
| 140 | } else if (entity instanceof ServerPlayer) {
|
| 141 | int addedFireTicks = entity.level().getRandom().nextInt(1, 3);
|
| 142 | entity.setRemainingFireTicks(entity.getRemainingFireTicks() + addedFireTicks);
|
| 143 | }
|
| 144 |
|
| 145 | if (entity.getRemainingFireTicks() >= 0) {
|
| 146 | entity.igniteForSeconds(8.0F);
|
| 147 | }
|
| 148 | }
|
| 149 | }
|
| 150 |
|
| 151 | @Override
|
| 152 | protected void onPlace(final BlockState state, final Level level, final BlockPos pos, final BlockState oldState, final boolean movedByPiston) {
|
| 153 | if (!oldState.is(state.getBlock())) {
|
| 154 | if (inPortalDimension(level)) {
|
| 155 | Optional<PortalShape> optionalShape = PortalShape.findEmptyPortalShape(level, pos, Direction.Axis.X);
|
| 156 | if (optionalShape.isPresent()) {
|
| 157 | optionalShape.get().createPortalBlocks(level);
|
| 158 | return;
|
| 159 | }
|
| 160 | }
|
| 161 |
|
| 162 | if (!state.canSurvive(level, pos)) {
|
| 163 | level.removeBlock(pos, false);
|
| 164 | }
|
| 165 | }
|
| 166 | }
|
| 167 |
|
| 168 | private static boolean inPortalDimension(final Level level) {
|
| 169 | return level.dimension() == Level.OVERWORLD || level.dimension() == Level.NETHER;
|
| 170 | }
|
| 171 |
|
| 172 | @Override
|
| 173 | public void spawnDestroyByEntityParticles(final Level level, final @Nullable Entity entity, final BlockPos pos, final BlockState state) {
|
| 174 | }
|
| 175 |
|
| 176 | @Override
|
| 177 | public BlockState playerWillDestroy(final Level level, final BlockPos pos, final BlockState state, final Player player) {
|
| 178 | if (!level.isClientSide()) {
|
| 179 | level.levelEvent(null, 1009, pos, 0);
|
| 180 | }
|
| 181 |
|
| 182 | return super.playerWillDestroy(level, pos, state, player);
|
| 183 | }
|
| 184 |
|
| 185 | public static boolean canBePlacedAt(final Level level, final BlockPos pos, final Direction forwardDirection) {
|
| 186 | BlockState state = level.getBlockState(pos);
|
| 187 | return !state.isAir() ? false : getState(level, pos).canSurvive(level, pos) || isPortal(level, pos, forwardDirection);
|
| 188 | }
|
| 189 |
|
| 190 | private static boolean isPortal(final Level level, final BlockPos pos, final Direction forwardDirection) {
|
| 191 | if (!inPortalDimension(level)) {
|
| 192 | return false;
|
| 193 | }
|
| 194 |
|
| 195 | BlockPos.MutableBlockPos testPos = pos.mutable();
|
| 196 |
|
| 197 | for (Direction face : Direction.values()) {
|
| 198 | BlockState state = level.getBlockState(testPos.set(pos).move(face));
|
| 199 | if (PortalShape.FRAME.test(state)) {
|
| 200 | Direction.Axis preferredAxis = forwardDirection.getAxis().isHorizontal()
|
| 201 | ? forwardDirection.getCounterClockWise().getAxis()
|
| 202 | : Direction.Plane.HORIZONTAL.getRandomAxis(level.getRandom());
|
| 203 | return PortalShape.findEmptyPortalShape(level, pos, preferredAxis).isPresent();
|
| 204 | }
|
| 205 | }
|
| 206 |
|
| 207 | return false;
|
| 208 | }
|
| 209 | }
|
| 210 |
|