Advertisement
Guest User

ChunkGen

a guest
Jul 18th, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.08 KB | None | 0 0
  1. package com.ubalube.adventuring.worldgen.world.library;
  2.  
  3. import java.util.HashMap;
  4. import java.util.List;
  5. import java.util.Map;
  6. import java.util.Random;
  7. import javax.annotation.Nullable;
  8.  
  9. import com.ubalube.adventuring.init.ModBlocks;
  10.  
  11. import net.minecraft.block.state.IBlockState;
  12. import net.minecraft.entity.EnumCreatureType;
  13. import net.minecraft.init.Biomes;
  14. import net.minecraft.init.Blocks;
  15. import net.minecraft.util.math.BlockPos;
  16. import net.minecraft.util.math.ChunkPos;
  17. import net.minecraft.world.World;
  18. import net.minecraft.world.biome.Biome;
  19. import net.minecraft.world.chunk.Chunk;
  20. import net.minecraft.world.chunk.ChunkPrimer;
  21. import net.minecraft.world.gen.FlatGeneratorInfo;
  22. import net.minecraft.world.gen.FlatLayerInfo;
  23. import net.minecraft.world.gen.IChunkGenerator;
  24. import net.minecraft.world.gen.MapGenBase;
  25. import net.minecraft.world.gen.NoiseGeneratorOctaves;
  26. import net.minecraft.world.gen.feature.WorldGenDungeons;
  27. import net.minecraft.world.gen.feature.WorldGenLakes;
  28. import net.minecraft.world.gen.structure.MapGenMineshaft;
  29. import net.minecraft.world.gen.structure.MapGenScatteredFeature;
  30. import net.minecraft.world.gen.structure.MapGenStronghold;
  31. import net.minecraft.world.gen.structure.MapGenStructure;
  32. import net.minecraft.world.gen.structure.MapGenVillage;
  33. import net.minecraft.world.gen.structure.StructureOceanMonument;
  34.  
  35. public class ChunkGeneratorTemplate implements IChunkGenerator
  36. {
  37. private final World world;
  38. private final Random random;
  39. private final IBlockState[] cachedBlockIDs = new IBlockState[256];
  40. private final FlatGeneratorInfo flatWorldGenInfo;
  41. private final Map<String, MapGenStructure> structureGenerators = new HashMap<String, MapGenStructure>();
  42.  
  43. public ChunkGeneratorTemplate(World worldIn, long seed, boolean generateStructures, String flatGeneratorSettings)
  44. {
  45. this.world = worldIn;
  46. this.random = new Random(seed);
  47. this.flatWorldGenInfo = FlatGeneratorInfo.createFlatGeneratorFromString(flatGeneratorSettings);
  48. int j = 0;
  49. int k = 0;
  50. boolean flag = true;
  51.  
  52. for (FlatLayerInfo flatlayerinfo : this.flatWorldGenInfo.getFlatLayers())
  53. {
  54. for (int i = flatlayerinfo.getMinY(); i < flatlayerinfo.getMinY() + flatlayerinfo.getLayerCount(); ++i)
  55. {
  56. IBlockState iblockstate = flatlayerinfo.getLayerMaterial();
  57.  
  58. if (iblockstate.getBlock() != Blocks.AIR)
  59. {
  60. flag = false;
  61. this.cachedBlockIDs[i] = iblockstate;
  62. }
  63. }
  64.  
  65. if (flatlayerinfo.getLayerMaterial().getBlock() == Blocks.AIR)
  66. {
  67. k += flatlayerinfo.getLayerCount();
  68. }
  69. else
  70. {
  71. j += flatlayerinfo.getLayerCount() + k;
  72. k = 0;
  73. }
  74. }
  75.  
  76. worldIn.setSeaLevel(j);
  77. }
  78.  
  79. /**
  80. * Generates the chunk at the specified position, from scratch
  81. */
  82. public Chunk generateChunk(int x, int z)
  83. {
  84. ChunkPrimer chunkprimer = new ChunkPrimer();
  85.  
  86. for (int i = 0; i < this.cachedBlockIDs.length; ++i)
  87. {
  88. IBlockState iblockstate = this.cachedBlockIDs[i];
  89.  
  90. if (iblockstate != null)
  91. {
  92. for (int j = 0; j < 16; ++j)
  93. {
  94. for (int k = 0; k < 16; ++k)
  95. {
  96. chunkprimer.setBlockState(j, i, k, iblockstate);
  97. }
  98. }
  99. }
  100. }
  101.  
  102. for (MapGenBase mapgenbase : this.structureGenerators.values())
  103. {
  104. mapgenbase.generate(this.world, x, z, chunkprimer);
  105. }
  106.  
  107. Chunk chunk = new Chunk(this.world, chunkprimer, x, z);
  108. Biome[] abiome = this.world.getBiomeProvider().yourbiomestuff, x * 16, z * 16, 16, 16);
  109. byte[] abyte = chunk.getBiomeArray();
  110.  
  111. for (int l = 0; l < abyte.length; ++l)
  112. {
  113. abyte[l] = (byte)Biome.getIdForBiome(abiome[l]);
  114. }
  115.  
  116. chunk.generateSkylightMap();
  117. return chunk;
  118. }
  119.  
  120. /**
  121. * Generate initial structures in this chunk, e.g. mineshafts, temples, lakes, and dungeons
  122. */
  123. public void populate(int x, int z)
  124. {
  125. net.minecraft.block.BlockFalling.fallInstantly = true;
  126. int i = x * 16;
  127. int j = z * 16;
  128. BlockPos blockpos = new BlockPos(i, 0, j);
  129. boolean flag = false;
  130. this.random.setSeed(this.world.getSeed());
  131. long k = this.random.nextLong() / 2L * 2L + 1L;
  132. long l = this.random.nextLong() / 2L * 2L + 1L;
  133. this.random.setSeed((long)x * k + (long)z * l ^ this.world.getSeed());
  134. ChunkPos chunkpos = new ChunkPos(x, z);
  135.  
  136. net.minecraftforge.event.ForgeEventFactory.onChunkPopulate(true, this, this.world, this.random, x, z, flag);
  137.  
  138. for (MapGenStructure mapgenstructure : this.structureGenerators.values())
  139. {
  140. boolean flag1 = mapgenstructure.generateStructure(this.world, this.random, chunkpos);
  141.  
  142. if (mapgenstructure instanceof MapGenVillage)
  143. {
  144. flag |= flag1;
  145. }
  146. }
  147.  
  148. net.minecraftforge.event.ForgeEventFactory.onChunkPopulate(false, this, this.world, this.random, x, z, flag);
  149. net.minecraft.block.BlockFalling.fallInstantly = false;
  150. }
  151.  
  152. /**
  153. * Called to generate additional structures after initial worldgen, used by ocean monuments
  154. */
  155. public boolean generateStructures(Chunk chunkIn, int x, int z)
  156. {
  157. return false;
  158. }
  159.  
  160. public List<Biome.SpawnListEntry> getPossibleCreatures(EnumCreatureType creatureType, BlockPos pos)
  161. {
  162. return null;
  163. }
  164.  
  165. @Nullable
  166. public BlockPos getNearestStructurePos(World worldIn, String structureName, BlockPos position, boolean findUnexplored)
  167. {
  168. MapGenStructure mapgenstructure = this.structureGenerators.get(structureName);
  169. return mapgenstructure != null ? mapgenstructure.getNearestStructurePos(worldIn, position, findUnexplored) : null;
  170. }
  171.  
  172. public boolean isInsideStructure(World worldIn, String structureName, BlockPos pos)
  173. {
  174. MapGenStructure mapgenstructure = this.structureGenerators.get(structureName);
  175. return mapgenstructure != null ? mapgenstructure.isInsideStructure(pos) : false;
  176. }
  177.  
  178. /**
  179. * Recreates data about structures intersecting given chunk (used for example by getPossibleCreatures), without
  180. * placing any blocks. When called for the first time before any chunk is generated - also initializes the internal
  181. * state needed by getPossibleCreatures.
  182. */
  183. public void recreateStructures(Chunk chunkIn, int x, int z)
  184. {
  185. for (MapGenStructure mapgenstructure : this.structureGenerators.values())
  186. {
  187. mapgenstructure.generate(this.world, x, z, (ChunkPrimer)null);
  188. }
  189. }
  190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement