ParticleResources .java

net.minecraft.client.particle.ParticleResources (class): members: SpriteParticleRegistration, MutableSpriteSet, PARTICLE_LISTER, getProviders, spriteSets.

net.minecraft.client.particleclass312 linesclient17.3 KB

Implements

API Surface (7)

void onReload(final Runnable onReload)
CompletableFuture<Void> reload(final SharedState currentReload, final Executor taskExecutor, final PreparationBarrier preparationBarrier, final Executor reloadExecutor)
Int2ObjectMap<ParticleProvider<?>> getProviders()
TextureAtlasSprite get(final int index, final int max)
TextureAtlasSprite get(final RandomSource random)
TextureAtlasSprite first()
void rebind(final List<TextureAtlasSprite> ids)

Fields & Constants (6)

LOGGER
PARTICLE_LISTER
spriteSets
providers
onReload
preparationBarrier

String literals (4)

"particles"
"upload"
"bindSpriteSets"
"Failed to load description for particle "

Full Source

ParticleResources.java312 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.client.particle;
2
3
import com.google.common.collect.ImmutableList;
4
import com.google.common.collect.Maps;
5
import com.mojang.logging.LogUtils;
6
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
7
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
8
import java.io.IOException;
9
import java.io.Reader;
10
import java.util.ArrayList;
11
import java.util.HashSet;
12
import java.util.List;
13
import java.util.Map;
14
import java.util.Optional;
15
import java.util.Set;
16
import java.util.concurrent.CompletableFuture;
17
import java.util.concurrent.Executor;
18
import java.util.stream.Collectors;
19
import net.fabricmc.api.EnvType;
20
import net.fabricmc.api.Environment;
21
import net.minecraft.client.renderer.texture.SpriteLoader;
22
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
23
import net.minecraft.client.resources.model.sprite.AtlasManager;
24
import net.minecraft.core.particles.ParticleOptions;
25
import net.minecraft.core.particles.ParticleType;
26
import net.minecraft.core.particles.ParticleTypes;
27
import net.minecraft.core.registries.BuiltInRegistries;
28
import net.minecraft.data.AtlasIds;
29
import net.minecraft.resources.FileToIdConverter;
30
import net.minecraft.resources.Identifier;
31
import net.minecraft.server.packs.resources.PreparableReloadListener;
32
import net.minecraft.server.packs.resources.Resource;
33
import net.minecraft.server.packs.resources.ResourceManager;
34
import net.minecraft.server.packs.resources.PreparableReloadListener.PreparationBarrier;
35
import net.minecraft.server.packs.resources.PreparableReloadListener.SharedState;
36
import net.minecraft.util.GsonHelper;
37
import net.minecraft.util.RandomSource;
38
import net.minecraft.util.Util;
39
import net.minecraft.util.profiling.Profiler;
40
import net.minecraft.util.profiling.ProfilerFiller;
41
import org.jspecify.annotations.Nullable;
42
import org.slf4j.Logger;
43
44
@Environment(EnvType.CLIENT)
45
public class ParticleResources implements PreparableReloadListener {
46
	private static final Logger LOGGER = LogUtils.getLogger();
47
	private static final FileToIdConverter PARTICLE_LISTER = FileToIdConverter.json("particles");
48
	private final Map<Identifier, ParticleResources.MutableSpriteSet> spriteSets = Maps.newHashMap();
49
	private final Int2ObjectMap<ParticleProvider<?>> providers = new Int2ObjectOpenHashMap<>();
50
	private @Nullable Runnable onReload;
51
52
	public ParticleResources() {
53
		this.registerProviders();
54
	}
55
56
	public void onReload(final Runnable onReload) {
57
		this.onReload = onReload;
58
	}
59
60
	private void registerProviders() {
61
		this.register(ParticleTypes.ANGRY_VILLAGER, HeartParticle.AngryVillagerProvider::new);
62
		this.register(ParticleTypes.BLOCK_MARKER, new BlockMarker.Provider());
63
		this.register(ParticleTypes.BLOCK, new TerrainParticle.Provider());
64
		this.register(ParticleTypes.BUBBLE, BubbleParticle.Provider::new);
65
		this.register(ParticleTypes.BUBBLE_COLUMN_UP, BubbleColumnUpParticle.Provider::new);
66
		this.register(ParticleTypes.BUBBLE_POP, BubblePopParticle.Provider::new);
67
		this.register(ParticleTypes.SULFUR_BUBBLES, SulfurBubbleParticle.Provider::new);
68
		this.register(ParticleTypes.NOXIOUS_GAS, NoxiousGasParticle.Provider::new);
69
		this.register(ParticleTypes.NOXIOUS_GAS_CLOUD, new NoxiousGasCloudParticle.Provider());
70
		this.register(ParticleTypes.GEYSER, new GeyserEruptionParticle.Provider());
71
		this.register(ParticleTypes.GEYSER_BASE, GeyserBaseParticle.Provider::new);
72
		this.register(ParticleTypes.GEYSER_POOF, GeyserBaseParticle.Provider::new);
73
		this.register(ParticleTypes.GEYSER_PLUME, GeyserPlumeParticle.Provider::new);
74
		this.register(ParticleTypes.CAMPFIRE_COSY_SMOKE, CampfireSmokeParticle.CosyProvider::new);
75
		this.register(ParticleTypes.CAMPFIRE_SIGNAL_SMOKE, CampfireSmokeParticle.SignalProvider::new);
76
		this.register(ParticleTypes.CLOUD, PlayerCloudParticle.Provider::new);
77
		this.register(ParticleTypes.COMPOSTER, SuspendedTownParticle.ComposterFillProvider::new);
78
		this.register(ParticleTypes.COPPER_FIRE_FLAME, FlameParticle.Provider::new);
79
		this.register(ParticleTypes.CRIT, CritParticle.Provider::new);
80
		this.register(ParticleTypes.CURRENT_DOWN, WaterCurrentDownParticle.Provider::new);
81
		this.register(ParticleTypes.DAMAGE_INDICATOR, CritParticle.DamageIndicatorProvider::new);
82
		this.register(ParticleTypes.DRAGON_BREATH, DragonBreathParticle.Provider::new);
83
		this.register(ParticleTypes.DOLPHIN, SuspendedTownParticle.DolphinSpeedProvider::new);
84
		this.register(ParticleTypes.DRIPPING_LAVA, DripParticle.LavaHangProvider::new);
85
		this.register(ParticleTypes.FALLING_LAVA, DripParticle.LavaFallProvider::new);
86
		this.register(ParticleTypes.LANDING_LAVA, DripParticle.LavaLandProvider::new);
87
		this.register(ParticleTypes.DRIPPING_WATER, DripParticle.WaterHangProvider::new);
88
		this.register(ParticleTypes.FALLING_WATER, DripParticle.WaterFallProvider::new);
89
		this.register(ParticleTypes.DUST, DustParticle.Provider::new);
90
		this.register(ParticleTypes.DUST_COLOR_TRANSITION, DustColorTransitionParticle.Provider::new);
91
		this.register(ParticleTypes.EFFECT, SpellParticle.InstantProvider::new);
92
		this.register(ParticleTypes.ELDER_GUARDIAN, new ElderGuardianParticle.Provider());
93
		this.register(ParticleTypes.ENCHANTED_HIT, CritParticle.MagicProvider::new);
94
		this.register(ParticleTypes.ENCHANT, FlyTowardsPositionParticle.EnchantProvider::new);
95
		this.register(ParticleTypes.END_ROD, EndRodParticle.Provider::new);
96
		this.register(ParticleTypes.ENTITY_EFFECT, SpellParticle.MobEffectProvider::new);
97
		this.register(ParticleTypes.EXPLOSION_EMITTER, new HugeExplosionSeedParticle.Provider());
98
		this.register(ParticleTypes.EXPLOSION, HugeExplosionParticle.Provider::new);
99
		this.register(ParticleTypes.SONIC_BOOM, SonicBoomParticle.Provider::new);
100
		this.register(ParticleTypes.FALLING_DUST, FallingDustParticle.Provider::new);
101
		this.register(ParticleTypes.GUST, GustParticle.Provider::new);
102
		this.register(ParticleTypes.SMALL_GUST, GustParticle.SmallProvider::new);
103
		this.register(ParticleTypes.GUST_EMITTER_LARGE, new GustSeedParticle.Provider(3.0, 7, 0));
104
		this.register(ParticleTypes.GUST_EMITTER_SMALL, new GustSeedParticle.Provider(1.0, 3, 2));
105
		this.register(ParticleTypes.FIREWORK, FireworkParticles.SparkProvider::new);
106
		this.register(ParticleTypes.FISHING, WakeParticle.Provider::new);
107
		this.register(ParticleTypes.FLAME, FlameParticle.Provider::new);
108
		this.register(ParticleTypes.INFESTED, SpellParticle.Provider::new);
109
		this.register(ParticleTypes.SCULK_SOUL, EmissiveRisingParticle.EmissiveProvider::new);
110
		this.register(ParticleTypes.SCULK_CHARGE, SculkChargeParticle.Provider::new);
111
		this.register(ParticleTypes.SCULK_CHARGE_POP, SculkChargePopParticle.Provider::new);
112
		this.register(ParticleTypes.SOUL, EmissiveRisingParticle.Provider::new);
113
		this.register(ParticleTypes.SOUL_FIRE_FLAME, FlameParticle.Provider::new);
114
		this.register(ParticleTypes.FLASH, FireworkParticles.FlashProvider::new);
115
		this.register(ParticleTypes.HAPPY_VILLAGER, SuspendedTownParticle.HappyVillagerProvider::new);
116
		this.register(ParticleTypes.HEART, HeartParticle.Provider::new);
117
		this.register(ParticleTypes.INSTANT_EFFECT, SpellParticle.InstantProvider::new);
118
		this.register(ParticleTypes.ITEM, new BreakingItemParticle.Provider());
119
		this.register(ParticleTypes.ITEM_SLIME, new BreakingItemParticle.SlimeProvider());
120
		this.register(ParticleTypes.ITEM_COBWEB, new BreakingItemParticle.CobwebProvider());
121
		this.register(ParticleTypes.ITEM_SNOWBALL, new BreakingItemParticle.SnowballProvider());
122
		this.register(ParticleTypes.LARGE_SMOKE, LargeSmokeParticle.Provider::new);
123
		this.register(ParticleTypes.LAVA, LavaParticle.Provider::new);
124
		this.register(ParticleTypes.MYCELIUM, SuspendedTownParticle.Provider::new);
125
		this.register(ParticleTypes.NAUTILUS, FlyTowardsPositionParticle.NautilusProvider::new);
126
		this.register(ParticleTypes.NOTE, NoteParticle.Provider::new);
127
		this.register(ParticleTypes.POOF, ExplodeParticle.Provider::new);
128
		this.register(ParticleTypes.PORTAL, PortalParticle.Provider::new);
129
		this.register(ParticleTypes.RAIN, WaterDropParticle.Provider::new);
130
		this.register(ParticleTypes.SMOKE, SmokeParticle.Provider::new);
131
		this.register(ParticleTypes.WHITE_SMOKE, WhiteSmokeParticle.Provider::new);
132
		this.register(ParticleTypes.SNEEZE, PlayerCloudParticle.SneezeProvider::new);
133
		this.register(ParticleTypes.SNOWFLAKE, SnowflakeParticle.Provider::new);
134
		this.register(ParticleTypes.SPIT, SpitParticle.Provider::new);
135
		this.register(ParticleTypes.SWEEP_ATTACK, AttackSweepParticle.Provider::new);
136
		this.register(ParticleTypes.TOTEM_OF_UNDYING, TotemParticle.Provider::new);
137
		this.register(ParticleTypes.SQUID_INK, SquidInkParticle.Provider::new);
138
		this.register(ParticleTypes.UNDERWATER, SuspendedParticle.UnderwaterProvider::new);
139
		this.register(ParticleTypes.SPLASH, SplashParticle.Provider::new);
140
		this.register(ParticleTypes.WITCH, SpellParticle.WitchProvider::new);
141
		this.register(ParticleTypes.DRIPPING_HONEY, DripParticle.HoneyHangProvider::new);
142
		this.register(ParticleTypes.FALLING_HONEY, DripParticle.HoneyFallProvider::new);
143
		this.register(ParticleTypes.LANDING_HONEY, DripParticle.HoneyLandProvider::new);
144
		this.register(ParticleTypes.FALLING_NECTAR, DripParticle.NectarFallProvider::new);
145
		this.register(ParticleTypes.FALLING_SPORE_BLOSSOM, DripParticle.SporeBlossomFallProvider::new);
146
		this.register(ParticleTypes.SPORE_BLOSSOM_AIR, SuspendedParticle.SporeBlossomAirProvider::new);
147
		this.register(ParticleTypes.ASH, AshParticle.Provider::new);
148
		this.register(ParticleTypes.CRIMSON_SPORE, SuspendedParticle.CrimsonSporeProvider::new);
149
		this.register(ParticleTypes.WARPED_SPORE, SuspendedParticle.WarpedSporeProvider::new);
150
		this.register(ParticleTypes.DRIPPING_OBSIDIAN_TEAR, DripParticle.ObsidianTearHangProvider::new);
151
		this.register(ParticleTypes.FALLING_OBSIDIAN_TEAR, DripParticle.ObsidianTearFallProvider::new);
152
		this.register(ParticleTypes.LANDING_OBSIDIAN_TEAR, DripParticle.ObsidianTearLandProvider::new);
153
		this.register(ParticleTypes.REVERSE_PORTAL, ReversePortalParticle.ReversePortalProvider::new);
154
		this.register(ParticleTypes.WHITE_ASH, WhiteAshParticle.Provider::new);
155
		this.register(ParticleTypes.SMALL_FLAME, FlameParticle.SmallFlameProvider::new);
156
		this.register(ParticleTypes.DRIPPING_DRIPSTONE_WATER, DripParticle.DripstoneWaterHangProvider::new);
157
		this.register(ParticleTypes.FALLING_DRIPSTONE_WATER, DripParticle.DripstoneWaterFallProvider::new);
158
		this.register(ParticleTypes.CHERRY_LEAVES, FallingLeavesParticle.CherryProvider::new);
159
		this.register(ParticleTypes.PALE_OAK_LEAVES, FallingLeavesParticle.PaleOakProvider::new);
160
		this.register(ParticleTypes.RED_POPLAR_LEAVES, FallingLeavesParticle.PoplarProvider::new);
161
		this.register(ParticleTypes.ORANGE_POPLAR_LEAVES, FallingLeavesParticle.PoplarProvider::new);
162
		this.register(ParticleTypes.YELLOW_POPLAR_LEAVES, FallingLeavesParticle.PoplarProvider::new);
163
		this.register(ParticleTypes.TINTED_LEAVES, FallingLeavesParticle.TintedLeavesProvider::new);
164
		this.register(ParticleTypes.DRIPPING_DRIPSTONE_LAVA, DripParticle.DripstoneLavaHangProvider::new);
165
		this.register(ParticleTypes.FALLING_DRIPSTONE_LAVA, DripParticle.DripstoneLavaFallProvider::new);
166
		this.register(ParticleTypes.VIBRATION, VibrationSignalParticle.Provider::new);
167
		this.register(ParticleTypes.TRAIL, TrailParticle.Provider::new);
168
		this.register(ParticleTypes.PAUSE_MOB_GROWTH, SimpleVerticalParticle.PauseMobGrowthProvider::new);
169
		this.register(ParticleTypes.RESET_MOB_GROWTH, SimpleVerticalParticle.ResetMobGrowthProvider::new);
170
		this.register(ParticleTypes.GLOW_SQUID_INK, SquidInkParticle.GlowInkProvider::new);
171
		this.register(ParticleTypes.GLOW, GlowParticle.GlowSquidProvider::new);
172
		this.register(ParticleTypes.WAX_ON, GlowParticle.WaxOnProvider::new);
173
		this.register(ParticleTypes.WAX_OFF, GlowParticle.WaxOffProvider::new);
174
		this.register(ParticleTypes.ELECTRIC_SPARK, GlowParticle.ElectricSparkProvider::new);
175
		this.register(ParticleTypes.SCRAPE, GlowParticle.ScrapeProvider::new);
176
		this.register(ParticleTypes.SHRIEK, ShriekParticle.Provider::new);
177
		this.register(ParticleTypes.EGG_CRACK, SuspendedTownParticle.EggCrackProvider::new);
178
		this.register(ParticleTypes.DUST_PLUME, DustPlumeParticle.Provider::new);
179
		this.register(ParticleTypes.TRIAL_SPAWNER_DETECTED_PLAYER, TrialSpawnerDetectionParticle.Provider::new);
180
		this.register(ParticleTypes.TRIAL_SPAWNER_DETECTED_PLAYER_OMINOUS, TrialSpawnerDetectionParticle.Provider::new);
181
		this.register(ParticleTypes.VAULT_CONNECTION, FlyTowardsPositionParticle.VaultConnectionProvider::new);
182
		this.register(ParticleTypes.DUST_PILLAR, new TerrainParticle.DustPillarProvider());
183
		this.register(ParticleTypes.RAID_OMEN, SpellParticle.Provider::new);
184
		this.register(ParticleTypes.TRIAL_OMEN, SpellParticle.Provider::new);
185
		this.register(ParticleTypes.OMINOUS_SPAWNING, FlyStraightTowardsParticle.OminousSpawnProvider::new);
186
		this.register(ParticleTypes.BLOCK_CRUMBLE, new TerrainParticle.CrumblingProvider());
187
		this.register(ParticleTypes.FIREFLY, FireflyParticle.FireflyProvider::new);
188
		this.register(ParticleTypes.SULFUR_CUBE_GOO, BreakingItemParticle.SulfurCubeProvider::new);
189
	}
190
191
	private <T extends ParticleOptions> void register(final ParticleType<T> type, final ParticleProvider<T> provider) {
192
		this.providers.put(BuiltInRegistries.PARTICLE_TYPE.getId(type), provider);
193
	}
194
195
	private <T extends ParticleOptions> void register(final ParticleType<T> type, final ParticleResources.SpriteParticleRegistration<T> provider) {
196
		ParticleResources.MutableSpriteSet spriteSet = new ParticleResources.MutableSpriteSet();
197
		this.spriteSets.put(BuiltInRegistries.PARTICLE_TYPE.getKey(type), spriteSet);
198
		this.providers.put(BuiltInRegistries.PARTICLE_TYPE.getId(type), provider.create(spriteSet));
199
	}
200
201
	@Override
202
	public CompletableFuture<Void> reload(
203
		final SharedState currentReload, final Executor taskExecutor, final PreparationBarrier preparationBarrier, final Executor reloadExecutor
204
	) {
205
		ResourceManager manager = currentReload.resourceManager();
206
207
		@Environment(EnvType.CLIENT)
208
		record ParticleDefinition(Identifier id, Optional<List<Identifier>> sprites) {
209
		}
210
211
		CompletableFuture<List<ParticleDefinition>> spriteSetsToLoad = CompletableFuture.<Map<Identifier, Resource>>supplyAsync(
212
				() -> PARTICLE_LISTER.listMatchingResources(manager), taskExecutor
213
			)
214
			.thenCompose(definitionsToScan -> {
215
				List<CompletableFuture<ParticleDefinition>> loadTasks = new ArrayList<>(definitionsToScan.size());
216
				definitionsToScan.forEach((resourceId, resource) -> {
217
					Identifier particleId = PARTICLE_LISTER.fileToId(resourceId);
218
					loadTasks.add(CompletableFuture.supplyAsync(() -> new ParticleDefinition(particleId, this.loadParticleDescription(particleId, resource)), taskExecutor));
219
				});
220
				return Util.sequence(loadTasks);
221
			});
222
		CompletableFuture<SpriteLoader.Preparations> pendingSprites = currentReload.get(AtlasManager.PENDING_STITCH).get(AtlasIds.PARTICLES);
223
		return CompletableFuture.allOf(spriteSetsToLoad, pendingSprites).thenCompose(preparationBarrier::wait).thenAcceptAsync(unused -> {
224
			if (this.onReload != null) {
225
				this.onReload.run();
226
			}
227
228
			ProfilerFiller reloadProfiler = Profiler.get();
229
			reloadProfiler.push("upload");
230
			SpriteLoader.Preparations sprites = pendingSprites.join();
231
			reloadProfiler.popPush("bindSpriteSets");
232
			Set<Identifier> missingSprites = new HashSet<>();
233
			TextureAtlasSprite missingSprite = sprites.missing();
234
			spriteSetsToLoad.join().forEach(p -> {
235
				Optional<List<Identifier>> spriteIds = p.sprites();
236
				if (!spriteIds.isEmpty()) {
237
					List<TextureAtlasSprite> contents = new ArrayList<>();
238
239
					for (Identifier spriteId : spriteIds.get()) {
240
						TextureAtlasSprite sprite = sprites.getSprite(spriteId);
241
						if (sprite == null) {
242
							missingSprites.add(spriteId);
243
							contents.add(missingSprite);
244
						} else {
245
							contents.add(sprite);
246
						}
247
					}
248
249
					if (contents.isEmpty()) {
250
						contents.add(missingSprite);
251
					}
252
253
					this.spriteSets.get(p.id()).rebind(contents);
254
				}
255
			});
256
			if (!missingSprites.isEmpty()) {
257
				LOGGER.warn("Missing particle sprites: {}", missingSprites.stream().sorted().map(Identifier::toString).collect(Collectors.joining(",")));
258
			}
259
260
			reloadProfiler.pop();
261
		}, reloadExecutor);
262
	}
263
264
	private Optional<List<Identifier>> loadParticleDescription(final Identifier id, final Resource resource) {
265
		if (!this.spriteSets.containsKey(id)) {
266
			LOGGER.debug("Redundant texture list for particle: {}", id);
267
			return Optional.empty();
268
		}
269
270
		try (Reader reader = resource.openAsReader()) {
271
			ParticleDescription description = ParticleDescription.fromJson(GsonHelper.parse(reader));
272
			return Optional.of(description.getTextures());
273
		} catch (IOException e) {
274
			throw new IllegalStateException("Failed to load description for particle " + id, e);
275
		}
276
	}
277
278
	public Int2ObjectMap<ParticleProvider<?>> getProviders() {
279
		return this.providers;
280
	}
281
282
	@Environment(EnvType.CLIENT)
283
	private static class MutableSpriteSet implements SpriteSet {
284
		private List<TextureAtlasSprite> sprites;
285
286
		@Override
287
		public TextureAtlasSprite get(final int index, final int max) {
288
			return this.sprites.get(index * (this.sprites.size() - 1) / max);
289
		}
290
291
		@Override
292
		public TextureAtlasSprite get(final RandomSource random) {
293
			return this.sprites.get(random.nextInt(this.sprites.size()));
294
		}
295
296
		@Override
297
		public TextureAtlasSprite first() {
298
			return this.sprites.getFirst();
299
		}
300
301
		public void rebind(final List<TextureAtlasSprite> ids) {
302
			this.sprites = ImmutableList.copyOf(ids);
303
		}
304
	}
305
306
	@FunctionalInterface
307
	@Environment(EnvType.CLIENT)
308
	private interface SpriteParticleRegistration<T extends ParticleOptions> {
309
		ParticleProvider<T> create(SpriteSet spriteSet);
310
	}
311
}
312

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