Jump to content

Odd Ore Generation | 1.8


fr0st

Recommended Posts

Hi.

 

I have a world generation system that mostly works. The problem is that with my system, I register 5 different ore generators, but odd things happen. The ores (and other features) generate fine, but it seems that for each world, one ore is "chosen" and that's the only ore that is generated. It's usually the first one in the list, so copper. I have added a few placeholder ores to test it further, but the issue still occurs.

 

[spoiler=WorldGenRegistry.java (getInstance().registerFeatures() called during Init)]

package projectcoal.common.registry;

import net.minecraft.init.Blocks;
import net.minecraftforge.fml.common.registry.GameRegistry;
import projectcoal.common.block.BlockOre;
import projectcoal.common.world.gen.WorldGenFeature;
import projectcoal.common.world.gen.feature.*;

public class WorldGenRegistry {

private static WorldGenRegistry instance;

private final static int NETHER = -1, OVERWORLD = 0, END = 1;

public static WorldGenRegistry getInstance(){
	return instance == null ? new WorldGenRegistry() : instance;
}

public void registerFeatures() {
	registerFeature(new FeatureOre(BlockOre.EnumType.COPPER, Blocks.air, 18, 6, 6, 96, 256, false), OVERWORLD);
	registerFeature(new FeatureOre(BlockOre.EnumType.LITHIUM, Blocks.air, 18, 6, 6, 96, 256, false), OVERWORLD);
	registerFeature(new FeatureOre(BlockOre.EnumType.TIN, Blocks.air, 18, 6, 6, 96, 256, false), OVERWORLD);
	registerFeature(new FeatureOre(BlockOre.EnumType.SILVER, Blocks.air, 18, 6, 6, 96, 256, false), OVERWORLD);
	registerFeature(new FeatureOre(BlockOre.EnumType.LEAD, Blocks.air, 18, 6, 6, 96, 256, false), OVERWORLD);

	//registerFeature(new FeatureTree(BlockRegistry.getBlockFromMap("block_log_nether").getDefaultState(), BlockRegistry.getBlockFromMap("block_leaves_nether").getDefaultState(), true, 6, 4, Blocks.soul_sand.getDefaultState()), NETHER);
}

public void registerFeature(Feature feature, int... dimensions) {
	GameRegistry.registerWorldGenerator(new WorldGenFeature(feature, dimensions), 1000);
}
}

 

 

[spoiler=WorldGenFeature.java]

package projectcoal.common.world.gen;

import java.util.Random;

import projectcoal.common.world.gen.feature.Feature;
import net.minecraft.world.World;
import net.minecraft.world.chunk.IChunkProvider;
import net.minecraftforge.fml.common.IWorldGenerator;

public class WorldGenFeature implements IWorldGenerator {

private final Feature feature;
private int[] dimensions = { 0 };

public WorldGenFeature(Feature feature, int... dimensions) {
	this.feature = feature;
	this.dimensions = dimensions;
}

@Override
public void generate(Random random, int chunkX, int chunkZ, World world, IChunkProvider chunkGenerator, IChunkProvider chunkProvider) {
	for (int dimension : dimensions) {
		if (dimension == world.provider.getDimensionId()) {
			feature.tryGenerate(world, chunkX, chunkZ, random);
		}
	}
}
}

 

 

[spoiler=Feature.java]

package projectcoal.common.world.gen.feature;

import java.util.Random;

import net.minecraft.block.state.IBlockState;
import net.minecraft.util.BlockPos;
import net.minecraft.world.World;

public abstract class Feature {

public boolean notifyBlocks;
public int count, minYCoord, maxYCoord;

public Feature(int count, int minY, int maxY, boolean notifyBlocks) {
	this.count = count;
	this.minYCoord = minY;
	this.maxYCoord = maxY;
	this.notifyBlocks = notifyBlocks;
}

public boolean tryGenerate(World world, int chunkX, int chunkZ, Random random) {
	generate(world, chunkX, chunkZ, random);
	world.getChunkFromChunkCoords(chunkX, chunkZ).setChunkModified();
	return true;
}

public abstract void generate(World world, int chunkX, int chunkZ, Random random);

protected void setBlock(World worldIn, BlockPos pos, IBlockState blockState) {
	if (this.notifyBlocks) {
		worldIn.setBlockState(pos, blockState, 3);
	} else {
		worldIn.setBlockState(pos, blockState, 2);
	}
}
}

 

 

[spoiler=FeatureOre.java]

package projectcoal.common.world.gen.feature;

import java.util.Random;

import projectcoal.common.IMetadataItem;
import net.minecraft.block.Block;
import net.minecraft.block.state.IBlockState;
import net.minecraft.block.state.pattern.BlockHelper;
import net.minecraft.util.BlockPos;
import net.minecraft.world.World;
import net.minecraft.world.gen.feature.WorldGenMinable;

