001package net.minecraft.world.gen.structure;
002
003import java.util.List;
004import java.util.Random;
005import net.minecraft.block.Block;
006import net.minecraft.item.Item;
007import net.minecraft.tileentity.TileEntityMobSpawner;
008import net.minecraft.util.WeightedRandomChestContent;
009import net.minecraft.world.World;
010
011import net.minecraftforge.common.ChestGenHooks;
012import static net.minecraftforge.common.ChestGenHooks.*;
013
014public class ComponentMineshaftCorridor extends StructureComponent
015{
016    private final boolean hasRails;
017    private final boolean hasSpiders;
018    private boolean spawnerPlaced;
019
020    /**
021     * A count of the different sections of this mine. The space between ceiling supports.
022     */
023    private int sectionCount;
024
025    public ComponentMineshaftCorridor(int par1, Random par2Random, StructureBoundingBox par3StructureBoundingBox, int par4)
026    {
027        super(par1);
028        this.coordBaseMode = par4;
029        this.boundingBox = par3StructureBoundingBox;
030        this.hasRails = par2Random.nextInt(3) == 0;
031        this.hasSpiders = !this.hasRails && par2Random.nextInt(23) == 0;
032
033        if (this.coordBaseMode != 2 && this.coordBaseMode != 0)
034        {
035            this.sectionCount = par3StructureBoundingBox.getXSize() / 5;
036        }
037        else
038        {
039            this.sectionCount = par3StructureBoundingBox.getZSize() / 5;
040        }
041    }
042
043    public static StructureBoundingBox findValidPlacement(List par0List, Random par1Random, int par2, int par3, int par4, int par5)
044    {
045        StructureBoundingBox var6 = new StructureBoundingBox(par2, par3, par4, par2, par3 + 2, par4);
046        int var7;
047
048        for (var7 = par1Random.nextInt(3) + 2; var7 > 0; --var7)
049        {
050            int var8 = var7 * 5;
051
052            switch (par5)
053            {
054                case 0:
055                    var6.maxX = par2 + 2;
056                    var6.maxZ = par4 + (var8 - 1);
057                    break;
058                case 1:
059                    var6.minX = par2 - (var8 - 1);
060                    var6.maxZ = par4 + 2;
061                    break;
062                case 2:
063                    var6.maxX = par2 + 2;
064                    var6.minZ = par4 - (var8 - 1);
065                    break;
066                case 3:
067                    var6.maxX = par2 + (var8 - 1);
068                    var6.maxZ = par4 + 2;
069            }
070
071            if (StructureComponent.findIntersecting(par0List, var6) == null)
072            {
073                break;
074            }
075        }
076
077        return var7 > 0 ? var6 : null;
078    }
079
080    /**
081     * Initiates construction of the Structure Component picked, at the current Location of StructGen
082     */
083    public void buildComponent(StructureComponent par1StructureComponent, List par2List, Random par3Random)
084    {
085        int var4 = this.getComponentType();
086        int var5 = par3Random.nextInt(4);
087
088        switch (this.coordBaseMode)
089        {
090            case 0:
091                if (var5 <= 1)
092                {
093                    StructureMineshaftPieces.getNextComponent(par1StructureComponent, par2List, par3Random, this.boundingBox.minX, this.boundingBox.minY - 1 + par3Random.nextInt(3), this.boundingBox.maxZ + 1, this.coordBaseMode, var4);
094                }
095                else if (var5 == 2)
096                {
097                    StructureMineshaftPieces.getNextComponent(par1StructureComponent, par2List, par3Random, this.boundingBox.minX - 1, this.boundingBox.minY - 1 + par3Random.nextInt(3), this.boundingBox.maxZ - 3, 1, var4);
098                }
099                else
100                {
101                    StructureMineshaftPieces.getNextComponent(par1StructureComponent, par2List, par3Random, this.boundingBox.maxX + 1, this.boundingBox.minY - 1 + par3Random.nextInt(3), this.boundingBox.maxZ - 3, 3, var4);
102                }
103
104                break;
105            case 1:
106                if (var5 <= 1)
107                {
108                    StructureMineshaftPieces.getNextComponent(par1StructureComponent, par2List, par3Random, this.boundingBox.minX - 1, this.boundingBox.minY - 1 + par3Random.nextInt(3), this.boundingBox.minZ, this.coordBaseMode, var4);
109                }
110                else if (var5 == 2)
111                {
112                    StructureMineshaftPieces.getNextComponent(par1StructureComponent, par2List, par3Random, this.boundingBox.minX, this.boundingBox.minY - 1 + par3Random.nextInt(3), this.boundingBox.minZ - 1, 2, var4);
113                }
114                else
115                {
116                    StructureMineshaftPieces.getNextComponent(par1StructureComponent, par2List, par3Random, this.boundingBox.minX, this.boundingBox.minY - 1 + par3Random.nextInt(3), this.boundingBox.maxZ + 1, 0, var4);
117                }
118
119                break;
120            case 2:
121                if (var5 <= 1)
122                {
123                    StructureMineshaftPieces.getNextComponent(par1StructureComponent, par2List, par3Random, this.boundingBox.minX, this.boundingBox.minY - 1 + par3Random.nextInt(3), this.boundingBox.minZ - 1, this.coordBaseMode, var4);
124                }
125                else if (var5 == 2)
126                {
127                    StructureMineshaftPieces.getNextComponent(par1StructureComponent, par2List, par3Random, this.boundingBox.minX - 1, this.boundingBox.minY - 1 + par3Random.nextInt(3), this.boundingBox.minZ, 1, var4);
128                }
129                else
130                {
131                    StructureMineshaftPieces.getNextComponent(par1StructureComponent, par2List, par3Random, this.boundingBox.maxX + 1, this.boundingBox.minY - 1 + par3Random.nextInt(3), this.boundingBox.minZ, 3, var4);
132                }
133
134                break;
135            case 3:
136                if (var5 <= 1)
137                {
138                    StructureMineshaftPieces.getNextComponent(par1StructureComponent, par2List, par3Random, this.boundingBox.maxX + 1, this.boundingBox.minY - 1 + par3Random.nextInt(3), this.boundingBox.minZ, this.coordBaseMode, var4);
139                }
140                else if (var5 == 2)
141                {
142                    StructureMineshaftPieces.getNextComponent(par1StructureComponent, par2List, par3Random, this.boundingBox.maxX - 3, this.boundingBox.minY - 1 + par3Random.nextInt(3), this.boundingBox.minZ - 1, 2, var4);
143                }
144                else
145                {
146                    StructureMineshaftPieces.getNextComponent(par1StructureComponent, par2List, par3Random, this.boundingBox.maxX - 3, this.boundingBox.minY - 1 + par3Random.nextInt(3), this.boundingBox.maxZ + 1, 0, var4);
147                }
148        }
149
150        if (var4 < 8)
151        {
152            int var6;
153            int var7;
154
155            if (this.coordBaseMode != 2 && this.coordBaseMode != 0)
156            {
157                for (var6 = this.boundingBox.minX + 3; var6 + 3 <= this.boundingBox.maxX; var6 += 5)
158                {
159                    var7 = par3Random.nextInt(5);
160
161                    if (var7 == 0)
162                    {
163                        StructureMineshaftPieces.getNextComponent(par1StructureComponent, par2List, par3Random, var6, this.boundingBox.minY, this.boundingBox.minZ - 1, 2, var4 + 1);
164                    }
165                    else if (var7 == 1)
166                    {
167                        StructureMineshaftPieces.getNextComponent(par1StructureComponent, par2List, par3Random, var6, this.boundingBox.minY, this.boundingBox.maxZ + 1, 0, var4 + 1);
168                    }
169                }
170            }
171            else
172            {
173                for (var6 = this.boundingBox.minZ + 3; var6 + 3 <= this.boundingBox.maxZ; var6 += 5)
174                {
175                    var7 = par3Random.nextInt(5);
176
177                    if (var7 == 0)
178                    {
179                        StructureMineshaftPieces.getNextComponent(par1StructureComponent, par2List, par3Random, this.boundingBox.minX - 1, this.boundingBox.minY, var6, 1, var4 + 1);
180                    }
181                    else if (var7 == 1)
182                    {
183                        StructureMineshaftPieces.getNextComponent(par1StructureComponent, par2List, par3Random, this.boundingBox.maxX + 1, this.boundingBox.minY, var6, 3, var4 + 1);
184                    }
185                }
186            }
187        }
188    }
189
190    /**
191     * second Part of Structure generating, this for example places Spiderwebs, Mob Spawners, it closes Mineshafts at
192     * the end, it adds Fences...
193     */
194    public boolean addComponentParts(World par1World, Random par2Random, StructureBoundingBox par3StructureBoundingBox)
195    {
196        if (this.isLiquidInStructureBoundingBox(par1World, par3StructureBoundingBox))
197        {
198            return false;
199        }
200        else
201        {
202            int var8 = this.sectionCount * 5 - 1;
203            this.fillWithBlocks(par1World, par3StructureBoundingBox, 0, 0, 0, 2, 1, var8, 0, 0, false);
204            this.randomlyFillWithBlocks(par1World, par3StructureBoundingBox, par2Random, 0.8F, 0, 2, 0, 2, 2, var8, 0, 0, false);
205
206            if (this.hasSpiders)
207            {
208                this.randomlyFillWithBlocks(par1World, par3StructureBoundingBox, par2Random, 0.6F, 0, 0, 0, 2, 1, var8, Block.web.blockID, 0, false);
209            }
210
211            int var9;
212            int var10;
213            int var11;
214
215            for (var9 = 0; var9 < this.sectionCount; ++var9)
216            {
217                var10 = 2 + var9 * 5;
218                this.fillWithBlocks(par1World, par3StructureBoundingBox, 0, 0, var10, 0, 1, var10, Block.fence.blockID, 0, false);
219                this.fillWithBlocks(par1World, par3StructureBoundingBox, 2, 0, var10, 2, 1, var10, Block.fence.blockID, 0, false);
220
221                if (par2Random.nextInt(4) == 0)
222                {
223                    this.fillWithBlocks(par1World, par3StructureBoundingBox, 0, 2, var10, 0, 2, var10, Block.planks.blockID, 0, false);
224                    this.fillWithBlocks(par1World, par3StructureBoundingBox, 2, 2, var10, 2, 2, var10, Block.planks.blockID, 0, false);
225                }
226                else
227                {
228                    this.fillWithBlocks(par1World, par3StructureBoundingBox, 0, 2, var10, 2, 2, var10, Block.planks.blockID, 0, false);
229                }
230
231                this.randomlyPlaceBlock(par1World, par3StructureBoundingBox, par2Random, 0.1F, 0, 2, var10 - 1, Block.web.blockID, 0);
232                this.randomlyPlaceBlock(par1World, par3StructureBoundingBox, par2Random, 0.1F, 2, 2, var10 - 1, Block.web.blockID, 0);
233                this.randomlyPlaceBlock(par1World, par3StructureBoundingBox, par2Random, 0.1F, 0, 2, var10 + 1, Block.web.blockID, 0);
234                this.randomlyPlaceBlock(par1World, par3StructureBoundingBox, par2Random, 0.1F, 2, 2, var10 + 1, Block.web.blockID, 0);
235                this.randomlyPlaceBlock(par1World, par3StructureBoundingBox, par2Random, 0.05F, 0, 2, var10 - 2, Block.web.blockID, 0);
236                this.randomlyPlaceBlock(par1World, par3StructureBoundingBox, par2Random, 0.05F, 2, 2, var10 - 2, Block.web.blockID, 0);
237                this.randomlyPlaceBlock(par1World, par3StructureBoundingBox, par2Random, 0.05F, 0, 2, var10 + 2, Block.web.blockID, 0);
238                this.randomlyPlaceBlock(par1World, par3StructureBoundingBox, par2Random, 0.05F, 2, 2, var10 + 2, Block.web.blockID, 0);
239                this.randomlyPlaceBlock(par1World, par3StructureBoundingBox, par2Random, 0.05F, 1, 2, var10 - 1, Block.torchWood.blockID, 0);
240                this.randomlyPlaceBlock(par1World, par3StructureBoundingBox, par2Random, 0.05F, 1, 2, var10 + 1, Block.torchWood.blockID, 0);
241
242                ChestGenHooks info = ChestGenHooks.getInfo(MINESHAFT_CORRIDOR);
243
244                if (par2Random.nextInt(100) == 0)
245                {
246                    this.generateStructureChestContents(par1World, par3StructureBoundingBox, par2Random, 2, 0, var10 - 1, info.getItems(par2Random), info.getCount(par2Random));
247                }
248
249                if (par2Random.nextInt(100) == 0)
250                {
251                    this.generateStructureChestContents(par1World, par3StructureBoundingBox, par2Random, 0, 0, var10 + 1, info.getItems(par2Random), info.getCount(par2Random));
252                }
253
254                if (this.hasSpiders && !this.spawnerPlaced)
255                {
256                    var11 = this.getYWithOffset(0);
257                    int var12 = var10 - 1 + par2Random.nextInt(3);
258                    int var13 = this.getXWithOffset(1, var12);
259                    var12 = this.getZWithOffset(1, var12);
260
261                    if (par3StructureBoundingBox.isVecInside(var13, var11, var12))
262                    {
263                        this.spawnerPlaced = true;
264                        par1World.setBlockWithNotify(var13, var11, var12, Block.mobSpawner.blockID);
265                        TileEntityMobSpawner var14 = (TileEntityMobSpawner)par1World.getBlockTileEntity(var13, var11, var12);
266
267                        if (var14 != null)
268                        {
269                            var14.setMobID("CaveSpider");
270                        }
271                    }
272                }
273            }
274
275            for (var9 = 0; var9 <= 2; ++var9)
276            {
277                for (var10 = 0; var10 <= var8; ++var10)
278                {
279                    var11 = this.getBlockIdAtCurrentPosition(par1World, var9, -1, var10, par3StructureBoundingBox);
280
281                    if (var11 == 0)
282                    {
283                        this.placeBlockAtCurrentPosition(par1World, Block.planks.blockID, 0, var9, -1, var10, par3StructureBoundingBox);
284                    }
285                }
286            }
287
288            if (this.hasRails)
289            {
290                for (var9 = 0; var9 <= var8; ++var9)
291                {
292                    var10 = this.getBlockIdAtCurrentPosition(par1World, 1, -1, var9, par3StructureBoundingBox);
293
294                    if (var10 > 0 && Block.opaqueCubeLookup[var10])
295                    {
296                        this.randomlyPlaceBlock(par1World, par3StructureBoundingBox, par2Random, 0.7F, 1, 0, var9, Block.rail.blockID, this.getMetadataWithOffset(Block.rail.blockID, 0));
297                    }
298                }
299            }
300
301            return true;
302        }
303    }
304}