minecraft-src/net/minecraft/client/resources/TextureAtlasHolder.java
2025-07-04 01:41:11 +03:00

71 lines
2.8 KiB
Java

package net.minecraft.client.resources;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.renderer.texture.SpriteLoader;
import net.minecraft.client.renderer.texture.TextureAtlas;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.client.renderer.texture.TextureManager;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.packs.metadata.MetadataSectionSerializer;
import net.minecraft.server.packs.resources.PreparableReloadListener;
import net.minecraft.server.packs.resources.ResourceManager;
import net.minecraft.util.profiling.ProfilerFiller;
@Environment(EnvType.CLIENT)
public abstract class TextureAtlasHolder implements PreparableReloadListener, AutoCloseable {
private final TextureAtlas textureAtlas;
private final ResourceLocation atlasInfoLocation;
private final Set<MetadataSectionSerializer<?>> metadataSections;
public TextureAtlasHolder(TextureManager textureManager, ResourceLocation textureAtlasLocation, ResourceLocation atlasInfoLocation) {
this(textureManager, textureAtlasLocation, atlasInfoLocation, SpriteLoader.DEFAULT_METADATA_SECTIONS);
}
public TextureAtlasHolder(
TextureManager textureManager, ResourceLocation textureAtlasLocation, ResourceLocation atlasInfoLocation, Set<MetadataSectionSerializer<?>> metadataSections
) {
this.atlasInfoLocation = atlasInfoLocation;
this.textureAtlas = new TextureAtlas(textureAtlasLocation);
textureManager.register(this.textureAtlas.location(), this.textureAtlas);
this.metadataSections = metadataSections;
}
/**
* Gets a sprite associated with the passed resource location.
*/
protected TextureAtlasSprite getSprite(ResourceLocation location) {
return this.textureAtlas.getSprite(location);
}
@Override
public final CompletableFuture<Void> reload(
PreparableReloadListener.PreparationBarrier preparationBarrier,
ResourceManager resourceManager,
ProfilerFiller preparationsProfiler,
ProfilerFiller reloadProfiler,
Executor backgroundExecutor,
Executor gameExecutor
) {
return SpriteLoader.create(this.textureAtlas)
.loadAndStitch(resourceManager, this.atlasInfoLocation, 0, backgroundExecutor, this.metadataSections)
.thenCompose(SpriteLoader.Preparations::waitForUpload)
.thenCompose(preparationBarrier::wait)
.thenAcceptAsync(preparations -> this.apply(preparations, reloadProfiler), gameExecutor);
}
private void apply(SpriteLoader.Preparations preparations, ProfilerFiller profiler) {
profiler.startTick();
profiler.push("upload");
this.textureAtlas.upload(preparations);
profiler.pop();
profiler.endTick();
}
public void close() {
this.textureAtlas.clearTextureData();
}
}