diff --git a/src/main/java/codechicken/lib/gui/modular/sprite/CCGuiTextures.java b/src/main/java/codechicken/lib/gui/modular/sprite/CCGuiTextures.java
index a3aaf28d..a59ec2bf 100644
--- a/src/main/java/codechicken/lib/gui/modular/sprite/CCGuiTextures.java
+++ b/src/main/java/codechicken/lib/gui/modular/sprite/CCGuiTextures.java
@@ -14,6 +14,7 @@
*
* Created by brandon3055 on 21/10/2023
*/
+// TODO 1.20.4, use GuiTextures instance.
public class CCGuiTextures {
private static final ModAtlasHolder ATLAS_HOLDER = new ModAtlasHolder(CodeChickenLib.MOD_ID, "textures/atlas/gui.png", "gui");
private static final Map MATERIAL_CACHE = new HashMap<>();
@@ -56,4 +57,4 @@ public static Supplier getter(Supplier texture) {
public static Material getUncached(String texture) {
return new Material(ATLAS_HOLDER.atlasLocation(), new ResourceLocation(CodeChickenLib.MOD_ID, "gui/" + texture), ATLAS_HOLDER::getSprite);
}
-}
\ No newline at end of file
+}
diff --git a/src/main/java/codechicken/lib/gui/modular/sprite/GuiTextures.java b/src/main/java/codechicken/lib/gui/modular/sprite/GuiTextures.java
new file mode 100644
index 00000000..a21cef63
--- /dev/null
+++ b/src/main/java/codechicken/lib/gui/modular/sprite/GuiTextures.java
@@ -0,0 +1,88 @@
+package codechicken.lib.gui.modular.sprite;
+
+import net.minecraft.resources.ResourceLocation;
+import net.minecraftforge.client.event.RegisterClientReloadListenersEvent;
+import net.minecraftforge.data.loading.DatagenModLoader;
+import net.minecraftforge.eventbus.api.IEventBus;
+
+import javax.annotation.Nullable;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.function.Supplier;
+
+import static java.util.Objects.requireNonNull;
+
+/**
+ * Gui texture handler implementation.
+ * This sets up a custom atlas that will be populated with all textures in "modid:textures/gui/"
+ *
+ * Created by brandon3055 on 21/10/2023
+ */
+public class GuiTextures {
+
+ private final String modId;
+ private final @Nullable ModAtlasHolder atlasHolder;
+ private final Map materialCache = new HashMap<>();
+
+ public GuiTextures(String modId) {
+ this.modId = modId;
+ if (DatagenModLoader.isRunningDataGen()) {
+ atlasHolder = null;
+ } else {
+ atlasHolder = new ModAtlasHolder(modId, "textures/atlas/gui.png", "gui");
+ }
+ }
+
+ /**
+ * Called to initialize the {@link GuiTextures} helper
+ *
+ * @param bus Your mod event bus.
+ */
+ public void init(IEventBus bus) {
+ bus.addListener(this::onRegisterReloadListeners);
+ }
+
+ private void onRegisterReloadListeners(RegisterClientReloadListenersEvent event) {
+ getAtlasHolder().init();
+ event.registerReloadListener(getAtlasHolder());
+ }
+
+ /**
+ * @return The underlying {@link ModAtlasHolder} instance.
+ */
+ public ModAtlasHolder getAtlasHolder() {
+ return requireNonNull(atlasHolder, "AtlasHolder not available when datagen is running.");
+ }
+
+ /**
+ * Returns a cached Material for the specified gui texture.
+ * Warning: Do not use this if you intend to use the material with multiple render types.
+ * The material will cache the first render type it is used with.
+ * Instead use {@link #getUncached(String)}
+ *
+ * @param texture The texture path relative to "modid:gui/"
+ */
+ public Material get(String texture) {
+ return materialCache.computeIfAbsent(modId + ":" + texture, e -> getUncached(texture));
+ }
+
+ public Material get(Supplier texture) {
+ return get(texture.get());
+ }
+
+ public Supplier getter(Supplier texture) {
+ return () -> get(texture.get());
+ }
+
+ /**
+ * Use this to retrieve a new uncached material for the specified gui texture.
+ * Feel free to hold onto the returned material.
+ * Storing it somewhere is more efficient than recreating it every render frame.
+ *
+ * @param texture The texture path relative to "modid:gui/"
+ * @return A new Material for the specified gui texture.
+ */
+ public Material getUncached(String texture) {
+ return new Material(getAtlasHolder().atlasLocation(), new ResourceLocation(modId, "gui/" + texture), getAtlasHolder()::getSprite);
+ }
+}
diff --git a/src/main/java/codechicken/lib/gui/modular/sprite/ModAtlasHolder.java b/src/main/java/codechicken/lib/gui/modular/sprite/ModAtlasHolder.java
index 9d361894..d8260d6b 100644
--- a/src/main/java/codechicken/lib/gui/modular/sprite/ModAtlasHolder.java
+++ b/src/main/java/codechicken/lib/gui/modular/sprite/ModAtlasHolder.java
@@ -4,6 +4,7 @@
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.PackResources;
import net.minecraft.server.packs.resources.PreparableReloadListener;
@@ -22,6 +23,7 @@
* Created by brandon3055 on 20/08/2023
*/
public class ModAtlasHolder implements PreparableReloadListener, AutoCloseable {
+
private final TextureAtlas textureAtlas;
private final ResourceLocation atlasLocation;
private final ResourceLocation atlasInfoLocation;
@@ -42,6 +44,14 @@ public ModAtlasHolder(String modid, String atlasLocation, String atlasInfoLocati
this.atlasLocation = new ResourceLocation(modid, atlasLocation);
this.textureAtlas = new TextureAtlas(this.atlasLocation);
this.modid = modid;
+ // TODO remove in 1.20.4
+ TextureManager textureManager = Minecraft.getInstance().getTextureManager();
+ if (textureManager != null) {
+ textureManager.register(this.textureAtlas.location(), this.textureAtlas);
+ }
+ }
+
+ public void init() {
Minecraft.getInstance().getTextureManager().register(this.textureAtlas.location(), this.textureAtlas);
}
@@ -77,6 +87,7 @@ public void close() {
}
public static class ModResourceManager implements ResourceManager {
+
private final ResourceManager wrapped;
private final String modid;
diff --git a/src/main/java/codechicken/lib/internal/ClientInit.java b/src/main/java/codechicken/lib/internal/ClientInit.java
index 1e1eb056..5190476d 100644
--- a/src/main/java/codechicken/lib/internal/ClientInit.java
+++ b/src/main/java/codechicken/lib/internal/ClientInit.java
@@ -82,7 +82,7 @@ private static void onRegisterGeometryLoaders(ModelEvent.RegisterGeometryLoaders
event.register("class", new ClassModelLoader());
}
- public static void onResourceReload(RegisterClientReloadListenersEvent event) {
+ private static void onResourceReload(RegisterClientReloadListenersEvent event) {
event.registerReloadListener(CCGuiTextures.getAtlasHolder());
event.registerReloadListener((ResourceManagerReloadListener) e -> CursorHelper.onResourceReload());
}