Jump to content

[1.7.2] world.getblock and check room for spawning


nightscout01

Recommended Posts

So, I am trying to get the current block of some cords: namely (x,y,z) and then return if the block is different then the one listed. However, I do not know how to check for multiple blocks. I have tried the java "or" function and tried an array, how does one go about this, nighscout01

 

My world.getblock code:

if (world.getBlock(x, y, z) !=Blocks.grass) return;
if (world.getBlock(x + 15, y, z) != Blocks.grass) return;
if (world.getBlock(x + 15, y, z + 12) != Blocks.grass) return;
if (world.getBlock(x, y, z + 12) != Blocks.grass) return;

 

On a similar note, this is the "check room and blocks for spawning" file the converter gives me, however, I cannot figure out on how to implement this method of doing it into my current structure gen code.

 

protected Block[] GetValidSpawnBlocks()
{
	return new Block[]
	{
		Blocks.grass,
	};
}

public boolean LocationIsValidSpawn(World world, int i, int j, int k)
{
	int distanceToAir = 0;
	Block checkBlock = world.getBlock(i, j, k);

	while (checkBlock != Blocks.air)
	{
		distanceToAir++;
		checkBlock = world.getBlock(i, j + distanceToAir, k);
	}
	if (distanceToAir > 1)
	{
		return false;
	}
	j += distanceToAir - 1;

	Block block = world.getBlock(i, j, k);
	Block blockAbove = world.getBlock(i, j + 1, k);
	Block blockBelow = world.getBlock(i, j - 1, k);

	for (Block l : GetValidSpawnBlocks())
	{
		if (blockAbove != Blocks.air)
		{
			return false;
		}
		if (block == l)
		{
			return true;
		}
		else if (block == Blocks.snow_layer && blockBelow == l)
		{
			return true;
		}
		else if (block.getMaterial() == Material.plants && blockBelow == l)
		{
			return true;
		}
	}
	return false;
}

 

My current structure gen file

 

 

package net.morestructures.mod.worldgen;

import static net.minecraftforge.common.ChestGenHooks.DUNGEON_CHEST;
import static net.minecraftforge.common.ChestGenHooks.PYRAMID_JUNGLE_CHEST;

import java.util.Random;

import net.minecraft.block.Block;
import net.minecraft.init.Blocks;
import net.minecraft.tileentity.TileEntityChest;
import net.minecraft.tileentity.TileEntityMobSpawner;
import net.minecraft.util.WeightedRandomChestContent;
import net.minecraft.world.World;
import net.minecraft.world.biome.BiomeGenBase;
import net.minecraft.world.chunk.IChunkProvider;
import net.minecraftforge.common.ChestGenHooks;
import cpw.mods.fml.common.IWorldGenerator;

public class SwampTemple implements IWorldGenerator {




public void generate(Random random, int chunkX, int chunkZ, World world, IChunkProvider chunkGenerator, IChunkProvider chunkProvider) 
{
if (world.provider.dimensionId == 0)
generateSurface(world, random, chunkX * 16, chunkZ * 16);
}

private void generateSurface(World world, Random random, int i, int k)
{

int N = 1;


if(random.nextInt(N) != 0) return;


int x = i + random.nextInt(15);
int z = k + random.nextInt(15);

int y = world.getHeightValue(x, z)-1; //Get the top block



if(world.getWorldChunkManager().getBiomeGenAt(x, y)!=BiomeGenBase.swampland) return;

//Get the right block
if (world.getBlock(x, y, z) !=Blocks.grass) return;
if (world.getBlock(x + 15, y, z) != Blocks.grass) return;
if (world.getBlock(x + 15, y, z + 12) != Blocks.grass) return;
if (world.getBlock(x, y, z + 12) != Blocks.grass) return;



//debugging information
System.out.println("Generating a structure");


generate0(world, random, x, y, z);
}


private void generate0(World world, Random random, int x, int y, int z) //I recommend replacing i,j,k with x,y,z. More intuitive
{
world.setBlock.blockname
ect ect ect

 

 

Thanks, and please help, nightscout01

Link to comment
Share on other sites

I'm not exactly sure what you want to do.

 

From what I understood you want a function that checks if there are certain blocks in certain locations, and return if there isn't?

 

If so your method should be a boolean rather than a void, and then return false if any block isn't what you want it to be, and then otherwise return true.

 

Something like

public boolean blockCheck(World world, int x, int y, int z)
{
    if(world.getBlock(x,y,z) != Blocks.grass
                || world.getBlock(x + 15, y, z) != Blocks.grass
                || world.getBlock(x + 15, y, z + 12) != Blocks.grass
                || world.getBlock(x, y, z + 12) != Blocks.grass)
    {
        return false;
    }
    return true;
}

BEFORE ASKING FOR HELP READ THE EAQ!

 

I'll help if I can. Apologies if I do something obviously stupid. :D

 

If you don't know basic Java yet, go and follow these tutorials.

Link to comment
Share on other sites

You could do something like this:

public Block[] array = {Blocks.grass, whateverOtherBlocks}

 

and then later to check if it's in the array:

boolean flag = false;

for(Block block : array)
{
    if(world.getBlock(x, y, z) == block)
    {
        flag = true;
    }
}

if(!flag)
{
    return;
}

 

If you're only using the array once, or it's really small, then you could just use the || (or) operator. I'm not sure what you mean by it not working, but it would look something like

Block block = world.getBlock(x,y,z);

if(block == Blocks.grass || block == anotherBlock || block == anotherBlockAsWell)
{
    //DO WHATEVER
}
else
{
    return;
}

BEFORE ASKING FOR HELP READ THE EAQ!

 

I'll help if I can. Apologies if I do something obviously stupid. :D

 

If you don't know basic Java yet, go and follow these tutorials.

Link to comment
Share on other sites

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.