Advertisement
HalestormXV

Untitled

Apr 7th, 2018
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.48 KB | None | 0 0
  1. package halestormxv.world.gen;
  2.  
  3. import halestormxv.init.BlockInit;
  4. import halestormxv.objects.blocks.BlockDirts;
  5. import halestormxv.utility.HighQualityRandom;
  6. import halestormxv.utility.handlers.EnumHandlerWood;
  7. import halestormxv.world.biomes.BiomeLupresiumForest;
  8. import halestormxv.world.biomes.BiomeMysticLands;
  9. import halestormxv.world.gen.generators.WorldGenLupresiumTree;
  10. import halestormxv.world.gen.generators.WorldGenMysticTree;
  11. import halestormxv.world.gen.generators.WorldGenStructure;
  12. import net.minecraft.block.Block;
  13. import net.minecraft.init.Blocks;
  14. import net.minecraft.util.math.BlockPos;
  15. import net.minecraft.world.World;
  16. import net.minecraft.world.WorldType;
  17. import net.minecraft.world.biome.Biome;
  18. import net.minecraft.world.chunk.IChunkProvider;
  19. import net.minecraft.world.gen.IChunkGenerator;
  20. import net.minecraft.world.gen.feature.WorldGenerator;
  21. import net.minecraftforge.fml.common.IWorldGenerator;
  22. import net.minecraftforge.fml.common.registry.ForgeRegistries;
  23.  
  24. import java.util.*;
  25. import java.util.Arrays;
  26. import java.util.stream.Collectors;
  27.  
  28. public class WorldGenCustomStuffs implements IWorldGenerator {
  29.     /*###STRUCTURES###*/
  30.     //private static final WorldGenStructure ALTAR_OF_FIRE = new WorldGenStructure("altar_fire");
  31.  
  32.     /*###TREES###*/
  33.     private final WorldGenerator LUPRESIUM_TREE = new WorldGenLupresiumTree();
  34.     private final WorldGenerator MYSTIC_TREE = new WorldGenMysticTree();
  35.     private Block lupresiumDirt = BlockInit.DIRT.getDefaultState().withProperty(BlockDirts.VARIANT, EnumHandlerWood.EnumTypeWood.LUPRESIUM).getBlock();
  36.     private Block mysticDirt = BlockInit.DIRT.getDefaultState().withProperty(BlockDirts.VARIANT, EnumHandlerWood.EnumTypeWood.MYSTIC).getBlock();
  37.  
  38.     @Override
  39.     //Greater the Chance the Less Likely it is to Spawn. If chance is 100 then there is a 1 in 100 chance of equaling 0. Reaching 0 will trigger the spawn.
  40.     public void generate(Random random, int chunkX, int chunkZ, World world, IChunkGenerator chunkGenerator, IChunkProvider chunkProvider) {
  41.         switch (world.provider.getDimension()) {
  42.             case -1:
  43.                 break;
  44.  
  45.             case 0:
  46.                 generateStructures(new WorldGenStructure("cultist_hut"), world, random, chunkX, chunkZ, 10, Blocks.GRASS, getBiomeList().toArray(new Class[getBiomeList().size()]));
  47.                 generateRuneAltar(new WorldGenStructure("air_temple"), world, random, chunkX, chunkZ, 120, 98, 176);
  48.                 generateRuneAltar(new WorldGenStructure("earth_temple"), world, random, chunkX, chunkZ, 180, 84, 152);
  49.                 generateTrees(LUPRESIUM_TREE, world, random, chunkX, chunkZ, 10, lupresiumDirt, BiomeLupresiumForest.class);
  50.                 generateTrees(MYSTIC_TREE, world, random, chunkX, chunkZ, 10, mysticDirt, BiomeMysticLands.class);
  51.                 break;
  52.  
  53.             case 1:
  54.                 break;
  55.  
  56.             case 2:
  57.                 break;
  58.  
  59.         }
  60.     }
  61.  
  62.     private void generateStructures(WorldGenerator generator, World world, Random random, int chunkX, int chunkZ, int chance, Block topBlock, Class<?>... classes) {
  63.         HighQualityRandom highQualityRandom = new HighQualityRandom();
  64.         ArrayList<Class<?>> classesList = new ArrayList<Class<?>>(Arrays.asList(classes));
  65.         int struc_x = (chunkX * 16) + highQualityRandom.nextInt(15);
  66.         int struc_z = (chunkZ * 16) + highQualityRandom.nextInt(15);
  67.         float y = 0;
  68.         for (int x = 0; x < struc_x; x++)
  69.         {
  70.             for (int z = 0; z < struc_z; z++)
  71.             {
  72.                 int oldY = Math.round(y);
  73.                 y += calculateGenerationHeight(world, x, z, topBlock);
  74.                 y /= 2;
  75.                 if (Math.round(y) != oldY){ return; }
  76.             }
  77.         }
  78.         int struc_y = (int)y;
  79.         BlockPos pos = new BlockPos(struc_x, struc_y, struc_z);
  80.         Class<?> biome = world.provider.getBiomeForCoords(pos).getClass();
  81.         if (world.getWorldType() != WorldType.FLAT) {
  82.             if (classesList.contains(biome) || classesList.isEmpty()) {
  83.                 if (highQualityRandom.nextInt(chance) == 0)
  84.                 {
  85.  
  86.                     generator.generate(world, highQualityRandom, pos);
  87.                 }
  88.             }
  89.         }
  90.     }
  91.  
  92.     private void generateRuneAltar(WorldGenerator generator, World world, Random random, int chunkX, int chunkZ, int chance, int yMin, int yMax, Class<?>... classes) {
  93.         ArrayList<Class<?>> classesList = new ArrayList<Class<?>>(Arrays.asList(classes));
  94.         HighQualityRandom highQualityRandom = new HighQualityRandom();
  95.         int x = (chunkX * 16) + random.nextInt(15);
  96.         int z = (chunkZ * 16) + random.nextInt(15);
  97.         int y = highQualityRandom.nextInt(yMax - yMin) + yMin;
  98.         BlockPos pos = new BlockPos(x, y, z);
  99.         Class<?> biome = world.provider.getBiomeForCoords(pos).getClass();
  100.         if (world.getWorldType() != WorldType.FLAT) {
  101.             if (classesList.contains(biome) || classesList.isEmpty()) {
  102.                 if (random.nextInt(chance) == 0) {
  103.                     generator.generate(world, random, pos);
  104.                 }
  105.             }
  106.         }
  107.     }
  108.  
  109.     private void generateTrees(WorldGenerator generator, World world, Random random, int chunkX, int chunkZ, int chance, Block topBlock, Class<?>... classes) {
  110.         ArrayList<Class<?>> classesList = new ArrayList<Class<?>>(Arrays.asList(classes));
  111.         int x = (chunkX * 16) + random.nextInt(15);
  112.         int z = (chunkZ * 16) + random.nextInt(15);
  113.         int y = calculateGenerationHeight(world, x, z, topBlock);
  114.         BlockPos pos = new BlockPos(x, y, z);
  115.         Class<?> biome = world.provider.getBiomeForCoords(pos).getClass();
  116.         if (world.getWorldType() != WorldType.FLAT) {
  117.             if (classesList.contains(biome)) {
  118.                 if (random.nextInt(chance) == 0) {
  119.                     generator.generate(world, random, pos);
  120.                 }
  121.             }
  122.         }
  123.     }
  124.  
  125.     private static int calculateGenerationHeight(World world, int x, int z, Block topBlock) {
  126.         int y = world.getHeight();
  127.         boolean foundGround = false;
  128.         while (!foundGround && y-- >= 0) {
  129.             Block block = world.getBlockState(new BlockPos(x, y, z)).getBlock();
  130.             foundGround = block == topBlock;
  131.         }
  132.         return y;
  133.     }
  134.  
  135.     private static List<Class> getBiomeList() {
  136.         return ForgeRegistries.BIOMES.getValues().stream().map(Biome::getBiomeClass).collect(Collectors.toList());
  137.     }
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement