Jump to content

Recommended Posts

Posted

I'm making a little mod with food, armors and tools and I need to generate my crops inside my world. (Like Wild Crops)

One should spawn only near water like sugar canes one randomly on a farmland block. I have already my seeds and crops but my WorldGen class for my crops is empty because I don't know where to start (Never did it).

Can someone help me? Maybe without a full code but guiding me throw the generating.

Thanks

Posted

This is how I usually create my worldgen features

  1. Implement IWorldGenerator in your worldgen class(es). This will give you access to a generate method which will be called during worldgen.
  2. Be sure to check what Dimension(id) and/or what WorldType is being used by the current world. Don't think you want to generate stuff in the Nether/End etc.
  3. The mentioned generate method will give you a chunkX & chunkZ parameters. Do not multiply by 16 to get blockcoords, but rather use bit-shifting (blockpos = chunkpos << 4).
    1. Whilst multiplication from chunkpos to blockpos will result in correct numbers, division (block->chunk) will result in incorrect values for negative coordinates, due to how integers are rounded. It is for the best to consistently use one method to calculate rather than mix-and-match based on the situation.
  4. Always try to generate your features inside of the currently generating chunk. Do not generate along the border. This can cause cascading chunk-generations, and cause lag.
    Add 8 to both your x & z block-coordinates to get the (almost) centre of the current chunk, then use a random value between -7 to 7 (min-max values) to truly get a random position within the chunk. As for y-coordinates, you can use World::getTopSolidOrLiquidBlock to get the top-most solid y-value. Be sure to check if it is not a fluid though!
  5. For your normal crops, you only need to check for Dirt or Grass blocks, switch them out for Farmland (be sure to place some water next to it) and then place the crop ontop of that
  6. Sugarcane is a bit trickier. You can have a look through the WorldGenReed class in your IDE to see how Vanilla spawns Sugarcane
  7. Lastly, you need to register your IWorldGenerator implementing class during  init (FMLInitializationEvent) using GameRegistry.registerWorldGenerator(WorldGenerator, weight) where weight is an integer determining when they are run. Higher weights tend to run after generators with less weight.

Also previously known as eAndPi.

"Pi, is there a station coming up where we can board your train of thought?" -Kronnn

Published Mods: Underworld

Handy links: Vic_'s Forge events Own WIP Tutorials.

Posted

This is for an older version, but I think the basic idea still works as an example:

GenerateHerbs.java

 

Note that I did not think to explicitly check dimensions, though the herbs would not have been prevented by other functions.  Note the isLocationValid method at the end: This actually asks each herb if it should generate, and I think that is a good pattern -- vanilla basically does the same thing with mob spawns.  So each of your crops would have some kind of "canGenHere" method(s) and you would then put tests for that specific crop there.

Developer of Doomlike Dungeons.

Posted (edited)
3 hours ago, Matryoshika said:

This is how I usually create my worldgen features

  1. Implement IWorldGenerator in your worldgen class(es). This will give you access to a generate method which will be called during worldgen.
  2. Be sure to check what Dimension(id) and/or what WorldType is being used by the current world. Don't think you want to generate stuff in the Nether/End etc.
  3. The mentioned generate method will give you a chunkX & chunkZ parameters. Do not multiply by 16 to get blockcoords, but rather use bit-shifting (blockpos = chunkpos << 4).
    1. Whilst multiplication from chunkpos to blockpos will result in correct numbers, division (block->chunk) will result in incorrect values for negative coordinates, due to how integers are rounded. It is for the best to consistently use one method to calculate rather than mix-and-match based on the situation.
  4. Always try to generate your features inside of the currently generating chunk. Do not generate along the border. This can cause cascading chunk-generations, and cause lag.
    Add 8 to both your x & z block-coordinates to get the (almost) centre of the current chunk, then use a random value between -7 to 7 (min-max values) to truly get a random position within the chunk. As for y-coordinates, you can use World::getTopSolidOrLiquidBlock to get the top-most solid y-value. Be sure to check if it is not a fluid though!
  5. For your normal crops, you only need to check for Dirt or Grass blocks, switch them out for Farmland (be sure to place some water next to it) and then place the crop ontop of that
  6. Sugarcane is a bit trickier. You can have a look through the WorldGenReed class in your IDE to see how Vanilla spawns Sugarcane
  7. Lastly, you need to register your IWorldGenerator implementing class during  init (FMLInitializationEvent) using GameRegistry.registerWorldGenerator(WorldGenerator, weight) where weight is an integer determining when they are run. Higher weights tend to run after generators with less weight.

I reply a bit late but I was trying. I know, I didn't add everything you said but I'm learning this world gen

@Override
	public void generate(Random random, int chunkX, int chunkZ, World world, IChunkGenerator chunkGenerator,
			IChunkProvider chunkProvider) {
		if (world.provider.getDimension() == 0) {
			for (int i = 0; i < 64; i++) {

				int X = random.nextInt(16);
				int Z = random.nextInt(16);
				int Y = 0;
				System.out.println("1");
				BlockPos surface = world.getTopSolidOrLiquidBlock(new BlockPos(X,Y, Z));
				BlockPos blockpos1 = surface.down();
				System.out.println("2");
				System.out.println("Y: " + surface);
					if (world.isAirBlock(surface) && world.provider.isSurfaceWorld() && world.getBlockState(blockpos1).getBlock() == Blocks.GRASS
							|| world.getBlockState(blockpos1).getBlock() == Blocks.DIRT) {
						System.out.println("3");
						if (InitBlocks.RICE_CROP.canPlaceBlockAt(world, surface)) {
							System.out.println("4");
							world.setBlockState(blockpos1, Blocks.FARMLAND.getDefaultState(), 2);
							System.out.println("Attemping to place a farmland at X: " + X + ", Y: " + Y + ", Z: " + Z);
							world.setBlockState(surface, InitBlocks.RICE_CROP.getDefaultState());
							System.out.println("Attemping to place a rice crop at X: " + X + ", Y: " + Y + ", Z: " + Z);
					}
				}
			}
		}
	}

