[Java] Let's create a mod for Minecraft 1.14.4 [8. Add and generate ore]

(This article is one of a series of commentary articles)

First article: Introduction Previous article: 7. Add progress Next article: 9. Add and generate trees

Add ore

Finally, we will enter the full-scale part. If you make a mod-specific material, you must prepare a way to obtain it. If it was a primary resource, it would need to be generated in the world. This time we will learn to add ore.

First, let's add an ore block as already explained in 2. Add Block. Since the process is exactly the same, I will omit the explanation, but I added BlockList.ExampleOre.

BlockList.java


//...
public class BlockList {
    public static Block ExampleOre = new Block(
            Block.Properties.create(Material.ROCK)
                    .hardnessAndResistance(3.0F, 3.0F)
                    .lightValue(15))
            .setRegistryName(new ResourceLocation(ExampleMod.MOD_ID, "example_ore"));

    @SubscribeEvent
    public static void registerBlocks(RegistryEvent.Register<Block> event) {
        event.getRegistry().registerAll(
                ExampleOre
        );
    }

    @SubscribeEvent
    public static void registerBlockItems(RegistryEvent.Register<Item> event) {
        event.getRegistry().registerAll(
                new BlockItem(ExampleOre, new Item.Properties().group(ExampleItemGroup.DEFAULT))
                        .setRegistryName(new ResourceLocation(ExampleMod.MOD_ID, "example_ore"))
        );
    }
}

** * Addition ** It would have been better to define a unique BlockExampleOre class that inherits from the Block class instead of the Block class to set the experience point drop during mining.

BlockExampleOre.java


package jp.koteko.example_mod.blocks;

import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.MathHelper;

public class BlockExampleOre extends Block {
    public BlockExampleOre(Block.Properties properties) {
        super(properties);
    }

    @Override
    public int getExpDrop(BlockState state, net.minecraft.world.IWorldReader reader, BlockPos pos, int fortune, int silktouch) {
        return silktouch == 0 ? MathHelper.nextInt(RANDOM, 3, 7) : 0;
    }
}


\src\main\resources
   ├ assets
   │  └ example_mod
   │     ├ blockstates
   │     │  └ example_ore.json
   │     ├ lang
   │     │  └ en_us.json
   │     │  └ ja_jp.json
   │     ├ models
   │     │  ├ block
   │     │  │  └ example_ore.json
   │     │  └ item
   │     │     └ example_ore.json
   │     └ textures
   │        ├ blocks
   │        │  └ example_ore.png
   │        └ items
   └ data
      └ example_mod
         └ loot_tables
            └ blocks
               └ example_ore.json

Add recipes as appropriate.

Ore formation

Now let's add some code to generate this.

\src\main\java\jp\koteko\example_mod\
   ├ items
   ├ lists
   ├ world
   │   └ WorldGenOres.java
   └ ExampleMod.java

Place WorldGenOres.java.

WorldGenOres.java


package jp.koteko.example_mod.world;

import jp.koteko.example_mod.lists.BlockList;
import net.minecraft.block.Block;
import net.minecraft.world.biome.Biome;
import net.minecraft.world.gen.GenerationStage;
import net.minecraft.world.gen.feature.Feature;
import net.minecraft.world.gen.feature.OreFeatureConfig;
import net.minecraft.world.gen.placement.CountRangeConfig;
import net.minecraft.world.gen.placement.Placement;
import net.minecraftforge.registries.ForgeRegistries;

public class WorldGenOres {
    public static void setup() {
        addOreToOverworld(
                BlockList.ExampleOre,
                17,
                new CountRangeConfig(20, 0, 0, 128)
        );
    }

    private static void addOreToOverworld(Block blockIn, int size, CountRangeConfig countRangeConfigIn) {
        for(Biome biome : ForgeRegistries.BIOMES) {
            if(!biome.getCategory().equals(Biome.Category.NETHER) && !biome.getCategory().equals(Biome.Category.THEEND)) {
                biome.addFeature(
                        GenerationStage.Decoration.UNDERGROUND_ORES,
                        Biome.createDecoratedFeature(
                                Feature.ORE,
                                new OreFeatureConfig(
                                        OreFeatureConfig.FillerBlockType.NATURAL_STONE,
                                        blockIn.getDefaultState(),
                                        size
                                ),
                                Placement.COUNT_RANGE,
                                countRangeConfigIn
                        )
                );
            }
        }
    }
}

The writing method is just an example, so you can write it more simply or with a higher degree of abstraction. ~~ The more I think about what is best, the more I don't understand. ~~ The core part is biome.addFeature (), which is a method to add a Feature or feature to an instance of the Biome class. This will generate features during biome generation.

Commentary


