001    package net.minecraft.world;
002    
003    import cpw.mods.fml.relauncher.Side;
004    import cpw.mods.fml.relauncher.SideOnly;
005    
006    import java.io.File;
007    import java.util.ArrayList;
008    import java.util.HashSet;
009    import java.util.Iterator;
010    import java.util.List;
011    import java.util.Random;
012    import java.util.Set;
013    import java.util.TreeSet;
014    import net.minecraft.block.Block;
015    import net.minecraft.block.BlockEventData;
016    import net.minecraft.crash.CrashReport;
017    import net.minecraft.crash.CrashReportCategory;
018    import net.minecraft.entity.Entity;
019    import net.minecraft.entity.EntityTracker;
020    import net.minecraft.entity.EnumCreatureType;
021    import net.minecraft.entity.INpc;
022    import net.minecraft.entity.effect.EntityLightningBolt;
023    import net.minecraft.entity.passive.EntityAnimal;
024    import net.minecraft.entity.passive.EntityWaterMob;
025    import net.minecraft.entity.player.EntityPlayer;
026    import net.minecraft.entity.player.EntityPlayerMP;
027    import net.minecraft.item.Item;
028    import net.minecraft.network.packet.Packet38EntityStatus;
029    import net.minecraft.network.packet.Packet54PlayNoteBlock;
030    import net.minecraft.network.packet.Packet60Explosion;
031    import net.minecraft.network.packet.Packet70GameEvent;
032    import net.minecraft.network.packet.Packet71Weather;
033    import net.minecraft.profiler.Profiler;
034    import net.minecraft.server.MinecraftServer;
035    import net.minecraft.server.management.PlayerManager;
036    import net.minecraft.tileentity.TileEntity;
037    import net.minecraft.util.ChunkCoordinates;
038    import net.minecraft.util.IProgressUpdate;
039    import net.minecraft.util.IntHashMap;
040    import net.minecraft.util.MathHelper;
041    import net.minecraft.util.ReportedException;
042    import net.minecraft.util.Vec3;
043    import net.minecraft.util.WeightedRandom;
044    import net.minecraft.util.WeightedRandomChestContent;
045    import net.minecraft.world.biome.BiomeGenBase;
046    import net.minecraft.world.biome.SpawnListEntry;
047    import net.minecraft.world.biome.WorldChunkManager;
048    import net.minecraft.world.chunk.Chunk;
049    import net.minecraft.world.chunk.IChunkProvider;
050    import net.minecraft.world.chunk.storage.AnvilChunkLoader;
051    import net.minecraft.world.chunk.storage.ExtendedBlockStorage;
052    import net.minecraft.world.chunk.storage.IChunkLoader;
053    import net.minecraft.world.gen.ChunkProviderServer;
054    import net.minecraft.world.gen.feature.WorldGeneratorBonusChest;
055    import net.minecraft.world.storage.ISaveHandler;
056    
057    import net.minecraftforge.common.ChestGenHooks;
058    import static net.minecraftforge.common.ChestGenHooks.*;
059    import net.minecraftforge.common.DimensionManager;
060    import net.minecraftforge.common.MinecraftForge;
061    import net.minecraftforge.event.world.WorldEvent;
062    
063    public class WorldServer extends World
064    {
065        private final MinecraftServer mcServer;
066        private final EntityTracker theEntityTracker;
067        private final PlayerManager thePlayerManager;
068        private Set field_73064_N;
069    
070        /** All work to do in future ticks. */
071        private TreeSet pendingTickListEntries;
072        public ChunkProviderServer theChunkProviderServer;
073    
074        /** set by CommandServerSave{all,Off,On} */
075        public boolean canNotSave;
076    
077        /** is false if there are no players */
078        private boolean allPlayersSleeping;
079        private int updateEntityTick = 0;
080        private final Teleporter field_85177_Q;
081    
082        /**
083         * Double buffer of ServerBlockEventList[] for holding pending BlockEventData's
084         */
085        private ServerBlockEventList[] blockEventCache = new ServerBlockEventList[] {new ServerBlockEventList((ServerBlockEvent)null), new ServerBlockEventList((ServerBlockEvent)null)};
086    
087        /**
088         * The index into the blockEventCache; either 0, or 1, toggled in sendBlockEventPackets  where all BlockEvent are
089         * applied locally and send to clients.
090         */
091        private int blockEventCacheIndex = 0;
092        public static final WeightedRandomChestContent[] bonusChestContent = new WeightedRandomChestContent[] {new WeightedRandomChestContent(Item.stick.shiftedIndex, 0, 1, 3, 10), new WeightedRandomChestContent(Block.planks.blockID, 0, 1, 3, 10), new WeightedRandomChestContent(Block.wood.blockID, 0, 1, 3, 10), new WeightedRandomChestContent(Item.axeStone.shiftedIndex, 0, 1, 1, 3), new WeightedRandomChestContent(Item.axeWood.shiftedIndex, 0, 1, 1, 5), new WeightedRandomChestContent(Item.pickaxeStone.shiftedIndex, 0, 1, 1, 3), new WeightedRandomChestContent(Item.pickaxeWood.shiftedIndex, 0, 1, 1, 5), new WeightedRandomChestContent(Item.appleRed.shiftedIndex, 0, 2, 3, 5), new WeightedRandomChestContent(Item.bread.shiftedIndex, 0, 2, 3, 3)};
093    
094        /** An IntHashMap of entity IDs (integers) to their Entity objects. */
095        private IntHashMap entityIdMap;
096    
097        /** Stores the recently processed (lighting) chunks */
098        protected Set<ChunkCoordIntPair> doneChunks = new HashSet<ChunkCoordIntPair>();
099        public List<Teleporter> customTeleporters = new ArrayList<Teleporter>();
100    
101        public WorldServer(MinecraftServer par1MinecraftServer, ISaveHandler par2ISaveHandler, String par3Str, int par4, WorldSettings par5WorldSettings, Profiler par6Profiler)
102        {
103            super(par2ISaveHandler, par3Str, par5WorldSettings, WorldProvider.getProviderForDimension(par4), par6Profiler);
104            this.mcServer = par1MinecraftServer;
105            this.theEntityTracker = new EntityTracker(this);
106            this.thePlayerManager = new PlayerManager(this, par1MinecraftServer.getConfigurationManager().getViewDistance());
107    
108            if (this.entityIdMap == null)
109            {
110                this.entityIdMap = new IntHashMap();
111            }
112    
113            if (this.field_73064_N == null)
114            {
115                this.field_73064_N = new HashSet();
116            }
117    
118            if (this.pendingTickListEntries == null)
119            {
120                this.pendingTickListEntries = new TreeSet();
121            }
122    
123            this.field_85177_Q = new Teleporter(this);
124            DimensionManager.setWorld(par4, this);
125        }
126    
127        /**
128         * Runs a single tick for the world
129         */
130        public void tick()
131        {
132            super.tick();
133    
134            if (this.getWorldInfo().isHardcoreModeEnabled() && this.difficultySetting < 3)
135            {
136                this.difficultySetting = 3;
137            }
138    
139            this.provider.worldChunkMgr.cleanupCache();
140    
141            if (this.areAllPlayersAsleep())
142            {
143                boolean var1 = false;
144    
145                if (this.spawnHostileMobs && this.difficultySetting >= 1)
146                {
147                    ;
148                }
149    
150                if (!var1)
151                {
152                    long var2 = this.worldInfo.getWorldTime() + 24000L;
153                    this.worldInfo.setWorldTime(var2 - var2 % 24000L);
154                    this.wakeAllPlayers();
155                }
156            }
157    
158            this.theProfiler.startSection("mobSpawner");
159    
160            if (this.getGameRules().getGameRuleBooleanValue("doMobSpawning"))
161            {
162                SpawnerAnimals.findChunksForSpawning(this, this.spawnHostileMobs, this.spawnPeacefulMobs, this.worldInfo.getWorldTotalTime() % 400L == 0L);
163            }
164    
165            this.theProfiler.endStartSection("chunkSource");
166            this.chunkProvider.unload100OldestChunks();
167            int var4 = this.calculateSkylightSubtracted(1.0F);
168    
169            if (var4 != this.skylightSubtracted)
170            {
171                this.skylightSubtracted = var4;
172            }
173    
174            this.sendAndApplyBlockEvents();
175            this.worldInfo.incrementTotalWorldTime(this.worldInfo.getWorldTotalTime() + 1L);
176            this.worldInfo.setWorldTime(this.worldInfo.getWorldTime() + 1L);
177            this.theProfiler.endStartSection("tickPending");
178            this.tickUpdates(false);
179            this.theProfiler.endStartSection("tickTiles");
180            this.tickBlocksAndAmbiance();
181            this.theProfiler.endStartSection("chunkMap");
182            this.thePlayerManager.updatePlayerInstances();
183            this.theProfiler.endStartSection("village");
184            this.villageCollectionObj.tick();
185            this.villageSiegeObj.tick();
186            this.theProfiler.endStartSection("portalForcer");
187            this.field_85177_Q.func_85189_a(this.getTotalWorldTime());
188            for (Teleporter tele : customTeleporters)
189            {
190                tele.func_85189_a(getTotalWorldTime());
191            }
192            this.theProfiler.endSection();
193            this.sendAndApplyBlockEvents();
194        }
195    
196        /**
197         * only spawns creatures allowed by the chunkProvider
198         */
199        public SpawnListEntry spawnRandomCreature(EnumCreatureType par1EnumCreatureType, int par2, int par3, int par4)
200        {
201            List var5 = this.getChunkProvider().getPossibleCreatures(par1EnumCreatureType, par2, par3, par4);
202            return var5 != null && !var5.isEmpty() ? (SpawnListEntry)WeightedRandom.getRandomItem(this.rand, var5) : null;
203        }
204    
205        /**
206         * Updates the flag that indicates whether or not all players in the world are sleeping.
207         */
208        public void updateAllPlayersSleepingFlag()
209        {
210            this.allPlayersSleeping = !this.playerEntities.isEmpty();
211            Iterator var1 = this.playerEntities.iterator();
212    
213            while (var1.hasNext())
214            {
215                EntityPlayer var2 = (EntityPlayer)var1.next();
216    
217                if (!var2.isPlayerSleeping())
218                {
219                    this.allPlayersSleeping = false;
220                    break;
221                }
222            }
223        }
224    
225        protected void wakeAllPlayers()
226        {
227            this.allPlayersSleeping = false;
228            Iterator var1 = this.playerEntities.iterator();
229    
230            while (var1.hasNext())
231            {
232                EntityPlayer var2 = (EntityPlayer)var1.next();
233    
234                if (var2.isPlayerSleeping())
235                {
236                    var2.wakeUpPlayer(false, false, true);
237                }
238            }
239    
240            this.resetRainAndThunder();
241        }
242    
243        private void resetRainAndThunder()
244        {
245            provider.resetRainAndThunder();
246        }
247    
248        public boolean areAllPlayersAsleep()
249        {
250            if (this.allPlayersSleeping && !this.isRemote)
251            {
252                Iterator var1 = this.playerEntities.iterator();
253                EntityPlayer var2;
254    
255                do
256                {
257                    if (!var1.hasNext())
258                    {
259                        return true;
260                    }
261    
262                    var2 = (EntityPlayer)var1.next();
263                }
264                while (var2.isPlayerFullyAsleep());
265    
266                return false;
267            }
268            else
269            {
270                return false;
271            }
272        }
273    
274        @SideOnly(Side.CLIENT)
275    
276        /**
277         * Sets a new spawn location by finding an uncovered block at a random (x,z) location in the chunk.
278         */
279        public void setSpawnLocation()
280        {
281            if (this.worldInfo.getSpawnY() <= 0)
282            {
283                this.worldInfo.setSpawnY(64);
284            }
285    
286            int var1 = this.worldInfo.getSpawnX();
287            int var2 = this.worldInfo.getSpawnZ();
288            int var3 = 0;
289    
290            while (this.getFirstUncoveredBlock(var1, var2) == 0)
291            {
292                var1 += this.rand.nextInt(8) - this.rand.nextInt(8);
293                var2 += this.rand.nextInt(8) - this.rand.nextInt(8);
294                ++var3;
295    
296                if (var3 == 10000)
297                {
298                    break;
299                }
300            }
301    
302            this.worldInfo.setSpawnX(var1);
303            this.worldInfo.setSpawnZ(var2);
304        }
305    
306        /**
307         * plays random cave ambient sounds and runs updateTick on random blocks within each chunk in the vacinity of a
308         * player
309         */
310        protected void tickBlocksAndAmbiance()
311        {
312            super.tickBlocksAndAmbiance();
313            int var1 = 0;
314            int var2 = 0;
315            Iterator var3 = this.activeChunkSet.iterator();
316    
317            doneChunks.retainAll(activeChunkSet);
318            if (doneChunks.size() == activeChunkSet.size())
319            {
320                doneChunks.clear();
321            }
322    
323            final long time = -System.currentTimeMillis();
324    
325            while (var3.hasNext())
326            {
327                ChunkCoordIntPair var4 = (ChunkCoordIntPair)var3.next();
328                int var5 = var4.chunkXPos * 16;
329                int var6 = var4.chunkZPos * 16;
330                this.theProfiler.startSection("getChunk");
331                Chunk var7 = this.getChunkFromChunkCoords(var4.chunkXPos, var4.chunkZPos);
332                this.moodSoundAndLightCheck(var5, var6, var7);
333                this.theProfiler.endStartSection("tickChunk");
334                if (System.currentTimeMillis() + time <= 4 && doneChunks.add(var4)) { //Limits and evenly distributes the lighting update time
335                    var7.updateSkylight();
336                }
337                this.theProfiler.endStartSection("thunder");
338                int var8;
339                int var9;
340                int var10;
341                int var11;
342    
343                if (provider.canDoLightning(var7) && this.rand.nextInt(100000) == 0 && this.isRaining() && this.isThundering())
344                {
345                    this.updateLCG = this.updateLCG * 3 + 1013904223;
346                    var8 = this.updateLCG >> 2;
347                    var9 = var5 + (var8 & 15);
348                    var10 = var6 + (var8 >> 8 & 15);
349                    var11 = this.getPrecipitationHeight(var9, var10);
350    
351                    if (this.canLightningStrikeAt(var9, var11, var10))
352                    {
353                        this.addWeatherEffect(new EntityLightningBolt(this, (double)var9, (double)var11, (double)var10));
354                    }
355                }
356    
357                this.theProfiler.endStartSection("iceandsnow");
358                int var13;
359    
360                if (provider.canDoRainSnowIce(var7) && this.rand.nextInt(16) == 0)
361                {
362                    this.updateLCG = this.updateLCG * 3 + 1013904223;
363                    var8 = this.updateLCG >> 2;
364                    var9 = var8 & 15;
365                    var10 = var8 >> 8 & 15;
366                    var11 = this.getPrecipitationHeight(var9 + var5, var10 + var6);
367    
368                    if (this.isBlockFreezableNaturally(var9 + var5, var11 - 1, var10 + var6))
369                    {
370                        this.setBlockWithNotify(var9 + var5, var11 - 1, var10 + var6, Block.ice.blockID);
371                    }
372    
373                    if (this.isRaining() && this.canSnowAt(var9 + var5, var11, var10 + var6))
374                    {
375                        this.setBlockWithNotify(var9 + var5, var11, var10 + var6, Block.snow.blockID);
376                    }
377    
378                    if (this.isRaining())
379                    {
380                        BiomeGenBase var12 = this.getBiomeGenForCoords(var9 + var5, var10 + var6);
381    
382                        if (var12.canSpawnLightningBolt())
383                        {
384                            var13 = this.getBlockId(var9 + var5, var11 - 1, var10 + var6);
385    
386                            if (var13 != 0)
387                            {
388                                Block.blocksList[var13].fillWithRain(this, var9 + var5, var11 - 1, var10 + var6);
389                            }
390                        }
391                    }
392                }
393    
394                this.theProfiler.endStartSection("tickTiles");
395                ExtendedBlockStorage[] var19 = var7.getBlockStorageArray();
396                var9 = var19.length;
397    
398                for (var10 = 0; var10 < var9; ++var10)
399                {
400                    ExtendedBlockStorage var21 = var19[var10];
401    
402                    if (var21 != null && var21.getNeedsRandomTick())
403                    {
404                        for (int var20 = 0; var20 < 3; ++var20)
405                        {
406                            this.updateLCG = this.updateLCG * 3 + 1013904223;
407                            var13 = this.updateLCG >> 2;
408                            int var14 = var13 & 15;
409                            int var15 = var13 >> 8 & 15;
410                            int var16 = var13 >> 16 & 15;
411                            int var17 = var21.getExtBlockID(var14, var16, var15);
412                            ++var2;
413                            Block var18 = Block.blocksList[var17];
414    
415                            if (var18 != null && var18.getTickRandomly())
416                            {
417                                ++var1;
418                                var18.updateTick(this, var14 + var5, var16 + var21.getYLocation(), var15 + var6, this.rand);
419                            }
420                        }
421                    }
422                }
423    
424                this.theProfiler.endSection();
425            }
426        }
427    
428        /**
429         * Schedules a tick to a block with a delay (Most commonly the tick rate)
430         */
431        public void scheduleBlockUpdate(int par1, int par2, int par3, int par4, int par5)
432        {
433            this.func_82740_a(par1, par2, par3, par4, par5, 0);
434        }
435    
436        public void func_82740_a(int par1, int par2, int par3, int par4, int par5, int par6)
437        {
438            NextTickListEntry var7 = new NextTickListEntry(par1, par2, par3, par4);
439            boolean isForced = getPersistentChunks().containsKey(new ChunkCoordIntPair(var7.xCoord >> 4, var7.zCoord >> 4));
440            byte var8 = isForced ? (byte)0 : 8;
441    
442            if (this.scheduledUpdatesAreImmediate && par4 > 0)
443            {
444                if (Block.blocksList[par4].func_82506_l())
445                {
446                    if (this.checkChunksExist(var7.xCoord - var8, var7.yCoord - var8, var7.zCoord - var8, var7.xCoord + var8, var7.yCoord + var8, var7.zCoord + var8))
447                    {
448                        int var9 = this.getBlockId(var7.xCoord, var7.yCoord, var7.zCoord);
449    
450                        if (var9 == var7.blockID && var9 > 0)
451                        {
452                            Block.blocksList[var9].updateTick(this, var7.xCoord, var7.yCoord, var7.zCoord, this.rand);
453                        }
454                    }
455    
456                    return;
457                }
458    
459                par5 = 1;
460            }
461    
462            if (this.checkChunksExist(par1 - var8, par2 - var8, par3 - var8, par1 + var8, par2 + var8, par3 + var8))
463            {
464                if (par4 > 0)
465                {
466                    var7.setScheduledTime((long)par5 + this.worldInfo.getWorldTotalTime());
467                    var7.func_82753_a(par6);
468                }
469    
470                if (!this.field_73064_N.contains(var7))
471                {
472                    this.field_73064_N.add(var7);
473                    this.pendingTickListEntries.add(var7);
474                }
475            }
476        }
477    
478        /**
479         * Schedules a block update from the saved information in a chunk. Called when the chunk is loaded.
480         */
481        public void scheduleBlockUpdateFromLoad(int par1, int par2, int par3, int par4, int par5)
482        {
483            NextTickListEntry var6 = new NextTickListEntry(par1, par2, par3, par4);
484    
485            if (par4 > 0)
486            {
487                var6.setScheduledTime((long)par5 + this.worldInfo.getWorldTotalTime());
488            }
489    
490            if (!this.field_73064_N.contains(var6))
491            {
492                this.field_73064_N.add(var6);
493                this.pendingTickListEntries.add(var6);
494            }
495        }
496    
497        /**
498         * Updates (and cleans up) entities and tile entities
499         */
500        public void updateEntities()
501        {
502            if (this.playerEntities.isEmpty() && getPersistentChunks().isEmpty())
503            {
504                if (this.updateEntityTick++ >= 1200)
505                {
506                    return;
507                }
508            }
509            else
510            {
511                this.resetUpdateEntityTick();
512            }
513    
514            super.updateEntities();
515        }
516    
517        /**
518         * Resets the updateEntityTick field to 0
519         */
520        public void resetUpdateEntityTick()
521        {
522            this.updateEntityTick = 0;
523        }
524    
525        /**
526         * Runs through the list of updates to run and ticks them
527         */
528        public boolean tickUpdates(boolean par1)
529        {
530            int var2 = this.pendingTickListEntries.size();
531    
532            if (var2 != this.field_73064_N.size())
533            {
534                throw new IllegalStateException("TickNextTick list out of synch");
535            }
536            else
537            {
538                if (var2 > 1000)
539                {
540                    var2 = 1000;
541                }
542    
543                for (int var3 = 0; var3 < var2; ++var3)
544                {
545                    NextTickListEntry var4 = (NextTickListEntry)this.pendingTickListEntries.first();
546    
547                    if (!par1 && var4.scheduledTime > this.worldInfo.getWorldTotalTime())
548                    {
549                        break;
550                    }
551    
552                    this.pendingTickListEntries.remove(var4);
553                    this.field_73064_N.remove(var4);
554                    boolean isForced = getPersistentChunks().containsKey(new ChunkCoordIntPair(var4.xCoord >> 4, var4.zCoord >> 4));
555                    byte var5 = isForced ? (byte)0 : 8;
556    
557                    if (this.checkChunksExist(var4.xCoord - var5, var4.yCoord - var5, var4.zCoord - var5, var4.xCoord + var5, var4.yCoord + var5, var4.zCoord + var5))
558                    {
559                        int var6 = this.getBlockId(var4.xCoord, var4.yCoord, var4.zCoord);
560    
561                        if (var6 == var4.blockID && var6 > 0)
562                        {
563                            try
564                            {
565                                Block.blocksList[var6].updateTick(this, var4.xCoord, var4.yCoord, var4.zCoord, this.rand);
566                            }
567                            catch (Throwable var13)
568                            {
569                                CrashReport var8 = CrashReport.makeCrashReport(var13, "Exception while ticking a block");
570                                CrashReportCategory var9 = var8.makeCategory("Block being ticked");
571                                int var10;
572    
573                                try
574                                {
575                                    var10 = this.getBlockMetadata(var4.xCoord, var4.yCoord, var4.zCoord);
576                                }
577                                catch (Throwable var12)
578                                {
579                                    var10 = -1;
580                                }
581    
582                                CrashReportCategory.func_85068_a(var9, var4.xCoord, var4.yCoord, var4.zCoord, var6, var10);
583                                throw new ReportedException(var8);
584                            }
585                        }
586                    }
587                }
588    
589                return !this.pendingTickListEntries.isEmpty();
590            }
591        }
592    
593        public List getPendingBlockUpdates(Chunk par1Chunk, boolean par2)
594        {
595            ArrayList var3 = null;
596            ChunkCoordIntPair var4 = par1Chunk.getChunkCoordIntPair();
597            int var5 = var4.chunkXPos << 4;
598            int var6 = var5 + 16;
599            int var7 = var4.chunkZPos << 4;
600            int var8 = var7 + 16;
601            Iterator var9 = this.pendingTickListEntries.iterator();
602    
603            while (var9.hasNext())
604            {
605                NextTickListEntry var10 = (NextTickListEntry)var9.next();
606    
607                if (var10.xCoord >= var5 && var10.xCoord < var6 && var10.zCoord >= var7 && var10.zCoord < var8)
608                {
609                    if (par2)
610                    {
611                        this.field_73064_N.remove(var10);
612                        var9.remove();
613                    }
614    
615                    if (var3 == null)
616                    {
617                        var3 = new ArrayList();
618                    }
619    
620                    var3.add(var10);
621                }
622            }
623    
624            return var3;
625        }
626    
627        /**
628         * Will update the entity in the world if the chunk the entity is in is currently loaded or its forced to update.
629         * Args: entity, forceUpdate
630         */
631        public void updateEntityWithOptionalForce(Entity par1Entity, boolean par2)
632        {
633            if (!this.mcServer.getCanSpawnAnimals() && (par1Entity instanceof EntityAnimal || par1Entity instanceof EntityWaterMob))
634            {
635                par1Entity.setDead();
636            }
637    
638            if (!this.mcServer.getCanSpawnNPCs() && par1Entity instanceof INpc)
639            {
640                par1Entity.setDead();
641            }
642    
643            if (!(par1Entity.riddenByEntity instanceof EntityPlayer))
644            {
645                super.updateEntityWithOptionalForce(par1Entity, par2);
646            }
647        }
648    
649        /**
650         * direct call to super.updateEntityWithOptionalForce
651         */
652        public void uncheckedUpdateEntity(Entity par1Entity, boolean par2)
653        {
654            super.updateEntityWithOptionalForce(par1Entity, par2);
655        }
656    
657        /**
658         * Creates the chunk provider for this world. Called in the constructor. Retrieves provider from worldProvider?
659         */
660        protected IChunkProvider createChunkProvider()
661        {
662            IChunkLoader var1 = this.saveHandler.getChunkLoader(this.provider);
663            this.theChunkProviderServer = new ChunkProviderServer(this, var1, this.provider.createChunkGenerator());
664            return this.theChunkProviderServer;
665        }
666    
667        /**
668         * pars: min x,y,z , max x,y,z
669         */
670        public List getAllTileEntityInBox(int par1, int par2, int par3, int par4, int par5, int par6)
671        {
672            ArrayList var7 = new ArrayList();
673    
674            for(int x = (par1 >> 4); x <= (par4 >> 4); x++)
675            {
676                for(int z = (par3 >> 4); z <= (par6 >> 4); z++)
677                {
678                    Chunk chunk = getChunkFromChunkCoords(x, z);
679                    if (chunk != null)
680                    {
681                        for(Object obj : chunk.chunkTileEntityMap.values())
682                        {
683                            TileEntity entity = (TileEntity)obj;
684                            if (!entity.isInvalid())
685                            {
686                                if (entity.xCoord >= par1 && entity.yCoord >= par2 && entity.zCoord >= par3 &&
687                                    entity.xCoord <= par4 && entity.yCoord <= par5 && entity.zCoord <= par6)
688                                {
689                                    var7.add(entity);
690                                }
691                            }
692                        }
693                    }
694                }
695            }
696            return var7;
697        }
698    
699        /**
700         * Called when checking if a certain block can be mined or not. The 'spawn safe zone' check is located here.
701         */
702        public boolean canMineBlock(EntityPlayer par1EntityPlayer, int par2, int par3, int par4)
703        {
704            return super.canMineBlock(par1EntityPlayer, par2, par3, par4);
705        }
706    
707        public boolean canMineBlockBody(EntityPlayer par1EntityPlayer, int par2, int par3, int par4)
708        {
709            int var5 = MathHelper.abs_int(par2 - this.worldInfo.getSpawnX());
710            int var6 = MathHelper.abs_int(par4 - this.worldInfo.getSpawnZ());
711    
712            if (var5 > var6)
713            {
714                var6 = var5;
715            }
716    
717            return var6 > mcServer.getSpawnProtectionSize() || this.mcServer.getConfigurationManager().areCommandsAllowed(par1EntityPlayer.username) || this.mcServer.isSinglePlayer();
718        }
719    
720        protected void initialize(WorldSettings par1WorldSettings)
721        {
722            if (this.entityIdMap == null)
723            {
724                this.entityIdMap = new IntHashMap();
725            }
726    
727            if (this.field_73064_N == null)
728            {
729                this.field_73064_N = new HashSet();
730            }
731    
732            if (this.pendingTickListEntries == null)
733            {
734                this.pendingTickListEntries = new TreeSet();
735            }
736    
737            this.createSpawnPosition(par1WorldSettings);
738            super.initialize(par1WorldSettings);
739        }
740    
741        /**
742         * creates a spawn position at random within 256 blocks of 0,0
743         */
744        protected void createSpawnPosition(WorldSettings par1WorldSettings)
745        {
746            if (!this.provider.canRespawnHere())
747            {
748                this.worldInfo.setSpawnPosition(0, this.provider.getAverageGroundLevel(), 0);
749            }
750            else
751            {
752                this.findingSpawnPoint = true;
753                WorldChunkManager var2 = this.provider.worldChunkMgr;
754                List var3 = var2.getBiomesToSpawnIn();
755                Random var4 = new Random(this.getSeed());
756                ChunkPosition var5 = var2.findBiomePosition(0, 0, 256, var3, var4);
757                int var6 = 0;
758                int var7 = this.provider.getAverageGroundLevel();
759                int var8 = 0;
760    
761                if (var5 != null)
762                {
763                    var6 = var5.x;
764                    var8 = var5.z;
765                }
766                else
767                {
768                    System.out.println("Unable to find spawn biome");
769                }
770    
771                int var9 = 0;
772    
773                while (!this.provider.canCoordinateBeSpawn(var6, var8))
774                {
775                    var6 += var4.nextInt(64) - var4.nextInt(64);
776                    var8 += var4.nextInt(64) - var4.nextInt(64);
777                    ++var9;
778    
779                    if (var9 == 1000)
780                    {
781                        break;
782                    }
783                }
784    
785                this.worldInfo.setSpawnPosition(var6, var7, var8);
786                this.findingSpawnPoint = false;
787    
788                if (par1WorldSettings.isBonusChestEnabled())
789                {
790                    this.createBonusChest();
791                }
792            }
793        }
794    
795        /**
796         * Creates the bonus chest in the world.
797         */
798        protected void createBonusChest()
799        {
800            WorldGeneratorBonusChest var1 = new WorldGeneratorBonusChest(ChestGenHooks.getItems(BONUS_CHEST, rand), ChestGenHooks.getCount(BONUS_CHEST, rand));
801    
802            for (int var2 = 0; var2 < 10; ++var2)
803            {
804                int var3 = this.worldInfo.getSpawnX() + this.rand.nextInt(6) - this.rand.nextInt(6);
805                int var4 = this.worldInfo.getSpawnZ() + this.rand.nextInt(6) - this.rand.nextInt(6);
806                int var5 = this.getTopSolidOrLiquidBlock(var3, var4) + 1;
807    
808                if (var1.generate(this, this.rand, var3, var5, var4))
809                {
810                    break;
811                }
812            }
813        }
814    
815        /**
816         * Gets the hard-coded portal location to use when entering this dimension.
817         */
818        public ChunkCoordinates getEntrancePortalLocation()
819        {
820            return this.provider.getEntrancePortalLocation();
821        }
822    
823        /**
824         * Saves all chunks to disk while updating progress bar.
825         */
826        public void saveAllChunks(boolean par1, IProgressUpdate par2IProgressUpdate) throws MinecraftException
827        {
828            if (this.chunkProvider.canSave())
829            {
830                if (par2IProgressUpdate != null)
831                {
832                    par2IProgressUpdate.displayProgressMessage("Saving level");
833                }
834    
835                this.saveLevel();
836    
837                if (par2IProgressUpdate != null)
838                {
839                    par2IProgressUpdate.resetProgresAndWorkingMessage("Saving chunks");
840                }
841    
842                this.chunkProvider.saveChunks(par1, par2IProgressUpdate);
843                MinecraftForge.EVENT_BUS.post(new WorldEvent.Save(this));
844            }
845        }
846    
847        /**
848         * Saves the chunks to disk.
849         */
850        protected void saveLevel() throws MinecraftException
851        {
852            this.checkSessionLock();
853            this.saveHandler.saveWorldInfoWithPlayer(this.worldInfo, this.mcServer.getConfigurationManager().getTagsFromLastWrite());
854            this.mapStorage.saveAllData();
855            this.perWorldStorage.saveAllData();
856        }
857    
858        /**
859         * Start the skin for this entity downloading, if necessary, and increment its reference counter
860         */
861        protected void obtainEntitySkin(Entity par1Entity)
862        {
863            super.obtainEntitySkin(par1Entity);
864            this.entityIdMap.addKey(par1Entity.entityId, par1Entity);
865            Entity[] var2 = par1Entity.getParts();
866    
867            if (var2 != null)
868            {
869                for (int var3 = 0; var3 < var2.length; ++var3)
870                {
871                    this.entityIdMap.addKey(var2[var3].entityId, var2[var3]);
872                }
873            }
874        }
875    
876        /**
877         * Decrement the reference counter for this entity's skin image data
878         */
879        protected void releaseEntitySkin(Entity par1Entity)
880        {
881            super.releaseEntitySkin(par1Entity);
882            this.entityIdMap.removeObject(par1Entity.entityId);
883            Entity[] var2 = par1Entity.getParts();
884    
885            if (var2 != null)
886            {
887                for (int var3 = 0; var3 < var2.length; ++var3)
888                {
889                    this.entityIdMap.removeObject(var2[var3].entityId);
890                }
891            }
892        }
893    
894        /**
895         * Returns the Entity with the given ID, or null if it doesn't exist in this World.
896         */
897        public Entity getEntityByID(int par1)
898        {
899            return (Entity)this.entityIdMap.lookup(par1);
900        }
901    
902        /**
903         * adds a lightning bolt to the list of lightning bolts in this world.
904         */
905        public boolean addWeatherEffect(Entity par1Entity)
906        {
907            if (super.addWeatherEffect(par1Entity))
908            {
909                this.mcServer.getConfigurationManager().sendToAllNear(par1Entity.posX, par1Entity.posY, par1Entity.posZ, 512.0D, this.provider.dimensionId, new Packet71Weather(par1Entity));
910                return true;
911            }
912            else
913            {
914                return false;
915            }
916        }
917    
918        /**
919         * sends a Packet 38 (Entity Status) to all tracked players of that entity
920         */
921        public void setEntityState(Entity par1Entity, byte par2)
922        {
923            Packet38EntityStatus var3 = new Packet38EntityStatus(par1Entity.entityId, par2);
924            this.getEntityTracker().sendPacketToAllAssociatedPlayers(par1Entity, var3);
925        }
926    
927        /**
928         * returns a new explosion. Does initiation (at time of writing Explosion is not finished)
929         */
930        public Explosion newExplosion(Entity par1Entity, double par2, double par4, double par6, float par8, boolean par9, boolean par10)
931        {
932            Explosion var11 = new Explosion(this, par1Entity, par2, par4, par6, par8);
933            var11.isFlaming = par9;
934            var11.isSmoking = par10;
935            var11.doExplosionA();
936            var11.doExplosionB(false);
937    
938            if (!par10)
939            {
940                var11.affectedBlockPositions.clear();
941            }
942    
943            Iterator var12 = this.playerEntities.iterator();
944    
945            while (var12.hasNext())
946            {
947                EntityPlayer var13 = (EntityPlayer)var12.next();
948    
949                if (var13.getDistanceSq(par2, par4, par6) < 4096.0D)
950                {
951                    ((EntityPlayerMP)var13).playerNetServerHandler.sendPacketToPlayer(new Packet60Explosion(par2, par4, par6, par8, var11.affectedBlockPositions, (Vec3)var11.func_77277_b().get(var13)));
952                }
953            }
954    
955            return var11;
956        }
957    
958        /**
959         * Adds a block event with the given Args to the blockEventCache. During the next tick(), the block specified will
960         * have its onBlockEvent handler called with the given parameters. Args: X,Y,Z, BlockID, EventID, EventParameter
961         */
962        public void addBlockEvent(int par1, int par2, int par3, int par4, int par5, int par6)
963        {
964            BlockEventData var7 = new BlockEventData(par1, par2, par3, par4, par5, par6);
965            Iterator var8 = this.blockEventCache[this.blockEventCacheIndex].iterator();
966            BlockEventData var9;
967    
968            do
969            {
970                if (!var8.hasNext())
971                {
972                    this.blockEventCache[this.blockEventCacheIndex].add(var7);
973                    return;
974                }
975    
976                var9 = (BlockEventData)var8.next();
977            }
978            while (!var9.equals(var7));
979        }
980    
981        /**
982         * Send and apply locally all pending BlockEvents to each player with 64m radius of the event.
983         */
984        private void sendAndApplyBlockEvents()
985        {
986            while (!this.blockEventCache[this.blockEventCacheIndex].isEmpty())
987            {
988                int var1 = this.blockEventCacheIndex;
989                this.blockEventCacheIndex ^= 1;
990                Iterator var2 = this.blockEventCache[var1].iterator();
991    
992                while (var2.hasNext())
993                {
994                    BlockEventData var3 = (BlockEventData)var2.next();
995    
996                    if (this.onBlockEventReceived(var3))
997                    {
998                        this.mcServer.getConfigurationManager().sendToAllNear((double)var3.getX(), (double)var3.getY(), (double)var3.getZ(), 64.0D, this.provider.dimensionId, new Packet54PlayNoteBlock(var3.getX(), var3.getY(), var3.getZ(), var3.getBlockID(), var3.getEventID(), var3.getEventParameter()));
999                    }
1000                }
1001    
1002                this.blockEventCache[var1].clear();
1003            }
1004        }
1005    
1006        /**
1007         * Called to apply a pending BlockEvent to apply to the current world.
1008         */
1009        private boolean onBlockEventReceived(BlockEventData par1BlockEventData)
1010        {
1011            int var2 = this.getBlockId(par1BlockEventData.getX(), par1BlockEventData.getY(), par1BlockEventData.getZ());
1012    
1013            if (var2 == par1BlockEventData.getBlockID())
1014            {
1015                Block.blocksList[var2].onBlockEventReceived(this, par1BlockEventData.getX(), par1BlockEventData.getY(), par1BlockEventData.getZ(), par1BlockEventData.getEventID(), par1BlockEventData.getEventParameter());
1016                return true;
1017            }
1018            else
1019            {
1020                return false;
1021            }
1022        }
1023    
1024        /**
1025         * Syncs all changes to disk and wait for completion.
1026         */
1027        public void flush()
1028        {
1029            this.saveHandler.flush();
1030        }
1031    
1032        /**
1033         * Updates all weather states.
1034         */
1035        protected void updateWeather()
1036        {
1037            boolean var1 = this.isRaining();
1038            super.updateWeather();
1039    
1040            if (var1 != this.isRaining())
1041            {
1042                if (var1)
1043                {
1044                    this.mcServer.getConfigurationManager().sendPacketToAllPlayers(new Packet70GameEvent(2, 0));
1045                }
1046                else
1047                {
1048                    this.mcServer.getConfigurationManager().sendPacketToAllPlayers(new Packet70GameEvent(1, 0));
1049                }
1050            }
1051        }
1052    
1053        /**
1054         * Gets the MinecraftServer.
1055         */
1056        public MinecraftServer getMinecraftServer()
1057        {
1058            return this.mcServer;
1059        }
1060    
1061        /**
1062         * Gets the EntityTracker
1063         */
1064        public EntityTracker getEntityTracker()
1065        {
1066            return this.theEntityTracker;
1067        }
1068    
1069        public PlayerManager getPlayerManager()
1070        {
1071            return this.thePlayerManager;
1072        }
1073    
1074        public Teleporter func_85176_s()
1075        {
1076            return this.field_85177_Q;
1077        }
1078    
1079        public File getChunkSaveLocation()
1080        {
1081            return ((AnvilChunkLoader)theChunkProviderServer.currentChunkLoader).chunkSaveLocation;
1082        }
1083    }