001package net.minecraft.util;
002
003import java.util.ArrayList;
004import java.util.Iterator;
005import java.util.List;
006import net.minecraft.block.Block;
007import net.minecraft.entity.Entity;
008import net.minecraft.entity.EntityLiving;
009import net.minecraft.entity.player.EntityPlayer;
010import net.minecraft.item.ItemStack;
011
012public class CombatTracker
013{
014    private final List field_94556_a = new ArrayList();
015    private final EntityLiving field_94554_b;
016    private int field_94555_c = 0;
017    private boolean field_94552_d = false;
018    private boolean field_94553_e = false;
019    private String field_94551_f;
020
021    public CombatTracker(EntityLiving par1EntityLiving)
022    {
023        this.field_94554_b = par1EntityLiving;
024    }
025
026    public void func_94545_a()
027    {
028        this.func_94542_g();
029
030        if (this.field_94554_b.isOnLadder())
031        {
032            int i = this.field_94554_b.worldObj.getBlockId(MathHelper.floor_double(this.field_94554_b.posX), MathHelper.floor_double(this.field_94554_b.boundingBox.minY), MathHelper.floor_double(this.field_94554_b.posZ));
033
034            if (i == Block.ladder.blockID)
035            {
036                this.field_94551_f = "ladder";
037            }
038            else if (i == Block.vine.blockID)
039            {
040                this.field_94551_f = "vines";
041            }
042        }
043        else if (this.field_94554_b.isInWater())
044        {
045            this.field_94551_f = "water";
046        }
047    }
048
049    public void func_94547_a(DamageSource par1DamageSource, int par2, int par3)
050    {
051        this.func_94549_h();
052        this.func_94545_a();
053        CombatEntry combatentry = new CombatEntry(par1DamageSource, this.field_94554_b.ticksExisted, par2, par3, this.field_94551_f, this.field_94554_b.fallDistance);
054        this.field_94556_a.add(combatentry);
055        this.field_94555_c = this.field_94554_b.ticksExisted;
056        this.field_94553_e = true;
057        this.field_94552_d |= combatentry.func_94559_f();
058    }
059
060    public String func_94546_b()
061    {
062        if (this.field_94556_a.size() == 0)
063        {
064            return this.field_94554_b.func_96090_ax() + " died";
065        }
066        else
067        {
068            CombatEntry combatentry = this.func_94544_f();
069            CombatEntry combatentry1 = (CombatEntry)this.field_94556_a.get(this.field_94556_a.size() - 1);
070            String s = "";
071            String s1 = combatentry1.func_94558_h();
072            Entity entity = combatentry1.func_94560_a().getEntity();
073
074            if (combatentry != null && combatentry1.func_94560_a() == DamageSource.fall)
075            {
076                String s2 = combatentry.func_94558_h();
077
078                if (combatentry.func_94560_a() != DamageSource.fall && combatentry.func_94560_a() != DamageSource.outOfWorld)
079                {
080                    if (s2 != null && (s1 == null || !s2.equals(s1)))
081                    {
082                        Entity entity1 = combatentry.func_94560_a().getEntity();
083                        ItemStack itemstack = entity1 instanceof EntityLiving ? ((EntityLiving)entity1).getHeldItem() : null;
084
085                        if (itemstack != null && itemstack.hasDisplayName())
086                        {
087                            s = StatCollector.translateToLocalFormatted("death.fell.assist.item", new Object[] {this.field_94554_b.func_96090_ax(), s1, itemstack.getDisplayName()});
088                        }
089                        else
090                        {
091                            s = StatCollector.translateToLocalFormatted("death.fell.assist", new Object[] {this.field_94554_b.func_96090_ax(), s2});
092                        }
093                    }
094                    else if (s1 != null)
095                    {
096                        ItemStack itemstack1 = entity instanceof EntityLiving ? ((EntityLiving)entity).getHeldItem() : null;
097
098                        if (itemstack1 != null && itemstack1.hasDisplayName())
099                        {
100                            s = StatCollector.translateToLocalFormatted("death.fell.finish.item", new Object[] {this.field_94554_b.func_96090_ax(), s1, itemstack1.getDisplayName()});
101                        }
102                        else
103                        {
104                            s = StatCollector.translateToLocalFormatted("death.fell.finish", new Object[] {this.field_94554_b.func_96090_ax(), s1});
105                        }
106                    }
107                    else
108                    {
109                        s = StatCollector.translateToLocalFormatted("death.fell.killer", new Object[] {this.field_94554_b.func_96090_ax()});
110                    }
111                }
112                else
113                {
114                    s = StatCollector.translateToLocalFormatted("death.fell.accident." + this.func_94548_b(combatentry), new Object[] {this.field_94554_b.func_96090_ax()});
115                }
116            }
117            else
118            {
119                s = combatentry1.func_94560_a().getDeathMessage(this.field_94554_b);
120            }
121
122            return s;
123        }
124    }
125
126    public EntityLiving func_94550_c()
127    {
128        EntityLiving entityliving = null;
129        EntityPlayer entityplayer = null;
130        int i = 0;
131        int j = 0;
132        Iterator iterator = this.field_94556_a.iterator();
133
134        while (iterator.hasNext())
135        {
136            CombatEntry combatentry = (CombatEntry)iterator.next();
137
138            if (combatentry.func_94560_a().getEntity() instanceof EntityPlayer && (entityplayer == null || combatentry.func_94563_c() > j))
139            {
140                j = combatentry.func_94563_c();
141                entityplayer = (EntityPlayer)combatentry.func_94560_a().getEntity();
142            }
143
144            if (combatentry.func_94560_a().getEntity() instanceof EntityLiving && (entityliving == null || combatentry.func_94563_c() > i))
145            {
146                i = combatentry.func_94563_c();
147                entityliving = (EntityLiving)combatentry.func_94560_a().getEntity();
148            }
149        }
150
151        if (entityplayer != null && j >= i / 3)
152        {
153            return entityplayer;
154        }
155        else
156        {
157            return entityliving;
158        }
159    }
160
161    private CombatEntry func_94544_f()
162    {
163        CombatEntry combatentry = null;
164        CombatEntry combatentry1 = null;
165        byte b0 = 0;
166        float f = 0.0F;
167
168        for (int i = 0; i < this.field_94556_a.size(); ++i)
169        {
170            CombatEntry combatentry2 = (CombatEntry)this.field_94556_a.get(i);
171            CombatEntry combatentry3 = i > 0 ? (CombatEntry)this.field_94556_a.get(i - 1) : null;
172
173            if ((combatentry2.func_94560_a() == DamageSource.fall || combatentry2.func_94560_a() == DamageSource.outOfWorld) && combatentry2.func_94561_i() > 0.0F && (combatentry == null || combatentry2.func_94561_i() > f))
174            {
175                if (i > 0)
176                {
177                    combatentry = combatentry3;
178                }
179                else
180                {
181                    combatentry = combatentry2;
182                }
183
184                f = combatentry2.func_94561_i();
185            }
186
187            if (combatentry2.func_94562_g() != null && (combatentry1 == null || combatentry2.func_94563_c() > b0))
188            {
189                combatentry1 = combatentry2;
190            }
191        }
192
193        if (f > 5.0F && combatentry != null)
194        {
195            return combatentry;
196        }
197        else if (b0 > 5 && combatentry1 != null)
198        {
199            return combatentry1;
200        }
201        else
202        {
203            return null;
204        }
205    }
206
207    private String func_94548_b(CombatEntry par1CombatEntry)
208    {
209        return par1CombatEntry.func_94562_g() == null ? "generic" : par1CombatEntry.func_94562_g();
210    }
211
212    private void func_94542_g()
213    {
214        this.field_94551_f = null;
215    }
216
217    private void func_94549_h()
218    {
219        int i = this.field_94552_d ? 300 : 100;
220
221        if (this.field_94553_e && this.field_94554_b.ticksExisted - this.field_94555_c > i)
222        {
223            this.field_94556_a.clear();
224            this.field_94553_e = false;
225            this.field_94552_d = false;
226        }
227    }
228}