Advertisement
Guest User

Untitled

a guest
Jan 26th, 2018
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.89 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.chunk.IChunkProvider;
  17. import net.minecraft.world.gen.IChunkGenerator;
  18. import net.minecraft.world.gen.feature.WorldGenerator;
  19. import net.minecraftforge.fml.common.IWorldGenerator;
  20. import net.minecraftforge.fml.common.registry.ForgeRegistries;
  21. import scala.actors.threadpool.Arrays;
  22. import java.util.ArrayList;
  23. import java.util.Random;
  24.  
  25. public class WorldGenCustomStuffs implements IWorldGenerator
  26. {
  27.     /*###STRUCTURES###*/
  28.     private static final WorldGenStructure CULTIST_HUT = new WorldGenStructure("cultist_hut");
  29.  
  30.     /*###TREES###*/
  31.     private final WorldGenerator LUPRESIUM_TREE = new WorldGenLupresiumTree();
  32.     private final WorldGenerator MYSTIC_TREE = new WorldGenMysticTree();
  33.     private Block lupresiumDirt = BlockInit.DIRT.getDefaultState().withProperty(BlockDirts.VARIANT, EnumHandlerWood.EnumTypeWood.LUPRESIUM).getBlock();
  34.     private Block mysticDirt = BlockInit.DIRT.getDefaultState().withProperty(BlockDirts.VARIANT, EnumHandlerWood.EnumTypeWood.MYSTIC).getBlock();
  35.  
  36.     @Override
  37.     public void generate(Random random, int chunkX, int chunkZ, World world, IChunkGenerator chunkGenerator, IChunkProvider chunkProvider)
  38.     {
  39.         switch(world.provider.getDimension())
  40.         {
  41.             case -1:
  42.                 break;
  43.  
  44.             case 0:
  45.                 generateStructures(CULTIST_HUT, world, random, chunkX, chunkZ, 5, Blocks.GRASS);
  46.                 generateTrees(LUPRESIUM_TREE, world, random, chunkX, chunkZ, 10, lupresiumDirt, BiomeLupresiumForest.class);
  47.                 generateTrees(MYSTIC_TREE, world, random, chunkX, chunkZ, 10, mysticDirt, BiomeMysticLands.class);
  48.                 break;
  49.  
  50.             case 1:
  51.                 break;
  52.  
  53.             case 2:
  54.                 break;
  55.  
  56.         }
  57.  
  58.     }
  59.  
  60.     //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.
  61.     private void generateStructures(WorldGenerator generator, World world, Random random, int chunkX, int chunkZ, int chance, Block topBlock, Class<?>... classes)
  62.     {
  63.         ArrayList<Class<?>> classesList = new ArrayList<Class<?>>(Arrays.asList(classes));
  64.  
  65.         int x = (chunkX * 16) + random.nextInt(15);
  66.         int z = (chunkZ * 16) + random.nextInt(15);
  67.         int y = calculateGenerationHeight(world, x, z, topBlock);
  68.         BlockPos pos = new BlockPos(x,y,z);
  69.         Class<?> biome = world.provider.getBiomeForCoords(pos).getClass();
  70.  
  71.         if(world.getWorldType() != WorldType.FLAT)
  72.         {
  73.             if(classesList.contains(biome) || classesList.isEmpty())
  74.             {
  75.                 if(random.nextInt(chance) == 0)
  76.                 {
  77.                     //Greater the Chance the Less Likely to spawn. If chance = 100 there is a 1 in a 100 chance of it being 0
  78.                     generator.generate(world, random, pos);
  79.                 }
  80.             }
  81.         }
  82.     }
  83.  
  84.     private void generateTrees(WorldGenerator generator, World world, Random random, int chunkX, int chunkZ, int chance, Block topBlock, Class<?>... classes)
  85.     {
  86.         ArrayList<Class<?>> classesList = new ArrayList<Class<?>>(Arrays.asList(classes));
  87.  
  88.         int x = (chunkX * 16) + random.nextInt(15);
  89.         int z = (chunkZ * 16) + random.nextInt(15);
  90.         int y = calculateGenerationHeight(world, x, z, topBlock);
  91.         BlockPos pos = new BlockPos(x,y,z);
  92.         Class<?> biome = world.provider.getBiomeForCoords(pos).getClass();
  93.  
  94.         if(world.getWorldType() != WorldType.FLAT)
  95.         {
  96.             if(classesList.contains(biome))
  97.             {
  98.                 if(random.nextInt(chance) == 0)
  99.                 {
  100.                     //Greater the Chance the Less Likely to spawn. If chance = 100 there is a 1 in a 100 chance of it being 0
  101.                     generator.generate(world, random, pos);
  102.                 }
  103.             }
  104.         }
  105.     }
  106.  
  107.     private static int calculateGenerationHeight(World world, int x, int z, Block topBlock)
  108.     {
  109.         int y = world.getHeight();
  110.         boolean foundGround = false;
  111.         while (!foundGround && y-- >= 0)
  112.         {
  113.             Block block = world.getBlockState(new BlockPos(x,y,z)).getBlock();
  114.             foundGround = block == topBlock;
  115.         }
  116.         return y;
  117.     }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement