| 1 | package net.minecraft.world.entity;
|
| 2 |
|
| 3 | import com.google.common.collect.Sets;
|
| 4 | import java.util.List;
|
| 5 | import java.util.Optional;
|
| 6 | import java.util.Set;
|
| 7 | import java.util.stream.Stream;
|
| 8 | import net.minecraft.advancements.triggers.CriteriaTriggers;
|
| 9 | import net.minecraft.core.BlockPos;
|
| 10 | import net.minecraft.network.syncher.SynchedEntityData;
|
| 11 | import net.minecraft.server.level.ServerLevel;
|
| 12 | import net.minecraft.server.level.ServerPlayer;
|
| 13 | import net.minecraft.sounds.SoundEvents;
|
| 14 | import net.minecraft.sounds.SoundSource;
|
| 15 | import net.minecraft.util.RandomSource;
|
| 16 | import net.minecraft.world.Difficulty;
|
| 17 | import net.minecraft.world.damagesource.DamageSource;
|
| 18 | import net.minecraft.world.item.HoneycombItem;
|
| 19 | import net.minecraft.world.level.Level;
|
| 20 | import net.minecraft.world.level.block.BaseFireBlock;
|
| 21 | import net.minecraft.world.level.block.LightningRodBlock;
|
| 22 | import net.minecraft.world.level.block.WeatheringCopper;
|
| 23 | import net.minecraft.world.level.block.state.BlockState;
|
| 24 | import net.minecraft.world.level.gameevent.GameEvent;
|
| 25 | import net.minecraft.world.level.storage.ValueInput;
|
| 26 | import net.minecraft.world.level.storage.ValueOutput;
|
| 27 | import net.minecraft.world.phys.AABB;
|
| 28 | import net.minecraft.world.phys.Vec3;
|
| 29 | import org.jspecify.annotations.Nullable;
|
| 30 |
|
| 31 | public class LightningBolt extends Entity {
|
| 32 | private static final int START_LIFE = 2;
|
| 33 | private static final double DAMAGE_RADIUS = 3.0;
|
| 34 | private static final double DETECTION_RADIUS = 15.0;
|
| 35 | private int life;
|
| 36 | public long seed;
|
| 37 | private int flashes;
|
| 38 | private boolean visualOnly;
|
| 39 | private @Nullable ServerPlayer cause;
|
| 40 | private final Set<Entity> hitEntities = Sets.newHashSet();
|
| 41 | private int blocksSetOnFire;
|
| 42 |
|
| 43 | public LightningBolt(final EntityType<? extends LightningBolt> type, final Level level) {
|
| 44 | super(type, level);
|
| 45 | this.life = 2;
|
| 46 | this.seed = this.random.nextLong();
|
| 47 | this.flashes = this.random.nextInt(3) + 1;
|
| 48 | }
|
| 49 |
|
| 50 | public void setVisualOnly(final boolean visualOnly) {
|
| 51 | this.visualOnly = visualOnly;
|
| 52 | }
|
| 53 |
|
| 54 | @Override
|
| 55 | public SoundSource getSoundSource() {
|
| 56 | return SoundSource.WEATHER;
|
| 57 | }
|
| 58 |
|
| 59 | public @Nullable ServerPlayer getCause() {
|
| 60 | return this.cause;
|
| 61 | }
|
| 62 |
|
| 63 | public void setCause(final @Nullable ServerPlayer cause) {
|
| 64 | this.cause = cause;
|
| 65 | }
|
| 66 |
|
| 67 | private void powerLightningRod() {
|
| 68 | BlockPos strikePosition = this.getStrikePosition();
|
| 69 | BlockState stateBelow = this.level().getBlockState(strikePosition);
|
| 70 | if (stateBelow.getBlock() instanceof LightningRodBlock lightningRodBlock) {
|
| 71 | lightningRodBlock.onLightningStrike(stateBelow, this.level(), strikePosition);
|
| 72 | }
|
| 73 | }
|
| 74 |
|
| 75 | @Override
|
| 76 | public void tick() {
|
| 77 | super.tick();
|
| 78 | if (this.life == 2) {
|
| 79 | if (this.level().isClientSide()) {
|
| 80 | if (!this.isSilent()) {
|
| 81 | this.level()
|
| 82 | .playLocalSound(
|
| 83 | this.getX(), this.getY(), this.getZ(), SoundEvents.LIGHTNING_BOLT_THUNDER, SoundSource.WEATHER, 10000.0F, 0.8F + this.random.nextFloat() * 0.2F, false
|
| 84 | );
|
| 85 | this.level()
|
| 86 | .playLocalSound(
|
| 87 | this.getX(), this.getY(), this.getZ(), SoundEvents.LIGHTNING_BOLT_IMPACT, SoundSource.WEATHER, 2.0F, 0.5F + this.random.nextFloat() * 0.2F, false
|
| 88 | );
|
| 89 | }
|
| 90 | } else {
|
| 91 | Difficulty difficulty = this.level().getDifficulty();
|
| 92 | if (difficulty == Difficulty.NORMAL || difficulty == Difficulty.HARD) {
|
| 93 | this.spawnFire(4);
|
| 94 | }
|
| 95 |
|
| 96 | this.powerLightningRod();
|
| 97 | clearCopperOnLightningStrike(this.level(), this.getStrikePosition());
|
| 98 | this.gameEvent(GameEvent.LIGHTNING_STRIKE);
|
| 99 | }
|
| 100 | }
|
| 101 |
|
| 102 | this.life--;
|
| 103 | if (this.life < 0) {
|
| 104 | if (this.flashes == 0) {
|
| 105 | if (this.level() instanceof ServerLevel) {
|
| 106 | List<Entity> viewers = this.level()
|
| 107 | .getEntities(
|
| 108 | this,
|
| 109 | new AABB(this.getX() - 15.0, this.getY() - 15.0, this.getZ() - 15.0, this.getX() + 15.0, this.getY() + 6.0 + 15.0, this.getZ() + 15.0),
|
| 110 | entityx -> entityx.isAlive() && !this.hitEntities.contains(entityx)
|
| 111 | );
|
| 112 |
|
| 113 | for (ServerPlayer player : ((ServerLevel)this.level()).getPlayers(playerx -> playerx.distanceTo(this) < 256.0F)) {
|
| 114 | CriteriaTriggers.LIGHTNING_STRIKE.trigger(player, this, viewers);
|
| 115 | }
|
| 116 | }
|
| 117 |
|
| 118 | this.discard();
|
| 119 | } else if (this.life < -this.random.nextInt(10)) {
|
| 120 | this.flashes--;
|
| 121 | this.life = 1;
|
| 122 | this.seed = this.random.nextLong();
|
| 123 | this.spawnFire(0);
|
| 124 | }
|
| 125 | }
|
| 126 |
|
| 127 | if (this.life >= 0) {
|
| 128 | if (!(this.level() instanceof ServerLevel)) {
|
| 129 | this.level().setSkyFlashTime(2);
|
| 130 | } else if (!this.visualOnly) {
|
| 131 | List<Entity> entities = this.level()
|
| 132 | .getEntities(
|
| 133 | this, new AABB(this.getX() - 3.0, this.getY() - 3.0, this.getZ() - 3.0, this.getX() + 3.0, this.getY() + 6.0 + 3.0, this.getZ() + 3.0), Entity::isAlive
|
| 134 | );
|
| 135 |
|
| 136 | for (Entity entity : entities) {
|
| 137 | entity.thunderHit((ServerLevel)this.level(), this);
|
| 138 | }
|
| 139 |
|
| 140 | this.hitEntities.addAll(entities);
|
| 141 | if (this.cause != null) {
|
| 142 | CriteriaTriggers.CHANNELED_LIGHTNING.trigger(this.cause, entities);
|
| 143 | }
|
| 144 | }
|
| 145 | }
|
| 146 | }
|
| 147 |
|
| 148 | private BlockPos getStrikePosition() {
|
| 149 | Vec3 position = this.position();
|
| 150 | return BlockPos.containing(position.x, position.y - 1.0E-6, position.z);
|
| 151 | }
|
| 152 |
|
| 153 | private void spawnFire(final int additionalSources) {
|
| 154 | if (!this.visualOnly && this.level() instanceof ServerLevel level) {
|
| 155 | BlockPos var7 = this.blockPosition();
|
| 156 | if (level.canSpreadFireAround(var7)) {
|
| 157 | BlockState fire = BaseFireBlock.getState(level, var7);
|
| 158 | if (level.getBlockState(var7).isAir() && fire.canSurvive(level, var7)) {
|
| 159 | level.setBlockAndUpdate(var7, fire);
|
| 160 | this.blocksSetOnFire++;
|
| 161 | }
|
| 162 |
|
| 163 | for (int i = 0; i < additionalSources; i++) {
|
| 164 | BlockPos nearbyPos = var7.offset(this.random.nextInt(3) - 1, this.random.nextInt(3) - 1, this.random.nextInt(3) - 1);
|
| 165 | fire = BaseFireBlock.getState(level, nearbyPos);
|
| 166 | if (level.getBlockState(nearbyPos).isAir() && fire.canSurvive(level, nearbyPos)) {
|
| 167 | level.setBlockAndUpdate(nearbyPos, fire);
|
| 168 | this.blocksSetOnFire++;
|
| 169 | }
|
| 170 | }
|
| 171 | }
|
| 172 | }
|
| 173 | }
|
| 174 |
|
| 175 | private static void clearCopperOnLightningStrike(final Level level, final BlockPos struckPos) {
|
| 176 | BlockState struckState = level.getBlockState(struckPos);
|
| 177 | boolean isWaxed = HoneycombItem.WAX_OFF_BY_BLOCK.get().get(struckState.getBlock()) != null;
|
| 178 | boolean isWeatheringCopper = struckState.getBlock() instanceof WeatheringCopper;
|
| 179 | if (isWeatheringCopper || isWaxed) {
|
| 180 | if (isWeatheringCopper) {
|
| 181 | level.setBlockAndUpdate(struckPos, WeatheringCopper.getFirst(level.getBlockState(struckPos)));
|
| 182 | }
|
| 183 |
|
| 184 | BlockPos.MutableBlockPos workPos = struckPos.mutable();
|
| 185 | RandomSource random = level.getRandom();
|
| 186 | int strikesCount = random.nextInt(3) + 3;
|
| 187 |
|
| 188 | for (int strike = 0; strike < strikesCount; strike++) {
|
| 189 | int stepCount = random.nextInt(8) + 1;
|
| 190 | randomWalkCleaningCopper(level, struckPos, workPos, stepCount);
|
| 191 | }
|
| 192 | }
|
| 193 | }
|
| 194 |
|
| 195 | private static void randomWalkCleaningCopper(final Level level, final BlockPos originalStrikePos, final BlockPos.MutableBlockPos workPos, final int stepCount) {
|
| 196 | workPos.set(originalStrikePos);
|
| 197 |
|
| 198 | for (int step = 0; step < stepCount; step++) {
|
| 199 | Optional<BlockPos> stepPos = randomStepCleaningCopper(level, workPos);
|
| 200 | if (stepPos.isEmpty()) {
|
| 201 | break;
|
| 202 | }
|
| 203 |
|
| 204 | workPos.set(stepPos.get());
|
| 205 | }
|
| 206 | }
|
| 207 |
|
| 208 | private static Optional<BlockPos> randomStepCleaningCopper(final Level level, final BlockPos pos) {
|
| 209 | for (BlockPos candidate : BlockPos.randomInCube(level.getRandom(), 10, pos, 1)) {
|
| 210 | BlockState state = level.getBlockState(candidate);
|
| 211 | if (state.getBlock() instanceof WeatheringCopper) {
|
| 212 | WeatheringCopper.getPrevious(state).ifPresent(s -> level.setBlockAndUpdate(candidate, s));
|
| 213 | level.levelEvent(3002, candidate, -1);
|
| 214 | return Optional.of(candidate);
|
| 215 | }
|
| 216 | }
|
| 217 |
|
| 218 | return Optional.empty();
|
| 219 | }
|
| 220 |
|
| 221 | @Override
|
| 222 | public boolean shouldRenderAtSqrDistance(final double distance) {
|
| 223 | double size = 64.0 * getViewScale();
|
| 224 | return distance < size * size;
|
| 225 | }
|
| 226 |
|
| 227 | @Override
|
| 228 | protected void defineSynchedData(final SynchedEntityData.Builder entityData) {
|
| 229 | }
|
| 230 |
|
| 231 | @Override
|
| 232 | protected void readAdditionalSaveData(final ValueInput input) {
|
| 233 | }
|
| 234 |
|
| 235 | @Override
|
| 236 | protected void addAdditionalSaveData(final ValueOutput output) {
|
| 237 | }
|
| 238 |
|
| 239 | public int getBlocksSetOnFire() {
|
| 240 | return this.blocksSetOnFire;
|
| 241 | }
|
| 242 |
|
| 243 | public Stream<Entity> getHitEntities() {
|
| 244 | return this.hitEntities.stream().filter(Entity::isAlive);
|
| 245 | }
|
| 246 |
|
| 247 | @Override
|
| 248 | public final boolean hurtServer(final ServerLevel level, final DamageSource source, final float damage) {
|
| 249 | return false;
|
| 250 | }
|
| 251 | }
|
| 252 |
|