| 1 | package net.minecraft.world.level.material;
|
| 2 |
|
| 3 | import java.util.Optional;
|
| 4 | import net.minecraft.core.BlockPos;
|
| 5 | import net.minecraft.core.Direction;
|
| 6 | import net.minecraft.core.particles.ParticleOptions;
|
| 7 | import net.minecraft.core.particles.ParticleTypes;
|
| 8 | import net.minecraft.server.level.ServerLevel;
|
| 9 | import net.minecraft.sounds.SoundEvent;
|
| 10 | import net.minecraft.sounds.SoundEvents;
|
| 11 | import net.minecraft.sounds.SoundSource;
|
| 12 | import net.minecraft.tags.BlockTags;
|
| 13 | import net.minecraft.tags.FluidTags;
|
| 14 | import net.minecraft.util.RandomSource;
|
| 15 | import net.minecraft.world.attribute.EnvironmentAttributes;
|
| 16 | import net.minecraft.world.entity.Entity;
|
| 17 | import net.minecraft.world.entity.InsideBlockEffectApplier;
|
| 18 | import net.minecraft.world.entity.InsideBlockEffectType;
|
| 19 | import net.minecraft.world.item.Item;
|
| 20 | import net.minecraft.world.item.Items;
|
| 21 | import net.minecraft.world.level.BlockGetter;
|
| 22 | import net.minecraft.world.level.Level;
|
| 23 | import net.minecraft.world.level.LevelAccessor;
|
| 24 | import net.minecraft.world.level.LevelReader;
|
| 25 | import net.minecraft.world.level.block.BaseFireBlock;
|
| 26 | import net.minecraft.world.level.block.Blocks;
|
| 27 | import net.minecraft.world.level.block.LiquidBlock;
|
| 28 | import net.minecraft.world.level.block.state.BlockState;
|
| 29 | import net.minecraft.world.level.block.state.StateDefinition;
|
| 30 | import net.minecraft.world.level.gamerules.GameRules;
|
| 31 | import org.jspecify.annotations.Nullable;
|
| 32 |
|
| 33 | public abstract class LavaFluid extends FlowingFluid {
|
| 34 | public static final int LIGHT_EMISSION = 15;
|
| 35 | public static final float MIN_LEVEL_CUTOFF = 0.44444445F;
|
| 36 |
|
| 37 | @Override
|
| 38 | public Fluid getFlowing() {
|
| 39 | return Fluids.FLOWING_LAVA;
|
| 40 | }
|
| 41 |
|
| 42 | @Override
|
| 43 | public Fluid getSource() {
|
| 44 | return Fluids.LAVA;
|
| 45 | }
|
| 46 |
|
| 47 | @Override
|
| 48 | public Item getBucket() {
|
| 49 | return Items.LAVA_BUCKET;
|
| 50 | }
|
| 51 |
|
| 52 | @Override
|
| 53 | public void animateTick(final Level level, final BlockPos pos, final FluidState fluidState, final RandomSource random) {
|
| 54 | BlockPos above = pos.above();
|
| 55 | if (level.getBlockState(above).isAir() && !level.getBlockState(above).isSolidRender()) {
|
| 56 | if (random.nextInt(100) == 0) {
|
| 57 | double xx = pos.getX() + random.nextDouble();
|
| 58 | double yy = pos.getY() + 1.0;
|
| 59 | double zz = pos.getZ() + random.nextDouble();
|
| 60 | level.addParticle(ParticleTypes.LAVA, xx, yy, zz, 0.0, 0.0, 0.0);
|
| 61 | level.playLocalSound(xx, yy, zz, SoundEvents.LAVA_POP, SoundSource.AMBIENT, 0.2F + random.nextFloat() * 0.2F, 0.9F + random.nextFloat() * 0.15F, false);
|
| 62 | }
|
| 63 |
|
| 64 | if (random.nextInt(200) == 0) {
|
| 65 | level.playLocalSound(
|
| 66 | pos.getX(),
|
| 67 | pos.getY(),
|
| 68 | pos.getZ(),
|
| 69 | SoundEvents.LAVA_AMBIENT,
|
| 70 | SoundSource.AMBIENT,
|
| 71 | 0.2F + random.nextFloat() * 0.2F,
|
| 72 | 0.9F + random.nextFloat() * 0.15F,
|
| 73 | false
|
| 74 | );
|
| 75 | }
|
| 76 | }
|
| 77 | }
|
| 78 |
|
| 79 | @Override
|
| 80 | public void randomTick(final ServerLevel level, final BlockPos pos, final FluidState fluidState, final RandomSource random) {
|
| 81 | if (level.canSpreadFireAround(pos)) {
|
| 82 | int passes = random.nextInt(3);
|
| 83 | if (passes > 0) {
|
| 84 | BlockPos testPos = pos;
|
| 85 |
|
| 86 | for (int pass = 0; pass < passes; pass++) {
|
| 87 | testPos = testPos.offset(random.nextInt(3) - 1, 1, random.nextInt(3) - 1);
|
| 88 | if (!level.isLoaded(testPos)) {
|
| 89 | return;
|
| 90 | }
|
| 91 |
|
| 92 | BlockState blockState = level.getBlockState(testPos);
|
| 93 | if (blockState.isAir()) {
|
| 94 | if (this.hasFlammableNeighbours(level, testPos)) {
|
| 95 | level.setBlockAndUpdate(testPos, BaseFireBlock.getState(level, testPos));
|
| 96 | return;
|
| 97 | }
|
| 98 | } else if (blockState.is(BlockTags.BLOCKS_LAVA_FIRE_SPREAD)) {
|
| 99 | return;
|
| 100 | }
|
| 101 | }
|
| 102 | } else {
|
| 103 | for (int i = 0; i < 3; i++) {
|
| 104 | BlockPos testPos = pos.offset(random.nextInt(3) - 1, 0, random.nextInt(3) - 1);
|
| 105 | if (!level.isLoaded(testPos)) {
|
| 106 | return;
|
| 107 | }
|
| 108 |
|
| 109 | if (level.isEmptyBlock(testPos.above()) && this.isFlammable(level, testPos)) {
|
| 110 | level.setBlockAndUpdate(testPos.above(), BaseFireBlock.getState(level, testPos));
|
| 111 | }
|
| 112 | }
|
| 113 | }
|
| 114 | }
|
| 115 | }
|
| 116 |
|
| 117 | @Override
|
| 118 | protected void entityInside(final Level level, final BlockPos pos, final Entity entity, final InsideBlockEffectApplier effectApplier) {
|
| 119 | effectApplier.apply(InsideBlockEffectType.CLEAR_FREEZE);
|
| 120 | effectApplier.apply(InsideBlockEffectType.LAVA_IGNITE);
|
| 121 | effectApplier.runAfter(InsideBlockEffectType.LAVA_IGNITE, Entity::lavaHurt);
|
| 122 | }
|
| 123 |
|
| 124 | private boolean hasFlammableNeighbours(final LevelReader level, final BlockPos pos) {
|
| 125 | for (Direction direction : Direction.values()) {
|
| 126 | if (this.isFlammable(level, pos.relative(direction))) {
|
| 127 | return true;
|
| 128 | }
|
| 129 | }
|
| 130 |
|
| 131 | return false;
|
| 132 | }
|
| 133 |
|
| 134 | private boolean isFlammable(final LevelReader level, final BlockPos pos) {
|
| 135 | return level.isInsideBuildHeight(pos.getY()) && !level.hasChunkAt(pos) ? false : level.getBlockState(pos).ignitedByLava();
|
| 136 | }
|
| 137 |
|
| 138 | @Override
|
| 139 | public @Nullable ParticleOptions getDripParticle() {
|
| 140 | return ParticleTypes.DRIPPING_LAVA;
|
| 141 | }
|
| 142 |
|
| 143 | @Override
|
| 144 | protected void beforeDestroyingBlock(final LevelAccessor level, final BlockPos pos, final BlockState state) {
|
| 145 | this.fizz(level, pos);
|
| 146 | }
|
| 147 |
|
| 148 | @Override
|
| 149 | public int getSlopeFindDistance(final LevelReader level) {
|
| 150 | return isFastLava(level) ? 4 : 2;
|
| 151 | }
|
| 152 |
|
| 153 | @Override
|
| 154 | public BlockState createLegacyBlock(final FluidState fluidState) {
|
| 155 | return Blocks.LAVA.defaultBlockState().setValue(LiquidBlock.LEVEL, getLegacyLevel(fluidState));
|
| 156 | }
|
| 157 |
|
| 158 | @Override
|
| 159 | public boolean isSame(final Fluid other) {
|
| 160 | return other == Fluids.LAVA || other == Fluids.FLOWING_LAVA;
|
| 161 | }
|
| 162 |
|
| 163 | @Override
|
| 164 | public int getDropOff(final LevelReader level) {
|
| 165 | return isFastLava(level) ? 1 : 2;
|
| 166 | }
|
| 167 |
|
| 168 | @Override
|
| 169 | public boolean canBeReplacedWith(final FluidState state, final BlockGetter level, final BlockPos pos, final Fluid other, final Direction direction) {
|
| 170 | return state.getHeight(level, pos) >= 0.44444445F && other.is(FluidTags.WATER);
|
| 171 | }
|
| 172 |
|
| 173 | @Override
|
| 174 | public int getTickDelay(final LevelReader level) {
|
| 175 | return isFastLava(level) ? 10 : 30;
|
| 176 | }
|
| 177 |
|
| 178 | @Override
|
| 179 | public int getSpreadDelay(final Level level, final BlockPos pos, final FluidState oldFluidState, final FluidState newFluidState) {
|
| 180 | int result = this.getTickDelay(level);
|
| 181 | if (!oldFluidState.isEmpty()
|
| 182 | && !newFluidState.isEmpty()
|
| 183 | && !oldFluidState.getValue(FALLING)
|
| 184 | && !newFluidState.getValue(FALLING)
|
| 185 | && newFluidState.getHeight(level, pos) > oldFluidState.getHeight(level, pos)
|
| 186 | && level.getRandom().nextInt(4) != 0) {
|
| 187 | result *= 4;
|
| 188 | }
|
| 189 |
|
| 190 | return result;
|
| 191 | }
|
| 192 |
|
| 193 | private void fizz(final LevelAccessor level, final BlockPos pos) {
|
| 194 | level.levelEvent(1501, pos, 0);
|
| 195 | }
|
| 196 |
|
| 197 | @Override
|
| 198 | protected boolean canConvertToSource(final ServerLevel level) {
|
| 199 | return level.getGameRules().get(GameRules.LAVA_SOURCE_CONVERSION);
|
| 200 | }
|
| 201 |
|
| 202 | @Override
|
| 203 | protected void spreadTo(final LevelAccessor level, final BlockPos pos, final BlockState state, final Direction direction, final FluidState target) {
|
| 204 | if (direction == Direction.DOWN) {
|
| 205 | FluidState fluidState = level.getFluidState(pos);
|
| 206 | if (this.is(FluidTags.LAVA) && fluidState.is(FluidTags.WATER)) {
|
| 207 | if (state.getBlock() instanceof LiquidBlock) {
|
| 208 | level.setBlockAndUpdate(pos, Blocks.STONE.defaultBlockState());
|
| 209 | }
|
| 210 |
|
| 211 | this.fizz(level, pos);
|
| 212 | return;
|
| 213 | }
|
| 214 | }
|
| 215 |
|
| 216 | super.spreadTo(level, pos, state, direction, target);
|
| 217 | }
|
| 218 |
|
| 219 | @Override
|
| 220 | protected boolean isRandomlyTicking() {
|
| 221 | return true;
|
| 222 | }
|
| 223 |
|
| 224 | @Override
|
| 225 | protected float getExplosionResistance() {
|
| 226 | return 100.0F;
|
| 227 | }
|
| 228 |
|
| 229 | @Override
|
| 230 | public Optional<SoundEvent> getPickupSound() {
|
| 231 | return Optional.of(SoundEvents.BUCKET_FILL_LAVA);
|
| 232 | }
|
| 233 |
|
| 234 | private static boolean isFastLava(final LevelReader level) {
|
| 235 | return level.environmentAttributes().getDimensionValue(EnvironmentAttributes.FAST_LAVA);
|
| 236 | }
|
| 237 |
|
| 238 | public static class Flowing extends LavaFluid {
|
| 239 | @Override
|
| 240 | protected void createFluidStateDefinition(final StateDefinition.Builder<Fluid, FluidState> builder) {
|
| 241 | super.createFluidStateDefinition(builder);
|
| 242 | builder.add(LEVEL);
|
| 243 | }
|
| 244 |
|
| 245 | @Override
|
| 246 | public int getAmount(final FluidState fluidState) {
|
| 247 | return fluidState.getValue(LEVEL);
|
| 248 | }
|
| 249 |
|
| 250 | @Override
|
| 251 | public boolean isSource(final FluidState fluidState) {
|
| 252 | return false;
|
| 253 | }
|
| 254 | }
|
| 255 |
|
| 256 | public static class Source extends LavaFluid {
|
| 257 | @Override
|
| 258 | public int getAmount(final FluidState fluidState) {
|
| 259 | return 8;
|
| 260 | }
|
| 261 |
|
| 262 | @Override
|
| 263 | public boolean isSource(final FluidState fluidState) {
|
| 264 | return true;
|
| 265 | }
|
| 266 | }
|
| 267 | }
|
| 268 |
|