// net\minecraft\world\biome\DefaultBiomeFeatures.There are many examples in java
biome.addFeature(
        //Type of feature to add
        // net\minecraft\gen\GenerationStage.Look at java and choose the one that feels good
        GenerationStage.Decoration.UNDERGROUND_ORES,
        //Instance of the configured feature
        Biome.createDecoratedFeature(
                //Type of feature It seems that the one corresponding to the following argument is passed here
                Feature.ORE,
                //Feature config
                new OreFeatureConfig(
                        //Type of block to be replaced
                        // NATURAL_STONE is STONE,GRANITE,DIORITE,ANDESITE
                        OreFeatureConfig.FillerBlockType.NATURAL_STONE,
                        //Block state of the ore produced
                        BlockList.ExampleOre.getDefaultState(),
                        //Maximum number generated in one place
                        17
                ),
                // Placement(An object that handles a range?)It seems that the one corresponding to the following argument is passed here
                Placement.COUNT_RANGE,
                //Placement config
                // (count, bottomOffset, topOffset, maximum)
                //I'm not sure if the details are correct,
                //0 to maximum-Randomly selected numbers up to topOffset
                //It seems that it is moving like drawing an integer value with bottom Offset added count times.
                //In short, it seems that it determines the y-coordinate to generate count ore per chunk.
                new CountRangeConfig(20, 0, 0, 128)
        )
)

Finally, call the just defined WorldGenOres.setup () in the setup in the main file.

ExampleMod.java


//...
public class ExampleMod
{
    //...
    private void setup(final FMLCommonSetupEvent event)
    {
        WorldGenOres.setup();
    }
    //...
}

Start the game. キャプチャ.PNG ** Additional ore has been generated. ** **

development

Q. I want to generate it in the nether end Q. I want to generate only for a specific biome ** A. Let's judge the authenticity with biome.getCategory (). Equals (). ** ** The example shown this time was generated in all biomes on the ground. The code is the following part.

python


if(!biome.getCategory().equals(Biome.Category.NETHER) && !biome.getCategory().equals(Biome.Category.THEEND))

I turned it with for and did ʻaddFeature ()` to all non-nether and non-end biomes. I'm writing this because I think it would be nice to prepare a new method that changes this part as appropriate. Or I think that it may be possible to receive the biome array as an argument.

reference

Minecraft 1.14.4 Forge Mod Creation Part 8 [Addition and Generation of Ore]

Next article

9. Add and generate trees

Recommended Posts

[Java] Let's create a mod for Minecraft 1.14.4 [8. Add and generate ore]
[Java] Let's create a mod for Minecraft 1.16.1 [Add and generate trees]
[Java] Let's create a mod for Minecraft 1.14.4 [9. Add and generate trees]
[Java] Let's create a mod for Minecraft 1.14.4 [4. Add tools]
[Java] Let's create a mod for Minecraft 1.14.4 [5. Add armor]
[Java] Let's create a mod for Minecraft 1.14.4 [6. Add recipe]
[Java] Let's create a mod for Minecraft 1.16.1 [Add item]
[Java] Let's create a mod for Minecraft 1.14.4 [1. Add items]
[Java] Let's create a mod for Minecraft 1.14.4 [2. Add block]
[Java] Let's create a mod for Minecraft 1.16.1 [Add block]
[Java] Let's create a mod for Minecraft 1.14.4 [3. Add creative tab]
[Java] Let's create a mod for Minecraft 1.14.4 [Introduction]
[Java] Let's create a mod for Minecraft 1.16.1 [Introduction]
[Java] Let's create a mod for Minecraft 1.14.4 [99. Mod output]
[Java] Let's create a mod for Minecraft 1.14.4 [0. Basic file]
[Java] Let's create a mod for Minecraft 1.14.4 [Extra edition]
[Java] Let's create a mod for Minecraft 1.16.1 [Basic file]
[Java] Create and apply a slide master
Let's create a Java development environment (updating)
Let's install Docker on Windows 10 and create a verification environment for CentOS 8!
Let's create a timed process with Java Timer! !!
Let's create a super-simple web framework in Java
Create a portfolio app using Java and Spring Boot
[Java] Create a filter
[Java basics] Let's make a triangle with a for statement
Let's create a file upload system using Azure Computer Vision API and Azure Storage Java SDK
How to create a lightweight container image for Java apps
[Java twig] Create a parser combinator for recursive descent parsing 2
Create a Java and JavaScript team development environment (gradle environment construction)
Create a java method [Memo] [java11]
[Java] Create a temporary file
Java while and for statements
Sort List in descending order in Java and generate a new List non-destructively
Let's create a TODO application in Java 4 Implementation of posting function
Create a JVM for app distribution with JDK9 modules and jlink
Create a high-performance enum with fields and methods like Java with JavaScript
Let's create a TODO application in Java 8 Implementation of editing function
Let's create a TODO application in Java 1 Brief explanation of MVC
Let's create a parameter polymorphic mechanism with Generic Dao and Hibernate
AWS SDK for Java 1.11.x and 2.x
Java for beginners, expressions and operators 1
How to sign a Minecraft MOD
Java for beginners, expressions and operators 2
[Java] How to create a folder
Classes and instances Java for beginners
Create a fluentd server for testing
[Java] Create a jar file with both compressed and uncompressed with the jar command
[Java twig] Create a parser combinator for recursive descent parsing (also memoize)
[Docker] How to create a virtual environment for Rails and Nuxt.js apps
[Rails] How to create a table, add a column, and change the column type
Let's go with Watson Assistant (formerly Conversation) ⑤ Create a chatbot with Watson + Java + Slack
Create a java web application development environment with docker for mac part2
Let's create a TODO application in Java 9 Create TODO display Sort by date and time + Set due date default to today's date
Let's create a TODO application in Java 2 I want to create a template with Spring Initializr and make a Hello world