Minecraft 1.12.2模组开发(十四) 建筑生成 (structure generation)
Minecraft 1.12.2模组开发(二十二) 多种建筑生成
通过命令让我们获得一个建筑方块:
/give @s minecraft:structure_block
开发包\run\saves\1_8version_test(你的存档名称)\generated\minecraft\structures
{
"name": "re8joymod:tank1/start_pool", //格式:模组名:建筑名称/start_pool的文件路径
"fallback": "minecraft:empty",
"elements": [
{
"weight": 1,
"element": {
"location": "re8joymod:tank1", //格式:模组名:建筑名称
"processors": "minecraft:empty",
"projection": "rigid",
"element_type": "minecraft:single_pool_element"
}
}
]
}
package com.joy187.re8joymod.common.world.structure.structures;
import com.google.common.collect.ImmutableList;
import com.mojang.serialization.Codec;
import net.minecraft.entity.EntityType;
import net.minecraft.util.SharedSeedRandom;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.ChunkPos;
import net.minecraft.util.math.MutableBoundingBox;
import net.minecraft.util.math.vector.Vector3i;
import net.minecraft.util.registry.Registry;
import net.minecraft.world.IBlockReader;
import net.minecraft.world.biome.Biome;
import net.minecraft.world.biome.MobSpawnInfo;
import net.minecraft.world.biome.provider.BiomeProvider;
import net.minecraft.world.gen.ChunkGenerator;
import net.minecraft.world.gen.GenerationStage;
import net.minecraft.world.gen.Heightmap;
import net.minecraft.world.gen.feature.NoFeatureConfig;
import net.minecraft.world.gen.feature.jigsaw.JigsawManager;
import net.minecraft.world.gen.feature.structure.*;
import net.minecraft.util.registry.DynamicRegistries;
import net.minecraft.world.gen.feature.template.TemplateManager;
import net.minecraft.block.BlockState;
import net.minecraft.util.ResourceLocation;
import com.joy187.re8joymod.Utils;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import java.util.List;
public class Tank1Structure extends Structure {
public Tank1Structure(Codec codec) {
super(codec);
}
@Override
public IStartFactory getStartFactory() {
return Tank1Structure.Start::new;
}
@Override
public GenerationStage.Decoration step() {
return GenerationStage.Decoration.SURFACE_STRUCTURES;
}
private static final List STRUCTURE_MONSTERS = ImmutableList.of(
new MobSpawnInfo.Spawners(EntityType.ILLUSIONER, 10, 1, 4),
new MobSpawnInfo.Spawners(EntityType.VINDICATOR, 10, 1, 4)
);
private static final List STRUCTURE_CREATURES = ImmutableList.of(
new MobSpawnInfo.Spawners(EntityType.SHEEP, 10, 1, 5),
new MobSpawnInfo.Spawners(EntityType.RABBIT, 10, 1, 4)
);
@Override
public List getDefaultSpawnList() {
return STRUCTURE_MONSTERS;
}
@Override
public List getDefaultCreatureSpawnList() {
return STRUCTURE_CREATURES;
}
@Override
protected boolean isFeatureChunk(ChunkGenerator chunkGenerator, BiomeProvider biomeSource,
long seed, SharedSeedRandom chunkRandom, int chunkX, int chunkZ,
Biome biome, ChunkPos chunkPos, NoFeatureConfig featureConfig) {
BlockPos centerOfChunk = new BlockPos((chunkX << 4) + 7, 0, (chunkZ << 4) + 7);
int landHeight = chunkGenerator.getBaseHeight(centerOfChunk.getX(), centerOfChunk.getZ(), Heightmap.Type.WORLD_SURFACE_WG);
IBlockReader columnOfBlocks = chunkGenerator.getBaseColumn(centerOfChunk.getX(), centerOfChunk.getZ());
BlockState topBlock = columnOfBlocks.getBlockState(centerOfChunk.above(landHeight));
return topBlock.getFluidState().isEmpty();
}
@Override
public boolean getDefaultRestrictsSpawnsToInside() {
return true;
}
public static class Start extends StructureStart {
public Start(Structure structureIn, int chunkX, int chunkZ, MutableBoundingBox mutableBoundingBox, int referenceIn, long seedIn) {
super(structureIn, chunkX, chunkZ, mutableBoundingBox, referenceIn, seedIn);
}
@Override
public void generatePieces(DynamicRegistries dynamicRegistryManager, ChunkGenerator chunkGenerator, TemplateManager templateManagerIn, int chunkX, int chunkZ, Biome biomeIn, NoFeatureConfig config) {
// Turns the chunk coordinates into actual coordinates we can use
int x = chunkX * 16 + 7;
int z = chunkZ * 16 + 7;
/*
* We pass this into addPieces to tell it where to generate the structure.
* If addPieces's last parameter is true, blockpos's Y value is ignored and the
* structure will spawn at terrain height instead. Set that parameter to false to
* force the structure to spawn at blockpos's Y value instead. You got options here!
*/
BlockPos centerPos = new BlockPos(x, 0, z);
/*
* If you are doing Nether structures, you'll probably want to spawn your structure on top of ledges.
* Best way to do that is to use getBaseColumn to grab a column of blocks at the structure's x/z position.
* Then loop through it and look for land with air above it and set blockpos's Y value to it.
* Make sure to set the final boolean in JigsawManager.addPieces to false so
* that the structure spawns at blockpos's y value instead of placing the structure on the Bedrock roof!
*/
//IBlockReader blockReader = chunkGenerator.getBaseColumn(blockpos.getX(), blockpos.getZ());
// All a structure has to do is call this method to turn it into a jigsaw based structure!
JigsawManager.addPieces(
dynamicRegistryManager,
new VillageConfig(() -> dynamicRegistryManager.registryOrThrow(Registry.TEMPLATE_POOL_REGISTRY)
// The path to the starting Template Pool JSON file to read.
//
// Note, this is "structure_tutorial:run_down_house/start_pool" which means
// the game will automatically look into the following path for the template pool:
// "resources/data/structure_tutorial/worldgen/template_pool/run_down_house/start_pool.json"
// This is why your pool files must be in "data//worldgen/template_pool/"
// because the game automatically will check in worldgen/template_pool for the pools.
.get(new ResourceLocation(Utils.MOD_ID, "tank1/start_pool")),
// How many pieces outward from center can a recursive jigsaw structure spawn.
// Our structure is only 1 piece outward and isn't recursive so any value of 1 or more doesn't change anything.
// However, I recommend you keep this a decent value like 10 so people can use datapacks to add additional pieces to your structure easily.
// But don't make it too large for recursive structures like villages or you'll crash server due to hundreds of pieces attempting to generate!
10),
AbstractVillagePiece::new,
chunkGenerator,
templateManagerIn,
centerPos, // Position of the structure. Y value is ignored if last parameter is set to true.
this.pieces, // The list that will be populated with the jigsaw pieces after this method.
this.random,
false, // Special boundary adjustments for villages. It's... hard to explain. Keep this false and make your pieces not be partially intersecting.
// Either not intersecting or fully contained will make children pieces spawn just fine. It's easier that way.
true); // Place at heightmap (top land). Set this to false for structure to be place at the passed in blockpos's Y value instead.
// Definitely keep this false when placing structures in the nether as otherwise, heightmap placing will put the structure on the Bedrock roof.
// **THE FOLLOWING TWO LINES ARE OPTIONAL**
//
// Right here, you can do interesting stuff with the pieces in this.pieces such as offset the
// center piece by 50 blocks up for no reason, remove repeats of a piece or add a new piece so
// only 1 of that piece exists, etc. But you do not have access to the piece's blocks as this list
// holds just the piece's size and positions. Blocks will be placed later in JigsawManager.
//
// In this case, we do `piece.offset` to raise pieces up by 1 block so that the house is not right on
// the surface of water or sunken into land a bit.
//
// Then we extend the bounding box down by 1 by doing `piece.getBoundingBox().minY` which will cause the
// land formed around the structure to be lowered and not cover the doorstep. You can raise the bounding
// box to force the structure to be buried as well. This bounding box stuff with land is only for structures
// that you added to Structure.NOISE_AFFECTING_FEATURES field handles adding land around the base of structures.
//
// By lifting the house up by 1 and lowering the bounding box, the land at bottom of house will now be
// flush with the surrounding terrain without blocking off the doorstep.
this.pieces.forEach(piece -> piece.move(0, 1, 0));
this.pieces.forEach(piece -> piece.getBoundingBox().y0 -= 1);
// Since by default, the start piece of a structure spawns with it's corner at centerPos
// and will randomly rotate around that corner, we will center the piece on centerPos instead.
// This is so that our structure's start piece is now centered on the water check done in isFeatureChunk.
// Whatever the offset done to center the start piece, that offset is applied to all other pieces
// so the entire structure is shifted properly to the new spot.
Vector3i structureCenter = this.pieces.get(0).getBoundingBox().getCenter();
int xOffset = centerPos.getX() - structureCenter.getX();
int zOffset = centerPos.getZ() - structureCenter.getZ();
for(StructurePiece structurePiece : this.pieces){
structurePiece.move(xOffset, 0, zOffset);
}
this.calculateBoundingBox();
//用作Debug,查看建筑生成的位置
LogManager.getLogger().log(Level.DEBUG, "Rundown House at " +
this.pieces.get(0).getBoundingBox().x0 + " " +
this.pieces.get(0).getBoundingBox().y0 + " " +
this.pieces.get(0).getBoundingBox().z0);
}
}
}
package com.joy187.re8joymod.common.world.structure;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.joy187.re8joymod.Utils;
import com.joy187.re8joymod.common.world.structure.structures.Tank1Structure;
import net.minecraft.util.registry.WorldGenRegistries;
import net.minecraft.world.gen.feature.NoFeatureConfig;
import net.minecraft.world.gen.feature.structure.Structure;
import net.minecraft.world.gen.settings.DimensionStructuresSettings;
import net.minecraft.world.gen.settings.StructureSeparationSettings;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.fml.RegistryObject;
import net.minecraftforge.fml.common.ObfuscationReflectionHelper;
import net.minecraftforge.registries.DeferredRegister;
import net.minecraftforge.registries.ForgeRegistries;
import java.util.HashMap;
import java.util.Map;
public class ModStructures {
// STStructures.java
public static final DeferredRegister> STRUCTURES =
DeferredRegister.create(ForgeRegistries.STRUCTURE_FEATURES, Utils.MOD_ID);
// public static final RegistryObject> TANK1 = STRUCTURES.register("tank1", Tank1Structure::new);
public static final RegistryObject> TANK1 =
STRUCTURES.register("tank1", () -> (new Tank1Structure(NoFeatureConfig.CODEC)));
/* average distance apart in chunks between spawn attempts */
/* minimum distance apart in chunks between spawn attempts. MUST BE LESS THAN ABOVE VALUE*/
/* this modifies the seed of the structure so no two structures always spawn over each-other.
Make this large and unique. */
public static void setupStructures() {
setupMapSpacingAndLand(TANK1.get(),new StructureSeparationSettings(100,50, 1234567897), true);
}
public static > void setupMapSpacingAndLand(
F structure,
StructureSeparationSettings structureSeparationSettings,
boolean transformSurroundingLand)
{
/*
* We need to add our structures into the map in Structure class
* alongside vanilla structures or else it will cause errors.
*
* If the registration is setup properly for the structure,
* getRegistryName() should never return null.
*/
Structure.STRUCTURES_REGISTRY.put(structure.getRegistryName().toString(), structure);
/*
* Whether surrounding land will be modified automatically to conform to the bottom of the structure.
* Basically, it adds land at the base of the structure like it does for Villages and Outposts.
* Doesn't work well on structure that have pieces stacked vertically or change in heights.
*
* Note: The air space this method will create will be filled with water if the structure is below sealevel.
* This means this is best for structure above sealevel so keep that in mind.
*
* NOISE_AFFECTING_FEATURES requires AccessTransformer (See resources/META-INF/accesstransformer.cfg)
*/
if(transformSurroundingLand){
Structure.NOISE_AFFECTING_FEATURES = ImmutableList.>builder()
.addAll(Structure.NOISE_AFFECTING_FEATURES)
.add(structure)
.build();
}
/*
* This is the map that holds the default spacing of all structures.
* Always add your structure to here so that other mods can utilize it if needed.
*
* However, while it does propagate the spacing to some correct dimensions from this map,
* it seems it doesn't always work for code made dimensions as they read from this list beforehand.
*
* Instead, we will use the WorldEvent.Load event in StructureTutorialMain to add the structure
* spacing from this list into that dimension or to do dimension blacklisting properly.
* We also use our entry in DimensionStructuresSettings.DEFAULTS in WorldEvent.Load as well.
*
* DEFAULTS requires AccessTransformer (See resources/META-INF/accesstransformer.cfg)
*/
DimensionStructuresSettings.DEFAULTS =
ImmutableMap., StructureSeparationSettings>builder()
.putAll(DimensionStructuresSettings.DEFAULTS)
.put(structure, structureSeparationSettings)
.build();
/*
* There are very few mods that relies on seeing your structure in the noise settings registry before the world is made.
*
* You may see some mods add their spacings to DimensionSettings.BUILTIN_OVERWORLD instead of the NOISE_GENERATOR_SETTINGS loop below but
* that field only applies for the default overworld and won't add to other worldtypes or dimensions (like amplified or Nether).
* So yeah, don't do DimensionSettings.BUILTIN_OVERWORLD. Use the NOISE_GENERATOR_SETTINGS loop below instead if you must.
*/
WorldGenRegistries.NOISE_GENERATOR_SETTINGS.entrySet().forEach(settings -> {
Map, StructureSeparationSettings> structureMap = settings.getValue().structureSettings().structureConfig();
/*
* Pre-caution in case a mod makes the structure map immutable like datapacks do.
* I take no chances myself. You never know what another mods does...
*
* structureConfig requires AccessTransformer (See resources/META-INF/accesstransformer.cfg)
*/
if(structureMap instanceof ImmutableMap){
Map, StructureSeparationSettings> tempMap = new HashMap<>(structureMap);
tempMap.put(structure, structureSeparationSettings);
settings.getValue().structureSettings().structureConfig = tempMap;
}
else{
structureMap.put(structure, structureSeparationSettings);
}
});
}
public static void register(IEventBus eventBus) {
STRUCTURES.register(eventBus);
}
}
这说明你还没有配置accesstransformer.cfg文件(路径:**resources/META-INF/**accesstransformer.cfg)
在该路径下新建一个accesstransformer.cfg文件,将如下的代码copy进去:
public-f net.minecraft.world.gen.feature.structure.Structure field_236384_t_ #LAND_TRANSFORMING_STRUCTURES
public-f net.minecraft.world.gen.settings.DimensionStructuresSettings field_236191_b_ #DEFAULT_STRUCTURE_CONFIGS
public-f net.minecraft.world.gen.FlatGenerationSettings field_202247_j #STRUCTURES
public-f net.minecraft.world.gen.settings.DimensionStructuresSettings field_236193_d_ #structures
accessTransformer = file('src/main/resources/META-INF/accesstransformer.cfg')
package com.joy187.re8joymod.common.world.gen;
import com.joy187.re8joymod.Utils;
import com.joy187.re8joymod.common.world.structure.ModStructures;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.registry.Registry;
import net.minecraft.util.registry.WorldGenRegistries;
import net.minecraft.world.gen.FlatGenerationSettings;
import net.minecraft.world.gen.feature.IFeatureConfig;
import net.minecraft.world.gen.feature.StructureFeature;
public class ModConfiguredStructures {
/**
* Static instance of our structure so we can reference it and add it to biomes easily.
*/
//你可以添加多个
public static StructureFeature, ?> CONFIGURED_TANK1_HOUSE = ModStructures.TANK1.get().configured(IFeatureConfig.NONE);
/**
* Registers the configured structure which is what gets added to the biomes.
* Noticed we are not using a forge registry because there is none for configured structures.
*
* We can register configured structures at any time before a world is clicked on and made.
* But the best time to register configured features by code is honestly to do it in FMLCommonSetupEvent.
*/
public static void registerConfiguredStructures() {
Registry> registry = WorldGenRegistries.CONFIGURED_STRUCTURE_FEATURE;
//可以继续添加多个
Registry.register(registry, new ResourceLocation(Utils.MOD_ID, "configured_tank1_house"), CONFIGURED_TANK1_HOUSE);
/*
* Requires AccessTransformer ( see resources/META-INF/accesstransformer.cfg )
*/
//将上方的你的所有的建筑都加进来(put函数)
FlatGenerationSettings.STRUCTURE_FEATURES.put(ModStructures.TANK1.get(), CONFIGURED_TANK1_HOUSE);
}
}
package com.joy187.re8joymod.common.world.gen;
import com.joy187.re8joymod.common.init.BiomeInit;
import com.joy187.re8joymod.common.world.structure.ModStructures;
import net.minecraft.util.RegistryKey;
import net.minecraft.util.registry.Registry;
import net.minecraft.world.biome.Biome;
import net.minecraft.world.gen.feature.IFeatureConfig;
import net.minecraft.world.gen.feature.StructureFeature;
import net.minecraftforge.common.BiomeDictionary;
import net.minecraftforge.event.world.BiomeLoadingEvent;
import java.util.List;
import java.util.Set;
import java.util.function.Supplier;
public class ModStructureGeneration {
public static void generateStructures(final BiomeLoadingEvent event) {
RegistryKey key = RegistryKey.create(Registry.BIOME_REGISTRY, event.getName());
Set types = BiomeDictionary.getTypes(key);
//这里是MC中的地形,你可以让你的建筑生成在想要的地形中
//例如生成在平原:types.contains(BiomeDictionary.Type.PLAINS)
if(types.contains(BiomeInit.RE8_BIOME)) {
//你可以添加多个自己想要生成的建筑
event.getGeneration().getStructures().add(() -> ModConfiguredStructures.CONFIGURED_TANK1_HOUSE);
}
//else if... 可以添加多个想要生成的地形中的建筑
}
}
package com.joy187.re8joymod.common.world;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.joy187.re8joymod.Utils;
import com.joy187.re8joymod.common.world.gen.ModFlowerGeneration;
import com.joy187.re8joymod.common.world.gen.ModStructureGeneration;
import com.joy187.re8joymod.common.world.structure.ModStructures;
import com.joy187.re8joymod.common.world.structure.structures.Tank1Structure;
import com.mojang.serialization.Codec;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.registry.Registry;
import net.minecraft.util.registry.WorldGenRegistries;
import net.minecraft.world.World;
import net.minecraft.world.gen.ChunkGenerator;
import net.minecraft.world.gen.FlatChunkGenerator;
import net.minecraft.world.gen.feature.structure.Structure;
import net.minecraft.world.gen.settings.DimensionStructuresSettings;
import net.minecraft.world.gen.settings.StructureSeparationSettings;
import net.minecraft.world.server.ServerWorld;
import net.minecraftforge.event.world.BiomeLoadingEvent;
import net.minecraftforge.event.world.WorldEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.ObfuscationReflectionHelper;
import org.apache.logging.log4j.LogManager;
import javax.swing.plaf.synth.SynthTreeUI;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
@Mod.EventBusSubscriber(modid = Utils.MOD_ID)
public class ModWorldEvents {
//StructureTutorialMain.java
@SubscribeEvent
public static void biomeLoadingEvent(final BiomeLoadingEvent event) {
//加入建筑的生成事件的声明
ModStructureGeneration.generateStructures(event);
// ModOreGeneration.generateOres(event);
// ModFlowerGeneration.generateFlowers(event);
// ModTreeGeneration.generateTrees(event);
}
//加入空间生成的相关设置
@SubscribeEvent
public static void addDimensionalSpacing(final WorldEvent.Load event) {
if(event.getWorld() instanceof ServerWorld) {
ServerWorld serverWorld = (ServerWorld) event.getWorld();
//建筑生成异常就会抛出相应错误
try {
Method GETCODEC_METHOD =
ObfuscationReflectionHelper.findMethod(ChunkGenerator.class, "func_230347_a_");
ResourceLocation cgRL = Registry.CHUNK_GENERATOR.getKey(
(Codec extends ChunkGenerator>)GETCODEC_METHOD.invoke(serverWorld.getChunkSource().generator));
if (cgRL != null && cgRL.getNamespace().equals("terraforged")) {
return;
}
} catch (Exception e) {
LogManager.getLogger().error("Was unable to check if " + serverWorld.getLevel()
+ " is using Terraforged's ChunkGenerator.");
}
// 放止建筑在超平坦世界生成
if (serverWorld.getChunkSource().generator instanceof FlatChunkGenerator &&
serverWorld.getLevel().equals(World.OVERWORLD)) {
return;
}
// 将我们的建筑添加到建筑生成地图中
Map, StructureSeparationSettings> tempMap =
new HashMap<>(serverWorld.getChunkSource().generator.getSettings().structureConfig());
tempMap.putIfAbsent(ModStructures.TANK1.get(),
DimensionStructuresSettings.DEFAULTS.get(ModStructures.TANK1.get()));
serverWorld.getChunkSource().generator.getSettings().structureConfig = tempMap;
}
}
}
event.enqueueWork(() -> {
ModStructures.setupStructures();
ModConfiguredStructures.registerConfiguredStructures();
});
ModStructures.register(bus);
SurfaceBuilderInit.SURFACE_BUILDERS.register(bus);
/locate re8joymod:tank1
输入tp指令进行坐标传送:
tp @s 具体坐标