tutorial:features
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
tutorial:features [2021/06/17 12:27] – Change MC version in the header redgrapefruit | tutorial:features [2024/10/28 17:58] (current) – Fix new identifier vs Identifier.of cassiancc | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ===== Adding Features | + | ===== Adding Features ===== |
Rocks, trees, ores, and ponds are all examples of features. | Rocks, trees, ores, and ponds are all examples of features. | ||
They are simple generation additions to the world which generate depending on how they are configured. | They are simple generation additions to the world which generate depending on how they are configured. | ||
- | In this tutorial, we' | + | In this tutorial, we' |
+ | |||
+ | If you want to do something similar to vanilla (like ores or flower patches), you should first look for an existing feature you can use. | ||
+ | In that case, skip the " | ||
There are 3 steps that are required to add a feature to a biome. | There are 3 steps that are required to add a feature to a biome. | ||
Line 9: | Line 12: | ||
* Use [[https:// | * Use [[https:// | ||
- | Note that the Biome Modification API is marked as experimental. If the API doesn' | ||
==== Creating a feature ==== | ==== Creating a feature ==== | ||
- | A simple Feature looks like this: | + | Let's create a feature that spawns a 1x1 pillar of blocks on the ground. As an added challenge, let's also make it configurable, |
- | <code java> | + | We'll first create a new class for our feature. This is where the generation logic will go. |
- | public class StoneSpiralFeature | + | |
- | public | + | <yarncode |
- | super(config); | + | public class ExampleFeature |
+ | public | ||
+ | super(configCodec); | ||
} | } | ||
+ | // this method is what is called when the game tries to generate the feature. it is where the actual blocks get placed into the world. | ||
@Override | @Override | ||
- | public boolean | + | public boolean |
- | DefaultFeatureConfig config) { | + | |
- | | + | // the origin is the place where the game starts trying to place the feature |
- | | + | class_2338 origin = context.getOrigin(); |
+ | // we won't use the random here, but we could if we wanted to | ||
+ | class_5819 random = context.method_33654(); | ||
+ | | ||
- | for (int y = 1; y <= 15; y++) { | + | // don't worry about where these come from-- we'll implement these methods soon |
- | offset = offset.rotateYClockwise(); | + | |
- | world.setBlockState(topPos.up(y).offset(offset), | + | |
- | } | + | |
- | return true; | + | class_2680 blockState = class_7923.field_41175.get(blockId).method_9564(); |
- | } | + | // ensure the ID is okay |
- | } | + | if (blockState == null) throw new IllegalStateException(blockId + " could not be parsed to a valid block identifier!" |
- | </code> | + | |
- | The '' | + | // find the surface of the world |
- | You can pass in '' | + | class_2338 testPos = new class_2338(origin); |
+ | for (int y = 0; y < world.method_31605(); | ||
+ | | ||
+ | // the tag name is dirt, but includes grass, mud, podzol, etc. | ||
+ | if (world.method_8320(testPos).isIn(class_3481.field_29822)) { | ||
+ | if (world.method_8320(testPos.method_10086()).isOf(class_2246.field_10124)) { | ||
+ | | ||
+ | // create a simple pillar of blocks | ||
+ | world.method_8501(testPos, blockState, 0x10); | ||
+ | testPos = testPos.method_10086(); | ||
- | '' | + | // ensure we don't try to place blocks outside |
- | If the feature is configured to spawn every chunk, this would be called for each chunk being generated as well. | + | if (testPos.getY() >= world.method_8624()) break; |
- | In the case of the feature being configured to spawn at a certain rate per biome, '' | + | } |
+ | return true; | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | // | ||
+ | return false; | ||
+ | } | ||
+ | } | ||
- | In our implementation, we'll build a simple 15-block tall spiral of stone starting at the top block in the world. | + | </ |
+ | Now, we need to implement that '' | ||
+ | < | ||
+ | public record ExampleFeatureConfig(int number, Identifier blockId) implements FeatureConfig { | ||
+ | public static final Codec< | ||
+ | instance -> instance.group( | ||
+ | // you can add as many of these as you want, one for each parameter | ||
+ | Codecs.POSITIVE_INT.fieldOf(" | ||
+ | Identifier.CODEC.fieldOf(" | ||
+ | .apply(instance, | ||
+ | } | ||
+ | </ | ||
- | Features can be registered like most other content in the game, and there aren't any special builders or mechanics you'll have to worry about. | + | Now that we have our config defined, the errors in our feature class will resolve. But we're not done yet-- now we need to add our feature to the game. Features can be registered like most other content in the game, and there aren't any special builders or mechanics you'll have to worry about. |
<code java> | <code java> | ||
public class ExampleMod implements ModInitializer { | public class ExampleMod implements ModInitializer { | ||
- | private | + | public |
+ | public static final ExampleFeature EXAMPLE_FEATURE | ||
- | | + | |
- | public void onInitialize() { | + | public void onInitialize() { |
- | Registry.register(Registry.FEATURE, | + | Registry.register(Registries.FEATURE, |
- | } | + | } |
} | } | ||
</ | </ | ||
+ | |||
+ | If you plan to configure and use your feature using datapacks, you can stop here. To implement it in code, read on. | ||
==== Configuring a feature ==== | ==== Configuring a feature ==== | ||
- | We need to give a configuration to a feature. Make sure to register configured | + | We need to give a configuration to a feature, before we can add it to biomes. Make sure to register configured |
- | <code java> | + | <yarncode |
- | public class ExampleMod | + | public class FeatureExampleMod |
- | public static final ConfiguredFeature<?, | + | |
- | .decorate(Decorator.CHANCE.configure(new ChanceDecoratorConfig(5))); | + | |
- | @Override | + | |
- | | + | |
- | | + | |
- | + | | |
- | | + | EXAMPLE_FEATURE, |
- | new Identifier(" | + | new ExampleFeatureConfig(10, |
- | Registry.register(BuiltinRegistries.CONFIGURED_FEATURE, stoneSpiral.getValue(), STONE_SPIRAL_CONFIGURED); | + | |
- | } | + | |
+ | |||
+ | @Override | ||
+ | public void onInitialize() { | ||
+ | | ||
+ | Registry.register(BuiltinRegistries.CONFIGURED_FEATURE, EXAMPLE_FEATURE_ID, | ||
+ | } | ||
} | } | ||
- | </code> | + | </yarncode> |
- | The Decorator represents how the world chooses | + | FIXME The last line has to be updated |
- | To choose the correct Decorator, check out vanilla features with a similar style to your own. | + | |
- | The decorator config branches off this; in the case of '' | + | |
==== Adding a configured feature to a biome ==== | ==== Adding a configured feature to a biome ==== | ||
- | We use the Biome Modification API. | + | We use the Biome Modification API. The final stage of feature configuration is creating a '' |
- | <code java> | + | Our final initializer class looks like this: |
+ | <yarncode | ||
public class ExampleMod implements ModInitializer { | public class ExampleMod implements ModInitializer { | ||
- | [...] | ||
- | | + | public static final Identifier EXAMPLE_FEATURE_ID = Identifier.of(" |
- | public void onInitialize() { | + | public static final ExampleFeature EXAMPLE_FEATURE = new ExampleFeature(ExampleFeatureConfig.CODEC); |
- | [...] | + | public static final ConfiguredFeature< |
- | BiomeModifications.addFeature(BiomeSelectors.all(), GenerationStep.Feature.UNDERGROUND_ORES, stoneSpiral); | + | EXAMPLE_FEATURE, |
- | } | + | new ExampleFeatureConfig(10, |
+ | ); | ||
+ | // our PlacedFeature. this is what gets passed to the biome modification API to add to the biome. | ||
+ | public static PlacedFeature EXAMPLE_FEATURE_PLACED = new PlacedFeature( | ||
+ | RegistryEntry.of( | ||
+ | EXAMPLE_FEATURE_CONFIGURED | ||
+ | // the SquarePlacementModifier makes the feature generate a cluster of pillars each time | ||
+ | ), List.of(SquarePlacementModifier.of()) | ||
+ | ); | ||
+ | |||
+ | | ||
+ | public void onInitialize() { | ||
+ | // register the features | ||
+ | Registry.register(class_7923.field_41144, | ||
+ | |||
+ | // add it to overworld biomes using FAPI | ||
+ | | ||
+ | | ||
+ | // the feature is to be added while flowers and trees are being generated | ||
+ | | ||
+ | RegistryKey.of(RegistryKeys.PLACED_FEATURE, | ||
+ | } | ||
} | } | ||
- | </code> | + | </yarncode> |
The first argument of '' | The first argument of '' | ||
Line 101: | Line 161: | ||
For above-ground houses you may go with '' | For above-ground houses you may go with '' | ||
- | === Result === | + | For more information, |
- | {{https://i.imgur.com/Kr59o0B.png}} | + | |
tutorial/features.1623932868.txt.gz · Last modified: 2021/06/17 12:27 by redgrapefruit