Advertisement
HalestormXV

Untitled

Apr 6th, 2018
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.12 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, 80, 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.  
  63.     private void generateStructures(WorldGenerator generator, World world, Random random, int chunkX, int chunkZ, int chance, Block topBlock, 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.         int y = calculateGenerationHeight(world, x, z, topBlock);
  69.         BlockPos pos = new BlockPos(x, y, z);
  70.         Class<?> biome = world.provider.getBiomeForCoords(pos).getClass();
  71.         if (world.getWorldType() != WorldType.FLAT) {
  72.             if (classesList.contains(biome) || classesList.isEmpty()) {
  73.                 if (highQualityRandom.nextInt(chance) == 0) {
  74.                     generator.generate(world, highQualityRandom, pos);
  75.                 }
  76.             }
  77.         }
  78.     }
  79.  
  80.     private void generateRuneAltar(WorldGenerator generator, World world, Random random, int chunkX, int chunkZ, int chance, int yMin, int yMax, Class<?>... classes) {
  81.         ArrayList<Class<?>> classesList = new ArrayList<Class<?>>(Arrays.asList(classes));
  82.         HighQualityRandom highQualityRandom = new HighQualityRandom();
  83.         int x = (chunkX * 16) + random.nextInt(15);
  84.         int z = (chunkZ * 16) + random.nextInt(15);
  85.         int y = highQualityRandom.nextInt(yMax - yMin) + yMin;
  86.         BlockPos pos = new BlockPos(x, y, z);
  87.         Class<?> biome = world.provider.getBiomeForCoords(pos).getClass();
  88.         if (world.getWorldType() != WorldType.FLAT) {
  89.             if (classesList.contains(biome) || classesList.isEmpty()) {
  90.                 if (random.nextInt(chance) == 0) {
  91.                     generator.generate(world, random, pos);
  92.                 }
  93.             }
  94.         }
  95.     }
  96.  
  97.     private void generateTrees(WorldGenerator generator, World world, Random random, int chunkX, int chunkZ, int chance, Block topBlock, Class<?>... classes) {
  98.         ArrayList<Class<?>> classesList = new ArrayList<Class<?>>(Arrays.asList(classes));
  99.         int x = (chunkX * 16) + random.nextInt(15);
  100.         int z = (chunkZ * 16) + random.nextInt(15);
  101.         int y = calculateGenerationHeight(world, x, z, topBlock);
  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)) {
  106.                 if (random.nextInt(chance) == 0) {
  107.                     generator.generate(world, random, pos);
  108.                 }
  109.             }
  110.         }
  111.     }
  112.  
  113.     private static int calculateGenerationHeight(World world, int x, int z, Block topBlock) {
  114.         int y = world.getHeight();
  115.         boolean foundGround = false;
  116.         while (!foundGround && y-- >= 0) {
  117.             Block block = world.getBlockState(new BlockPos(x, y, z)).getBlock();
  118.             foundGround = block == topBlock;
  119.         }
  120.         return y;
  121.     }
  122.  
  123.     private static List<Class> getBiomeList() {
  124.         return ForgeRegistries.BIOMES.getValues().stream().map(Biome::getBiomeClass).collect(Collectors.toList());
  125.     }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement