Spaces:
Build error
Build error
File size: 10,626 Bytes
d46f4a3 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 |
package net.minecraft.world.entity;
import com.google.common.collect.Sets;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Stream;
import javax.annotation.Nullable;
import net.minecraft.advancements.CriteriaTriggers;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.syncher.SynchedEntityData;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.Difficulty;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.level.GameRules;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.BaseFireBlock;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.LightningRodBlock;
import net.minecraft.world.level.block.WeatheringCopper;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.gameevent.GameEvent;
import net.minecraft.world.phys.AABB;
import net.minecraft.world.phys.Vec3;
public class LightningBolt extends Entity {
private static final int START_LIFE = 2;
private static final double DAMAGE_RADIUS = 3.0;
private static final double DETECTION_RADIUS = 15.0;
private int life;
public long seed;
private int flashes;
private boolean visualOnly;
@Nullable
private ServerPlayer cause;
private final Set<Entity> hitEntities = Sets.newHashSet();
private int blocksSetOnFire;
public LightningBolt(EntityType<? extends LightningBolt> p_20865_, Level p_20866_) {
super(p_20865_, p_20866_);
this.life = 2;
this.seed = this.random.nextLong();
this.flashes = this.random.nextInt(3) + 1;
}
public void setVisualOnly(boolean p_20875_) {
this.visualOnly = p_20875_;
}
@Override
public SoundSource getSoundSource() {
return SoundSource.WEATHER;
}
@Nullable
public ServerPlayer getCause() {
return this.cause;
}
public void setCause(@Nullable ServerPlayer p_20880_) {
this.cause = p_20880_;
}
private void powerLightningRod() {
BlockPos blockpos = this.getStrikePosition();
BlockState blockstate = this.level().getBlockState(blockpos);
if (blockstate.is(Blocks.LIGHTNING_ROD)) {
((LightningRodBlock)blockstate.getBlock()).onLightningStrike(blockstate, this.level(), blockpos);
}
}
@Override
public void tick() {
super.tick();
if (this.life == 2) {
if (this.level().isClientSide()) {
this.level()
.playLocalSound(
this.getX(),
this.getY(),
this.getZ(),
SoundEvents.LIGHTNING_BOLT_THUNDER,
SoundSource.WEATHER,
10000.0F,
0.8F + this.random.nextFloat() * 0.2F,
false
);
this.level()
.playLocalSound(
this.getX(),
this.getY(),
this.getZ(),
SoundEvents.LIGHTNING_BOLT_IMPACT,
SoundSource.WEATHER,
2.0F,
0.5F + this.random.nextFloat() * 0.2F,
false
);
} else {
Difficulty difficulty = this.level().getDifficulty();
if (difficulty == Difficulty.NORMAL || difficulty == Difficulty.HARD) {
this.spawnFire(4);
}
this.powerLightningRod();
clearCopperOnLightningStrike(this.level(), this.getStrikePosition());
this.gameEvent(GameEvent.LIGHTNING_STRIKE);
}
}
this.life--;
if (this.life < 0) {
if (this.flashes == 0) {
if (this.level() instanceof ServerLevel) {
List<Entity> list = this.level()
.getEntities(
this,
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
),
p_147140_ -> p_147140_.isAlive() && !this.hitEntities.contains(p_147140_)
);
for (ServerPlayer serverplayer : ((ServerLevel)this.level()).getPlayers(p_326774_ -> p_326774_.distanceTo(this) < 256.0F)) {
CriteriaTriggers.LIGHTNING_STRIKE.trigger(serverplayer, this, list);
}
}
this.discard();
} else if (this.life < -this.random.nextInt(10)) {
this.flashes--;
this.life = 1;
this.seed = this.random.nextLong();
this.spawnFire(0);
}
}
if (this.life >= 0) {
if (!(this.level() instanceof ServerLevel)) {
this.level().setSkyFlashTime(2);
} else if (!this.visualOnly) {
List<Entity> list1 = this.level()
.getEntities(
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
);
for (Entity entity : list1) {
entity.thunderHit((ServerLevel)this.level(), this);
}
this.hitEntities.addAll(list1);
if (this.cause != null) {
CriteriaTriggers.CHANNELED_LIGHTNING.trigger(this.cause, list1);
}
}
}
}
private BlockPos getStrikePosition() {
Vec3 vec3 = this.position();
return BlockPos.containing(vec3.x, vec3.y - 1.0E-6, vec3.z);
}
private void spawnFire(int p_20871_) {
if (!this.visualOnly && this.level() instanceof ServerLevel serverlevel && serverlevel.getGameRules().getBoolean(GameRules.RULE_DOFIRETICK)) {
BlockPos blockpos1 = this.blockPosition();
BlockState blockstate = BaseFireBlock.getState(this.level(), blockpos1);
if (this.level().getBlockState(blockpos1).isAir() && blockstate.canSurvive(this.level(), blockpos1)) {
this.level().setBlockAndUpdate(blockpos1, blockstate);
this.blocksSetOnFire++;
}
for (int i = 0; i < p_20871_; i++) {
BlockPos blockpos = blockpos1.offset(this.random.nextInt(3) - 1, this.random.nextInt(3) - 1, this.random.nextInt(3) - 1);
blockstate = BaseFireBlock.getState(this.level(), blockpos);
if (this.level().getBlockState(blockpos).isAir() && blockstate.canSurvive(this.level(), blockpos)) {
this.level().setBlockAndUpdate(blockpos, blockstate);
this.blocksSetOnFire++;
}
}
}
}
private static void clearCopperOnLightningStrike(Level p_147151_, BlockPos p_147152_) {
BlockState blockstate = p_147151_.getBlockState(p_147152_);
BlockPos blockpos;
BlockState blockstate1;
if (blockstate.is(Blocks.LIGHTNING_ROD)) {
blockpos = p_147152_.relative(blockstate.getValue(LightningRodBlock.FACING).getOpposite());
blockstate1 = p_147151_.getBlockState(blockpos);
} else {
blockpos = p_147152_;
blockstate1 = blockstate;
}
if (blockstate1.getBlock() instanceof WeatheringCopper) {
p_147151_.setBlockAndUpdate(blockpos, WeatheringCopper.getFirst(p_147151_.getBlockState(blockpos)));
BlockPos.MutableBlockPos blockpos$mutableblockpos = p_147152_.mutable();
int i = p_147151_.random.nextInt(3) + 3;
for (int j = 0; j < i; j++) {
int k = p_147151_.random.nextInt(8) + 1;
randomWalkCleaningCopper(p_147151_, blockpos, blockpos$mutableblockpos, k);
}
}
}
private static void randomWalkCleaningCopper(Level p_147146_, BlockPos p_147147_, BlockPos.MutableBlockPos p_147148_, int p_147149_) {
p_147148_.set(p_147147_);
for (int i = 0; i < p_147149_; i++) {
Optional<BlockPos> optional = randomStepCleaningCopper(p_147146_, p_147148_);
if (optional.isEmpty()) {
break;
}
p_147148_.set(optional.get());
}
}
private static Optional<BlockPos> randomStepCleaningCopper(Level p_147154_, BlockPos p_147155_) {
for (BlockPos blockpos : BlockPos.randomInCube(p_147154_.random, 10, p_147155_, 1)) {
BlockState blockstate = p_147154_.getBlockState(blockpos);
if (blockstate.getBlock() instanceof WeatheringCopper) {
WeatheringCopper.getPrevious(blockstate).ifPresent(p_147144_ -> p_147154_.setBlockAndUpdate(blockpos, p_147144_));
p_147154_.levelEvent(3002, blockpos, -1);
return Optional.of(blockpos);
}
}
return Optional.empty();
}
@Override
public boolean shouldRenderAtSqrDistance(double p_20869_) {
double d0 = 64.0 * getViewScale();
return p_20869_ < d0 * d0;
}
@Override
protected void defineSynchedData(SynchedEntityData.Builder p_336100_) {
}
@Override
protected void readAdditionalSaveData(CompoundTag p_20873_) {
}
@Override
protected void addAdditionalSaveData(CompoundTag p_20877_) {
}
public int getBlocksSetOnFire() {
return this.blocksSetOnFire;
}
public Stream<Entity> getHitEntities() {
return this.hitEntities.stream().filter(Entity::isAlive);
}
@Override
public final boolean hurtServer(ServerLevel p_368015_, DamageSource p_364945_, float p_364228_) {
return false;
}
} |