This is my class now, I have spam of 1, 2 and "surface". Help me to solve. I don't want a solution or I won't learn it and next world gen I will get the same error

This is my "surface" spam:

Cattura.thumb.PNG.be18c7619785aa56794608b8b66a8158.PNG

 

Edit: Now "Work" but sometimes try to spawn at Y = 0

and spawn like 64 farmlands but I know why

 

Edit 2: It attempts to spawn farmland and crop in water

Edited by Cris16228
Posted (edited)

Your if-statement is faulty.
 

43 minutes ago, Cris16228 said:

BlockPos surface = world.getTopSolidOrLiquidBlock(new BlockPos(X,Y, Z));

 

43 minutes ago, Cris16228 said:

world.isAirBlock(surface)

surface here will always be a non-air-block. You have to check if surface.up() is an air-block, not the one that is guaranteed not to be.

Further-more, if you simplify your entire if-statement to if(A && B && C || D) then the logic is easier to see. if A, B and C are true, OR if D is true, then continue. Dirt-blocks will always pass this check, even if the air-block or surfaceWorld are false. You need to wrap C and D inside a parenthesis. if(A && B && (C || D)), to continue if either of those two are true, whilst still relying on the A and B checks.

 

43 minutes ago, Cris16228 said:

Edit 2: It attempts to spawn farmland and crop in water

3 hours ago, Matryoshika said:

As for y-coordinates, you can use World::getTopSolidOrLiquidBlock to get the top-most solid y-value. Be sure to check if it is not a fluid though!

Edited by Matryoshika

Also previously known as eAndPi.

"Pi, is there a station coming up where we can board your train of thought?" -Kronnn

Published Mods: Underworld

Handy links: Vic_'s Forge events Own WIP Tutorials.

Posted
11 minutes ago, Matryoshika said:

Your if-statement is faulty.
 

 

surface here will always be a non-air-block. You have to check if surface.up() is an air-block, not the one that is guaranteed not to be.

Further-more, if you simplify your entire if-statement to if(A && B && C || D) then the logic is easier to see. if A, B and C are true, OR if D is true, then continue. Dirt-blocks will always pass this check, even if the air-block or surfaceWorld are false. You need to wrap C and D inside a parenthesis. if(A && B && (C || D)), to continue if either of those two are true, whilst still relying on the A and B checks.

@Override
	public void generate(Random random, int chunkX, int chunkZ, World world, IChunkGenerator chunkGenerator,
			IChunkProvider chunkProvider) {
		int spawnInChunks = random.nextInt(7) + 1;
		if (world.provider.getDimension() == 0) {
			for (int i = 0; i < spawnInChunks; i++) {
				
				int X = random.nextInt(16);
				int Z = random.nextInt(16);
				int Y = 0;
				BlockPos surface = world.getTopSolidOrLiquidBlock(new BlockPos(X, Y, Z));
				BlockPos blockpos1 = surface.down();
				if (world.isAirBlock(surface.up()) && world.provider.isSurfaceWorld()
						&& (world.getBlockState(blockpos1).getBlock() == Blocks.GRASS
								|| world.getBlockState(blockpos1).getBlock() == Blocks.DIRT)) {
					if (InitBlocks.RICE_CROP.canPlaceBlockAt(world, surface)) {
						world.setBlockState(blockpos1.north(), Blocks.WATER.getDefaultState(), 2);
						world.setBlockState(blockpos1, Blocks.FARMLAND.getDefaultState(), 2);
						world.setBlockState(surface,
								InitBlocks.RICE_CROP.getDefaultState().withProperty(RiceCrop.AGE, 7));
					}
				}
			}
		}
	}

Never found a crop in 5 mins, is there a way to generate them a bit more near the player and not 200+ blocks away? (I'm using IDE Minecraft with 10 render distance)

Posted
23 minutes ago, Cris16228 said:

int spawnInChunks = random.nextInt(7) + 1;

You could make this higher

About Me

Spoiler

My Discord - Cadiboo#8887

My WebsiteCadiboo.github.io

My ModsCadiboo.github.io/projects

My TutorialsCadiboo.github.io/tutorials

Versions below 1.14.4 are no longer supported on this forum. Use the latest version to receive support.

When asking support remember to include all relevant log files (logs are found in .minecraft/logs/), code if applicable and screenshots if possible.

Only download mods from trusted sites like CurseForge (minecraft.curseforge.com). A list of bad sites can be found here, with more information available at stopmodreposts.org

Edit your own signature at www.minecraftforge.net/forum/settings/signature/ (Make sure to check its compatibility with the Dark Theme)

Posted
1 minute ago, Cadiboo said:

You could make this higher

The problems are:
1) They are all together and I wanted, if possible, put them randomly in a chunk

2) The code up not work, never generate my crops or causes cascading worldgen lag

3) They spawn far from me (I always generate a new world). I don't want them within 2 blocks when creating a world just asking if it's normal

Posted

Any help? The code seems to work but if there are some fixes for my code or any suggestion on how to change the spawn per chunk, spread them more in the chunk and generate my crops also near me and not far

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.