Jump to content

[1.8] Issues with village crop spawning!


HeXHaX

Recommended Posts

I am making a basic mod that adds a few new crops.  I know this is overdone; I'm just getting my feet wet.  Anyways, the issue I'm having is abnormal spawning of village crops.

 

Basic overview of my code:

For each x, z coordinate in each chunk, get the height of the terrain.

If the block at that position is wheat, potatoes, or carrots, then call the spawnCrops function (I am using a 100 percent chance of spawning for testing here.)

Inside this spawnCrops function, if the crop's random spawn number is withing the spawn range, then use two while loops to iterate through all the crops in the row in both directions, replacing them with the mod crop chosen for that row.

 

My issue:

    The crops do not fill up the entire row.

 

Village Crop Generation class:

package com.hexhax.xpansion.worldGen;

import java.util.ArrayList;
import java.util.Random;

import net.minecraft.block.Block;
import net.minecraft.block.properties.PropertyInteger;
import net.minecraft.block.state.IBlockState;
import net.minecraft.init.Blocks;
import net.minecraft.util.BlockPos;
import net.minecraft.world.World;

import com.hexhax.xpansion.common.EnumCrops;
import com.hexhax.xpansion.init.XpansionBlocks;

public class GenerateVillageCrops {
public static boolean spawnCrops(World world, BlockPos pos, Random rand, int spawnChance, Block cropAtPos)
{
	int direction = getRowDirection(world, pos);
	/*
	 * cropChoice can have values between 1 and 2. 
	 */
	int cropChoice = rand.nextInt(3) + 1;

	if( rand.nextInt(100) < spawnChance )
	{
		boolean flag = false;
		if( direction == 1 )
		{
			BlockPos newPos = new BlockPos( pos );
			while( !flag )
			{
				if( world.getBlockState(newPos.east()).getBlock() == cropAtPos )
				{
					spawnOneCrop(world, newPos, cropChoice, rand);
					newPos = newPos.east();
				}
				else
				{
					flag = true;
				}
			}
			flag = false;
			newPos = new BlockPos( pos );
			while( !flag )
			{
				if( world.getBlockState(newPos.west()).getBlock() == cropAtPos )
				{
					spawnOneCrop(world, newPos, cropChoice, rand);
					newPos = newPos.west();
				}
				else
				{
					flag = true;
				}
			}
		}
		else if( direction == 2 )
		{
			BlockPos newPos = new BlockPos( pos );
			while( !flag )
			{
				if( world.getBlockState(newPos.north()).getBlock() == cropAtPos )
				{
					spawnOneCrop(world, newPos, cropChoice, rand);
					newPos = newPos.north();
				}
				else
				{
					flag = true;
				}
			}
			flag = false;
			newPos = new BlockPos( pos );
			while( !flag )
			{
				if( world.getBlockState(newPos.south()).getBlock() == cropAtPos )
				{
					spawnOneCrop(world, newPos, cropChoice, rand);
					newPos = newPos.south();
				}
				else
				{
					flag = true;
				}
			}
		}
		return true;
	}

	return false;
}

/*
 * Returns an integer telling the direction of the crop row.
 */
private static int getRowDirection(World world, BlockPos pos)
{
	boolean north = false;
	boolean south = false;
	boolean east = false;
	boolean west = false;

	if( isBlockReplacablePlant(world, pos) )
	{
		if( isBlockReplacablePlant(world, pos.north(1)) && isBlockReplacablePlant(world, pos.north(2)) )
		{
			north = true;
		}
		if( isBlockReplacablePlant(world, pos.south(1)) && isBlockReplacablePlant(world, pos.south(2)) )
		{
			south = true;
		}
		if( isBlockReplacablePlant(world, pos.east(1)) && isBlockReplacablePlant(world, pos.east(2)) )
		{
			east = true;
		}
		if( isBlockReplacablePlant(world, pos.west(1)) && isBlockReplacablePlant(world, pos.west(2)) )
		{
			west = true;
		}
		if( north || south )
		{
			return 1; 
		}
		else if( east || west )
		{
			return 2;
		}
	}
	return 0;
}

private static boolean isBlockReplacablePlant(World world, BlockPos pos)
{
	Block block = world.getBlockState(pos).getBlock();
	if( block == Blocks.wheat || block == Blocks.potatoes || block == Blocks.carrots )
	{
		return true;
	}
	return false;
}

private static void spawnOneCrop(World world, BlockPos pos, int cropChoice, Random rand)
{
	switch( cropChoice )
	{
	case 1:
		world.setBlockState(pos, XpansionBlocks.tomato_crop.getDefaultState().withProperty(PropertyInteger.create("age", 0, 7), rand.nextInt());
		break;
	case 2:
		world.setBlockState(pos, XpansionBlocks.tomato_crop.getDefaultState());
		break;
	}
}
}

 

Overworld World Generation Function:

private void GenerateOverworld(World world, int i, int j, Random random) {
	addOre(XpansionBlocks.copper_ore, Blocks.stone, random, world, i, j, 15, 60, 4, 10, 20);

	for( int l=i; l<i+16; l++)
	{
		for( int m=j; m<j+16; m++)
		{
			int k = 128;
			boolean isAir = true;
			BlockPos iterBlockPos = new BlockPos(l, k, m);
			while( isAir )
			{
				iterBlockPos = iterBlockPos.down();
				isAir = world.getBlockState(iterBlockPos).getBlock() == Blocks.air;
				k--;
			}

			BlockPos currentPosition = new BlockPos(l, k, m);
			if( world.getBlockState(currentPosition).getBlock() == Blocks.wheat || world.getBlockState(currentPosition).getBlock() == Blocks.carrots || world.getBlockState(currentPosition).getBlock() == Blocks.potatoes )
			{
				GenerateVillageCrops.spawnCrops(world, new BlockPos( l, k, m ), random, 100, world.getBlockState(currentPosition).getBlock() );
			}
		}
	}
}

Link to comment
Share on other sites

Screenshot of the generated crops would be needed to diagnose this issue.

I. Stellarium for Minecraft: Configurable Universe for Minecraft! (WIP)

II. Stellar Sky, Better Star Rendering&Sky Utility mod, had separated from Stellarium.

Link to comment
Share on other sites

In cases where your code doesn't do what you expect you need to go back to standard debugging -- meaning you need to trace the code execution. I do it by putting in console statements at every critical point in the execution. If you printed out the values that your loop is generated for example, I think you'd quickly find out why the math isn't working the way you expect.

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

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.