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)
  On 3/25/2019 at 7:20 PM, 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.
Expand  

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.
 

  On 3/25/2019 at 10:02 PM, Cris16228 said:

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

Expand  

 

  On 3/25/2019 at 10:02 PM, Cris16228 said:

world.isAirBlock(surface)

Expand  

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.

 

  On 3/25/2019 at 10:02 PM, Cris16228 said:

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

Expand  
  On 3/25/2019 at 7:20 PM, 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!

Expand  
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
  On 3/25/2019 at 10:43 PM, 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.

Expand  
@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
  On 3/25/2019 at 11:07 PM, Cris16228 said:

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

Expand  

You could make this higher

About Me

  Reveal hidden contents

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
  On 3/25/2019 at 11:31 PM, Cadiboo said:

You could make this higher

Expand  

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

    • Sorry for the late response, but the game opened and I made a world but it's stuck at 0% Here's the latest.log https://mclo.gs/peEb1R8 I disabled The Factory Must Grow and soulsweapons
    • Hey everyone! Two of my friends downloaded this modpack and are having an issue with the blocks loading in correctly. Grass looks like bamboo, waystones look like two barrels stacked on eachother, and wheat looks like water and stairs. What is this problem? How can we fix it? Neither of my other friends or myself had this issue. 
    • I removed Yung's cave biome mod and It wasnt in one of those biomes however the log file said the same line (([25Apr2025 21:20:15.500] [Flywheel Task Executor #5/WARN] [Embeddium-MixinTaintDetector/]: Mod(s) [oculus] are modifying Embeddium class me.jellysquid.mods.sodium.client.render.vertex.serializers.VertexSerializerRegistryImpl, which may cause instability.))
    • Note: i had a couple of ideas, but i just don't know how to execute them. The main idea was to make the new block (let's exemplify with a mixer) be essentially a clone of the mechanical mixer (different texture tho) that would have a different recipe type (instead of mixing, advanced_mixing) and i would copy all the create mod recipes and edit the processing time while also adding the tier dependent ones.
    • Hi! Before everything, thank you for even reading. I'm coming here in need of help making a few aspects for a create mod addon. It's an addon that aims to add almost the full periodic table with realistic ways of obtaining all the elements and capability of almost full automation. For what purpose? A techy armor and to go to the moon and rocky planets of the solar system. It'll have 3 different tiers of machines (mixer, millstone, crushing wheels): basic (just the normal create mod machines but renamed), advanced (25% faster and has recipes only possible for this tier and above) end elite (75% faster than basic and recipes only available for this tier). The problem is, I'm not a coder. I know less than the basics. I know how to do everything else but these machine tiers and i need some help if you can.
  • Topics

  • Who's Online (See full list)

    • There are no registered users currently online
×
×
  • Create New...

Important Information

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