minecraft-src/net/minecraft/client/renderer/texture/PreloadedTexture.java
2025-07-04 01:41:11 +03:00

47 lines
1.9 KiB
Java

package net.minecraft.client.renderer.texture;
import com.mojang.blaze3d.systems.RenderSystem;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.Util;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.packs.resources.ResourceManager;
import org.jetbrains.annotations.Nullable;
@Environment(EnvType.CLIENT)
public class PreloadedTexture extends SimpleTexture {
@Nullable
private CompletableFuture<SimpleTexture.TextureImage> future;
public PreloadedTexture(ResourceManager resourceManager, ResourceLocation location, Executor backgroundExecutor) {
super(location);
this.future = CompletableFuture.supplyAsync(() -> SimpleTexture.TextureImage.load(resourceManager, location), backgroundExecutor);
}
@Override
protected SimpleTexture.TextureImage getTextureImage(ResourceManager resourceManager) {
if (this.future != null) {
SimpleTexture.TextureImage textureImage = (SimpleTexture.TextureImage)this.future.join();
this.future = null;
return textureImage;
} else {
return SimpleTexture.TextureImage.load(resourceManager, this.location);
}
}
public CompletableFuture<Void> getFuture() {
return this.future == null ? CompletableFuture.completedFuture(null) : this.future.thenApply(textureImage -> null);
}
@Override
public void reset(TextureManager textureManager, ResourceManager resourceManager, ResourceLocation path, Executor executor) {
this.future = CompletableFuture.supplyAsync(() -> SimpleTexture.TextureImage.load(resourceManager, this.location), Util.backgroundExecutor());
this.future.thenRunAsync(() -> textureManager.register(this.location, this), executor(executor));
}
private static Executor executor(Executor executor) {
return runnable -> executor.execute(() -> RenderSystem.recordRenderCall(runnable::run));
}
}