(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
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
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. ** Additional ore has been generated. ** **
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.
Minecraft 1.14.4 Forge Mod Creation Part 8 [Addition and Generation of Ore]