Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.TheRPGAdventurer.server.world;
- import java.util.Map.Entry;
- import java.util.Random;
- import com.TheRPGAdventurer.RealmOfTheDragons;
- import com.TheRPGAdventurer.RealmOfTheDragonsConfig;
- import com.TheRPGAdventurer.RealmOfTheDragonsLootTables;
- import com.TheRPGAdventurer.server.util.Utils;
- import net.minecraft.block.Block;
- import net.minecraft.init.Biomes;
- import net.minecraft.init.Blocks;
- import net.minecraft.tileentity.TileEntity;
- import net.minecraft.tileentity.TileEntityChest;
- import net.minecraft.util.ResourceLocation;
- import net.minecraft.util.math.BlockPos;
- import net.minecraft.world.World;
- import net.minecraft.world.WorldServer;
- import net.minecraft.world.chunk.IChunkGenerator;
- import net.minecraft.world.chunk.IChunkProvider;
- import net.minecraft.world.gen.structure.template.PlacementSettings;
- import net.minecraft.world.gen.structure.template.Template;
- import net.minecraft.world.gen.structure.template.TemplateManager;
- import net.minecraftforge.fml.common.IWorldGenerator;
- public class ROTDWorldGenerator implements IWorldGenerator {
- @Override
- public void generate(Random rand, int chunkX, int chunkZ, World world, IChunkGenerator chunkGenerator, IChunkProvider chunkProvider) {
- int blockX = chunkX * 16;
- int blockZ = chunkZ * 16;
- switch (world.provider.getDimension()) {
- case -1:
- generateNether(world, rand, blockX, blockZ);
- break;
- case 0:
- generateOverworld(world, rand, blockX, blockZ);
- break;
- case 1:
- generateEnd(world, rand, blockX, blockZ);
- break;
- }
- }
- private void generateNether(World world, Random rand, int chunkX, int chunkZ) {}
- private void generateEnd(World world, Random rand, int chunkX, int chunkZ) {}
- private void generateOverworld(World world, Random rand, int blockX, int blockZ) {
- generateOverworldStructures(world, rand, blockX, blockZ);
- }
- private void generateOverworldStructures(World world, Random rand, int blockX, int blockZ) {
- if (RealmOfTheDragonsConfig.spawnStructures) {
- WorldServer server = (WorldServer) world;
- TemplateManager manager = server.getStructureTemplateManager();
- // templates
- Template testhouse = manager.getTemplate(world.getMinecraftServer(), new ResourceLocation(RealmOfTheDragons.MODID, "testhouse"));
- // structures
- if ((int) (Math.random() * RealmOfTheDragonsConfig.testhousechance) == 0) { // testhouse chance is 1500
- int randX = blockX + (int) Math.random() * 16;
- int randZ = blockZ + (int) Math.random() * 16;
- int groundY = getGroundFromAbove(world, randX, randZ);
- BlockPos pos = new BlockPos(randX, groundY, randZ);
- if (canSpawnHere(testhouse, world, pos) && world.getBiome(pos) == Biomes.PLAINS) {
- RealmOfTheDragons.LOGGER.info("Generating testhouse at " + pos);
- testhouse.addBlocksToWorld(world, pos, new PlacementSettings());
- }
- }
- }
- }
- /**
- * Gets the Y-value of the ground at a specifix x/y coordinate.
- * @param world
- * @param x
- * @param z
- * @return
- */
- private int getGroundFromAbove(World world, int x, int z) {
- int y = 255;
- boolean foundGround = false;
- while(!foundGround && y-- >= 63)
- {
- Block blockAt = world.getBlockState(new BlockPos(x,y,z)).getBlock();
- foundGround = blockAt == Blocks.DIRT || blockAt == Blocks.GRASS || blockAt == Blocks.SAND || blockAt == Blocks.SNOW || blockAt == Blocks.SNOW_LAYER;
- }
- return y;
- }
- private boolean canSpawnHere(Template template, World world, BlockPos posAboveGround) {
- int zwidth = template.getSize().getZ();
- int xwidth = template.getSize().getX();
- // check all the corners to see which ones are replaceable
- boolean corner1 = isCornerValid(world, posAboveGround);
- boolean corner2 = isCornerValid(world, posAboveGround.add(xwidth, 0, zwidth));
- // if Y > 20 and all corners pass the test, it's okay to spawn the structure
- return posAboveGround.getY() > 63 && corner1 && corner2;
- }
- private boolean isCornerValid(World world, BlockPos pos) {
- int variation = 3;
- int highestBlock = getGroundFromAbove(world, pos.getX(), pos.getZ());
- if (highestBlock > pos.getY() - variation && highestBlock < pos.getY() + variation)
- return true;
- RealmOfTheDragons.LOGGER.info("Canceling gen for this structure.");
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment