Advertisement
Guest User

Untitled

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