Skip to content

Commit 5a147e6

Browse files
committed
Finalise the image reloader
1 parent 5c110c8 commit 5a147e6

File tree

11 files changed

+42
-13
lines changed

11 files changed

+42
-13
lines changed

common/src/main/java/dev/isxander/yacl3/gui/image/ImageRendererManager.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@
33
import com.mojang.blaze3d.systems.RenderSystem;
44
import dev.isxander.yacl3.gui.image.impl.AnimatedDynamicTextureImage;
55
import dev.isxander.yacl3.impl.utils.YACLConstants;
6+
import dev.isxander.yacl3.platform.YACLPlatform;
67
import net.minecraft.client.Minecraft;
78
import net.minecraft.resources.ResourceLocation;
9+
import net.minecraft.server.packs.resources.Resource;
810

911
import java.util.List;
1012
import java.util.Map;
1113
import java.util.Optional;
1214
import java.util.concurrent.*;
13-
import java.util.function.Function;
15+
import java.util.function.BiFunction;
1416
import java.util.function.Predicate;
1517
import java.util.function.Supplier;
1618

@@ -23,11 +25,11 @@ public class ImageRendererManager {
2325
static final List<PreloadedImageFactory> PRELOADED_IMAGE_FACTORIES = List.of(
2426
new PreloadedImageFactory(
2527
location -> location.getPath().endsWith(".webp"),
26-
AnimatedDynamicTextureImage::createWEBPFromTexture
28+
AnimatedDynamicTextureImage::createWEBPFromResource
2729
),
2830
new PreloadedImageFactory(
2931
location -> location.getPath().endsWith(".gif"),
30-
AnimatedDynamicTextureImage::createGIFFromTexture
32+
AnimatedDynamicTextureImage::createGIFFromResource
3133
)
3234
);
3335

@@ -37,6 +39,13 @@ public static <T extends ImageRenderer> Optional<T> getImage(ResourceLocation id
3739
}
3840

3941
if (IMAGE_CACHE.containsKey(id)) {
42+
// warn developers if they don't put their webp/gif images `/textures` folder
43+
if (YACLPlatform.isDevelopmentEnv()) {
44+
if (PRELOADED_IMAGE_FACTORIES.stream().anyMatch(factory -> factory.predicate().test(id))) {
45+
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);
46+
}
47+
}
48+
4049
return Optional.ofNullable((T) IMAGE_CACHE.get(id).getNow(null));
4150
}
4251

