Jump to content

Recommended Posts

Posted

So I'm currently trying to spawn my custom dungeon in separate places, but they 100% of the time, spawn right next to each other and intersect.

 

Segment of code from my IWorldGenerator:

public void generateSurface(World world, Random random, int chunkX, int chunkZ)
{

	for(int i = 0;i < 2;i++)
	{

		int x = chunkZ + random.nextInt(16) - random.nextInt(16);
		int y = random.nextInt(64);
		int z = chunkZ + random.nextInt(16) - random.nextInt(16);

		WorldGenGoldDungeons gen = new WorldGenGoldDungeons();

		if(gen.generate(world, random, x, y, z))
		{

			System.out.println("X: " + x + "Y: " +  y + "Z: " + z);

		}

	}

}

 

And here is my WorldGenerator class:

 

package net.gloriarpg.worldgen;

import java.util.Random;

import net.gloriarpg.helper.BlockHelper;
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.item.Item;
import net.minecraft.tileentity.TileEntityChest;
import net.minecraft.tileentity.TileEntityMobSpawner;
import net.minecraft.util.WeightedRandomChestContent;
import net.minecraft.world.World;
import net.minecraft.world.gen.feature.WorldGenerator;
import net.minecraftforge.common.ChestGenHooks;
import net.minecraftforge.common.DungeonHooks;


public class WorldGenGoldDungeons extends WorldGenerator
{

    public boolean generate(World par1World, Random par2Random, int par3, int par4, int par5)
    {
        byte b0 = 3;
        int l = par2Random.nextInt(2) + 2;
        int i1 = par2Random.nextInt(2) + 2;
        int j1 = 0;
        int k1;
        int l1;
        int i2;
        
        int plank = par2Random.nextInt(2);

        for (k1 = par3 - l - 1; k1 <= par3 + l + 1; ++k1)
        {
            for (l1 = par4 - 1; l1 <= par4 + b0 + 1; ++l1)
            {
                for (i2 = par5 - i1 - 1; i2 <= par5 + i1 + 1; ++i2)
                {
                    Material material = par1World.getBlockMaterial(k1, l1, i2);

                    if (l1 == par4 - 1 && !material.isSolid())
                    {
                        return false;
                    }

                    if (l1 == par4 + b0 + 1 && !material.isSolid())
                    {
                        return false;
                    }

                    if ((k1 == par3 - l - 1 || k1 == par3 + l + 1 || i2 == par5 - i1 - 1 || i2 == par5 + i1 + 1) && l1 == par4 && par1World.isAirBlock(k1, l1, i2) && par1World.isAirBlock(k1, l1 + 1, i2))
                    {
                        ++j1;
                    }
                }
            }
        }

        if (j1 >= 1 && j1 <= 5)
        {
            for (k1 = par3 - l - 1; k1 <= par3 + l + 1; ++k1)
            {
                for (l1 = par4 + b0; l1 >= par4 - 1; --l1)
                {
                    for (i2 = par5 - i1 - 1; i2 <= par5 + i1 + 1; ++i2)
                    {
                        if (k1 != par3 - l - 1 && l1 != par4 - 1 && i2 != par5 - i1 - 1 && k1 != par3 + l + 1 && l1 != par4 + b0 + 1 && i2 != par5 + i1 + 1)
                        {
                            par1World.setBlockToAir(k1, l1, i2);
                        }
                        else if (l1 >= 0 && !par1World.getBlockMaterial(k1, l1 - 1, i2).isSolid())
                        {
                            par1World.setBlockToAir(k1, l1, i2);
                        }
                        else if (par1World.getBlockMaterial(k1, l1, i2).isSolid())
                        {
                            if (l1 == par4 - 1 && par2Random.nextInt(4) != 0)
                            {
                                par1World.setBlock(k1, l1, i2, Block.planks.blockID, plank, 2);
                            }
                            else
                            {
                                par1World.setBlock(k1, l1, i2, Block.planks.blockID, 0, 2);
                            }
                        }
                    }
                }
            }

            k1 = 0;

            while (k1 < 2)
            {
                l1 = 0;

                while (true)
                {
                    if (l1 < 3)
                    {
                        label101:
                        {
                            i2 = par3 + par2Random.nextInt(l * 2 + 1) - l;
                            int j2 = par5 + par2Random.nextInt(i1 * 2 + 1) - i1;

                            if (par1World.isAirBlock(i2, par4, j2))
                            {
                                int k2 = 0;

                                if (par1World.getBlockMaterial(i2 - 1, par4, j2).isSolid())
                                {
                                    ++k2;
                                }

                                if (par1World.getBlockMaterial(i2 + 1, par4, j2).isSolid())
                                {
                                    ++k2;
                                }

                                if (par1World.getBlockMaterial(i2, par4, j2 - 1).isSolid())
                                {
                                    ++k2;
                                }

                                if (par1World.getBlockMaterial(i2, par4, j2 + 1).isSolid())
                                {
                                    ++k2;
                                }

                                if (k2 == 1)
                                {
                                    par1World.setBlock(i2, par4, j2, BlockHelper.goldChest.blockID, 0, 2);
                                    TileEntityChest tileentitychest = (TileEntityChest)par1World.getBlockTileEntity(i2, par4, j2);

                                    if (tileentitychest != null)
                                    {
                                        ChestGenHooks info = ChestGenHooks.getInfo(ChestGenHooks.DUNGEON_CHEST);
                                        WeightedRandomChestContent.generateChestContents(par2Random, info.getItems(par2Random), tileentitychest, info.getCount(par2Random));
                                    }

                                    break label101;
                                }
                            }

                            ++l1;
                            continue;
                        }
                    }

                    ++k1;
                    break;
                }
            }

            par1World.setBlock(par3, par4, par5, Block.mobSpawner.blockID, 0, 2);
            TileEntityMobSpawner tileentitymobspawner = (TileEntityMobSpawner)par1World.getBlockTileEntity(par3, par4, par5);

            if (tileentitymobspawner != null)
            {
                tileentitymobspawner.getSpawnerLogic().setMobID(this.pickMobSpawner(par2Random));
            }
            else
            {
                System.err.println("Failed to fetch mob spawner entity at (" + par3 + ", " + par4 + ", " + par5 + ")");
            }

            return true;
        }
        else
        {
            return false;
        }
    }
    
