Advertisement
HalestormXV

Untitled

Apr 9th, 2018
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.91 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, getBiomeList().toArray(new Class[getBiomeList().size()]));
  47.                 generateRuneAltar(new WorldGenStructure("air_temple"), world, random, chunkX, chunkZ, 360, 98, 176);
  48.                 generateRuneAltar(new WorldGenStructure("earth_temple"), world, random, chunkX, chunkZ, 280, 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.  
  63.     private void generateStructures(WorldGenerator generator, World world, Random random, int chunkX, int chunkZ, int chance, Class<?>... classes) {
  64.         HighQualityRandom highQualityRandom = new HighQualityRandom();
  65.         ArrayList<Class<?>> classesList = new ArrayList<Class<?>>(Arrays.asList(classes));
  66.         int x = (chunkX * 16) + highQualityRandom.nextInt(15);
  67.         int z = (chunkZ * 16) + highQualityRandom.nextInt(15);
  68.         float y = calculateGenerationHeight(world, x, z);
  69.         for (int xSize = 0; xSize < 9; x++)
  70.         {
  71.             for (int zSize = 0; zSize < 7; z++)
  72.             {
  73.                 int oldY = Math.round(y);
  74.                 y += calculateGenerationHeight(world, x + xSize, z + zSize);
  75.                 y /= 2;
  76.                 if (Math.round(y) != oldY)
  77.                 {
  78.                     return;
  79.                 }
  80.             }
  81.         }
  82.         int struc_y = (int) y - 1;
  83.         BlockPos pos = new BlockPos(x, struc_y, z);
  84.         Class<?> biome = world.provider.getBiomeForCoords(pos).getClass();
  85.         if (world.getWorldType() != WorldType.FLAT) {
  86.             if (classesList.contains(biome) || classesList.isEmpty()) {
  87.                 if (highQualityRandom.nextInt(chance) == 0)
  88.                 {
  89.  
  90.                     generator.generate(world, highQualityRandom, pos);
  91.                 }
  92.             }
  93.         }
  94.     }
  95.  
  96.     private void generateRuneAltar(WorldGenerator generator, World world, Random random, int chunkX, int chunkZ, int chance, int yMin, int yMax, Class<?>... classes) {
  97.         ArrayList<Class<?>> classesList = new ArrayList<Class<?>>(Arrays.asList(classes));
  98.         HighQualityRandom highQualityRandom = new HighQualityRandom();
  99.         int x = (chunkX * 16) + random.nextInt(15);
  100.         int z = (chunkZ * 16) + random.nextInt(15);
  101.         int y = highQualityRandom.nextInt(yMax - yMin) + yMin;
  102.         BlockPos pos = new BlockPos(x, y, z);
  103.         Class<?> biome = world.provider.getBiomeForCoords(pos).getClass();
  104.         if (world.getWorldType() != WorldType.FLAT) {
  105.             if (classesList.contains(biome) || classesList.isEmpty()) {
  106.                 if (random.nextInt(chance) == 0) {
  107.                     generator.generate(world, random, pos);
  108.                 }
  109.             }
  110.         }
  111.     }
  112.  
  113.     private void generateTrees(WorldGenerator generator, World world, Random random, int chunkX, int chunkZ, int chance, Block topBlock, Class<?>... classes) {
  114.         ArrayList<Class<?>> classesList = new ArrayList<Class<?>>(Arrays.asList(classes));
  115.         int x = (chunkX * 16) + random.nextInt(15);
  116.         int z = (chunkZ * 16) + random.nextInt(15);
  117.         int y = calculateGenerationHeight(world, x, z);
  118.         BlockPos pos = new BlockPos(x, y, z);
  119.         Class<?> biome = world.provider.getBiomeForCoords(pos).getClass();
  120.         if (world.getWorldType() != WorldType.FLAT) {
  121.             if (classesList.contains(biome)) {
  122.                 if (random.nextInt(chance) == 0) {
  123.                     generator.generate(world, random, pos);
  124.                 }
  125.             }
  126.         }
  127.     }
  128.  
  129.     private static int calculateGenerationHeight(World world, int x, int z) {
  130.         int y = world.getHeight();
  131.         boolean foundGround = false;
  132.         while (!foundGround && y-- >= 64)
  133.         {
  134.             Block blockAt = world.getBlockState(new BlockPos(x, y, z)).getBlock();
  135.             foundGround =  blockAt == Blocks.WATER
  136.                     || blockAt == Blocks.FLOWING_WATER
  137.                     || blockAt == Blocks.GRASS
  138.                     || blockAt == Blocks.SAND
  139.                     || blockAt == Blocks.SNOW
  140.                     || blockAt == Blocks.SNOW_LAYER
  141.                     || blockAt == Blocks.GLASS
  142.                     || blockAt == Blocks.DIRT
  143.                     || blockAt == Blocks.MYCELIUM;
  144.         }
  145.         return y;
  146.     }
  147.  
  148.     private static List<Class> getBiomeList() {
  149.         return ForgeRegistries.BIOMES.getValues().stream().map(Biome::getBiomeClass).collect(Collectors.toList());
  150.     }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement