Updated Structure Builder

This commit is contained in:
Frank 2022-12-03 02:49:58 +01:00
parent 7604e60bab
commit 4fee0429f7
2 changed files with 128 additions and 54 deletions

View file

@ -3,10 +3,8 @@ package org.betterx.bclib.api.v2.levelgen.structures;
import org.betterx.bclib.api.v2.levelgen.biomes.BCLBiomeBuilder;
import com.mojang.serialization.Codec;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Holder;
import net.minecraft.core.HolderSet;
import net.minecraft.core.QuartPos;
import net.minecraft.core.*;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.core.registries.Registries;
import net.minecraft.data.worldgen.BootstapContext;
import net.minecraft.resources.ResourceKey;
@ -26,13 +24,92 @@ import java.util.function.Function;
import org.jetbrains.annotations.NotNull;
public class BCLStructure<S extends Structure> {
private final Holder<Structure> structure;
private final GenerationStep.Decoration featureStep;
private final List<ResourceLocation> biomes = Lists.newArrayList();
private final ResourceLocation id;
public static class Unbound<S extends Structure> extends BCLStructure<S> {
private final Function<Structure.StructureSettings, S> structureBuilder;
private final TerrainAdjustment terrainAdjustment;
protected Unbound(
@NotNull ResourceLocation id,
@NotNull GenerationStep.Decoration step,
@NotNull StructurePlacement placement,
@NotNull Codec<S> codec,
@NotNull TagKey<Biome> biomeTag,
@NotNull Function<Structure.StructureSettings, S> structureBuilder,
@NotNull TerrainAdjustment terrainAdjustment
) {
super(
id,
ResourceKey.create(Registries.STRUCTURE, id),
ResourceKey.create(Registries.STRUCTURE_SET, id),
step,
placement,
codec,
biomeTag,
BCLStructure.registerStructureType(id, codec)
);
this.structureBuilder = structureBuilder;
this.terrainAdjustment = terrainAdjustment;
}
public Bound<S> register(BootstapContext<Structure> bootstrapContext) {
S baseStructure = structureBuilder.apply(structure(
bootstrapContext,
this.biomeTag,
this.featureStep,
terrainAdjustment
));
Holder.Reference<Structure> structure = bootstrapContext.register(structureKey, baseStructure);
return new Bound<>(
this.id,
this.structureKey,
this.structureSetKey,
this.featureStep,
this.spreadConfig,
this.STRUCTURE_CODEC,
this.biomeTag,
this.structureType,
baseStructure,
structure
);
}
}
public static class Bound<S extends Structure> extends BCLStructure<S> {
public final S baseStructure;
public final Holder<Structure> structure;
private Bound(
@NotNull ResourceLocation id,
@NotNull ResourceKey<Structure> structureKey,
@NotNull ResourceKey<StructureSet> structureSetKey,
@NotNull GenerationStep.Decoration featureStep,
@NotNull StructurePlacement placement,
@NotNull Codec<S> codec,
@NotNull TagKey<Biome> biomeTag,
@NotNull StructureType<S> structureType,
@NotNull S baseStructure,
@NotNull Holder<Structure> structure
) {
super(id, structureKey, structureSetKey, featureStep, placement, codec, biomeTag, structureType);
this.baseStructure = baseStructure;
this.structure = structure;
}
public Holder<Structure> getStructure() {
return structure;
}
}
protected final GenerationStep.Decoration featureStep;
protected final List<ResourceLocation> biomes = Lists.newArrayList();
protected final ResourceLocation id;
public final TagKey<Biome> biomeTag;
public final ResourceKey<Structure> structureKey;
public final S baseStructure;
public final ResourceKey<StructureSet> structureSetKey;
public final StructurePlacement spreadConfig;
@ -40,63 +117,61 @@ public class BCLStructure<S extends Structure> {
public final Codec<S> STRUCTURE_CODEC;
private static HolderSet<Biome> biomes(TagKey<Biome> tagKey) {
private static HolderSet<Biome> biomes(BootstapContext<Structure> bootstrapContext, TagKey<Biome> tagKey) {
//TODO:1.19.3 Refactor
return null; //BuiltInRegistries.BIOME.getOrCreateTag(tagKey);
}
private static Structure.StructureSettings structure(
BootstapContext<Structure> bootstrapContext,
TagKey<Biome> tagKey,
Map<MobCategory, StructureSpawnOverride> map,
GenerationStep.Decoration decoration,
TerrainAdjustment terrainAdjustment
) {
return new Structure.StructureSettings(biomes(tagKey), map, decoration, terrainAdjustment);
return new Structure.StructureSettings(biomes(bootstrapContext, tagKey), map, decoration, terrainAdjustment);
}
private static Structure.StructureSettings structure(
BootstapContext<Structure> bootstrapContext,
TagKey<Biome> tagKey,
GenerationStep.Decoration decoration,
TerrainAdjustment terrainAdjustment
) {
return structure(tagKey, Map.of(), decoration, terrainAdjustment);
return structure(bootstrapContext, tagKey, Map.of(), decoration, terrainAdjustment);
}
private static <S extends Structure> StructureType<S> registerStructureType(
BootstapContext<StructureType<?>> bootstrapContext,
ResourceLocation id,
Codec<S> codec
) {
final ResourceKey<StructureType<?>> key = ResourceKey.create(Registries.STRUCTURE_TYPE, id);
return (StructureType<S>) bootstrapContext.register(key, () -> (Codec<Structure>) codec).value();
return (StructureType<S>) Registry.register(
BuiltInRegistries.STRUCTURE_TYPE,
key,
() -> (Codec<Structure>) codec
);
}
protected BCLStructure(
@NotNull ResourceLocation id,
@NotNull Function<Structure.StructureSettings, S> structureBuilder,
GenerationStep.Decoration step,
@NotNull ResourceKey<Structure> structureKey,
@NotNull ResourceKey<StructureSet> structureSetKey,
@NotNull GenerationStep.Decoration step,
@NotNull StructurePlacement placement,
@NotNull Codec<S> codec,
@NotNull TagKey<Biome> biomeTag,
@NotNull TerrainAdjustment terrainAdjustment
@NotNull StructureType<S> structureType
) {
this.id = id;
this.featureStep = step;
this.STRUCTURE_CODEC = codec;
this.spreadConfig = placement;
this.structureKey = ResourceKey.create(Registries.STRUCTURE, id);
this.structureSetKey = ResourceKey.create(Registries.STRUCTURE_SET, id);
//TODO:1.19.3 Refactor
this.structureType = null; //registerStructureType(id, STRUCTURE_CODEC);
this.structureKey = structureKey;
this.structureSetKey = structureSetKey;
this.biomeTag = biomeTag;
this.baseStructure = structureBuilder.apply(structure(this.biomeTag, featureStep, terrainAdjustment));
//TODO:1.19.3 Refactor
this.structure = null; //StructuresAccessor.callRegister(structureKey, this.baseStructure);
//TODO:1.19.3 Refactor
//StructureSets.register(structureSetKey, this.structure, spreadConfig);
this.structureType = structureType;
}
/**
@ -133,10 +208,6 @@ public class BCLStructure<S extends Structure> {
);
}
public Holder<Structure> getStructure() {
return structure;
}
public GenerationStep.Decoration getFeatureStep() {
return featureStep;
}
@ -168,4 +239,11 @@ public class BCLStructure<S extends Structure> {
public List<ResourceLocation> getBiomes() {
return biomes;
}
public void registerSet(BootstapContext<StructureSet> bootstrapContext) {
bootstrapContext.register(structureSetKey, new StructureSet(
bootstrapContext.lookup(Registries.STRUCTURE).getOrThrow(structureKey),
spreadConfig
));
}
}

View file

@ -3,7 +3,6 @@ package org.betterx.bclib.api.v2.levelgen.structures;
import org.betterx.worlds.together.tag.v3.TagManager;
import com.mojang.serialization.Codec;
import net.minecraft.data.worldgen.BootstapContext;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.tags.TagKey;
import net.minecraft.world.level.biome.Biome;
@ -17,14 +16,9 @@ import net.minecraft.world.level.levelgen.structure.placement.StructurePlacement
import java.util.function.Function;
public class BCLStructureBuilder<S extends Structure> {
public record Context(BootstapContext<Structure> bootstrapContext) {
}
private static final BCLStructureBuilder INSTANCE = new BCLStructureBuilder();
private ResourceLocation structureID;
private Function<Structure.StructureSettings, S> structureBuilder;
private final ResourceLocation structureID;
private final Function<Structure.StructureSettings, S> structureBuilder;
private GenerationStep.Decoration step;
@ -36,23 +30,25 @@ public class BCLStructureBuilder<S extends Structure> {
private TerrainAdjustment terrainAdjustment;
private BCLStructureBuilder() {
private BCLStructureBuilder(
ResourceLocation structureID,
Function<Structure.StructureSettings, S> structureBuilder
) {
this.structureID = structureID;
this.structureBuilder = structureBuilder;
this.step = GenerationStep.Decoration.SURFACE_STRUCTURES;
this.terrainAdjustment = TerrainAdjustment.NONE;
this.codec = null;
this.placement = null;
this.biomeTag = null;
}
public static <S extends Structure> BCLStructureBuilder<S> start(
ResourceLocation structureID,
Function<Structure.StructureSettings, S> structureBuilder
) {
INSTANCE.structureID = structureID;
INSTANCE.structureBuilder = structureBuilder;
INSTANCE.step = GenerationStep.Decoration.SURFACE_STRUCTURES;
INSTANCE.terrainAdjustment = TerrainAdjustment.NONE;
INSTANCE.codec = null;
INSTANCE.placement = null;
INSTANCE.biomeTag = null;
return INSTANCE;
return new BCLStructureBuilder<>(structureID, structureBuilder);
}
public BCLStructureBuilder<S> adjustment(TerrainAdjustment value) {
@ -102,13 +98,13 @@ public class BCLStructureBuilder<S extends Structure> {
if (codec == null) codec(Structure.simpleCodec(structureBuilder));
if (biomeTag == null) biomeTag(structureID.getNamespace(), structureID.getPath());
return new BCLStructure<>(
return new BCLStructure.Unbound<>(
structureID,
structureBuilder,
step,
placement,
codec,
biomeTag,
structureBuilder,
terrainAdjustment
);
}