@@ -45,8 +54,6 @@ public static <T extends ImageRenderer> Optional<T> getImage(ResourceLocation id
4554

4655
@SuppressWarnings("unchecked")
4756
public static <T extends ImageRenderer> CompletableFuture<T> registerImage(ResourceLocation id, ImageRendererFactory factory) {
48-
System.out.println(PRELOADED_IMAGE_CACHE.get(id));
49-
5057
if (IMAGE_CACHE.containsKey(id)) {
5158
return (CompletableFuture<T>) IMAGE_CACHE.get(id);
5259
}
@@ -66,7 +73,7 @@ public static <T extends ImageRenderer> CompletableFuture<T> registerImage(Resou
6673
return (CompletableFuture<T>) future;
6774
}
6875

69-
private static <T extends ImageRenderer> void completeImageFactory(ResourceLocation id, Supplier<Optional<ImageRendererFactory.ImageSupplier>> supplier, CompletableFuture<ImageRenderer> future) {
76+
private static void completeImageFactory(ResourceLocation id, Supplier<Optional<ImageRendererFactory.ImageSupplier>> supplier, CompletableFuture<ImageRenderer> future) {
7077
RenderSystem.assertOnRenderThread();
7178

7279
ImageRendererFactory.ImageSupplier completableImage = supplier.get().orElse(null);
@@ -111,7 +118,7 @@ static Optional<ImageRendererFactory.ImageSupplier> safelyPrepareFactory(Resourc
111118
}
112119
}
113120

114-
public record PreloadedImageFactory(Predicate<ResourceLocation> predicate, Function<ResourceLocation, ImageRendererFactory> factory) {
121+
public record PreloadedImageFactory(Predicate<ResourceLocation> predicate, BiFunction<Resource, ResourceLocation, ImageRendererFactory> factory) {
115122
}
116123

117124
private record CompletedSupplier<T>(T get) implements Supplier<T> {

common/src/main/java/dev/isxander/yacl3/gui/image/YACLImageReloadListener.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ public CompletableFuture<Void> reload(
2828
Executor backgroundExecutor,
2929
Executor gameExecutor
3030
) {
31+
YACLConstants.LOGGER.info("YACL is reloading images");
3132
Map<ResourceLocation, Resource> imageResources = resourceManager.listResources(
32-
"",
33+
"textures",
3334
location -> ImageRendererManager.PRELOADED_IMAGE_FACTORIES
3435
.stream()
3536
.anyMatch(factory -> factory.predicate().test(location))
@@ -51,7 +52,7 @@ public CompletableFuture<Void> reload(
5152
ImageRendererFactory imageFactory = ImageRendererManager.PRELOADED_IMAGE_FACTORIES
5253
.stream()
5354
.filter(factory -> factory.predicate().test(location))
54-
.map(factory -> factory.factory().apply(location))
55+
.map(factory -> factory.factory().apply(resource, location))
5556
.findAny()
5657
.orElseThrow();
5758

@@ -94,6 +95,8 @@ public CompletableFuture<Void> reload(
9495
});
9596
}
9697

98+
YACLConstants.LOGGER.info("YACL has found {} images", imageResources.size());
99+
97100
return CompletableFuture.allOf(futures.toArray(CompletableFuture[]::new));
98101
}
99102
}

common/src/main/java/dev/isxander/yacl3/gui/image/impl/AnimatedDynamicTextureImage.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,10 @@ public static ImageRendererFactory createGIFFromTexture(ResourceLocation texture
103103
};
104104
}
105105

106+
public static ImageRendererFactory createGIFFromResource(Resource resource, ResourceLocation resourceLocation) {
107+
return () -> createGIFSupplier(resource.open(), resourceLocation);
108+
}
109+
106110
public static ImageRendererFactory createGIFFromPath(Path path, ResourceLocation uniqueLocation) {
107111
return () -> createGIFSupplier(new FileInputStream(path.toFile()), uniqueLocation);
108112
}
@@ -116,6 +120,10 @@ public static ImageRendererFactory createWEBPFromTexture(ResourceLocation textur
116120
};
117121
}
118122

123+
public static ImageRendererFactory createWEBPFromResource(Resource resource, ResourceLocation resourceLocation) {
124+
return () -> createWEBPSupplier(resource.open(), resourceLocation);
125+
}
126+
119127
public static ImageRendererFactory createWEBPFromPath(Path path, ResourceLocation uniqueLocation) {
120128
return () -> createWEBPSupplier(new FileInputStream(path.toFile()), uniqueLocation);
121129
}

neoforge/src/main/java/dev/isxander/yacl3/platform/neoforge/YACLForgeEntrypoint.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
public class YACLForgeEntrypoint {
1212
public YACLForgeEntrypoint(IEventBus modEventBus) {
1313
modEventBus.addListener(RegisterClientReloadListenersEvent.class, event -> {
14-
System.out.println("image reload event");
1514
event.registerReloadListener(new YACLImageReloadListener());
1615
});
1716
}

test-common/src/main/java/dev/isxander/yacl3/test/GuiTest.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ private static Screen getFullTestSuite(Screen parent) {
8383
.append(Component.literal("e").withStyle(style -> style.withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, Component.literal("e")))))
8484
.withStyle(style -> style.withClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, "https://isxander.dev")))
8585
)
86-
.webpImage(new ResourceLocation("yacl3", "reach-around-placement.webp"))
86+
.webpImage(new ResourceLocation("yacl3", "textures/reach-around-placement.webp"))
8787
.build())
8888
.binding(
8989
defaults.booleanToggle,
@@ -100,7 +100,7 @@ private static Screen getFullTestSuite(Screen parent) {
100100
.name(Component.literal("Custom Boolean Toggle"))
101101
.description(val -> OptionDescription.createBuilder()
102102
.text(Component.literal("You can customize controllers like so! YACL is truly infinitely customizable! This tooltip is long in order to demonstrate the cool, smooth scrolling of these descriptions. Did you know, they are also super clickable?! I know, cool right, YACL 3.x really is amazing."))
103-
.image(Path.of("D:\\Xander\\Downloads\\_MG_0860-Enhanced-NR.png"), new ResourceLocation("yacl", "f.webp")) // TODO: Add img file to git?
103+
.webpImage(new ResourceLocation("yacl3", "textures/sample-1.webp"))
104104
.build())
105105
.binding(
106106
defaults.customBooleanToggle,
@@ -114,7 +114,10 @@ private static Screen getFullTestSuite(Screen parent) {
114114
.build())
115115
.option(Option.<Boolean>createBuilder()
116116
.name(Component.literal("Tick Box"))
117-
.description(OptionDescription.of(Component.literal("There are even alternate methods of displaying the same data type!")))
117+
.description(OptionDescription.createBuilder()
118+
.text(Component.literal("There are even alternate methods of displaying the same data type!"))
119+
.webpImage(new ResourceLocation("yacl3", "textures/sample-2.webp"))
120+
.build())
118121
.binding(
119122
defaults.tickbox,
120123
() -> config.tickbox,
@@ -127,6 +130,9 @@ private static Screen getFullTestSuite(Screen parent) {
127130
.name(Component.literal("Slider Controllers"))
128131
.option(Option.<Integer>createBuilder()
129132
.name(Component.literal("Int Slider"))
133+
.description(OptionDescription.createBuilder()
134+
.webpImage(new ResourceLocation("yacl3", "textures/sample-3.webp"))
135+
.build())
130136
.binding(
131137
defaults.intSlider,
132138
() -> config.intSlider,
@@ -136,6 +142,9 @@ private static Screen getFullTestSuite(Screen parent) {
136142
.build())
137143
.option(Option.<Double>createBuilder()
138144
.name(Component.literal("Double Slider"))
145+
.description(OptionDescription.createBuilder()
146+
.webpImage(new ResourceLocation("yacl3", "textures/sample-4.webp"))
147+
.build())
139148
.binding(
140149
defaults.doubleSlider,
141150
() -> config.doubleSlider,
@@ -145,6 +154,9 @@ private static Screen getFullTestSuite(Screen parent) {
145154
.build())
146155
.option(Option.<Float>createBuilder()
147156
.name(Component.literal("Float Slider"))
157+
.description(OptionDescription.createBuilder()
158+
.webpImage(new ResourceLocation("yacl3", "textures/sample-5.webp"))
159+
.build())
148160
.binding(
149161
defaults.floatSlider,
150162
() -> config.floatSlider,

test-common/src/main/resources/assets/yacl3/reach-around-placement.webp renamed to test-common/src/main/resources/assets/yacl3/textures/reach-around-placement.webp

File renamed without changes.
10.2 KB
Loading
21.8 KB
Loading
16.7 KB
Loading
20.3 KB
Loading

0 commit comments

Comments
 (0)