ParticleUtils .java

net.minecraft.util.ParticleUtils (class, 147 lines): members: spawnParticlesOnBlockFaces, spawnParticlesOnBlockFace, spawnSmashAttackParticles, face, pos.

net.minecraft.utilclass147 linescommon6.4 KB

API Surface (8)

void spawnParticlesOnBlockFaces(final Level level, final BlockPos pos, final ParticleOptions particle, final IntProvider particlesPerFaceRange)
void spawnParticlesOnBlockFace(final Level level, final BlockPos pos, final ParticleOptions particle, final IntProvider particlesPerFaceRange, final Direction face, final Supplier<Vec3> speedSupplier, final double stepFactor)
void spawnParticlesAlongAxis(final Direction.Axis attachedAxis, final Level level, final BlockPos pos, final double radius, final ParticleOptions particle, final UniformInt sparkCount)
void spawnParticleOnFace(final Level level, final BlockPos pos, final Direction face, final ParticleOptions particle, final Vec3 speed, final double stepFactor)
void spawnParticleBelow(final Level level, final BlockPos pos, final RandomSource random, final ParticleOptions particle)
void spawnParticleInBlock(final LevelAccessor level, final BlockPos pos, final int count, final ParticleOptions particle)
void spawnParticles(final LevelAccessor level, final BlockPos pos, final int count, final double spreadWidth, final double spreadHeight, final boolean allowFloatingParticles, final ParticleOptions particle)
void spawnSmashAttackParticles(final LevelAccessor level, final BlockPos pos, final int count)

Fields & Constants (11)

level
pos
particle
particlesPerFaceRange
face
speedSupplier
speed
count
spreadWidth
spreadHeight
allowFloatingParticles

Full Source

