11package alexiil .mc .mod .load .render ;
22
3- import java .util .HashSet ;
4- import java .util .Set ;
3+ import java .util .ArrayList ;
4+ import java .util .HashMap ;
5+ import java .util .List ;
6+ import java .util .Map ;
57
68import net .minecraft .client .renderer .texture .ITextureObject ;
79import net .minecraft .client .renderer .texture .ITickableTextureObject ;
810import net .minecraft .client .renderer .texture .TextureManager ;
911import net .minecraft .client .resources .IResourceManager ;
1012import net .minecraft .util .ResourceLocation ;
1113
14+ import alexiil .mc .mod .load .CLSLog ;
15+ import alexiil .mc .mod .load .CustomLoadingScreen ;
16+
1217public class TextureManagerCLS extends TextureManager {
1318
14- private final Set <ResourceLocation > textures = new HashSet <>();
19+ /** Map of texture to last access tick. */
20+ private final Map <ResourceLocation , Long > textures = new HashMap <>();
21+ // If we're forced to put a full object into the map value, we may as well intern them by only creating them once
22+ private Long currentTime = System .currentTimeMillis ();
1523
1624 public TextureManagerCLS (IResourceManager resourceManager ) {
1725 super (resourceManager );
1826 }
1927
28+ private void onTextureAccess (ResourceLocation resource ) {
29+ textures .put (resource , currentTime );
30+ }
31+
2032 @ Override
2133 public void bindTexture (ResourceLocation resource ) {
2234 super .bindTexture (resource );
23- textures .add (resource );
35+ onTextureAccess (resource );
36+ }
37+
38+ @ Override
39+ public ITextureObject getTexture (ResourceLocation resource ) {
40+ ITextureObject obj = super .getTexture (resource );
41+ if (obj != null ) {
42+ onTextureAccess (resource );
43+ }
44+ return obj ;
2445 }
2546
2647 @ Override
@@ -31,19 +52,51 @@ public void deleteTexture(ResourceLocation textureLocation) {
3152
3253 @ Override
3354 public boolean loadTexture (ResourceLocation textureLocation , ITextureObject textureObj ) {
34- textures . add (textureLocation );
55+ onTextureAccess (textureLocation );
3556 return super .loadTexture (textureLocation , textureObj );
3657 }
3758
3859 @ Override
3960 public boolean loadTickableTexture (ResourceLocation textureLocation , ITickableTextureObject textureObj ) {
40- textures . add (textureLocation );
61+ onTextureAccess (textureLocation );
4162 return super .loadTickableTexture (textureLocation , textureObj );
4263 }
4364
4465 public void deleteAll () {
45- for (ResourceLocation location : textures .toArray (new ResourceLocation [0 ])) {
66+ for (ResourceLocation location : textures .keySet (). toArray (new ResourceLocation [0 ])) {
4667 deleteTexture (location );
4768 }
4869 }
70+
71+ public void onFrame () {
72+ if (CustomLoadingScreen .textureClearInterval == 0 ) {
73+ return ;
74+ }
75+
76+ Long last = currentTime ;
77+ long next = System .currentTimeMillis ();
78+
79+ if (last + 1000 < next ) {
80+ return ;
81+ }
82+
83+ currentTime = next ;
84+
85+ long minTime = currentTime - (CustomLoadingScreen .textureClearInterval * 1000 );
86+
87+ List <ResourceLocation > toRemove = new ArrayList <>();
88+
89+ for (Map .Entry <ResourceLocation , Long > entry : textures .entrySet ()) {
90+ if (entry .getValue () < minTime ) {
91+ toRemove .add (entry .getKey ());
92+ }
93+ }
94+
95+ for (ResourceLocation tex : toRemove ) {
96+ if (CustomLoadingScreen .debugResourceLoading ) {
97+ CLSLog .info ("[debug] Automatically deleting texture " + tex );
98+ }
99+ deleteTexture (tex );
100+ }
101+ }
49102}
0 commit comments