User Tools

Site Tools


tutorial:features

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
tutorial:features [2022/09/03 23:28] – add github link miirtutorial:features [2023/12/18 01:19] (current) – [Adding a configured feature to a biome] update code solidblock
Line 20: Line 20:
 <yarncode java> <yarncode java>
 public class ExampleFeature extends class_3031<ExampleFeatureConfig> { public class ExampleFeature extends class_3031<ExampleFeatureConfig> {
-  public Example(Codec<class_3111> configCodec) { +  public ExampleFeature(Codec<FeatureConfig> configCodec) { 
-    smethod_10086er(configCodec);+    super(configCodec);
   }   }
  
Line 36: Line 36:
         // don't worry about where these come from-- we'll implement these methods soon         // don't worry about where these come from-- we'll implement these methods soon
         int number = config.number();         int number = config.number();
-        class_2960 blockID = config.blockID();+        class_2960 blockId = config.blockId();
  
-        class_2680 blockState = class_2378.field_11146.get(blockID).method_9564(); +        class_2680 blockState = class_7923.field_41175.get(blockId).method_9564(); 
-//        ensure the ID is okay +        // ensure the ID is okay 
-        if (blockState == null) throw new IllegalStateException(blockID + " could not be parsed to a valid block identifier!");+        if (blockState == null) throw new IllegalStateException(blockId + " could not be parsed to a valid block identifier!");
  
         // find the surface of the world         // find the surface of the world
Line 50: Line 50:
                 if (world.method_8320(testPos.method_10086()).isOf(class_2246.field_10124)) {                 if (world.method_8320(testPos.method_10086()).isOf(class_2246.field_10124)) {
                     for (int i = 0; i < number; i++) {                     for (int i = 0; i < number; i++) {
-//            create a simple pillar of blocks+                        // create a simple pillar of blocks
                         world.method_8501(testPos, blockState, 0x10);                         world.method_8501(testPos, blockState, 0x10);
                         testPos = testPos.method_10086();                         testPos = testPos.method_10086();
Line 61: Line 61:
             }             }
         }         }
-//        the game couldn't find a place to put the pillar+       // the game couldn't find a place to put the pillar
         return false;         return false;
     }     }
Line 69: Line 69:
 Now, we need to implement that ''ExampleFeatureConfig'' record. This is where we define the variables that we use in our ''Feature''. This config is essentially a wrapper for the parameters we want to pass to our feature. Note: while this tutorial only uses integers and BlockStates, other useful objects in the game also have codecs that can give you more control over how your feature generates. ''BlockStateProvider''s are a good example of this. Now, we need to implement that ''ExampleFeatureConfig'' record. This is where we define the variables that we use in our ''Feature''. This config is essentially a wrapper for the parameters we want to pass to our feature. Note: while this tutorial only uses integers and BlockStates, other useful objects in the game also have codecs that can give you more control over how your feature generates. ''BlockStateProvider''s are a good example of this.
 <yarncode java> <yarncode java>
-public record ExampleFeatureConfig(int number, Identifier blockID) implements FeatureConfig { +public record ExampleFeatureConfig(int number, Identifier blockId) implements FeatureConfig { 
-    public ExampleFeatureConfig(int number, Identifier blockID) { +    public static final Codec<ExampleFeatureConfig> CODEC = RecordCodecBuilder.create( 
-        this.blockID = blockID; +        instance -> instance.group(
-        this.number = number; +
-    } +
- +
-    public static Codec<ExampleFeatureConfig> CODEC = RecordCodecBuilder.create( +
-        instance -> +
-                instance.group(+
                         // you can add as many of these as you want, one for each parameter                         // you can add as many of these as you want, one for each parameter
                         Codecs.POSITIVE_INT.fieldOf("number").forGetter(ExampleFeatureConfig::number),                         Codecs.POSITIVE_INT.fieldOf("number").forGetter(ExampleFeatureConfig::number),
-                        Identifier.CODEC.fieldOf("blockID").forGetter(ExampleFeatureConfig::blockID))+                        Identifier.CODEC.fieldOf("blockID").forGetter(ExampleFeatureConfig::blockId))
                 .apply(instance, ExampleFeatureConfig::new));                 .apply(instance, ExampleFeatureConfig::new));
-     +}
-    public int number() { +
-        return number; +
-    } +
-    public Identifier blockID() { +
-        return blockID; +
-    }+
 </yarncode> </yarncode>
  
Line 94: Line 82:
  
 <code java> <code java>
-public class FeatureExampleMod implements ModInitializer { +public class ExampleMod implements ModInitializer { 
-    public static final Identifier EXAMPLE_FEATURE_ID = new Identifier("wiki-example", "example_feature"); +    public static final Identifier EXAMPLE_FEATURE_ID = new Identifier("tutorial", "example_feature"); 
-    public static Feature<ExampleFeatureConfig> EXAMPLE_FEATURE = new ExampleFeature(ExampleFeatureConfig.CODEC);+    public static final ExampleFeature EXAMPLE_FEATURE = new ExampleFeature(ExampleFeatureConfig.CODEC);
  
     @Override     @Override
     public void onInitialize() {     public void onInitialize() {
-        Registry.register(Registry.FEATURE, EXAMPLE_FEATURE_ID, EXAMPLE_FEATURE);+        Registry.register(Registries.FEATURE, EXAMPLE_FEATURE_ID, EXAMPLE_FEATURE);
     }     }
 } }
Line 113: Line 101:
 public class FeatureExampleMod implements ModInitializer { public class FeatureExampleMod implements ModInitializer {
  
-    public static final Identifier EXAMPLE_FEATURE_ID = new Identifier("wiki-example", "example_feature"); +    public static final Identifier EXAMPLE_FEATURE_ID = new Identifier("tutorial", "example_feature"); 
-    public static Feature<ExampleFeatureConfig> EXAMPLE_FEATURE = new ExampleFeature(ExampleFeatureConfig.CODEC);+    public static final ExampleFeature EXAMPLE_FEATURE = new ExampleFeature(ExampleFeatureConfig.CODEC);
  
-    public static ConfiguredFeature<ExampleFeatureConfig, ExampleFeature> EXAMPLE_FEATURE_CONFIGURED = new ConfiguredFeature<>+    public static final ConfiguredFeature<ExampleFeatureConfig, ExampleFeature> EXAMPLE_FEATURE_CONFIGURED = new ConfiguredFeature<>
-                    (ExampleFeature) EXAMPLE_FEATURE, +                    EXAMPLE_FEATURE, 
-                    new ExampleFeatureConfig(10, new Identifier("minecraft", "netherite_block"))+                    new ExampleFeatureConfig(10, new Identifier("minecraft", "netherite_block")));
     );     );
  
