User Tools

Site Tools


tutorial:mixin_accessors

This is an old revision of the document!


Adding Ores to Worlds

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.
  • Register 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. Quartz Ore will serve as our replacement throughout this tutorial. Replace references to Quartz Ore with your ore when appropriate.

Adding to the overworld

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

Making a ConfiguredFeatures

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.

  1. public class ExampleMod implements ModInitializer {
  2. public static ConfiguredFeature<?, ?> ORE_QUARTZ_OVERWORLD = Feature.ORE
  3. .configure(new OreFeatureConfig(
  4. OreFeatureConfig.Rules.BASE_STONE_OVERWORLD,
  5. Blocks.NETHER_QUARTZ_ORE.getDefaultState(),
  6. 9)) // vein size
  7. .decorate(Decorator.RANGE.configure(new RangeDecoratorConfig(
  8. 0, // bottom offset
  9. 0, // min y level
  10. 64))) // max y level
  11. .spreadHorizontally()
  12. .repeat(20); // number of veins per chunk
  13.  
  14. @Override
  15. public void onInitialize() {
  16. Registry.register(BuiltinRegistries.CONFIGURED_FEATURE, new Identifier("tutorial", "ore_quartz_overworld"), ORE_QUARTZ_OVERWORLD);
  17. }
  18. }

Registering the feature

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

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

Result

You should see quartz ore spawning in the overworld. You can use fill command to remove stone blocks surrounding you like this: /fill ~-4 0 ~-4 ~4 ~ ~4 minecraft:air replace minecraft:stone.

Adding to the end

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

Making a ConfiguredFeatures

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.

  1. public class ExampleMod implements ModInitializer {
  2. public static ConfiguredFeature<?, ?> ORE_QUARTZ_END = Feature.ORE
  3. .configure(new OreFeatureConfig(
  4. new BlockMatchRuleTest(Blocks.END_STONE), /* We use endstone! */
  5. Blocks.NETHER_QUARTZ_ORE.getDefaultState(),
  6. 9))
  7. .decorate(Decorator.RANGE.configure(new RangeDecoratorConfig(
  8. 0,
  9. 0,
  10. 64)))
  11. .spreadHorizontally()
  12. .repeat(20);
  13.  
  14. @Override
  15. public void onInitialize() {
  16. Registry.register(BuiltinRegistries.CONFIGURED_FEATURE, new Identifier("tutorial", "ore_quartz_end"), ORE_QUARTZ_END);
  17. }
  18. }

Registering the feature

Considering that no ore is generated in the end biomes on vanilla minecraft, DefaultBiomeFeature.addDefaultOres can't be used for adding an ore to the end biomes. Instead, we inject into DefaultBiomeCreator.method_31065 because this method is used to create every end biomes.

  1. @Mixin(DefaultBiomeCreator.class)
  2. public class DefaultBiomeCreatorMixin {
  3. @Inject(method = "method_31065(Lnet/minecraft/world/biome/GenerationSettings$Builder;)Lnet/minecraft/world/biome/Biome;", at = @At("HEAD"))
  4. private static void addEndOres(GenerationSettings.Builder builder, CallbackInfoReturnable<Biome> cir) {
  5. builder.feature(GenerationStep.Feature.UNDERGROUND_ORES, ExampleMod.ORE_QUARTZ_END);
  6. }
  7. }
tutorial/mixin_accessors.1598714307.txt.gz · Last modified: 2020/08/29 15:18 by siglong