001package net.minecraft.world.gen.feature;
002
003import java.util.Random;
004import net.minecraft.block.Block;
005import net.minecraft.block.material.Material;
006import net.minecraft.item.Item;
007import net.minecraft.item.ItemStack;
008import net.minecraft.tileentity.TileEntityChest;
009import net.minecraft.tileentity.TileEntityMobSpawner;
010import net.minecraft.util.WeightedRandom;
011import net.minecraft.util.WeightedRandomChestContent;
012import net.minecraft.world.World;
013
014import net.minecraftforge.common.ChestGenHooks;
015import net.minecraftforge.common.DungeonHooks;
016import net.minecraftforge.common.MinecraftForge;
017
018public class WorldGenDungeons extends WorldGenerator
019{
020    public boolean generate(World par1World, Random par2Random, int par3, int par4, int par5)
021    {
022        byte b0 = 3;
023        int l = par2Random.nextInt(2) + 2;
024        int i1 = par2Random.nextInt(2) + 2;
025        int j1 = 0;
026        int k1;
027        int l1;
028        int i2;
029
030        for (k1 = par3 - l - 1; k1 <= par3 + l + 1; ++k1)
031        {
032            for (l1 = par4 - 1; l1 <= par4 + b0 + 1; ++l1)
033            {
034                for (i2 = par5 - i1 - 1; i2 <= par5 + i1 + 1; ++i2)
035                {
036                    Material material = par1World.getBlockMaterial(k1, l1, i2);
037
038                    if (l1 == par4 - 1 && !material.isSolid())
039                    {
040                        return false;
041                    }
042
043                    if (l1 == par4 + b0 + 1 && !material.isSolid())
044                    {
045                        return false;
046                    }
047
048                    if ((k1 == par3 - l - 1 || k1 == par3 + l + 1 || i2 == par5 - i1 - 1 || i2 == par5 + i1 + 1) && l1 == par4 && par1World.isAirBlock(k1, l1, i2) && par1World.isAirBlock(k1, l1 + 1, i2))
049                    {
050                        ++j1;
051                    }
052                }
053            }
054        }
055
056        if (j1 >= 1 && j1 <= 5)
057        {
058            for (k1 = par3 - l - 1; k1 <= par3 + l + 1; ++k1)
059            {
060                for (l1 = par4 + b0; l1 >= par4 - 1; --l1)
061                {
062                    for (i2 = par5 - i1 - 1; i2 <= par5 + i1 + 1; ++i2)
063                    {
064                        if (k1 != par3 - l - 1 && l1 != par4 - 1 && i2 != par5 - i1 - 1 && k1 != par3 + l + 1 && l1 != par4 + b0 + 1 && i2 != par5 + i1 + 1)
065                        {
066                            par1World.setBlockToAir(k1, l1, i2);
067                        }
068                        else if (l1 >= 0 && !par1World.getBlockMaterial(k1, l1 - 1, i2).isSolid())
069                        {
070                            par1World.setBlockToAir(k1, l1, i2);
071                        }
072                        else if (par1World.getBlockMaterial(k1, l1, i2).isSolid())
073                        {
074                            if (l1 == par4 - 1 && par2Random.nextInt(4) != 0)
075                            {
076                                par1World.setBlock(k1, l1, i2, Block.cobblestoneMossy.blockID, 0, 2);
077                            }
078                            else
079                            {
080                                par1World.setBlock(k1, l1, i2, Block.cobblestone.blockID, 0, 2);
081                            }
082                        }
083                    }
084                }
085            }
086
087            k1 = 0;
088
089            while (k1 < 2)
090            {
091                l1 = 0;
092
093                while (true)
094                {
095                    if (l1 < 3)
096                    {
097                        label210:
098                        {
099                            i2 = par3 + par2Random.nextInt(l * 2 + 1) - l;
100                            int j2 = par5 + par2Random.nextInt(i1 * 2 + 1) - i1;
101
102                            if (par1World.isAirBlock(i2, par4, j2))
103                            {
104                                int k2 = 0;
105
106                                if (par1World.getBlockMaterial(i2 - 1, par4, j2).isSolid())
107                                {
108                                    ++k2;
109                                }
110
111                                if (par1World.getBlockMaterial(i2 + 1, par4, j2).isSolid())
112                                {
113                                    ++k2;
114                                }
115
116                                if (par1World.getBlockMaterial(i2, par4, j2 - 1).isSolid())
117                                {
118                                    ++k2;
119                                }
120
121                                if (par1World.getBlockMaterial(i2, par4, j2 + 1).isSolid())
122                                {
123                                    ++k2;
124                                }
125
126                                if (k2 == 1)
127                                {
128                                    par1World.setBlock(i2, par4, j2, Block.chest.blockID, 0, 2);
129                                    TileEntityChest tileentitychest = (TileEntityChest)par1World.getBlockTileEntity(i2, par4, j2);
130
131                                    if (tileentitychest != null)
132                                    {
133                                        ChestGenHooks info = ChestGenHooks.getInfo(ChestGenHooks.DUNGEON_CHEST);
134                                        WeightedRandomChestContent.generateChestContents(par2Random, info.getItems(par2Random), tileentitychest, info.getCount(par2Random));
135                                    }
136
137                                    break label210;
138                                }
139                            }
140
141                            ++l1;
142                            continue;
143                        }
144                    }
145
146                    ++k1;
147                    break;
148                }
149            }
150
151            par1World.setBlock(par3, par4, par5, Block.mobSpawner.blockID, 0, 2);
152            TileEntityMobSpawner tileentitymobspawner = (TileEntityMobSpawner)par1World.getBlockTileEntity(par3, par4, par5);
153
154            if (tileentitymobspawner != null)
155            {
156                tileentitymobspawner.func_98049_a().setMobID(this.pickMobSpawner(par2Random));
157            }
158            else
159            {
160                System.err.println("Failed to fetch mob spawner entity at (" + par3 + ", " + par4 + ", " + par5 + ")");
161            }
162
163            return true;
164        }
165        else
166        {
167            return false;
168        }
169    }
170
171    /**
172     * Picks potentially a random item to add to a dungeon chest.
173     */
174    private ItemStack pickCheckLootItem(Random par1Random)
175    {
176        return ChestGenHooks.getOneItem(ChestGenHooks.DUNGEON_CHEST, par1Random);
177    }
178
179    /**
180     * Randomly decides which spawner to use in a dungeon
181     */
182    private String pickMobSpawner(Random par1Random)
183    {
184        return DungeonHooks.getRandomDungeonMob(par1Random);
185    }
186}