HalestormXV

GenCustomStuffs

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