Skip to content

Commit

Permalink
Copy CCGuiTextures into a generic util class.
Browse files Browse the repository at this point in the history
  • Loading branch information
covers1624 committed Jul 3, 2024
1 parent 54bca18 commit d0dedce
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* <p>
* 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<String, Material> MATERIAL_CACHE = new HashMap<>();
Expand Down Expand Up @@ -56,4 +57,4 @@ public static Supplier<Material> getter(Supplier<String> texture) {
public static Material getUncached(String texture) {
return new Material(ATLAS_HOLDER.atlasLocation(), new ResourceLocation(CodeChickenLib.MOD_ID, "gui/" + texture), ATLAS_HOLDER::getSprite);
}
}
}
88 changes: 88 additions & 0 deletions src/main/java/codechicken/lib/gui/modular/sprite/GuiTextures.java
Original file line number Diff line number Diff line change
@@ -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/"
* <p>
* Created by brandon3055 on 21/10/2023
*/
public class GuiTextures {

private final String modId;
private final @Nullable ModAtlasHolder atlasHolder;
private final Map<String, Material> 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<String> texture) {
return get(texture.get());
}

public Supplier<Material> getter(Supplier<String> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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);
}

Expand Down Expand Up @@ -77,6 +87,7 @@ public void close() {
}

public static class ModResourceManager implements ResourceManager {

private final ResourceManager wrapped;
private final String modid;

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/codechicken/lib/internal/ClientInit.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down

0 comments on commit d0dedce

Please sign in to comment.