    private String pickMobSpawner(Random par1Random)
    {
    	
        return DungeonHooks.getRandomDungeonMob(par1Random);
        
    }

}

 

Kain

Posted

How do you set your random in your IWorldGenerator?

 

Also it does not appear you check to see whether or not you should be generating in a given chunk.  You're telling it to generate and passing a random, but not figuring out if you should.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Posted

Here's the whole IWorldGenerator:

 

@Override
public void generate(Random random, int chunkX, int chunkZ, World world, IChunkProvider chunkGenerator, IChunkProvider chunkProvider)
{

	switch(world.provider.dimensionId)
	{

	case(0):
	{

		this.generateSurface(world, random, chunkX, chunkZ);
		break;

	}

	}

}

public void generateSurface(World world, Random random, int chunkX, int chunkZ)
{

	for(int i = 0;i < 2;i++)
	{

		int x = chunkZ + random.nextInt(16) - random.nextInt(16);
		int y = random.nextInt(64);
		int z = chunkZ + random.nextInt(16) - random.nextInt(16);

		WorldGenGoldDungeons gen = new WorldGenGoldDungeons();

		if(gen.generate(world, random, x, y, z))
		{

			System.out.println("X: " + x + "Y: " +  y + "Z: " + z);

		}

	}

}

 

 

I'll go fix that.

Kain

Posted

WorldGenGoldDungeons gen = new WorldGenGoldDungeons();

 

You should move this to a class-level variable, rather than creating a new one every time a chunk is generated.

 

Also, you may be interested in this:

https://github.com/Draco18s/Artifacts/blob/master/draco18s/artifacts/worldgen/PlaceTraps.java

 

I use math to determine where things spawn, rather than randoms.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.