001package net.minecraft.tileentity;
002
003import cpw.mods.fml.relauncher.Side;
004import cpw.mods.fml.relauncher.SideOnly;
005import java.util.ArrayList;
006import java.util.Iterator;
007import java.util.List;
008import net.minecraft.entity.Entity;
009import net.minecraft.entity.EntityList;
010import net.minecraft.entity.EntityLiving;
011import net.minecraft.nbt.NBTBase;
012import net.minecraft.nbt.NBTTagCompound;
013import net.minecraft.nbt.NBTTagList;
014import net.minecraft.util.AxisAlignedBB;
015import net.minecraft.util.WeightedRandom;
016import net.minecraft.world.World;
017
018public abstract class MobSpawnerBaseLogic
019{
020    public int field_98286_b = 20;
021    private String field_98288_a = "Pig";
022    private List field_98285_e = null;
023    private WeightedRandomMinecart field_98282_f = null;
024    public double field_98287_c;
025    public double field_98284_d = 0.0D;
026    private int field_98283_g = 200;
027    private int field_98293_h = 800;
028    private int field_98294_i = 4;
029    private Entity field_98291_j;
030    private int field_98292_k = 6;
031    private int field_98289_l = 16;
032    private int field_98290_m = 4;
033
034    public String func_98276_e()
035    {
036        if (this.func_98269_i() == null)
037        {
038            if (this.field_98288_a.equals("Minecart"))
039            {
040                this.field_98288_a = "MinecartRideable";
041            }
042
043            return this.field_98288_a;
044        }
045        else
046        {
047            return this.func_98269_i().field_98223_c;
048        }
049    }
050
051    public void func_98272_a(String par1Str)
052    {
053        this.field_98288_a = par1Str;
054    }
055
056    public boolean func_98279_f()
057    {
058        return this.func_98271_a().getClosestPlayer((double)this.func_98275_b() + 0.5D, (double)this.func_98274_c() + 0.5D, (double)this.func_98266_d() + 0.5D, (double)this.field_98289_l) != null;
059    }
060
061    public void func_98278_g()
062    {
063        if (this.func_98279_f())
064        {
065            double d0;
066
067            if (this.func_98271_a().isRemote)
068            {
069                double d1 = (double)((float)this.func_98275_b() + this.func_98271_a().rand.nextFloat());
070                double d2 = (double)((float)this.func_98274_c() + this.func_98271_a().rand.nextFloat());
071                d0 = (double)((float)this.func_98266_d() + this.func_98271_a().rand.nextFloat());
072                this.func_98271_a().spawnParticle("smoke", d1, d2, d0, 0.0D, 0.0D, 0.0D);
073                this.func_98271_a().spawnParticle("flame", d1, d2, d0, 0.0D, 0.0D, 0.0D);
074
075                if (this.field_98286_b > 0)
076                {
077                    --this.field_98286_b;
078                }
079
080                this.field_98284_d = this.field_98287_c;
081                this.field_98287_c = (this.field_98287_c + (double)(1000.0F / ((float)this.field_98286_b + 200.0F))) % 360.0D;
082            }
083            else
084            {
085                if (this.field_98286_b == -1)
086                {
087                    this.func_98273_j();
088                }
089
090                if (this.field_98286_b > 0)
091                {
092                    --this.field_98286_b;
093                    return;
094                }
095
096                boolean flag = false;
097
098                for (int i = 0; i < this.field_98294_i; ++i)
099                {
100                    Entity entity = EntityList.createEntityByName(this.func_98276_e(), this.func_98271_a());
101
102                    if (entity == null)
103                    {
104                        return;
105                    }
106
107                    int j = this.func_98271_a().getEntitiesWithinAABB(entity.getClass(), AxisAlignedBB.getAABBPool().getAABB((double)this.func_98275_b(), (double)this.func_98274_c(), (double)this.func_98266_d(), (double)(this.func_98275_b() + 1), (double)(this.func_98274_c() + 1), (double)(this.func_98266_d() + 1)).expand((double)(this.field_98290_m * 2), 4.0D, (double)(this.field_98290_m * 2))).size();
108
109                    if (j >= this.field_98292_k)
110                    {
111                        this.func_98273_j();
112                        return;
113                    }
114
115                    d0 = (double)this.func_98275_b() + (this.func_98271_a().rand.nextDouble() - this.func_98271_a().rand.nextDouble()) * (double)this.field_98290_m;
116                    double d3 = (double)(this.func_98274_c() + this.func_98271_a().rand.nextInt(3) - 1);
117                    double d4 = (double)this.func_98266_d() + (this.func_98271_a().rand.nextDouble() - this.func_98271_a().rand.nextDouble()) * (double)this.field_98290_m;
118                    EntityLiving entityliving = entity instanceof EntityLiving ? (EntityLiving)entity : null;
119                    entity.setLocationAndAngles(d0, d3, d4, this.func_98271_a().rand.nextFloat() * 360.0F, 0.0F);
120
121                    if (entityliving == null || entityliving.getCanSpawnHere())
122                    {
123                        this.func_98265_a(entity);
124                        this.func_98271_a().playAuxSFX(2004, this.func_98275_b(), this.func_98274_c(), this.func_98266_d(), 0);
125
126                        if (entityliving != null)
127                        {
128                            entityliving.spawnExplosionParticle();
129                        }
130
131                        flag = true;
132                    }
133                }
134
135                if (flag)
136                {
137                    this.func_98273_j();
138                }
139            }
140        }
141    }
142
143    public Entity func_98265_a(Entity par1Entity)
144    {
145        if (this.func_98269_i() != null)
146        {
147            NBTTagCompound nbttagcompound = new NBTTagCompound();
148            par1Entity.addEntityID(nbttagcompound);
149            Iterator iterator = this.func_98269_i().field_98222_b.getTags().iterator();
150
151            while (iterator.hasNext())
152            {
153                NBTBase nbtbase = (NBTBase)iterator.next();
154                nbttagcompound.setTag(nbtbase.getName(), nbtbase.copy());
155            }
156
157            par1Entity.readFromNBT(nbttagcompound);
158
159            if (par1Entity.worldObj != null)
160            {
161                par1Entity.worldObj.spawnEntityInWorld(par1Entity);
162            }
163
164            NBTTagCompound nbttagcompound1;
165
166            for (Entity entity1 = par1Entity; nbttagcompound.hasKey("Riding"); nbttagcompound = nbttagcompound1)
167            {
168                nbttagcompound1 = nbttagcompound.getCompoundTag("Riding");
169                Entity entity2 = EntityList.createEntityByName(nbttagcompound1.getString("id"), this.func_98271_a());
170
171                if (entity2 != null)
172                {
173                    NBTTagCompound nbttagcompound2 = new NBTTagCompound();
174                    entity2.addEntityID(nbttagcompound2);
175                    Iterator iterator1 = nbttagcompound1.getTags().iterator();
176
177                    while (iterator1.hasNext())
178                    {
179                        NBTBase nbtbase1 = (NBTBase)iterator1.next();
180                        nbttagcompound2.setTag(nbtbase1.getName(), nbtbase1.copy());
181                    }
182
183                    entity2.readFromNBT(nbttagcompound2);
184                    entity2.setLocationAndAngles(entity1.posX, entity1.posY, entity1.posZ, entity1.rotationYaw, entity1.rotationPitch);
185                    this.func_98271_a().spawnEntityInWorld(entity2);
186                    entity1.mountEntity(entity2);
187                }
188
189                entity1 = entity2;
190            }
191        }
192        else if (par1Entity instanceof EntityLiving && par1Entity.worldObj != null)
193        {
194            ((EntityLiving)par1Entity).initCreature();
195            this.func_98271_a().spawnEntityInWorld(par1Entity);
196        }
197
198        return par1Entity;
199    }
200
201    private void func_98273_j()
202    {
203        if (this.field_98293_h <= this.field_98283_g)
204        {
205            this.field_98286_b = this.field_98283_g;
206        }
207        else
208        {
209            int i = this.field_98293_h - this.field_98283_g;
210            this.field_98286_b = this.field_98283_g + this.func_98271_a().rand.nextInt(i);
211        }
212
213        if (this.field_98285_e != null && this.field_98285_e.size() > 0)
214        {
215            this.func_98277_a((WeightedRandomMinecart)WeightedRandom.getRandomItem(this.func_98271_a().rand, this.field_98285_e));
216        }
217
218        this.func_98267_a(1);
219    }
220
221    public void func_98270_a(NBTTagCompound par1NBTTagCompound)
222    {
223        this.field_98288_a = par1NBTTagCompound.getString("EntityId");
224        this.field_98286_b = par1NBTTagCompound.getShort("Delay");
225
226        if (par1NBTTagCompound.hasKey("SpawnPotentials"))
227        {
228            this.field_98285_e = new ArrayList();
229            NBTTagList nbttaglist = par1NBTTagCompound.getTagList("SpawnPotentials");
230
231            for (int i = 0; i < nbttaglist.tagCount(); ++i)
232            {
233                this.field_98285_e.add(new WeightedRandomMinecart(this, (NBTTagCompound)nbttaglist.tagAt(i)));
234            }
235        }
236        else
237        {
238            this.field_98285_e = null;
239        }
240
241        if (par1NBTTagCompound.hasKey("SpawnData"))
242        {
243            this.func_98277_a(new WeightedRandomMinecart(this, par1NBTTagCompound.getCompoundTag("SpawnData"), this.field_98288_a));
244        }
245        else
246        {
247            this.func_98277_a((WeightedRandomMinecart)null);
248        }
249
250        if (par1NBTTagCompound.hasKey("MinSpawnDelay"))
251        {
252            this.field_98283_g = par1NBTTagCompound.getShort("MinSpawnDelay");
253            this.field_98293_h = par1NBTTagCompound.getShort("MaxSpawnDelay");
254            this.field_98294_i = par1NBTTagCompound.getShort("SpawnCount");
255        }
256
257        if (par1NBTTagCompound.hasKey("MaxNearbyEntities"))
258        {
259            this.field_98292_k = par1NBTTagCompound.getShort("MaxNearbyEntities");
260            this.field_98289_l = par1NBTTagCompound.getShort("RequiredPlayerRange");
261        }
262
263        if (par1NBTTagCompound.hasKey("SpawnRange"))
264        {
265            this.field_98290_m = par1NBTTagCompound.getShort("SpawnRange");
266        }
267
268        if (this.func_98271_a() != null && this.func_98271_a().isRemote)
269        {
270            this.field_98291_j = null;
271        }
272    }
273
274    public void func_98280_b(NBTTagCompound par1NBTTagCompound)
275    {
276        par1NBTTagCompound.setString("EntityId", this.func_98276_e());
277        par1NBTTagCompound.setShort("Delay", (short)this.field_98286_b);
278        par1NBTTagCompound.setShort("MinSpawnDelay", (short)this.field_98283_g);
279        par1NBTTagCompound.setShort("MaxSpawnDelay", (short)this.field_98293_h);
280        par1NBTTagCompound.setShort("SpawnCount", (short)this.field_98294_i);
281        par1NBTTagCompound.setShort("MaxNearbyEntities", (short)this.field_98292_k);
282        par1NBTTagCompound.setShort("RequiredPlayerRange", (short)this.field_98289_l);
283        par1NBTTagCompound.setShort("SpawnRange", (short)this.field_98290_m);
284
285        if (this.func_98269_i() != null)
286        {
287            par1NBTTagCompound.setCompoundTag("SpawnData", (NBTTagCompound)this.func_98269_i().field_98222_b.copy());
288        }
289
290        if (this.func_98269_i() != null || this.field_98285_e != null && this.field_98285_e.size() > 0)
291        {
292            NBTTagList nbttaglist = new NBTTagList();
293
294            if (this.field_98285_e != null && this.field_98285_e.size() > 0)
295            {
296                Iterator iterator = this.field_98285_e.iterator();
297
298                while (iterator.hasNext())
299                {
300                    WeightedRandomMinecart weightedrandomminecart = (WeightedRandomMinecart)iterator.next();
301                    nbttaglist.appendTag(weightedrandomminecart.func_98220_a());
302                }
303            }
304            else
305            {
306                nbttaglist.appendTag(this.func_98269_i().func_98220_a());
307            }
308
309            par1NBTTagCompound.setTag("SpawnPotentials", nbttaglist);
310        }
311    }
312
313    public boolean func_98268_b(int par1)
314    {
315        if (par1 == 1 && this.func_98271_a().isRemote)
316        {
317            this.field_98286_b = this.field_98283_g;
318            return true;
319        }
320        else
321        {
322            return false;
323        }
324    }
325
326    @SideOnly(Side.CLIENT)
327    public Entity func_98281_h()
328    {
329        if (this.field_98291_j == null)
330        {
331            Entity entity = EntityList.createEntityByName(this.func_98276_e(), (World)null);
332            entity = this.func_98265_a(entity);
333            this.field_98291_j = entity;
334        }
335
336        return this.field_98291_j;
337    }
338
339    public WeightedRandomMinecart func_98269_i()
340    {
341        return this.field_98282_f;
342    }
343
344    public void func_98277_a(WeightedRandomMinecart par1WeightedRandomMinecart)
345    {
346        this.field_98282_f = par1WeightedRandomMinecart;
347    }
348
349    public abstract void func_98267_a(int i);
350
351    public abstract World func_98271_a();
352
353    public abstract int func_98275_b();
354
355    public abstract int func_98274_c();
356
357    public abstract int func_98266_d();
358}