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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • When I first heard about Bitcoin back in 2018, I was skeptical. The idea of a decentralized, digital currency seemed too good to be true. But I was intrigued as I learned more about the technology behind it and its potential. I started small, investing just a few hundred dollars, dipping my toes into the cryptocurrency waters. At first, it was exhilarating to watch the value of my investment grow exponentially. I felt like I was part of the future, an early adopter of this revolutionary new asset. But that euphoria was short-lived. One day, I logged into my digital wallet only to find it empty - my Bitcoin had vanished without a trace. It turned out that the online exchange I had trusted had been hacked, and my funds were stolen. I was devastated, both financially and emotionally. All the potential I had seen in Bitcoin was tainted by the harsh reality that with decentralization came a lack of regulation and oversight. My hard-earned money was gone, lost to the ether of the digital world. This experience taught me a painful lesson about the price of trust in the uncharted territory of cryptocurrency. While the technology holds incredible promise, the risks can be catastrophic if you don't approach it with extreme caution. My Bitcoin investment gamble had failed, and I was left to pick up the pieces, wiser but poorer for having placed my faith in the wrong hands. My sincere appreciation goes to MUYERN TRUST HACKER. You are my hero in recovering my lost funds. Send a direct m a i l ( muyerntrusted ( @ ) mail-me ( . )c o m ) or message on whats app : + 1 ( 4-4-0 ) ( 3 -3 -5 ) ( 0-2-0-5 )
    • You could try posting a log (if there is no log at all, it may be the launcher you are using, the FAQ may have info on how to enable the log) as described in the FAQ, however this will probably need to be reported to/remedied by the mod author.
    • So me and a couple of friends are playing with a shitpost mod pack and one of the mods in the pack is corail tombstone and for some reason there is a problem with it, where on death to fire the player will get kicked out of the server and the tombstone will not spawn basically deleting an entire inventory, it doesn't matter what type of fire it is, whether it's from vanilla fire/lava, or from modded fire like ice&fire/lycanites and it's common enough to where everyone on the server has experienced at least once or twice and it doesn't give any crash log. a solution to this would be much appreciated thank you!
    • It is 1.12.2 - I have no idea if there is a 1.12 pack
  • Topics

×
×
  • Create New...

Important Information

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