Line 129: Line 117:
 } }
 </yarncode>  </yarncode> 
 +
 +FIXME The last line has to be updated
  
 ==== Adding a configured feature to a biome ==== ==== Adding a configured feature to a biome ====
Line 135: Line 125:
 Our final initializer class looks like this: Our final initializer class looks like this:
 <yarncode java> <yarncode java>
-public class FeatureExampleMod implements ModInitializer {+public class ExampleMod implements ModInitializer {
  
-    public static final Identifier EXAMPLE_FEATURE_ID = new Identifier("wiki-example", "example_feature"); +    public static final Identifier EXAMPLE_FEATURE_ID = new Identifier("tutorial", "example_feature"); 
-    public static Feature<ExampleFeatureConfig> EXAMPLE_FEATURE = new ExampleFeature(ExampleFeatureConfig.CODEC); +    public static final ExampleFeature EXAMPLE_FEATURE = new ExampleFeature(ExampleFeatureConfig.CODEC); 
-    public static ConfiguredFeature<ExampleFeatureConfig, ExampleFeature> EXAMPLE_FEATURE_CONFIGURED = new ConfiguredFeature<>+    public static final ConfiguredFeature<ExampleFeatureConfig, ExampleFeature> EXAMPLE_FEATURE_CONFIGURED = new ConfiguredFeature<>
-                    (ExampleFeature) EXAMPLE_FEATURE,+                    EXAMPLE_FEATURE,
                     new ExampleFeatureConfig(10, new Identifier("minecraft", "netherite_block"))                     new ExampleFeatureConfig(10, new Identifier("minecraft", "netherite_block"))
     );     );
Line 147: Line 137:
             RegistryEntry.of(             RegistryEntry.of(
                     EXAMPLE_FEATURE_CONFIGURED                     EXAMPLE_FEATURE_CONFIGURED
-//                    the SquarePlacementModifier makes the feature generate a cluster of pillars each time+                  //  the SquarePlacementModifier makes the feature generate a cluster of pillars each time
             ), List.of(SquarePlacementModifier.of())             ), List.of(SquarePlacementModifier.of())
     );     );
Line 153: Line 143:
     @Override     @Override
     public void onInitialize() {     public void onInitialize() {
-//        register the features +        // register the features 
-        Registry.register(Registry.FEATURE, EXAMPLE_FEATURE_ID, EXAMPLE_FEATURE); +        Registry.register(class_7923.field_41144, EXAMPLE_FEATURE_ID, EXAMPLE_FEATURE);
-        Registry.register(BuiltinRegistries.CONFIGURED_FEATURE, EXAMPLE_FEATURE_ID, EXAMPLE_FEATURE_CONFIGURED); +
-        Registry.register(BuiltinRegistries.PLACED_FEATURE, EXAMPLE_FEATURE_ID, EXAMPLE_FEATURE_PLACED);+
  
-//        add it to overworld biomes using FAPI+        // add it to overworld biomes using FAPI
         BiomeModifications.addFeature(         BiomeModifications.addFeature(
                 BiomeSelectors.foundInOverworld(),                 BiomeSelectors.foundInOverworld(),
                 // the feature is to be added while flowers and trees are being generated                 // the feature is to be added while flowers and trees are being generated
                 GenerationStep.Feature.VEGETAL_DECORATION,                 GenerationStep.Feature.VEGETAL_DECORATION,
-                RegistryKey.of(Registry.PLACED_FEATURE_KEY, EXAMPLE_FEATURE_ID));+                RegistryKey.of(RegistryKeys.PLACED_FEATURE, EXAMPLE_FEATURE_ID));
     }     }
 } }
tutorial/features.1662247736.txt.gz · Last modified: 2022/09/03 23:28 by miir