User Tools

Site Tools


tutorial:ores

This is an old revision of the document!


Generating Custom Ores

A lot of mods add their own ores, and you'll need a way to place them in existing biomes for players to find. In this tutorial, we'll look at adding ores to existing biomes. There are 2 steps that are required to add ores to biomes.

  • Make a ConfiguredFeatures. This defines how your ore block is spawned.
  • Add your feature by using mixin into Minecraft class where default features are listed.

We'll assume you've already created your own ore block at this point. Wool block will serve as our replacement throughout this tutorial. Replace references to wool with your ore when appropriate.

Adding to the overworld biomes

In this section, our goal will be spawning the ore in the overworld.

Making a ConfiguredFeature

First we need to create a ConfiguredFeatures. Make sure to register your ConfiguredFeature at onInitialize. Feel free to change the values to suit your mod.

public class ExampleMod implements ModInitializer {
  public static ConfiguredFeature<?, ?> ORE_WOOL_OVERWORLD = Feature.ORE
    .configure(new OreFeatureConfig(
      OreFeatureConfig.Rules.BASE_STONE_OVERWORLD,
      Blocks.WHITE_WOOL.getDefaultState(),
      9)) // vein size
    .decorate(Decorator.RANGE.configure(new RangeDecoratorConfig(
      0, // bottom offset
      0, // min y level
      64))) // max y level
    .spreadHorizontally()
    .repeat(20); // number of veins per chunk
 
  @Override
  public void onInitialize() {
    Registry.register(BuiltinRegistries.CONFIGURED_FEATURE, new Identifier("tutorial", "ore_wool_overworld"), ORE_WOOL_OVERWORLD);
  }
}

Adding the feature

Vanilla ore features that spawn in the overworld biomes are listed in DefaultBiomeFeatures.addDefaultOres. We modify this method to add our ore to the overworld via mixin.

@Mixin(DefaultBiomeFeatures.class)
public class DefaultBiomeFeaturesMixin {
  @Inject(method = "addDefaultOres(Lnet/minecraft/world/biome/GenerationSettings$Builder;)V", at = @At("TAIL"))
  private static void addDefaultOres(GenerationSettings.Builder builder, CallbackInfo ci) {
    builder.feature(GenerationStep.Feature.UNDERGROUND_ORES, ExampleMod.ORE_WOOL_OVERWORLD);
  }
}

Result

You should see wool spawning in the overworld. You can use below command to remove stone blocks surrounding you.

/fill ~-8 0 ~-8 ~8 ~ ~8 minecraft:air replace minecraft:stone

Adding to the nether biomes

In this section, based on the code in the previous section, we will add the ore to the nether biomes.

Making a ConfiguredFeature

We have to replace OreFeatureConfig.Rules.BASE_STONE_OVERWORLD with OreFeatureConfig.Rules.BASE_STONE_NETHER because blocks used as base block are different in the overall biomes and the nether biomes.

public class ExampleMod implements ModInitializer {
  public static ConfiguredFeature<?, ?> ORE_WOOL_NETHER = Feature.ORE
    .configure(new OreFeatureConfig(
      OreFeatureConfig.Rules.BASE_STONE_NETHER, // We use OreFeatureConfig.Rules.BASE_STONE_NETHER for nether
      Blocks.WHITE_WOOL.getDefaultState(),
      9))
    .decorate(Decorator.RANGE.configure(new RangeDecoratorConfig(
      0,
      0,
      64)))
    .spreadHorizontally()
    .repeat(20);
}

Adding the feature

DefaultBiomeFeatures.addNetherMineables is the nether version of DefaultBiomeFeatures.addDefaultOres.

@Mixin(DefaultBiomeFeatures.class)
public class DefaultBiomeFeaturesMixin {
  @Inject(method = "addNetherMineables(Lnet/minecraft/world/biome/GenerationSettings$Builder;)V", at = @At("TAIL"))
  private static void addNetherMineables(GenerationSettings.Builder builder, CallbackInfo ci) {
    builder.feature(GenerationStep.Feature.UNDERGROUND_ORES, ExampleMod.ORE_WOOL_NETHER);
  }
}

Adding to the end biomes

In this section, based on the code in the overworld section, we will add the ore to the end biomes.

Making a ConfiguredFeature

We replace OreFeatureConfig.Rules.BASE_STONE_OVERWORLD with new BlockMatchRuleTest(Blocks.END_STONE) because endstone is used as a base block in the end biomes.

public class ExampleMod implements ModInitializer {
  public static ConfiguredFeature<?, ?> ORE_WOOL_END = Feature.ORE
    .configure(new OreFeatureConfig(
      new BlockMatchRuleTest(Blocks.END_STONE), // base block is endstone in the end biomes
      Blocks.WHITE_WOOL.getDefaultState(),
      9))
    .decorate(Decorator.RANGE.configure(new RangeDecoratorConfig(
      0,
      0,
      64)))
    .spreadHorizontally()
    .repeat(20);
 
  @Override
  public void onInitialize() {
    Registry.register(BuiltinRegistries.CONFIGURED_FEATURE, new Identifier("tutorial", "ore_wool_end"), ORE_WOOL_END);
  }
}

Adding the feature

Considering that no ore is generated in the end biomes on vanilla minecraft, we don't have end biome version of DefaultBiomeFeatures.addDefaultOres. Instead, we inject into DefaultBiomeCreator.composeEndSpawnSettings because this method is used to create every end biomes.

@Mixin(DefaultBiomeCreator.class)
public class DefaultBiomeCreatorMixin {
  @Inject(method = "composeEndSpawnSettings(Lnet/minecraft/world/biome/GenerationSettings$Builder;)Lnet/minecraft/world/biome/Biome;", at = @At("HEAD"))
  private static void addEndOres(GenerationSettings.Builder builder, CallbackInfoReturnable<Biome> cir) {
    builder.feature(GenerationStep.Feature.UNDERGROUND_ORES, ExampleMod.ORE_WOOL_END);
  }
}
tutorial/ores.1599388827.txt.gz · Last modified: 2020/09/06 10:40 by siglong