public class FeatureOre extends Feature {

private final IBlockState oreBlock, stoneBlock;
private final int minVeinSize, maxVeinSize;

public FeatureOre(IMetadataItem oreBlock, Block stoneBlock, int count, int minVeinSize, int maxVeinSize, int minY, int maxY, boolean notifyBlocks) {
	super(count, minY, maxY, notifyBlocks);
	this.oreBlock = Block.getBlockFromItem(oreBlock.toItemStack(1).getItem()).getStateFromMeta(oreBlock.getMetadata());
	this.stoneBlock = stoneBlock.getDefaultState();
	this.minVeinSize = minVeinSize;
	this.maxVeinSize = maxVeinSize;
}

public FeatureOre(Block oreBlock, int oreMeta, Block stoneBlock, int stoneMeta, int count, int minVeinSize, int maxVeinSize, int minY, int maxY, boolean notifyBlocks) {
	super(count, minY, maxY, notifyBlocks);
	this.oreBlock = oreBlock.getStateFromMeta(oreMeta);
	this.stoneBlock = stoneBlock.getStateFromMeta(stoneMeta);
	this.minVeinSize = minVeinSize;
	this.maxVeinSize = maxVeinSize;
}

@Override
public void generate(World world, int chunkX, int chunkZ, Random random) {
	for (int i = 0; i < this.count; i++) {
		int x = (chunkX * 16) + random.nextInt(16);
		int y = minYCoord + random.nextInt(Math.max(maxYCoord - minYCoord, 0));
		int z = (chunkZ * 16) + random.nextInt(16);
		generateOre(world, random, new BlockPos(x, y, z));
	}
}

public void generateOre(World world, Random random, BlockPos pos) {
	int veinSize = minVeinSize + random.nextInt(Math.max(maxVeinSize - minVeinSize + 1, 0));
	new WorldGenMinable(oreBlock, veinSize, BlockHelper.forBlock(stoneBlock.getBlock())).generate(world, random, pos);
}
}

 

 

[spoiler=BlockOre.java (IMetadataItem is an interface that defines metadata blocks/items via enums)]

package projectcoal.common.block;

import java.util.List;

import org.apache.commons.lang3.text.WordUtils;

import projectcoal.common.IMetadataItem;
import projectcoal.common.block.prefab.BlockMultiPC;
import projectcoal.common.registry.BlockRegistry;
import net.minecraft.block.material.Material;
import net.minecraft.block.properties.IProperty;
import net.minecraft.block.properties.PropertyEnum;
import net.minecraft.block.state.BlockState;
import net.minecraft.block.state.IBlockState;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.IStringSerializable;

@SuppressWarnings({ "unchecked", "rawtypes" })
public class BlockOre extends BlockMultiPC {

public static final PropertyEnum VARIANT = PropertyEnum.create("variant", BlockOre.EnumType.class);

public BlockOre() {
	super(Material.rock);
	setResistance(6000000.0F);
	setDefaultState(this.blockState.getBaseState().withProperty(VARIANT, BlockOre.EnumType.COPPER));
}

public IBlockState getStateFromMeta(int meta) {
	return this.getDefaultState().withProperty(VARIANT, BlockOre.EnumType.get(meta));
}

public int getMetaFromState(IBlockState state) {
	return ((BlockOre.EnumType) state.getValue(VARIANT)).getMetadata();
}

protected BlockState createBlockState() {
	return new BlockState(this, new IProperty[] { VARIANT });
}

@Override
public void getSubBlocks(Item itemIn, CreativeTabs tab, List list) {
	for (BlockOre.EnumType variant : BlockOre.EnumType.values()) {
		list.add(new ItemStack(itemIn, 1, variant.getMetadata()));
	}
}

@Override
public boolean useCustomRender() {
	return false;
}

public static enum EnumType implements IMetadataItem, IStringSerializable {
	COPPER(0, "copper"), LITHIUM(1, "lithium"), TIN(2, "tin"), SILVER(3, "silver"), LEAD(4, "lead");

	private final String typeName;
	private final int metadata;
	private static final EnumType[] META_LOOKUP = new EnumType[values().length];

	static {
		for (EnumType variant : values()) {
			META_LOOKUP[variant.getMetadata()] = variant;
		}
	}

	private EnumType(int meta, String oreName) {
		typeName = oreName;
		metadata = meta;
	}

	@Override
	public String getUnlocalizedName() {
		return "block_ore_" + typeName;
	}

	@Override
	public ItemStack toItemStack(int amount) {
		return BlockRegistry.getItemStackFromMap(getUnlocalizedName(), amount, ordinal());
	}

	public static EnumType get(int ordinal) {
		return EnumType.values()[ordinal];
	}

	public static EnumType byMetadata(int meta) {
		if (meta < 0 || meta >= META_LOOKUP.length) {
			meta = 0;
		}

		return META_LOOKUP[meta];
	}

	@Override
	public String getName() {
		return typeName;
	}

	@Override
	public String toString() {
		return getName();
	}

	@Override
	public int getMetadata() {
		return metadata;
	}

	@Override
	public String getGenericName() {
		return "block_ore";
	}

	@Override
	public String getOreDictionaryName() {
		return "ore" + WordUtils.capitalize(typeName);
	}
}
}

 

 

I really can't find an issue with this system, I have debugged and all of the ores appear to be registered, but WorldGenMinable (called in FeatureOre) is always called with a copper ore block argument. That is what I don't get.

 

