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