diff options
author | isXander <xandersmith2008@gmail.com> | 2024-01-19 17:05:59 +0000 |
---|---|---|
committer | isXander <xandersmith2008@gmail.com> | 2024-01-19 17:05:59 +0000 |
commit | 5a147e615c3b66bd21fb2d07db11a83e5b9d64fb (patch) | |
tree | 94002a6b8a30be8dbc3c1368cb38f32401aa4d6f /common | |
parent | 5c110c8b525a051fc5bee0397e3c4a945fa0a129 (diff) | |
download | YetAnotherConfigLib-5a147e615c3b66bd21fb2d07db11a83e5b9d64fb.tar.gz YetAnotherConfigLib-5a147e615c3b66bd21fb2d07db11a83e5b9d64fb.tar.bz2 YetAnotherConfigLib-5a147e615c3b66bd21fb2d07db11a83e5b9d64fb.zip |
Finalise the image reloader
Diffstat (limited to 'common')
3 files changed, 27 insertions, 9 deletions
diff --git a/common/src/main/java/dev/isxander/yacl3/gui/image/ImageRendererManager.java b/common/src/main/java/dev/isxander/yacl3/gui/image/ImageRendererManager.java index 0c9b8a3..2f7ef50 100644 --- a/common/src/main/java/dev/isxander/yacl3/gui/image/ImageRendererManager.java +++ b/common/src/main/java/dev/isxander/yacl3/gui/image/ImageRendererManager.java @@ -3,14 +3,16 @@ package dev.isxander.yacl3.gui.image; import com.mojang.blaze3d.systems.RenderSystem; import dev.isxander.yacl3.gui.image.impl.AnimatedDynamicTextureImage; import dev.isxander.yacl3.impl.utils.YACLConstants; +import dev.isxander.yacl3.platform.YACLPlatform; import net.minecraft.client.Minecraft; import net.minecraft.resources.ResourceLocation; +import net.minecraft.server.packs.resources.Resource; import java.util.List; import java.util.Map; import java.util.Optional; import java.util.concurrent.*; -import java.util.function.Function; +import java.util.function.BiFunction; import java.util.function.Predicate; import java.util.function.Supplier; @@ -23,11 +25,11 @@ public class ImageRendererManager { static final List<PreloadedImageFactory> PRELOADED_IMAGE_FACTORIES = List.of( new PreloadedImageFactory( location -> location.getPath().endsWith(".webp"), - AnimatedDynamicTextureImage::createWEBPFromTexture + AnimatedDynamicTextureImage::createWEBPFromResource ), new PreloadedImageFactory( location -> location.getPath().endsWith(".gif"), - AnimatedDynamicTextureImage::createGIFFromTexture + AnimatedDynamicTextureImage::createGIFFromResource ) ); @@ -37,6 +39,13 @@ public class ImageRendererManager { } if (IMAGE_CACHE.containsKey(id)) { + // warn developers if they don't put their webp/gif images `/textures` folder + if (YACLPlatform.isDevelopmentEnv()) { + if (PRELOADED_IMAGE_FACTORIES.stream().anyMatch(factory -> factory.predicate().test(id))) { + YACLConstants.LOGGER.error("Image '{}' not preloaded. MAKE SURE THAT ALL YACL WEBP/GIF IMAGES ARE INSIDE YOUR ASSETS `/textures` FOLDER, ELSE THEY WILL NOT BE PRELOADED!!! THIS ERROR WILL NOT APPEAR IN PROD", id); + } + } + return Optional.ofNullable((T) IMAGE_CACHE.get(id).getNow(null)); } @@ -45,8 +54,6 @@ public class ImageRendererManager { @SuppressWarnings("unchecked") public static <T extends ImageRenderer> CompletableFuture<T> registerImage(ResourceLocation id, ImageRendererFactory factory) { - System.out.println(PRELOADED_IMAGE_CACHE.get(id)); - if (IMAGE_CACHE.containsKey(id)) { return (CompletableFuture<T>) IMAGE_CACHE.get(id); } @@ -66,7 +73,7 @@ public class ImageRendererManager { return (CompletableFuture<T>) future; } - private static <T extends ImageRenderer> void completeImageFactory(ResourceLocation id, Supplier<Optional<ImageRendererFactory.ImageSupplier>> supplier, CompletableFuture<ImageRenderer> future) { + private static void completeImageFactory(ResourceLocation id, Supplier<Optional<ImageRendererFactory.ImageSupplier>> supplier, CompletableFuture<ImageRenderer> future) { RenderSystem.assertOnRenderThread(); ImageRendererFactory.ImageSupplier completableImage = supplier.get().orElse(null); @@ -111,7 +118,7 @@ public class ImageRendererManager { } } - public record PreloadedImageFactory(Predicate<ResourceLocation> predicate, Function<ResourceLocation, ImageRendererFactory> factory) { + public record PreloadedImageFactory(Predicate<ResourceLocation> predicate, BiFunction<Resource, ResourceLocation, ImageRendererFactory> factory) { } private record CompletedSupplier<T>(T get) implements Supplier<T> { diff --git a/common/src/main/java/dev/isxander/yacl3/gui/image/YACLImageReloadListener.java b/common/src/main/java/dev/isxander/yacl3/gui/image/YACLImageReloadListener.java index cc259b0..bac8c56 100644 --- a/common/src/main/java/dev/isxander/yacl3/gui/image/YACLImageReloadListener.java +++ b/common/src/main/java/dev/isxander/yacl3/gui/image/YACLImageReloadListener.java @@ -28,8 +28,9 @@ public class YACLImageReloadListener implements PreparableReloadListener { Executor backgroundExecutor, Executor gameExecutor ) { + YACLConstants.LOGGER.info("YACL is reloading images"); Map<ResourceLocation, Resource> imageResources = resourceManager.listResources( - "", + "textures", location -> ImageRendererManager.PRELOADED_IMAGE_FACTORIES .stream() .anyMatch(factory -> factory.predicate().test(location)) @@ -51,7 +52,7 @@ public class YACLImageReloadListener implements PreparableReloadListener { ImageRendererFactory imageFactory = ImageRendererManager.PRELOADED_IMAGE_FACTORIES .stream() .filter(factory -> factory.predicate().test(location)) - .map(factory -> factory.factory().apply(location)) + .map(factory -> factory.factory().apply(resource, location)) .findAny() .orElseThrow(); @@ -94,6 +95,8 @@ public class YACLImageReloadListener implements PreparableReloadListener { }); } + YACLConstants.LOGGER.info("YACL has found {} images", imageResources.size()); + return CompletableFuture.allOf(futures.toArray(CompletableFuture[]::new)); } } diff --git a/common/src/main/java/dev/isxander/yacl3/gui/image/impl/AnimatedDynamicTextureImage.java b/common/src/main/java/dev/isxander/yacl3/gui/image/impl/AnimatedDynamicTextureImage.java index 39ddb55..2136f56 100644 --- a/common/src/main/java/dev/isxander/yacl3/gui/image/impl/AnimatedDynamicTextureImage.java +++ b/common/src/main/java/dev/isxander/yacl3/gui/image/impl/AnimatedDynamicTextureImage.java @@ -103,6 +103,10 @@ public class AnimatedDynamicTextureImage extends DynamicTextureImage { }; } + public static ImageRendererFactory createGIFFromResource(Resource resource, ResourceLocation resourceLocation) { + return () -> createGIFSupplier(resource.open(), resourceLocation); + } + public static ImageRendererFactory createGIFFromPath(Path path, ResourceLocation uniqueLocation) { return () -> createGIFSupplier(new FileInputStream(path.toFile()), uniqueLocation); } @@ -116,6 +120,10 @@ public class AnimatedDynamicTextureImage extends DynamicTextureImage { }; } + public static ImageRendererFactory createWEBPFromResource(Resource resource, ResourceLocation resourceLocation) { + return () -> createWEBPSupplier(resource.open(), resourceLocation); + } + public static ImageRendererFactory createWEBPFromPath(Path path, ResourceLocation uniqueLocation) { return () -> createWEBPSupplier(new FileInputStream(path.toFile()), uniqueLocation); } |