User Tools

Site Tools


tutorial:structures

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:structures [2019/02/16 20:58] asietutorial:structures [2022/11/05 12:06] (current) jab125
Line 1: Line 1:
-====== Generating Structures ======+:!: //**For versions 1.18 and beyond, the Fabric Structure API no longer exists and structures can be done entirely in datapacks. [[https://misode.github.io/guides/adding-custom-structures/|Please read this gist for more information.]] ** //
  
-===== Features =====+====== Adding Structure Features [1.16.3] ====== 
 +We’re going to look at registering and placing structures in your world.
  
-All code used in this tutorial is available here: [[https://github.com/Draylar/fabric-structure-example-repo|fabric-structure-example-repo]]+To view examples of vanilla structures in action, ''IglooFeature'' and ''IglooGenerator'' are a good place to start. However, the growing standard for making new structures is using Jigsaw Structures which can be easier to work with and lets you deal with less structure code. [[https://fabricmc.net/wiki/tutorial:jigsaw|Read more on Jigsaw Structures here and how to make them.]]
  
-==== Introduction ====+You are going to need a feature and generator for the most basic structure. 
 +The feature handles the process of registering the structure and loading it in when the world is generating. 
 +The generator handles the placement of blocks or loading in a structure file if you choose to do so.
  
-We’re going to look at registering and placing structures in your world.+Note that this tutorial depends on [[https://github.com/FabricMC/fabric/pull/1097|Biome Modification API in Fabric API]] which is marked as experimental. 
 +If the API doesn't work, consider using [[?rev=1599808070|the mixin version]].
  
-To view examples of 1.14 vanilla structures in actionIglooGenerator & IglooFeature are good place to start.+===== Creating a Feature ===== 
 +To create a basic feature, we recommend creating a class that extends ''StructureFeature<DefaultFeatureConfig>''. 
 +Various vanilla structures, such as shipwrecks, igloos, and temples, use ''StructureFeature<DefaultFeatureConfig>'' as base.
  
-You are going to need Feature and Generator for the most basic structure. The feature handles the process of registering the structure and loading it in when the world is generating-- it answers questions such as ‘should I spawn here?’ and ‘what is my name?’ The generator handles the placement of blocks or loading in a structure file if you choose to do so.+You will have to override ''getStructureStartFactory'' method. 
 +For ''getStructureStartFactory'', most vanilla structures make class that extends ''StructureStart'' inside their feature class.
  
-==== Creating a Feature ====+<code java> 
 +public class MyFeature extends StructureFeature<DefaultFeatureConfig>
 +  public MyFeature(Codec<DefaultFeatureConfig> codec) { 
 +    super(codec); 
 +  }
  
-To create a basic feature, we recommend creating a class that extends AbstractTempleFeature<DefaultFeatureConfig>. Various vanilla structures, such as Shipwrecks, Igloos, and Temples, use AbstractTempleFeature as a base. You will have to override the following methods:+  @Override 
 +  public StructureFeature.StructureStartFactory<DefaultFeatureConfig> getStructureStartFactory() { 
 +    return Start::new; 
 +  }
  
-  * shouldStartAt: return true for testing purposes. +  public static class Start extends StructureStart<DefaultFeatureConfig> { 
-  * getName: name of your structure +    public Start(StructureFeature<DefaultFeatureConfig> feature, int chunkX, int chunkZ, BlockBox box, int references
-  * method_14021 [getRadius]: radius of your structureused for placement +        long seed) { 
-  * method_13774 [getSeed]: a seed to use for generationput 0 for testing+      super(featurechunkX, chunkZ, box, references, seed); 
 +    }
  
-You can pass DefaultFeatureConfig::deserialize into your constructor for testing.+    // Called when the world attempts to spawn in a new structure, and is the gap between your feature and generator. 
 +    public void init(DynamicRegistryManager registryManager, ChunkGenerator chunkGenerator, StructureManager manager, int chunkX, 
 +        int chunkZ, Biome biome, DefaultFeatureConfig config) { 
 +      int x = chunkX * 16; 
 +      int z = chunkZ * 16; 
 +      int y = chunkGenerator.getHeight(x, z, Heightmap.Type.WORLD_SURFACE_WG); 
 +      BlockPos pos = new BlockPos(x, y, z); 
 +      BlockRotation rotation = BlockRotation.random(this.random); 
 +      MyGenerator.addPieces(manager, pos, rotation, this.children); 
 +      this.setBoundingBoxFromChildren(); 
 +    } 
 +  } 
 +
 +</code>
  
-For getStructureStartFactorymost vanilla structures make class that extends StructureStart inside their Feature class:+===== Creating a Generator ===== 
 +As you have probably noticedwe need to create generator.
  
-  public static class MyStructureStart extends StructureStart +This is where structure files and generating straight from a ''generate'' method part waysThere are two ways to go about this:
-    { +
-        public MyStructureStart (StructureFeature<?> structureFeature_1, int int_1, int int_2, Biome biome_1, MutableIntBoundingBox mutableIntBoundingBox_1, int int_3, long long_1) +
-        { +
-            super(structureFeature_1, int_1, int_2, biome_1, mutableIntBoundingBox_1, int_3, long_1); +
-        } +
-        @Override +
-        public void initialize(ChunkGenerator<?> chunkGenerator, StructureManager structureManager, int chunkX, int chunkZ, Biome biome) +
-        { +
-            DefaultFeatureConfig defaultFeatureConfig = chunkGenerator.getStructureConfig(biome, MyMainclass.myFeature); +
-            int x = chunkX * 16; +
-            int z = chunkZ * 16; +
-            BlockPos startingPos = new BlockPos(x, 0, z); +
-            Rotation rotation = Rotation.values()[this.random.nextInt(Rotation.values().length)]; +
-            MyGenerator.addParts(structureManager, startingPos, rotation, this.children, this.random, defaultFeatureConfig); +
-            this.setBoundingBoxFromChildren(); +
-        } +
-    } +
-     +
-This is called when the world attempts to spawn in a new structureand is the gap between your Feature and Generator. The reference to the variable in your main class doesn't exist yet, but we'll create it at the endYou can also just set the config equal to a new DefaultFeatureConfig. You can return this in getStructureStartFactory with return MyStructureStart::new.+
  
-This is where structure files and generating straight from a generate method part waysThere are two ways to go about this:+  * If you want, you can simply override ''generate'' in your piece class and use ''addBlock'' to place blocks directly in the world. This is a valid option and was popular pre-1.13. 
 +  * Use structure files. These are rather powerful at this point and are highly recommended.
  
-* If you wantyou can simply override generate in your Feature class and use setBlockState to place blocks directly in the world. This is valid option and was popular pre-1.13+In this tutorialwe'll use a structure file
-Use structure files and a GeneratorThese are rather powerful at this point and are highly recommended.+It doesn't need to override anything, but does require the following: 
 +  An identifier that points to your structure file; use ''"igloo/top"'' if you need an example. 
 +  * Some sort of setup method - ''addPieces'' is a good name.
  
-==== Creating a Generator ====+<code java> 
 +public class MyGenerator { 
 +  private static final Identifier IGLOO_TOP new Identifier("igloo/top");
  
-As you have probably noticedwe need to create a generator. We'll name it MyGeneratorand it's referenced in the initialize method of your StructureStart classIt doesn't need to override anythingbut does require the following:+  public static void addPieces(StructureManager managerBlockPos posBlockRotation rotation, List<StructurePiece> pieces) { 
 +    pieces.add(new MyPiece(manager, pos, IGLOO_TOProtation)); 
 +  } 
 +
 +</code>
  
-  * An Identifier that points to your structure file; use "igloo/top" if you need an example. +In your ''addPieces'' method, you can choose which structure pieces are added to your generation process.
-  * Some sort of setup method - addParts is a good name:+
  
-  public static void addParts(StructureManager structureManager_1, BlockPos blockPos_1, Rotation rotation_1,  +We're now going to create the piece we just referenced; make a class called ''MyPiece'' that extends ''SimpleStructurePiece'' //within your generator class//.
-  List<StructurePiece> list_1, Random random_1, DefaultFeatureConfig featureConfig) +
-     +
-    } +
-     +
-In your addParts method, you can choose which structure pieces are added to your generation processYou can add a piece like this:+
  
-  list_1.add(new MyGenerator.Piece(structureManager_1identifierblockPos, rotation_1));  +Override required methods, and add a constructor that takes in a ''StructureManager''''BlockPos''''Identifier'' and ''Rotation''. 
-   +**toNbt isn't required but is available if you need it**. 
-where the identifier is the path we created recently.+We're also implementing ''initializeStructureData'', which is not an override. 
 +We also have 2 constructors: 1 for our own pieces, and one for registry. 
 +A basic template would be:
  
-We're now going to create the Piece we just referenced; make a class called Piece that extends SimpleStructurePiece //within your generator class//.+<code java> 
 +public static class MyPiece extends SimpleStructurePiece 
 +  private final BlockRotation rotation; 
 +  private final Identifier template;
  
-Override required methods, and add a constructor that takes in a StructureManager, Identifier, BlockPosand Rotation**toNbt isn't required but is available if you need it**We're also implementing our own setStructureData with different arguments, so it's not an overrideWe also have 2 constructors: 1 for our own pieces, and one for registryA basic template would be:+  public MyPiece(StructureManager structureManagerCompoundTag compoundTag) { 
 +    super(ExampleMod.MY_PIECEcompoundTag); 
 +    this.template = new Identifier(compoundTag.getString("Template")); 
 +    this.rotation = BlockRotation.valueOf(compoundTag.getString("Rot")); 
 +    this.initializeStructureData(structureManager); 
 +  }
  
-  public static class Piece extends SimpleStructurePiece { +  public MyPiece(StructureManager structureManager, BlockPos pos, Identifier templateBlockRotation rotation) { 
-        private Rotation rotation; +    super(ExampleMod.MY_PIECE, 0); 
-        private Identifier template; +    this.pos = pos
-         +    this.rotation = rotation
-        public Piece(StructureManager structureManager_1, Identifier identifier_1BlockPos blockPos_1, Rotation rotation_1) { +    this.template = template;
-            super(MyModClass.myStructurePieceType, 0); +
-             +
-            this.pos = blockPos_1+
-            this.rotation = rotation_1+
-            this.template = identifier_1; +
-             +
-            this.setStructureData(structureManager_1); +
-        } +
-         +
-        public Piece(StructureManager structureManager_1, CompoundTag compoundTag_1) { +
-            super(MyModClass.myStructurePieceType, compoundTag_1); +
-            this.identifier = new Identifier(compoundTag_1.getString("Template")); +
-            this.rotation = Rotation.valueOf(compoundTag_1.getString("Rot")); +
-            this.setStructureData(structureManager_1); +
-        } +
-         +
-        @Override +
-        protected void toNbt(CompoundTag compoundTag_1) { +
-            super.toNbt(compoundTag_1); +
-            compoundTag_1.putString("Template", this.template.toString()); +
-            compoundTag_1.putString("Rot", this.rotation.name()); +
-        } +
-         +
-        public void setStructureData(StructureManager structureManager) +
-        { +
-            Structure structure_1 = structureManager.getStructureOrBlank(this.identifier); +
-            StructurePlacementData structurePlacementData_1 = (new StructurePlacementData()).setRotation(this.rotation).setMirrored(Mirror.NONE).setPosition(pos).addProcessor(BlockIgnoreStructureProcessor.IGNORE_STRUCTURE_BLOCKS); +
-            this.setStructureData(structure_1, this.pos, structurePlacementData_1); +
-        } +
-         +
-        @Override +
-        protected void handleMetadata(String string_1, BlockPos blockPos_1, IWorld iWorld_1, Random random_1, MutableIntBoundingBox mutableIntBoundingBox_1) { +
-        } +
-         +
-        @Override +
-        public boolean generate(IWorld iWorld_1, Random random_1, MutableIntBoundingBox mutableIntBoundingBox_1, ChunkPos chunkPos_1) +
-        { +
-           +
-        } +
-    } +
-     +
-handleMetadata is where you look at data blocks within your structure and do tasks based on what you find. In vanilla structures, data blocks are placed above chests so they can be filled with loot in this method.+
  
-We set the StructurePieceType to MyModClass.myStructurePiece type; this is the variable that holds your registered structure pieceWe'll handle that after we finish the generate functionwhich sets the position of your structure and generates it:+    this.initializeStructureData(structureManager); 
 +  } 
 + 
 +  private void initializeStructureData(StructureManager structureManager) { 
 +    Structure structure = structureManager.getStructureOrBlank(this.template); 
 +    StructurePlacementData placementData = (new StructurePlacementData()) 
 +      .setRotation(this.rotation) 
 +      .setMirror(BlockMirror.NONE) 
 +      .addProcessor(BlockIgnoreStructureProcessor.IGNORE_STRUCTURE_BLOCKS); 
 +    this.setStructureData(structure, this.posplacementData); 
 +  } 
 + 
 +  protected void toNbt(CompoundTag tag) { 
 +    super.toNbt(tag); 
 +    tag.putString("Template", this.template.toString()); 
 +    tag.putString("Rot", this.rotation.name()); 
 +  }
  
   @Override   @Override
-        public boolean generate(IWorld iWorld_1Random random_1MutableIntBoundingBox mutableIntBoundingBox_1ChunkPos chunkPos_1) +  protected void handleMetadata(String metadataBlockPos posServerWorldAccess serverWorldAccessRandom random
-        { +      BlockBox boundingBox{ 
-            int yHeight = iWorld_1.getTop(Heightmap.Type.WORLD_SURFACE_WGthis.pos.getX() + 8, this.pos.getZ() + 8); +  } 
-            this.pos = this.pos.add(0, yHeight - 1, 0); +} 
-            return  super.generate(iWorld_1, random_1, mutableIntBoundingBox_1, chunkPos_1); +</code>
-        }+
          
-In this case, we simply get the y position of the highest block in the middle of our chunk and generate the structure off that position+''handleMetadata'' is where you look at data blocks within your structure and can do tasks based on what you find. 
 +This can be good for dynamic stuff such as placing certain mobs based on what mod is on and so on.
  
-==== Registering Features ====+In vanilla structures, data blocks are placed above chests so they can be filled with loot in this method.  
 +HOWEVER, you do not need to use datablocks to place chests with loot. Instead, use this command to set a north facing chest with a loottable.  
 +Save this chest into your structure's nbt file and it will generate loot when opened for the first time. (Don't open the chest before saving to the nbt file!) 
 +<code>/setblock ~ ~ ~ minecraft:chest[facing=north]{LootTable:"modid:loottable"}</code>
  
-The last step is to register our features. We're going to need to register:+We set the ''StructurePieceType'' to ''ExampleMod.MY_PIECE''; this is the variable that holds your registered structure piece. 
  
-  * StructurePieceType +===== Registering Structures ===== 
-  * StructureFeature<DefaultFeatureConfig> +The last step is to register our structures. We're going to need to register:
-  * StructureFeature<?>+
  
-We're also going to need to add the structure to the STRUCTURES list and add it to each biome as a feature and generation step.+  * structure 
 +  * piece 
 +  * configured structure
  
-Registering piece type: +<code java> 
-  public static final StructurePieceType myStructurePieceType Registry.register(Registry.STRUCTURE_PIECE, "my_piece", MyGenerator.Piece::new)+public class ExampleMod implements ModInitializer { 
-   +  public static final StructurePieceType MY_PIECE = MyGenerator.MyPiece::new; 
-Registering feature: +  private static final StructureFeature<DefaultFeatureConfig> MY_STRUCTURE = new MyFeature(DefaultFeatureConfig.CODEC); 
-  public static final StructureFeature<DefaultFeatureConfig> myFeature Registry.register(Registry.FEATURE, "my_feature", new MyFeature()); +  private static final ConfiguredStructureFeature<?, ?> MY_CONFIGURED MY_STRUCTURE.configure(DefaultFeatureConfig.DEFAULT);
-   +
-Registering structure: +
-  public static final StructureFeature<?> myStructure Registry.register(Registry.STRUCTURE_FEATURE, "my_structure", myFeature); +
-   +
-To put your feature in the features list, you can use: +
-  Feature.STRUCTURES.put("My Awesome Feature", myFeature); +
-   +
-For testing, it's a good idea to register your feature to every biome and set the spawn rate to 100% so you can be sure it's spawning and working. You probably don't want your structure floating in the water, so we'll also filter that out. Add it to every biome by iterating over the biome list and adding it as a feature and generation step:+
  
-  for(Biome biome : Registry.BIOME) +  @Override 
- +  public void onInitialize() { 
- if(biome.getCategory() != Biome.Category.OCEAN && biome.getCategory() != Biome.Category.RIVER+    Registry.register(Registry.STRUCTURE_PIECE, new Identifier("tutorial", "my_piece"), MY_PIECE); 
- +    FabricStructureBuilder.create(new Identifier("tutorial", "my_structure"), MY_STRUCTURE
- biome.addStructureFeature(myFeature, new DefaultFeatureConfig()); +      .step(GenerationStep.Feature.SURFACE_STRUCTURES
- biome.addFeature(GenerationStep.Feature.SURFACE_STRUCTURES, Biome.configureFeature(myFeaturenew DefaultFeatureConfig(), Decorator.CHANCE_PASSTHROUGH, new ChanceDecoratorConfig(0))); +      .defaultConfig(328, 12345) 
-+      .adjustsSurface() 
- }+      .register();
  
-ChanceDecoratorConfig's argument is basically how many chunks it will skip over before generating0 is every chunk1 is every otherand 100 is every 100.+    RegistryKey<ConfiguredStructureFeature<?, ?>> myConfigured = RegistryKey.of(Registry.CONFIGURED_STRUCTURE_FEATURE_WORLDGEN, 
 +        new Identifier("tutorial""my_structure")); 
 +    BuiltinRegistries.add(BuiltinRegistries.CONFIGURED_STRUCTURE_FEATURE, myConfigured.getValue(), MY_CONFIGURED); 
 +  } 
 +
 +</code>
  
-You need to add your structure as a feature so your biome knows it existsand then as a generation step so it's actually generated.+===== Adding configured feature to biomes ===== 
 +In this tutorialwe add our structure to all biomes. 
 + 
 +<code java> 
 +public class ExampleMod implements ModInitializer { 
 +  [...] 
 +  
 +  @Override 
 +  public void onInitialize() { 
 +    [...]
  
-Load into your world, and if all went wellyou should be met with a //lot// of Igloos. +    BiomeModifications.addStructure(BiomeSelectors.all()myConfigured); 
 +  } 
 +
 +</code>
  
 +===== Result =====
 +You should be met with igloos.
 +You can use below command to find your structure in the world.
  
 +<code>
 +/locate tutorial:my_structure
 +</code>
  
 +{{tutorial:structures.png?800}}
  
tutorial/structures.1550350716.txt.gz · Last modified: 2019/02/16 20:58 by asie