001package net.minecraft.client.renderer.texture;
002
003import cpw.mods.fml.relauncher.Side;
004import cpw.mods.fml.relauncher.SideOnly;
005import java.awt.image.BufferedImage;
006import java.io.BufferedReader;
007import java.io.IOException;
008import java.io.InputStream;
009import java.io.InputStreamReader;
010import java.util.ArrayList;
011import java.util.Arrays;
012import java.util.HashMap;
013import java.util.Iterator;
014import java.util.List;
015import java.util.Map;
016import net.minecraft.block.Block;
017import net.minecraft.client.Minecraft;
018import net.minecraft.client.renderer.StitcherException;
019import net.minecraft.client.renderer.entity.RenderManager;
020import net.minecraft.client.texturepacks.ITexturePack;
021import net.minecraft.item.Item;
022import net.minecraft.util.Icon;
023import net.minecraftforge.client.ForgeHooksClient;
024import net.minecraftforge.common.ForgeDummyContainer;
025
026@SideOnly(Side.CLIENT)
027public class TextureMap implements IconRegister
028{
029    public final int field_94255_a;
030    public final String field_94253_b;
031    public final String field_94254_c;
032    public final String field_94251_d;
033    private final HashMap field_94252_e = new HashMap();
034    private BufferedImage field_94249_f = new BufferedImage(64, 64, 2);
035    private TextureStitched field_94250_g;
036    private Texture field_94257_h;
037    private final List field_94258_i = new ArrayList();
038    private final Map field_94256_j = new HashMap();
039
040    public TextureMap(int par1, String par2, String par3Str, BufferedImage par4BufferedImage)
041    {
042        this.field_94255_a = par1;
043        this.field_94253_b = par2;
044        this.field_94254_c = par3Str;
045        this.field_94251_d = ".png";
046        this.field_94249_f = par4BufferedImage;
047    }
048
049    public void func_94247_b()
050    {
051        this.field_94256_j.clear();
052        ForgeHooksClient.onTextureStitchedPre(this);
053        int i;
054        int j;
055
056        if (this.field_94255_a == 0)
057        {
058            Block[] ablock = Block.blocksList;
059            i = ablock.length;
060
061            for (j = 0; j < i; ++j)
062            {
063                Block block = ablock[j];
064
065                if (block != null)
066                {
067                    block.func_94332_a(this);
068                }
069            }
070
071            Minecraft.getMinecraft().renderGlobal.func_94140_a(this);
072            RenderManager.instance.func_94178_a(this);
073        }
074
075        Item[] aitem = Item.itemsList;
076        i = aitem.length;
077
078        for (j = 0; j < i; ++j)
079        {
080            Item item = aitem[j];
081
082            if (item != null && item.func_94901_k() == this.field_94255_a)
083            {
084                item.func_94581_a(this);
085            }
086        }
087
088        HashMap hashmap = new HashMap();
089        Stitcher stitcher = TextureManager.func_94267_b().func_94262_d(this.field_94253_b);
090        this.field_94252_e.clear();
091        this.field_94258_i.clear();
092        Texture texture = TextureManager.func_94267_b().func_94261_a("missingno", 2, this.field_94249_f.getWidth(), this.field_94249_f.getHeight(), 10496, 6408, 9728, 9728, false, this.field_94249_f);
093        StitchHolder stitchholder = new StitchHolder(texture);
094        stitcher.func_94312_a(stitchholder);
095        hashmap.put(stitchholder, Arrays.asList(new Texture[] {texture}));
096
097        for (Map.Entry<String, TextureStitched> entry : ((Map<String, TextureStitched>)field_94256_j).entrySet())
098        {
099            String name = entry.getKey();
100            String path;
101            if (name.indexOf(':') == -1)
102            {
103                path = this.field_94254_c + name + this.field_94251_d;
104            }
105            else
106            {
107                String domain = name.substring(0,name.indexOf(':'));
108                String file = name.substring(name.indexOf(':')+1);
109                path = "mods/" + domain +"/" + field_94254_c + file + field_94251_d;
110            }
111            List list = TextureManager.func_94267_b().createNewTexture(name, path, entry.getValue());
112
113            if (!list.isEmpty())
114            {
115                StitchHolder stitchholder1 = new StitchHolder((Texture)list.get(0));
116                stitcher.func_94312_a(stitchholder1);
117                hashmap.put(stitchholder1, list);
118            }
119        }
120
121        try
122        {
123            stitcher.func_94305_f();
124        }
125        catch (StitcherException stitcherexception)
126        {
127            throw stitcherexception;
128        }
129
130        this.field_94257_h = stitcher.func_94306_e();
131        Iterator iterator = stitcher.func_94309_g().iterator();
132
133        while (iterator.hasNext())
134        {
135            StitchSlot stitchslot = (StitchSlot)iterator.next();
136            StitchHolder stitchholder2 = stitchslot.func_94183_a();
137            Texture texture1 = stitchholder2.func_98150_a();
138            String s2 = texture1.func_94280_f();
139            List list1 = (List)hashmap.get(stitchholder2);
140            TextureStitched texturestitched = (TextureStitched)this.field_94256_j.get(s2);
141            boolean flag = false;
142
143            if (texturestitched == null)
144            {
145                flag = true;
146                texturestitched = TextureStitched.func_94220_a(s2);
147
148                if (!s2.equals("missingno"))
149                {
150                    Minecraft.getMinecraft().func_98033_al().func_98236_b("Couldn\'t find premade icon for " + s2 + " doing " + this.field_94253_b);
151                }
152            }
153
154            texturestitched.func_94218_a(this.field_94257_h, list1, stitchslot.func_94186_b(), stitchslot.func_94185_c(), stitchholder2.func_98150_a().func_94275_d(), stitchholder2.func_98150_a().func_94276_e(), stitchholder2.func_94195_e());
155            this.field_94252_e.put(s2, texturestitched);
156
157            if (!flag)
158            {
159                this.field_94256_j.remove(s2);
160            }
161
162            if (list1.size() > 1)
163            {
164                this.field_94258_i.add(texturestitched);
165                String s3;
166                if (s2.indexOf(':') == -1)
167                {
168                    s3 = field_94254_c + s2 + ".txt";
169                }
170                else
171                {
172                    String domain = s2.substring(0, s2.indexOf(':'));
173                    String file = s2.substring(s2.indexOf(':') + 1);
174                    s3 = "mods/" + domain + "/" + field_94254_c + file + ".txt";
175                }
176                ITexturePack itexturepack = Minecraft.getMinecraft().texturePackList.getSelectedTexturePack();
177                boolean flag1 = !itexturepack.func_98138_b("/" + this.field_94254_c + s2 + ".png", false);
178
179                try
180                {
181                    InputStream inputstream = itexturepack.func_98137_a("/" + s3, flag1);
182                    Minecraft.getMinecraft().func_98033_al().func_98233_a("Found animation info for: " + s3);
183                    texturestitched.func_94221_a(new BufferedReader(new InputStreamReader(inputstream)));
184                }
185                catch (IOException ioexception)
186                {
187                    ;
188                }
189            }
190        }
191
192        this.field_94250_g = (TextureStitched)this.field_94252_e.get("missingno");
193        iterator = this.field_94256_j.values().iterator();
194
195        while (iterator.hasNext())
196        {
197            TextureStitched texturestitched1 = (TextureStitched)iterator.next();
198            texturestitched1.func_94217_a(this.field_94250_g);
199        }
200
201        if (!ForgeDummyContainer.disableStitchedFileSaving)
202        {
203            this.field_94257_h.func_94279_c("debug.stitched_" + this.field_94253_b + ".png");
204        }
205        ForgeHooksClient.onTextureStitchedPost(this);
206        this.field_94257_h.func_94285_g();
207    }
208
209    public void func_94248_c()
210    {
211        Iterator iterator = this.field_94258_i.iterator();
212
213        while (iterator.hasNext())
214        {
215            TextureStitched texturestitched = (TextureStitched)iterator.next();
216            texturestitched.func_94219_l();
217        }
218    }
219
220    public Texture func_94246_d()
221    {
222        return this.field_94257_h;
223    }
224
225    public Icon func_94245_a(String par1Str)
226    {
227        if (par1Str == null)
228        {
229            (new RuntimeException("Don\'t register null!")).printStackTrace();
230        }
231
232        TextureStitched texturestitched = (TextureStitched)this.field_94256_j.get(par1Str);
233
234        if (texturestitched == null)
235        {
236            texturestitched = TextureStitched.func_94220_a(par1Str);
237            this.field_94256_j.put(par1Str, texturestitched);
238        }
239
240        return texturestitched;
241    }
242
243    public Icon func_96455_e()
244    {
245        return this.field_94250_g;
246    }
247
248    //===================================================================================================
249    //                                           Forge Start
250    //===================================================================================================
251    /**
252     * Grabs the registered entry for the specified name, returning null if there was not a entry. 
253     * Opposed to func_94245_a, this will not instantiate the entry, useful to test if a maping exists.
254     * 
255     * @param name The name of the entry to find
256     * @return The registered entry, null if nothing was registered.
257     */
258    public TextureStitched getTextureExtry(String name)
259    {
260        return (TextureStitched)field_94256_j.get(name);
261    }
262
263    /**
264     * Adds a texture registry entry to this map for the specified name if one does not already exist.
265     * Returns false if the map already contains a entry for the specified name. 
266     * 
267     * @param name Entry name
268     * @param entry Entry instance
269     * @return True if the entry was added to the map, false otherwise.
270     */
271    public boolean setTextureEntry(String name, TextureStitched entry)
272    {
273        if (!field_94256_j.containsKey(name))
274        {
275            field_94256_j.put(name, entry);
276            return true;
277        }
278        return false;
279    }
280}