Thanks in advance.

 

PS: Please, do not mind the arguments passed during registration, they are just for the sake of testing.

I try my best, so apologies if I said something obviously stupid!

Link to comment
Share on other sites

You are using

Blocks.air

as your stone block.

 

Edit: I did not realise something went wrong with posting.

Don't PM me with questions. They will be ignored! Make a thread on the appropriate board for support.

 

1.12 -> 1.13 primer by williewillus.

 

1.7.10 and older versions of Minecraft are no longer supported due to it's age! Update to the latest version for support.

 

http://www.howoldisminecraft1710.today/

Link to comment
Share on other sites

Since copper is zero, I suspect uninitialized data. Either something isn't being set when it should, or else you're using something before it has a chance to be initialized.

Get back into the debugger with breakpoints set in the FeatureOre constructor and your problem line.

The debugger is a powerful and necessary tool in any IDE, so learn how to use it. You'll be able to tell us more and get better help here if you investigate your runtime problems in the debugger before posting.

Link to comment
Share on other sites

Hmm the FeatureOre constructor works just as expected.

 

[spoiler=Variants]projectcoal:block_ore[variant=copper]

projectcoal:block_ore[variant=lithium]

projectcoal:block_ore[variant=tin]

projectcoal:block_ore[variant=silver]

projectcoal:block_ore[variant=lead]

 

 

All of them are passed (IMetadataItem oreBlock argument), but the problem resides in the generateOre function.

I am currently debugging it, and now only the TIN variant is used, so it isn't just copper. I'll further debug it and let you all know.

 

In the meantime, thanks.

 

Edit: Another test, another ore. This time lithium.

By the way, it seems that only copper, tin and lithium generate. (Each for every new world).

Pretty odd.

I try my best, so apologies if I said something obviously stupid!

Link to comment
Share on other sites

Alright, I have carried out a few more tests, but nothing came up. I got to generate every type of ore at least once, except for silver, which has never been picked by the generator so far.

The generated ore doesn't change each world created, nor every time the client is run from Eclipse, so I still don't get how the ore is chosen.

May the generators be conflicting?

 

This is a mystery that just goes beyond me, I am clueless.

Any bit of help would be greatly appreciated.

 

 

 

I try my best, so apologies if I said something obviously stupid!

Link to comment
Share on other sites

And what does the debugger show you while you step through all of your methods?

 

I have a suspicion: The static section in your enum might be running before your enum's constructor, calling getMetadata before the member metadata has been initialized. This could inject random garbage. Also, getMetadata should probably return this.metadata, since it is an instance member, not static.

 

Step through the initialization of your BlockOre's enum to see what's set when.

The debugger is a powerful and necessary tool in any IDE, so learn how to use it. You'll be able to tell us more and get better help here if you investigate your runtime problems in the debugger before posting.

Link to comment
Share on other sites

The static section runs after the enum's constructor, and everything runs as one would expect. The constructor gets called 5 times with the right arguments, and then the META_LOOKUP array is filled with the correct values.

 

Thanks for the heads up, but still nothing useful came up from the debugging.

 

I try my best, so apologies if I said something obviously stupid!

Link to comment
Share on other sites

Correction:

You debugged that area of the program and found no oddities.

 

You have not debugged the calls to

WorldGenFeature .generate()

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

Good point. I now have.

 

WorldGenFeature#[b]generate[/b]

is called by

GameRegistry#[b]generateWorld[/b]

5 consecutive times per chunk (chunkX and chunkZ changing after 5 times) in the order Copper-Silver-Lithium-Tin-Lead during world init, and it's copper that gets generated.

 

We might be a step closer now..

I try my best, so apologies if I said something obviously stupid!

Link to comment
Share on other sites

First, in

WorldGenFeature

, you can use

Arrays.contains(int[], int)

or

ArrayUtils.contains(int[], int)

to check if it should generate.

Try to track all five of your ore features, and make sure that for each chunk, each gets called and executed.

Link to comment
Share on other sites

Oooh... I just had another idea: Generation depends on some randomness. If you accidentally start each ore's randomness from the same constant seed, then they'll all try to generate in the same place, so only the first will succeed (the later ores unable to replace it).

 

Watch your randomization in the debugger. Is it progressing as it should from ore to ore?

The debugger is a powerful and necessary tool in any IDE, so learn how to use it. You'll be able to tell us more and get better help here if you investigate your runtime problems in the debugger before posting.

Link to comment
Share on other sites

And that is exactly what came to my mind, jeffryfisher, so I have just debugged the

FeatureOre#[b]generateOre[/b]

calls, and they always use the same BlockPos', so the randomness is shared across all 5

FeatureOre

s.

 

Any idea on how to go about this?

Thanks again.

 

Edit: I might try one thing out. I'll let you know in a few minutes.

 

Okay. Using a new Random instance for every time

WorldGenFeature#generate

is called instead of the Random argument,fixes my issue. Now I still have a slow world gen and densely packed ores, but there is no way these should be a problem.

 

Thanks a bunch everyone, have a good evening!

I try my best, so apologies if I said something obviously stupid!

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.