ParticleUtils.java147 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.util;
2
3
import java.util.function.Supplier;
4
import net.minecraft.core.BlockPos;
5
import net.minecraft.core.Direction;
6
import net.minecraft.core.particles.BlockParticleOption;
7
import net.minecraft.core.particles.ParticleOptions;
8
import net.minecraft.core.particles.ParticleTypes;
9
import net.minecraft.util.valueproviders.IntProvider;
10
import net.minecraft.util.valueproviders.UniformInt;
11
import net.minecraft.world.level.Level;
12
import net.minecraft.world.level.LevelAccessor;
13
import net.minecraft.world.level.block.state.BlockState;
14
import net.minecraft.world.phys.Vec3;
15
16
public class ParticleUtils {
17
	public static void spawnParticlesOnBlockFaces(final Level level, final BlockPos pos, final ParticleOptions particle, final IntProvider particlesPerFaceRange) {
18
		RandomSource random = level.getRandom();
19
20
		for (Direction direction : Direction.values()) {
21
			spawnParticlesOnBlockFace(level, pos, particle, particlesPerFaceRange, direction, () -> getRandomSpeedRanges(random), 0.55);
22
		}
23
	}
24
25
	public static void spawnParticlesOnBlockFace(
26
		final Level level,
27
		final BlockPos pos,
28
		final ParticleOptions particle,
29
		final IntProvider particlesPerFaceRange,
30
		final Direction face,
31
		final Supplier<Vec3> speedSupplier,
32
		final double stepFactor
33
	) {
34
		int particleCount = particlesPerFaceRange.sample(level.getRandom());
35
36
		for (int i = 0; i < particleCount; i++) {
37
			spawnParticleOnFace(level, pos, face, particle, speedSupplier.get(), stepFactor);
38
		}
39
	}
40
41
	private static Vec3 getRandomSpeedRanges(final RandomSource random) {
42
		return new Vec3(Mth.nextDouble(random, -0.5, 0.5), Mth.nextDouble(random, -0.5, 0.5), Mth.nextDouble(random, -0.5, 0.5));
43
	}
44
45
	public static void spawnParticlesAlongAxis(
46
		final Direction.Axis attachedAxis, final Level level, final BlockPos pos, final double radius, final ParticleOptions particle, final UniformInt sparkCount
47
	) {
48
		Vec3 centerOfBlock = Vec3.atCenterOf(pos);
49
		boolean stepX = attachedAxis == Direction.Axis.X;
50
		boolean stepY = attachedAxis == Direction.Axis.Y;
51
		boolean stepZ = attachedAxis == Direction.Axis.Z;
52
		RandomSource random = level.getRandom();
53
		int particleCount = sparkCount.sample(random);
54
55
		for (int i = 0; i < particleCount; i++) {
56
			double x = centerOfBlock.x + Mth.nextDouble(random, -1.0, 1.0) * (stepX ? 0.5 : radius);
57
			double y = centerOfBlock.y + Mth.nextDouble(random, -1.0, 1.0) * (stepY ? 0.5 : radius);
58
			double z = centerOfBlock.z + Mth.nextDouble(random, -1.0, 1.0) * (stepZ ? 0.5 : radius);
59
			double xBaseSpeed = stepX ? Mth.nextDouble(random, -1.0, 1.0) : 0.0;
60
			double yBaseSpeed = stepY ? Mth.nextDouble(random, -1.0, 1.0) : 0.0;
61
			double zBaseSpeed = stepZ ? Mth.nextDouble(random, -1.0, 1.0) : 0.0;
62
			level.addParticle(particle, x, y, z, xBaseSpeed, yBaseSpeed, zBaseSpeed);
63
		}
64
	}
65
66
	public static void spawnParticleOnFace(
67
		final Level level, final BlockPos pos, final Direction face, final ParticleOptions particle, final Vec3 speed, final double stepFactor
68
	) {
69
		Vec3 centerOfBlock = Vec3.atCenterOf(pos);
70
		int stepX = face.getStepX();
71
		int stepY = face.getStepY();
72
		int stepZ = face.getStepZ();
73
		RandomSource random = level.getRandom();
74
		double x = centerOfBlock.x + (stepX == 0 ? Mth.nextDouble(random, -0.5, 0.5) : stepX * stepFactor);
75
		double y = centerOfBlock.y + (stepY == 0 ? Mth.nextDouble(random, -0.5, 0.5) : stepY * stepFactor);
76
		double z = centerOfBlock.z + (stepZ == 0 ? Mth.nextDouble(random, -0.5, 0.5) : stepZ * stepFactor);
77
		double xBaseSpeed = stepX == 0 ? speed.x() : 0.0;
78
		double yBaseSpeed = stepY == 0 ? speed.y() : 0.0;
79
		double zBaseSpeed = stepZ == 0 ? speed.z() : 0.0;
80
		level.addParticle(particle, x, y, z, xBaseSpeed, yBaseSpeed, zBaseSpeed);
81
	}
82
83
	public static void spawnParticleBelow(final Level level, final BlockPos pos, final RandomSource random, final ParticleOptions particle) {
84
		double x = pos.getX() + random.nextDouble();
85
		double y = pos.getY() - 0.05;
86
		double z = pos.getZ() + random.nextDouble();
87
		level.addParticle(particle, x, y, z, 0.0, 0.0, 0.0);
88
	}
89
90
	public static void spawnParticleInBlock(final LevelAccessor level, final BlockPos pos, final int count, final ParticleOptions particle) {
91
		double spreadWidth = 0.5;
92
		BlockState blockState = level.getBlockState(pos);
93
		double spreadHeight = blockState.isAir() ? 1.0 : blockState.getShape(level, pos).max(Direction.Axis.Y);
94
		spawnParticles(level, pos, count, 0.5, spreadHeight, true, particle);
95
	}
96
97
	public static void spawnParticles(
98
		final LevelAccessor level,
99
		final BlockPos pos,
100
		final int count,
101
		final double spreadWidth,
102
		final double spreadHeight,
103
		final boolean allowFloatingParticles,
104
		final ParticleOptions particle
105
	) {
106
		RandomSource random = level.getRandom();
107
108
		for (int i = 0; i < count; i++) {
109
			double xVelocity = random.nextGaussian() * 0.02;
110
			double yVelocity = random.nextGaussian() * 0.02;
111
			double zVelocity = random.nextGaussian() * 0.02;
112
			double spreadStartOffset = 0.5 - spreadWidth;
113
			double x = pos.getX() + spreadStartOffset + random.nextDouble() * spreadWidth * 2.0;
114
			double y = pos.getY() + random.nextDouble() * spreadHeight;
115
			double z = pos.getZ() + spreadStartOffset + random.nextDouble() * spreadWidth * 2.0;
116
			if (allowFloatingParticles || !level.getBlockState(BlockPos.containing(x, y, z).below()).isAir()) {
117
				level.addParticle(particle, x, y, z, xVelocity, yVelocity, zVelocity);
118
			}
119
		}
120
	}
121
122
	public static void spawnSmashAttackParticles(final LevelAccessor level, final BlockPos pos, final int count) {
123
		Vec3 center = Vec3.atCenterOf(pos).add(0.0, 0.5, 0.0);
124
		BlockParticleOption particle = new BlockParticleOption(ParticleTypes.DUST_PILLAR, level.getBlockState(pos));
125
126
		for (int i = 0; i < count / 3.0F; i++) {
127
			double x = center.x + level.getRandom().nextGaussian() / 2.0;
128
			double y = center.y;
129
			double z = center.z + level.getRandom().nextGaussian() / 2.0;
130
			double xd = level.getRandom().nextGaussian() * 0.2F;
131
			double yd = level.getRandom().nextGaussian() * 0.2F;
132
			double zd = level.getRandom().nextGaussian() * 0.2F;
133
			level.addParticle(particle, x, y, z, xd, yd, zd);
134
		}
135
136
		for (int i = 0; i < count / 1.5F; i++) {
137
			double x = center.x + 3.5 * Math.cos(i) + level.getRandom().nextGaussian() / 2.0;
138
			double y = center.y;
139
			double z = center.z + 3.5 * Math.sin(i) + level.getRandom().nextGaussian() / 2.0;
140
			double xd = level.getRandom().nextGaussian() * 0.05F;
141
			double yd = level.getRandom().nextGaussian() * 0.05F;
142
			double zd = level.getRandom().nextGaussian() * 0.05F;
143
			level.addParticle(particle, x, y, z, xd, yd, zd);
144
		}
145
	}
146
}
147

Decompiled Minecraft 26.3 source of ParticleUtils (net/minecraft/util/ParticleUtils.java) with Mojang official mappings, stored verbatim on the site.