| 1 | package net.minecraft.client.multiplayer;
|
| 2 |
|
| 3 | import java.util.ArrayList;
|
| 4 | import java.util.List;
|
| 5 | import net.fabricmc.api.EnvType;
|
| 6 | import net.fabricmc.api.Environment;
|
| 7 | import net.minecraft.client.Minecraft;
|
| 8 | import net.minecraft.core.BlockPos;
|
| 9 | import net.minecraft.core.particles.ExplosionParticleInfo;
|
| 10 | import net.minecraft.server.level.ParticleStatus;
|
| 11 | import net.minecraft.util.RandomSource;
|
| 12 | import net.minecraft.util.random.WeightedList;
|
| 13 | import net.minecraft.util.random.WeightedRandom;
|
| 14 | import net.minecraft.world.phys.Vec3;
|
| 15 |
|
| 16 | @Environment(EnvType.CLIENT)
|
| 17 | public class ClientExplosionTracker {
|
| 18 | private static final int MAX_PARTICLES_PER_TICK = 512;
|
| 19 | private final List<ClientExplosionTracker.ExplosionInfo> explosions = new ArrayList<>();
|
| 20 |
|
| 21 | public void track(final Vec3 center, final float radius, final int blockCount, final WeightedList<ExplosionParticleInfo> blockParticles) {
|
| 22 | if (!blockParticles.isEmpty()) {
|
| 23 | this.explosions.add(new ClientExplosionTracker.ExplosionInfo(center, radius, blockCount, blockParticles));
|
| 24 | }
|
| 25 | }
|
| 26 |
|
| 27 | public void tick(final ClientLevel level) {
|
| 28 | if (Minecraft.getInstance().options.particles().get() != ParticleStatus.ALL) {
|
| 29 | this.explosions.clear();
|
| 30 | } else {
|
| 31 | int totalBlocks = WeightedRandom.getTotalWeight(this.explosions, ClientExplosionTracker.ExplosionInfo::blockCount);
|
| 32 | int totalParticles = Math.min(totalBlocks, 512);
|
| 33 |
|
| 34 | for (int i = 0; i < totalParticles; i++) {
|
| 35 | WeightedRandom.getRandomItem(level.getRandom(), this.explosions, totalBlocks, ClientExplosionTracker.ExplosionInfo::blockCount)
|
| 36 | .ifPresent(info -> this.addParticle(level, info));
|
| 37 | }
|
| 38 |
|
| 39 | this.explosions.clear();
|
| 40 | }
|
| 41 | }
|
| 42 |
|
| 43 | private void addParticle(final ClientLevel level, final ClientExplosionTracker.ExplosionInfo explosion) {
|
| 44 | RandomSource random = level.getRandom();
|
| 45 | Vec3 center = explosion.center();
|
| 46 | Vec3 directionFromCenter = new Vec3(random.nextFloat() * 2.0F - 1.0F, random.nextFloat() * 2.0F - 1.0F, random.nextFloat() * 2.0F - 1.0F).normalize();
|
| 47 | float radius = (float)Math.cbrt(random.nextFloat()) * explosion.radius();
|
| 48 | Vec3 localPos = directionFromCenter.scale(radius);
|
| 49 | Vec3 pos = center.add(localPos);
|
| 50 | if (level.getBlockState(BlockPos.containing(pos)).isAir()) {
|
| 51 | float speed = 0.5F / (radius / explosion.radius() + 0.1F) * random.nextFloat() * random.nextFloat() + 0.3F;
|
| 52 | ExplosionParticleInfo info = explosion.blockParticles.getRandomOrThrow(random);
|
| 53 | Vec3 particlePos = center.add(localPos.scale(info.scaling()));
|
| 54 | Vec3 particleVelocity = directionFromCenter.scale(speed * info.speed());
|
| 55 | level.addParticle(info.particle(), particlePos.x(), particlePos.y(), particlePos.z(), particleVelocity.x(), particleVelocity.y(), particleVelocity.z());
|
| 56 | }
|
| 57 | }
|
| 58 |
|
| 59 | @Environment(EnvType.CLIENT)
|
| 60 | private record ExplosionInfo(Vec3 center, float radius, int blockCount, WeightedList<ExplosionParticleInfo> blockParticles) {
|
| 61 | }
|
| 62 | }
|
| 63 |
|