    001package net.minecraft.client.renderer.texture;
    002
    003import cpw.mods.fml.relauncher.Side;
    004import cpw.mods.fml.relauncher.SideOnly;
    005import java.util.ArrayList;
    006import java.util.Arrays;
    007import java.util.HashSet;
    008import java.util.Iterator;
    009import java.util.List;
    010import java.util.Set;
    011import net.minecraft.client.renderer.StitcherException;
    012
    013@SideOnly(Side.CLIENT)
    014public class Stitcher
    015{
    016    private final Set field_94319_a;
    017    private final List field_94317_b;
    018    private int field_94318_c;
    019    private int field_94315_d;
    020    private final int field_94316_e;
    021    private final int field_94313_f;
    022    private final boolean field_94314_g;
    023    private final int field_94323_h;
    024    private Texture field_94322_k;
    025    private final String field_94320_l;
    026
    027    public Stitcher(String par1Str, int par2, int par3, boolean par4)
    028    {
    029        this(par1Str, par2, par3, par4, 0);
    030    }
    031
    032    public Stitcher(String par1, int par2, int par3, boolean par4, int par5)
    033    {
    034        this.field_94319_a = new HashSet(256);
    035        this.field_94317_b = new ArrayList(256);
    036        this.field_94318_c = 0;
    037        this.field_94315_d = 0;
    038        this.field_94320_l = par1;
    039        this.field_94316_e = par2;
    040        this.field_94313_f = par3;
    041        this.field_94314_g = par4;
    042        this.field_94323_h = par5;
    043    }
    044
    045    public void func_94312_a(StitchHolder par1StitchHolder)
    046    {
    047        if (this.field_94323_h > 0)
    048        {
    049            par1StitchHolder.func_94196_a(this.field_94323_h);
    050        }
    051
    052        this.field_94319_a.add(par1StitchHolder);
    053    }
    054
    055    public Texture func_94306_e()
    056    {
    057        if (this.field_94314_g)
    058        {
    059            this.field_94318_c = this.func_94308_a(this.field_94318_c);
    060            this.field_94315_d = this.func_94308_a(this.field_94315_d);
    061        }
    062
    063        this.field_94322_k = TextureManager.func_94267_b().func_98145_a(this.field_94320_l, 1, this.field_94318_c, this.field_94315_d, 6408);
    064        this.field_94322_k.func_94272_a(this.field_94322_k.func_94274_a(), -65536);
    065        List list = this.func_94309_g();
    066
    067        for (int i = 0; i < list.size(); ++i)
    068        {
    069            StitchSlot stitchslot = (StitchSlot)list.get(i);
    070            StitchHolder stitchholder = stitchslot.func_94183_a();
    071            this.field_94322_k.func_94281_a(stitchslot.func_94186_b(), stitchslot.func_94185_c(), stitchholder.func_98150_a(), stitchholder.func_94195_e());
    072        }
    073
    074        TextureManager.func_94267_b().func_94264_a(this.field_94320_l, this.field_94322_k);
    075        return this.field_94322_k;
    076    }
    077
    078    public void func_94305_f()
    079    {
    080        StitchHolder[] astitchholder = (StitchHolder[])this.field_94319_a.toArray(new StitchHolder[this.field_94319_a.size()]);
    081        Arrays.sort(astitchholder);
    082        this.field_94322_k = null;
    083
    084        for (int i = 0; i < astitchholder.length; ++i)
    085        {
    086            StitchHolder stitchholder = astitchholder[i];
    087
    088            if (!this.func_94310_b(stitchholder))
    089            {
    090                throw new StitcherException(stitchholder);
    091            }
    092        }
    093    }
    094
    095    public List func_94309_g()
    096    {
    097        ArrayList arraylist = new ArrayList();
    098        Iterator iterator = this.field_94317_b.iterator();
    099
    100        while (iterator.hasNext())
    101        {
    102            StitchSlot stitchslot = (StitchSlot)iterator.next();
    103            stitchslot.func_94184_a(arraylist);
    104        }
    105
    106        return arraylist;
    107    }
    108
    109    private int func_94308_a(int par1)
    110    {
    111        int j = par1 - 1;
    112        j |= j >> 1;
    113        j |= j >> 2;
    114        j |= j >> 4;
    115        j |= j >> 8;
    116        j |= j >> 16;
    117        return j + 1;
    118    }
    119
    120    private boolean func_94310_b(StitchHolder par1StitchHolder)
    121    {
    122        for (int i = 0; i < this.field_94317_b.size(); ++i)
    123        {
    124            if (((StitchSlot)this.field_94317_b.get(i)).func_94182_a(par1StitchHolder))
    125            {
    126                return true;
    127            }
    128
    129            par1StitchHolder.func_94194_d();
    130
    131            if (((StitchSlot)this.field_94317_b.get(i)).func_94182_a(par1StitchHolder))
    132            {
    133                return true;
    134            }
    135
    136            par1StitchHolder.func_94194_d();
    137        }
    138
    139        return this.func_94311_c(par1StitchHolder);
    140    }
    141
    142    private boolean func_94311_c(StitchHolder par1StitchHolder)
    143    {
    144        int i = Math.min(par1StitchHolder.func_94199_b(), par1StitchHolder.func_94197_a());
    145        boolean flag = this.field_94318_c == 0 && this.field_94315_d == 0;
    146        boolean flag1;
    147
    148        if (this.field_94314_g)
    149        {
    150            int j = this.func_94308_a(this.field_94318_c);
    151            int k = this.func_94308_a(this.field_94315_d);
    152            int l = this.func_94308_a(this.field_94318_c + i);
    153            int i1 = this.func_94308_a(this.field_94315_d + i);
    154            boolean flag2 = l <= this.field_94316_e;
    155            boolean flag3 = i1 <= this.field_94313_f;
    156
    157            if (!flag2 && !flag3)
    158            {
    159                return false;
    160            }
    161
    162            int j1 = Math.max(par1StitchHolder.func_94199_b(), par1StitchHolder.func_94197_a());
    163
    164            if (flag && !flag2 && this.func_94308_a(this.field_94315_d + j1) > this.field_94313_f)
    165            {
    166                return false;
    167            }
    168
    169            boolean flag4 = j != l;
    170            boolean flag5 = k != i1;
    171
    172            if (flag4 ^ flag5)
    173            {
    174                flag1 = flag4 && flag2;
    175            }
    176            else
    177            {
    178                flag1 = flag2 && j <= k;
    179            }
    180        }
    181        else
    182        {
    183            boolean flag6 = this.field_94318_c + i <= this.field_94316_e;
    184            boolean flag7 = this.field_94315_d + i <= this.field_94313_f;
    185
    186            if (!flag6 && !flag7)
    187            {
    188                return false;
    189            }
    190
    191            flag1 = (flag || this.field_94318_c <= this.field_94315_d) && flag6;
    192        }
    193
    194        StitchSlot stitchslot;
    195
    196        if (flag1)
    197        {
    198            if (par1StitchHolder.func_94197_a() > par1StitchHolder.func_94199_b())
    199            {
    200                par1StitchHolder.func_94194_d();
    201            }
    202
    203            if (this.field_94315_d == 0)
    204            {
    205                this.field_94315_d = par1StitchHolder.func_94199_b();
    206            }
    207
    208            stitchslot = new StitchSlot(this.field_94318_c, 0, par1StitchHolder.func_94197_a(), this.field_94315_d);
    209            this.field_94318_c += par1StitchHolder.func_94197_a();
    210        }
    211        else
    212        {
    213            stitchslot = new StitchSlot(0, this.field_94315_d, this.field_94318_c, par1StitchHolder.func_94199_b());
    214            this.field_94315_d += par1StitchHolder.func_94199_b();
    215        }
    216
    217        stitchslot.func_94182_a(par1StitchHolder);
    218        this.field_94317_b.add(stitchslot);
    219        return true;
    220    }
    221}