4242@ DebugDump
4343public class TextureGallery {
4444
45- private final Map <ResourcePath <Texture >, Integer > ordinalMap ;
45+ private final Map <ResourcePath <Texture >, TextureMapping > textureMappings ;
4646 private int nextId ;
4747
4848 public TextureGallery () {
49- this .ordinalMap = new HashMap <>();
49+ this .textureMappings = new HashMap <>();
5050 this .nextId = 0 ;
5151 }
5252
5353 public void clear () {
54- this .ordinalMap .clear ();
54+ this .textureMappings .clear ();
5555 this .nextId = 0 ;
5656 }
5757
5858 public int get (@ Nullable ResourcePath <Texture > textureResourcePath ) {
5959 if (textureResourcePath == null ) textureResourcePath = ResourcePack .MISSING_TEXTURE ;
60- Integer ordinal = ordinalMap .get (textureResourcePath );
61- return ordinal != null ? ordinal : 0 ;
60+ TextureMapping mapping = textureMappings .get (textureResourcePath );
61+ return mapping != null ? mapping . getId () : 0 ;
6262 }
6363
64- public synchronized int put (ResourcePath <Texture > textureResourcePath ) {
65- Integer ordinal = ordinalMap .putIfAbsent (textureResourcePath , nextId );
66- if (ordinal == null ) return nextId ++;
67- return ordinal ;
64+ public synchronized void put (ResourcePath <Texture > textureResourcePath ) {
65+ textureMappings .compute (textureResourcePath , (r , mapping ) -> {
66+ if (mapping == null )
67+ return new TextureMapping (nextId ++, textureResourcePath .getResource ());
68+
69+ Texture texture = textureResourcePath .getResource ();
70+ if (texture != null ) mapping .setTexture (texture );
71+ return mapping ;
72+ });
6873 }
6974
7075 public synchronized void put (ResourcePack resourcePack ) {
76+ this .put (ResourcePack .MISSING_TEXTURE ); // put this first
7177 resourcePack .getTextures ().keySet ()
7278 .stream ()
7379 .sorted (Comparator .comparing (Key ::getFormatted ))
7480 .forEach (this ::put );
7581 }
7682
77- public void writeTexturesFile (ResourcePack resourcePack , OutputStream out ) throws IOException {
83+ public void writeTexturesFile (OutputStream out ) throws IOException {
7884 Texture [] textures = new Texture [nextId ];
7985 Arrays .fill (textures , Texture .MISSING );
8086
81- ordinalMap .forEach ((textureResourcePath , ordinal ) -> {
82- Texture texture = textureResourcePath .getResource (resourcePack ::getTexture );
83- if (texture != null ) textures [ordinal ] = texture ;
84-
85- // make sure the resource-path doesn't get lost
86- if (textures [ordinal ].getResourcePath ().equals (ResourcePack .MISSING_TEXTURE ))
87- textures [ordinal ] = Texture .missing (textureResourcePath );
87+ this .textureMappings .forEach ((textureResourcePath , mapping ) -> {
88+ int ordinal = mapping .getId ();
89+ Texture texture = mapping .getTexture ();
90+ if (texture == null ) texture = Texture .missing (textureResourcePath );
91+ textures [ordinal ] = texture ;
8892 });
8993
9094 try (Writer writer = new OutputStreamWriter (out )) {
@@ -103,7 +107,7 @@ public static TextureGallery readTexturesFile(InputStream in) throws IOException
103107 for (int ordinal = 0 ; ordinal < textures .length ; ordinal ++) {
104108 Texture texture = textures [ordinal ];
105109 if (texture != null ) {
106- gallery .ordinalMap .put (texture .getResourcePath (), ordinal );
110+ gallery .textureMappings .put (texture .getResourcePath (), new TextureMapping ( ordinal , texture ) );
107111 }
108112 }
109113 } catch (JsonIOException ex ) {
@@ -112,4 +116,27 @@ public static TextureGallery readTexturesFile(InputStream in) throws IOException
112116 return gallery ;
113117 }
114118
119+ static class TextureMapping {
120+ private final int id ;
121+ private @ Nullable Texture texture ;
122+
123+ public TextureMapping (int id , @ Nullable Texture texture ) {
124+ this .id = id ;
125+ this .texture = texture ;
126+ }
127+
128+ public int getId () {
129+ return id ;
130+ }
131+
132+ public @ Nullable Texture getTexture () {
133+ return texture ;
134+ }
135+
136+ public void setTexture (@ Nullable Texture texture ) {
137+ this .texture = texture ;
138+ }
139+
140+ }
141+
115142}
0 commit comments