package com.progwml6.natura.world.dimension;
import java.util.List;
import java.util.Random;
import javax.annotation.Nullable;
import com.progwml6.natura.nether.NaturaNether;
import net.minecraft.block.BlockFalling;
import net.minecraft.block.material.Material;
import net.minecraft.block.state.IBlockState;
import net.minecraft.block.state.pattern.BlockMatcher;
import net.minecraft.entity.EnumCreatureType;
import net.minecraft.init.Blocks;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.ChunkPos;
import net.minecraft.util.math.MathHelper;
import net.minecraft.world.World;
import net.minecraft.world.biome.Biome;
import net.minecraft.world.chunk.Chunk;
import net.minecraft.world.chunk.ChunkPrimer;
import net.minecraft.world.chunk.IChunkGenerator;
import net.minecraft.world.gen.MapGenBase;
import net.minecraft.world.gen.MapGenCavesHell;
import net.minecraft.world.gen.NoiseGeneratorOctaves;
import net.minecraft.world.gen.feature.WorldGenBush;
import net.minecraft.world.gen.feature.WorldGenGlowStone1;
import net.minecraft.world.gen.feature.WorldGenGlowStone2;
import net.minecraft.world.gen.feature.WorldGenHellLava;
import net.minecraft.world.gen.feature.WorldGenMinable;
import net.minecraft.world.gen.feature.WorldGenerator;
import net.minecraft.world.gen.structure.MapGenNetherBridge;
public class ChunkProviderNetherite implements IChunkGenerator
{
protected static final IBlockState AIR = Blocks.AIR.getDefaultState();
protected static final IBlockState NETHERRACK = Blocks.NETHERRACK.getDefaultState();
protected static final IBlockState BEDROCK = Blocks.BEDROCK.getDefaultState();
protected static final IBlockState LAVA = Blocks.LAVA.getDefaultState();
protected static final IBlockState GRAVEL = Blocks.GRAVEL.getDefaultState();
protected static final IBlockState SOUL_SAND = Blocks.SOUL_SAND.getDefaultState();
protected static final IBlockState TAINTED_SOIL = NaturaNether.netherTaintedSoil.getDefaultState();
protected static final IBlockState HEAT_SAND = NaturaNether.netherHeatSand.getDefaultState();
private final World world;
private final boolean generateStructures;
private final Random rand;
/** Holds the noise used to determine whether slowsand can be generated at a location */
private double[] slowsandNoise = new double[256];
private double[] gravelNoise = new double[256];
private double[] depthBuffer = new double[256];
private double[] buffer;
private NoiseGeneratorOctaves lperlinNoise1;
private NoiseGeneratorOctaves lperlinNoise2;
private NoiseGeneratorOctaves perlinNoise1;
/** Determines whether slowsand or gravel can be generated at a location */
private NoiseGeneratorOctaves slowsandGravelNoiseGen;
/** Determines whether something other than nettherack can be generated at a location */
private NoiseGeneratorOctaves netherrackExculsivityNoiseGen;
public NoiseGeneratorOctaves scaleNoise;
public NoiseGeneratorOctaves depthNoise;
private final WorldGenNetheriteFire fireFeature = new WorldGenNetheriteFire();
private final WorldGenGlowStone1 lightGemGen = new WorldGenGlowStone1();
private final WorldGenGlowStone2 hellPortalGen = new WorldGenGlowStone2();
private final WorldGenerator quartzGen = new WorldGenMinable(Blocks.QUARTZ_ORE.getDefaultState(), 14, BlockMatcher.forBlock(Blocks.NETHERRACK));
private final WorldGenerator magmaGen = new WorldGenMinable(Blocks.MAGMA.getDefaultState(), 33, BlockMatcher.forBlock(Blocks.NETHERRACK));
private final WorldGenHellLava lavaTrapGen = new WorldGenHellLava(Blocks.FLOWING_LAVA, true);
private final WorldGenHellLava hellSpringGen = new WorldGenHellLava(Blocks.FLOWING_LAVA, false);
private final WorldGenBush brownMushroomFeature = new WorldGenBush(Blocks.BROWN_MUSHROOM);
private final WorldGenBush redMushroomFeature = new WorldGenBush(Blocks.RED_MUSHROOM);
protected MapGenNetherBridge genNetherBridge = new MapGenNetherBridge();
private MapGenBase genNetherCaves = new MapGenCavesHell();
double[] pnr;
double[] ar;
double[] br;
double[] noiseData4;
double[] dr;
public ChunkProviderNetherite(World worldIn, boolean p_i45637_2_, long seed)
{
this.world = worldIn;
this.generateStructures = p_i45637_2_;
this.rand = new Random(seed);
this.lperlinNoise1 = new NoiseGeneratorOctaves(this.rand, 16);
this.lperlinNoise2 = new NoiseGeneratorOctaves(this.rand, 16);
this.perlinNoise1 = new NoiseGeneratorOctaves(this.rand, 8);
this.slowsandGravelNoiseGen = new NoiseGeneratorOctaves(this.rand, 4);
this.netherrackExculsivityNoiseGen = new NoiseGeneratorOctaves(this.rand, 4);
this.scaleNoise = new NoiseGeneratorOctaves(this.rand, 10);
this.depthNoise = new NoiseGeneratorOctaves(this.rand, 16);
worldIn.setSeaLevel(63);
net.minecraftforge.event.terraingen.InitNoiseGensEvent.ContextHell ctx = new net.minecraftforge.event.terraingen.InitNoiseGensEvent.ContextHell(lperlinNoise1, lperlinNoise2, perlinNoise1, slowsandGravelNoiseGen, netherrackExculsivityNoiseGen, scaleNoise, depthNoise);
ctx = net.minecraftforge.event.terraingen.TerrainGen.getModdedNoiseGenerators(worldIn, this.rand, ctx);
this.lperlinNoise1 = ctx.getLPerlin1();
this.lperlinNoise2 = ctx.getLPerlin2();
this.perlinNoise1 = ctx.getPerlin();
this.slowsandGravelNoiseGen = ctx.getPerlin2();
this.netherrackExculsivityNoiseGen = ctx.getPerlin3();
this.scaleNoise = ctx.getScale();
this.depthNoise = ctx.getDepth();
this.genNetherBridge = (MapGenNetherBridge) net.minecraftforge.event.terraingen.TerrainGen.getModdedMapGen(genNetherBridge, net.minecraftforge.event.terraingen.InitMapGenEvent.EventType.NETHER_BRIDGE);
this.genNetherCaves = net.minecraftforge.event.terraingen.TerrainGen.getModdedMapGen(genNetherCaves, net.minecraftforge.event.terraingen.InitMapGenEvent.EventType.NETHER_CAVE);
}
public void prepareHeights(int p_185936_1_, int p_185936_2_, ChunkPrimer primer)
{
int j = this.world.getSeaLevel() / 2 + 1;
this.buffer = this.getHeights(this.buffer, p_185936_1_ * 4, 0, p_185936_2_ * 4, 5, 17, 5);
for (int j1 = 0; j1 < 4; ++j1)
{
for (int k1 = 0; k1 < 4; ++k1)
{
for (int l1 = 0; l1 < 16; ++l1)
{
double d1 = this.buffer[((j1 + 0) * 5 + k1 + 0) * 17 + l1 + 0];
double d2 = this.buffer[((j1 + 0) * 5 + k1 + 1) * 17 + l1 + 0];
double d3 = this.buffer[((j1 + 1) * 5 + k1 + 0) * 17 + l1 + 0];
double d4 = this.buffer[((j1 + 1) * 5 + k1 + 1) * 17 + l1 + 0];
double d5 = (this.buffer[((j1 + 0) * 5 + k1 + 0) * 17 + l1 + 1] - d1) * 0.125D;
double d6 = (this.buffer[((j1 + 0) * 5 + k1 + 1) * 17 + l1 + 1] - d2) * 0.125D;
double d7 = (this.buffer[((j1 + 1) * 5 + k1 + 0) * 17 + l1 + 1] - d3) * 0.125D;
double d8 = (this.buffer[((j1 + 1) * 5 + k1 + 1) * 17 + l1 + 1] - d4) * 0.125D;
for (int i2 = 0; i2 < 8; ++i2)
{
double d10 = d1;
double d11 = d2;
double d12 = (d3 - d1) * 0.25D;
double d13 = (d4 - d2) * 0.25D;
for (int j2 = 0; j2 < 4; ++j2)
{
double d15 = d10;
double d16 = (d11 - d10) * 0.25D;
for (int k2 = 0; k2 < 4; ++k2)
{
IBlockState iblockstate = null;
if (l1 * 8 + i2 < j)
{
iblockstate = LAVA;
}
if (d15 > 0.0D)
{
iblockstate = NETHERRACK;
}
if (d15 > 56.0D)
{
iblockstate = TAINTED_SOIL;
}
int l2 = j2 + j1 * 4;
int i3 = i2 + l1 * 8;
int j3 = k2 + k1 * 4;
primer.setBlockState(l2, i3, j3, iblockstate);
d15 += d16;
}
d10 += d12;
d11 += d13;
}
d1 += d5;
d2 += d6;
d3 += d7;
d4 += d8;
}
}
}
}
}
public void buildSurfaces(int p_185937_1_, int p_185937_2_, ChunkPrimer primer)
{
if (!net.minecraftforge.event.ForgeEventFactory.onReplaceBiomeBlocks(this, p_185937_1_, p_185937_2_, primer, this.world))
return;
int i = this.world.getSeaLevel() + 1;
this.slowsandNoise = this.slowsandGravelNoiseGen.generateNoiseOctaves(this.slowsandNoise, p_185937_1_ * 16, p_185937_2_ * 16, 0, 16, 16, 1, 0.03125D, 0.03125D, 1.0D);
this.gravelNoise = this.slowsandGravelNoiseGen.generateNoiseOctaves(this.gravelNoise, p_185937_1_ * 16, 109, p_185937_2_ * 16, 16, 1, 16, 0.03125D, 1.0D, 0.03125D);
this.depthBuffer = this.netherrackExculsivityNoiseGen.generateNoiseOctaves(this.depthBuffer, p_185937_1_ * 16, p_185937_2_ * 16, 0, 16, 16, 1, 0.0625D, 0.0625D, 0.0625D);
for (int j = 0; j < 16; ++j)
{
for (int k = 0; k < 16; ++k)
{
boolean flag = this.slowsandNoise[j + k * 16] + this.rand.nextDouble() * 0.2D > 0.0D;
boolean flag1 = this.gravelNoise[j + k * 16] + this.rand.nextDouble() * 0.2D > 0.0D;
int l = (int) (this.depthBuffer[j + k * 16] / 3.0D + 3.0D + this.rand.nextDouble() * 0.25D);
int i1 = -1;
IBlockState iblockstate = NETHERRACK;
IBlockState iblockstate1 = TAINTED_SOIL;
for (int j1 = 127; j1 >= 0; --j1)
{
if (j1 < 127 - this.rand.nextInt(5) && j1 > this.rand.nextInt(5))
{
IBlockState iblockstate2 = primer.getBlockState(k, j1, j);
if (iblockstate2.getBlock() != null && iblockstate2.getMaterial() != Material.AIR)
{
if (iblockstate2.getBlock() == Blocks.NETHERRACK)
{
if (i1 == -1)
{
if (l <= 0)
{
iblockstate = AIR;
iblockstate1 = NETHERRACK;
}
else if (j1 >= i - 4 && j1 <= i + 1)
{
iblockstate = NETHERRACK;
iblockstate1 = TAINTED_SOIL;
if (flag1)
{
iblockstate = GRAVEL;
iblockstate1 = NETHERRACK;
}
if (flag)
{
iblockstate = SOUL_SAND;
iblockstate1 = HEAT_SAND;
}
}
if (j1 < i && (iblockstate == null || iblockstate.getMaterial() == Material.AIR))
{
iblockstate = LAVA;
}
i1 = l;
if (j1 >= i - 1)
{
primer.setBlockState(k, j1, j, iblockstate);
}
else
{
primer.setBlockState(k, j1, j, iblockstate1);
}
}
else if (i1 > 0)
{
--i1;
primer.setBlockState(k, j1, j, iblockstate1);
}
}
}
else
{
i1 = -1;
}
}
else
{
primer.setBlockState(k, j1, j, BEDROCK);
}
}
}
}
}
@Override
public Chunk provideChunk(int x, int z)
{
this.rand.setSeed(x * 341873128712L + z * 132897987541L);
ChunkPrimer chunkprimer = new ChunkPrimer();
this.prepareHeights(x, z, chunkprimer);
this.buildSurfaces(x, z, chunkprimer);
this.genNetherCaves.generate(this.world, x, z, chunkprimer);
if (this.generateStructures)
{
this.genNetherBridge.generate(this.world, x, z, chunkprimer);
}
Chunk chunk = new ChunkNetherite(this.world, chunkprimer, x, z);
Biome[] abiome = this.world.getBiomeProvider().getBiomes((Biome[]) null, x * 16, z * 16, 16, 16);
byte[] abyte = chunk.getBiomeArray();
for (int i = 0; i < abyte.length; ++i)
{
abyte[i] = (byte) Biome.getIdForBiome(abiome[i]);
}
chunk.resetRelightChecks();
return chunk;
}
private double[] getHeights(double[] p_185938_1_, int p_185938_2_, int p_185938_3_, int p_185938_4_, int p_185938_5_, int p_185938_6_, int p_185938_7_)
{
if (p_185938_1_ == null)
{
p_185938_1_ = new double[p_185938_5_ * p_185938_6_ * p_185938_7_];
}
net.minecraftforge.event.terraingen.ChunkGeneratorEvent.InitNoiseField event = new net.minecraftforge.event.terraingen.ChunkGeneratorEvent.InitNoiseField(this, p_185938_1_, p_185938_2_, p_185938_3_, p_185938_4_, p_185938_5_, p_185938_6_, p_185938_7_);
net.minecraftforge.common.MinecraftForge.EVENT_BUS.post(event);
if (event.getResult() == net.minecraftforge.fml.common.eventhandler.Event.Result.DENY)
return event.getNoisefield();
this.noiseData4 = this.scaleNoise.generateNoiseOctaves(this.noiseData4, p_185938_2_, p_185938_3_, p_185938_4_, p_185938_5_, 1, p_185938_7_, 1.0D, 0.0D, 1.0D);
this.dr = this.depthNoise.generateNoiseOctaves(this.dr, p_185938_2_, p_185938_3_, p_185938_4_, p_185938_5_, 1, p_185938_7_, 100.0D, 0.0D, 100.0D);
this.pnr = this.perlinNoise1.generateNoiseOctaves(this.pnr, p_185938_2_, p_185938_3_, p_185938_4_, p_185938_5_, p_185938_6_, p_185938_7_, 8.555150000000001D, 34.2206D, 8.555150000000001D);
this.ar = this.lperlinNoise1.generateNoiseOctaves(this.ar, p_185938_2_, p_185938_3_, p_185938_4_, p_185938_5_, p_185938_6_, p_185938_7_, 684.412D, 2053.236D, 684.412D);
this.br = this.lperlinNoise2.generateNoiseOctaves(this.br, p_185938_2_, p_185938_3_, p_185938_4_, p_185938_5_, p_185938_6_, p_185938_7_, 684.412D, 2053.236D, 684.412D);
int i = 0;
double[] adouble = new double[p_185938_6_];
for (int j = 0; j < p_185938_6_; ++j)
{
adouble[j] = Math.cos(j * Math.PI * 6.0D / p_185938_6_) * 2.0D;
double d2 = j;
if (j > p_185938_6_ / 2)
{
d2 = p_185938_6_ - 1 - j;
}
if (d2 < 4.0D)
{
d2 = 4.0D - d2;
adouble[j] -= d2 * d2 * d2 * 10.0D;
}
}
for (int l = 0; l < p_185938_5_; ++l)
{
for (int i1 = 0; i1 < p_185938_7_; ++i1)
{
for (int k = 0; k < p_185938_6_; ++k)
{
double d4 = adouble[k];
double d5 = this.ar[i] / 512.0D;
double d6 = this.br[i] / 512.0D;
double d7 = (this.pnr[i] / 10.0D + 1.0D) / 2.0D;
double d8;
if (d7 < 0.0D)
{
d8 = d5;
}
else if (d7 > 1.0D)
{
d8 = d6;
}
else
{
d8 = d5 + (d6 - d5) * d7;
}
d8 = d8 - d4;
if (k > p_185938_6_ - 4)
{
double d9 = (k - (p_185938_6_ - 4)) / 3.0F;
d8 = d8 * (1.0D - d9) + -10.0D * d9;
}
if (k < 0.0D)
{
double d10 = (0.0D - k) / 4.0D;
d10 = MathHelper.clamp(d10, 0.0D, 1.0D);
d8 = d8 * (1.0D - d10) + -10.0D * d10;
}
p_185938_1_[i] = d8;
++i;
}
}
}
return p_185938_1_;
}
@Override
public void populate(int x, int z)
{
BlockFalling.fallInstantly = true;
net.minecraftforge.event.ForgeEventFactory.onChunkPopulate(true, this, this.world, this.rand, x, z, false);
int i = x * 16;
int j = z * 16;
BlockPos blockpos = new BlockPos(i, 0, j);
Biome biome = this.world.getBiome(blockpos.add(16, 0, 16));
ChunkPos chunkpos = new ChunkPos(x, z);
this.genNetherBridge.generateStructure(this.world, this.rand, chunkpos);
if (net.minecraftforge.event.terraingen.TerrainGen.populate(this, this.world, this.rand, x, z, false, net.minecraftforge.event.terraingen.PopulateChunkEvent.Populate.EventType.NETHER_LAVA))
for (int k = 0; k < 8; ++k)
{
this.hellSpringGen.generate(this.world, this.rand, blockpos.add(this.rand.nextInt(16) + 8, this.rand.nextInt(120) + 4, this.rand.nextInt(16) + 8));
}
if (net.minecraftforge.event.terraingen.TerrainGen.populate(this, this.world, this.rand, x, z, false, net.minecraftforge.event.terraingen.PopulateChunkEvent.Populate.EventType.FIRE))
for (int i1 = 0; i1 < this.rand.nextInt(this.rand.nextInt(10) + 1) + 1; ++i1)
{
this.fireFeature.generate(this.world, this.rand, blockpos.add(this.rand.nextInt(16) + 8, this.rand.nextInt(120) + 4, this.rand.nextInt(16) + 8));
}
if (net.minecraftforge.event.terraingen.TerrainGen.populate(this, this.world, this.rand, x, z, false, net.minecraftforge.event.terraingen.PopulateChunkEvent.Populate.EventType.GLOWSTONE))
{
for (int j1 = 0; j1 < this.rand.nextInt(this.rand.nextInt(10) + 1); ++j1)
{
this.lightGemGen.generate(this.world, this.rand, blockpos.add(this.rand.nextInt(16) + 8, this.rand.nextInt(120) + 4, this.rand.nextInt(16) + 8));
}
for (int k1 = 0; k1 < 10; ++k1)
{
this.hellPortalGen.generate(this.world, this.rand, blockpos.add(this.rand.nextInt(16) + 8, this.rand.nextInt(128), this.rand.nextInt(16) + 8));
}
} //Forge: End doGLowstone
net.minecraftforge.event.ForgeEventFactory.onChunkPopulate(false, this, this.world, this.rand, x, z, false);
net.minecraftforge.common.MinecraftForge.EVENT_BUS.post(new net.minecraftforge.event.terraingen.DecorateBiomeEvent.Pre(this.world, this.rand, blockpos));
if (net.minecraftforge.event.terraingen.TerrainGen.decorate(this.world, this.rand, blockpos, net.minecraftforge.event.terraingen.DecorateBiomeEvent.Decorate.EventType.SHROOM))
{
if (this.rand.nextBoolean())
{
this.brownMushroomFeature.generate(this.world, this.rand, blockpos.add(this.rand.nextInt(16) + 8, this.rand.nextInt(128), this.rand.nextInt(16) + 8));
}
if (this.rand.nextBoolean())
{
this.redMushroomFeature.generate(this.world, this.rand, blockpos.add(this.rand.nextInt(16) + 8, this.rand.nextInt(128), this.rand.nextInt(16) + 8));
}
}
if (net.minecraftforge.event.terraingen.TerrainGen.generateOre(this.world, this.rand, quartzGen, blockpos, net.minecraftforge.event.terraingen.OreGenEvent.GenerateMinable.EventType.QUARTZ))
for (int l1 = 0; l1 < 16; ++l1)
{
this.quartzGen.generate(this.world, this.rand, blockpos.add(this.rand.nextInt(16), this.rand.nextInt(108) + 10, this.rand.nextInt(16)));
}
int i2 = this.world.getSeaLevel() / 2 + 1;
if (net.minecraftforge.event.terraingen.TerrainGen.populate(this, this.world, this.rand, x, z, false, net.minecraftforge.event.terraingen.PopulateChunkEvent.Populate.EventType.NETHER_MAGMA))
for (int l = 0; l < 4; ++l)
{
this.magmaGen.generate(this.world, this.rand, blockpos.add(this.rand.nextInt(16), i2 - 5 + this.rand.nextInt(10), this.rand.nextInt(16)));
}
if (net.minecraftforge.event.terraingen.TerrainGen.populate(this, this.world, this.rand, x, z, false, net.minecraftforge.event.terraingen.PopulateChunkEvent.Populate.EventType.NETHER_LAVA2))
for (int j2 = 0; j2 < 16; ++j2)
{
this.lavaTrapGen.generate(this.world, this.rand, blockpos.add(this.rand.nextInt(16), this.rand.nextInt(108) + 10, this.rand.nextInt(16)));
}
biome.decorate(this.world, this.rand, new BlockPos(i, 0, j));
net.minecraftforge.common.MinecraftForge.EVENT_BUS.post(new net.minecraftforge.event.terraingen.DecorateBiomeEvent.Post(this.world, this.rand, blockpos));
BlockFalling.fallInstantly = false;
}
@Override
public boolean generateStructures(Chunk chunkIn, int x, int z)
{
return false;
}
@Override
public List<Biome.SpawnListEntry> getPossibleCreatures(EnumCreatureType creatureType, BlockPos pos)
{
if (creatureType == EnumCreatureType.MONSTER)
{
if (this.genNetherBridge.isInsideStructure(pos))
{
return this.genNetherBridge.getSpawnList();
}
if (this.genNetherBridge.isPositionInStructure(this.world, pos) && this.world.getBlockState(pos.down()).getBlock() == Blocks.NETHER_BRICK)
{
return this.genNetherBridge.getSpawnList();
}
}
Biome biome = this.world.getBiome(pos);
return biome.getSpawnableList(creatureType);
}
@Override
@Nullable
public BlockPos getStrongholdGen(World worldIn, String structureName, BlockPos position)
{
return null;
}
@Override
public void recreateStructures(Chunk chunkIn, int x, int z)
{
this.genNetherBridge.generate(this.world, x, z, (ChunkPrimer) null);
}
}