Guest User

Untitled

a guest
Jun 12th, 2017
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.45 KB | None | 0 0
  1. package com.TheRPGAdventurer.server.world;
  2.  
  3. import java.util.Map.Entry;
  4. import java.util.Random;
  5.  
  6. import com.TheRPGAdventurer.RealmOfTheDragons;
  7. import com.TheRPGAdventurer.RealmOfTheDragonsConfig;
  8. import com.TheRPGAdventurer.RealmOfTheDragonsLootTables;
  9. import com.TheRPGAdventurer.server.util.Utils;
  10.  
  11. import net.minecraft.block.Block;
  12. import net.minecraft.init.Biomes;
  13. import net.minecraft.init.Blocks;
  14. import net.minecraft.tileentity.TileEntity;
  15. import net.minecraft.tileentity.TileEntityChest;
  16. import net.minecraft.util.ResourceLocation;
  17. import net.minecraft.util.math.BlockPos;
  18. import net.minecraft.world.World;
  19. import net.minecraft.world.WorldServer;
  20. import net.minecraft.world.chunk.IChunkGenerator;
  21. import net.minecraft.world.chunk.IChunkProvider;
  22. import net.minecraft.world.gen.structure.template.PlacementSettings;
  23. import net.minecraft.world.gen.structure.template.Template;
  24. import net.minecraft.world.gen.structure.template.TemplateManager;
  25. import net.minecraftforge.fml.common.IWorldGenerator;
  26.  
  27. public class ROTDWorldGenerator implements IWorldGenerator {
  28.  
  29. @Override
  30. public void generate(Random rand, int chunkX, int chunkZ, World world, IChunkGenerator chunkGenerator, IChunkProvider chunkProvider) {
  31. int blockX = chunkX * 16;
  32. int blockZ = chunkZ * 16;
  33.  
  34. switch (world.provider.getDimension()) {
  35. case -1:
  36. generateNether(world, rand, blockX, blockZ);
  37. break;
  38. case 0:
  39. generateOverworld(world, rand, blockX, blockZ);
  40. break;
  41. case 1:
  42. generateEnd(world, rand, blockX, blockZ);
  43. break;
  44. }
  45. }
  46.  
  47. private void generateNether(World world, Random rand, int chunkX, int chunkZ) {}
  48. private void generateEnd(World world, Random rand, int chunkX, int chunkZ) {}
  49.  
  50. private void generateOverworld(World world, Random rand, int blockX, int blockZ) {
  51. generateOverworldStructures(world, rand, blockX, blockZ);
  52. }
  53.  
  54. private void generateOverworldStructures(World world, Random rand, int blockX, int blockZ) {
  55.  
  56. if (RealmOfTheDragonsConfig.spawnStructures) {
  57.  
  58. WorldServer server = (WorldServer) world;
  59. TemplateManager manager = server.getStructureTemplateManager();
  60.  
  61. // templates
  62. Template testhouse = manager.getTemplate(world.getMinecraftServer(), new ResourceLocation(RealmOfTheDragons.MODID, "testhouse"));
  63.  
  64. // structures
  65. if ((int) (Math.random() * RealmOfTheDragonsConfig.testhousechance) == 0) { // testhouse chance is 1500
  66.  
  67. int randX = blockX + (int) Math.random() * 16;
  68. int randZ = blockZ + (int) Math.random() * 16;
  69. int groundY = getGroundFromAbove(world, randX, randZ);
  70. BlockPos pos = new BlockPos(randX, groundY, randZ);
  71.  
  72. if (canSpawnHere(testhouse, world, pos) && world.getBiome(pos) == Biomes.PLAINS) {
  73.  
  74. RealmOfTheDragons.LOGGER.info("Generating testhouse at " + pos);
  75. testhouse.addBlocksToWorld(world, pos, new PlacementSettings());
  76. }
  77. }
  78. }
  79. }
  80. /**
  81. * Gets the Y-value of the ground at a specifix x/y coordinate.
  82. * @param world
  83. * @param x
  84. * @param z
  85. * @return
  86. */
  87. private int getGroundFromAbove(World world, int x, int z) {
  88. int y = 255;
  89. boolean foundGround = false;
  90. while(!foundGround && y-- >= 63)
  91. {
  92. Block blockAt = world.getBlockState(new BlockPos(x,y,z)).getBlock();
  93. foundGround = blockAt == Blocks.DIRT || blockAt == Blocks.GRASS || blockAt == Blocks.SAND || blockAt == Blocks.SNOW || blockAt == Blocks.SNOW_LAYER;
  94. }
  95.  
  96. return y;
  97. }
  98.  
  99. private boolean canSpawnHere(Template template, World world, BlockPos posAboveGround) {
  100. int zwidth = template.getSize().getZ();
  101. int xwidth = template.getSize().getX();
  102.  
  103. // check all the corners to see which ones are replaceable
  104. boolean corner1 = isCornerValid(world, posAboveGround);
  105. boolean corner2 = isCornerValid(world, posAboveGround.add(xwidth, 0, zwidth));
  106.  
  107. // if Y > 20 and all corners pass the test, it's okay to spawn the structure
  108. return posAboveGround.getY() > 63 && corner1 && corner2;
  109. }
  110.  
  111. private boolean isCornerValid(World world, BlockPos pos) {
  112. int variation = 3;
  113. int highestBlock = getGroundFromAbove(world, pos.getX(), pos.getZ());
  114.  
  115. if (highestBlock > pos.getY() - variation && highestBlock < pos.getY() + variation)
  116. return true;
  117.  
  118. RealmOfTheDragons.LOGGER.info("Canceling gen for this structure.");
  119.  
  120. return true;
  121. }
  122.  
  123. }
Advertisement
Add Comment
Please, Sign In to add comment