Skip to content

Commit

Permalink
Do not hold on to AbstractTexture objects across resource reloads.
Browse files Browse the repository at this point in the history
  • Loading branch information
coderbot16 committed Nov 29, 2021
1 parent 6a231af commit 5850322
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions src/main/java/net/coderbot/iris/pipeline/CustomTextureManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.texture.AbstractTexture;
import net.minecraft.client.renderer.texture.MissingTextureAtlasSprite;
import net.minecraft.client.renderer.texture.TextureManager;
import net.minecraft.resources.ResourceLocation;

import java.io.IOException;
import java.util.ArrayList;
Expand Down Expand Up @@ -81,11 +83,20 @@ private IntSupplier createCustomTexture(CustomTextureData textureData) throws IO

return texture::getId;
} else if (textureData instanceof CustomTextureData.ResourceData) {
AbstractTexture texture = Minecraft.getInstance().getTextureManager()
.getTexture(((CustomTextureData.ResourceData) textureData).getResourceLocation());

// TODO: Should we give something else if the texture isn't there? This will need some thought
return texture != null ? texture::getId : MissingTextureAtlasSprite.getTexture()::getId;
ResourceLocation textureLocation = ((CustomTextureData.ResourceData) textureData).getResourceLocation();
TextureManager textureManager = Minecraft.getInstance().getTextureManager();

// NB: We have to re-query the TextureManager for the texture object every time. This is because the
// AbstractTexture object could be removed / deleted from the TextureManager on resource reloads,
// and we could end up holding on to a deleted texture unless we added special code to handle resource
// reloads. Re-fetching the texture from the TextureManager every time is the most robust approach for
// now.
return () -> {
AbstractTexture texture = textureManager.getTexture(textureLocation);

// TODO: Should we give something else if the texture isn't there? This will need some thought
return texture != null ? texture.getId() : MissingTextureAtlasSprite.getTexture().getId();
};
} else {
throw new IllegalArgumentException("Unable to handle custom texture data " + textureData);
}
Expand Down

0 comments on commit 5850322

Please sign in to comment.