| 1 | package net.minecraft.world.level.levelgen.feature;
|
| 2 |
|
| 3 | import com.mojang.serialization.Codec;
|
| 4 | import com.mojang.serialization.MapCodec;
|
| 5 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 6 | import java.util.List;
|
| 7 | import java.util.Optional;
|
| 8 | import net.minecraft.core.BlockPos;
|
| 9 | import net.minecraft.core.Direction;
|
| 10 | import net.minecraft.core.Holder;
|
| 11 | import net.minecraft.core.Vec3i;
|
| 12 | import net.minecraft.resources.Identifier;
|
| 13 | import net.minecraft.util.RandomSource;
|
| 14 | import net.minecraft.util.Util;
|
| 15 | import net.minecraft.util.random.WeightedList;
|
| 16 | import net.minecraft.world.level.WorldGenLevel;
|
| 17 | import net.minecraft.world.level.block.Rotation;
|
| 18 | import net.minecraft.world.level.chunk.ChunkGenerator;
|
| 19 | import net.minecraft.world.level.levelgen.structure.templatesystem.StructurePlaceSettings;
|
| 20 | import net.minecraft.world.level.levelgen.structure.templatesystem.StructureProcessor;
|
| 21 | import net.minecraft.world.level.levelgen.structure.templatesystem.StructureProcessorList;
|
| 22 | import net.minecraft.world.level.levelgen.structure.templatesystem.StructureProcessorType;
|
| 23 | import net.minecraft.world.level.levelgen.structure.templatesystem.StructureTemplate;
|
| 24 | import net.minecraft.world.level.levelgen.structure.templatesystem.StructureTemplateManager;
|
| 25 |
|
| 26 | public record TemplateFeature(WeightedList<TemplateFeature.TemplateEntry> templates, Optional<Holder<StructureProcessorList>> processors) implements Feature {
|
| 27 | public static final MapCodec<TemplateFeature> CODEC = RecordCodecBuilder.mapCodec(
|
| 28 | i -> i.group(
|
| 29 | WeightedList.codec(TemplateFeature.TemplateEntry.CODEC).fieldOf("templates").forGetter(TemplateFeature::templates),
|
| 30 | StructureProcessorType.LIST_CODEC.optionalFieldOf("processors").forGetter(TemplateFeature::processors)
|
| 31 | )
|
| 32 | .apply(i, TemplateFeature::new)
|
| 33 | );
|
| 34 |
|
| 35 | public TemplateFeature(final WeightedList<TemplateFeature.TemplateEntry> templates) {
|
| 36 | this(templates, Optional.empty());
|
| 37 | }
|
| 38 |
|
| 39 | public static TemplateFeature simple(final Identifier id, final Holder<StructureProcessorList> processors) {
|
| 40 | return new TemplateFeature(WeightedList.of(TemplateFeature.TemplateEntry.of(id)), Optional.of(processors));
|
| 41 | }
|
| 42 |
|
| 43 | public static TemplateFeature simple(final Identifier id) {
|
| 44 | return new TemplateFeature(WeightedList.of(TemplateFeature.TemplateEntry.of(id)), Optional.empty());
|
| 45 | }
|
| 46 |
|
| 47 | @Override
|
| 48 | public MapCodec<TemplateFeature> codec() {
|
| 49 | return CODEC;
|
| 50 | }
|
| 51 |
|
| 52 | @Override
|
| 53 | public boolean place(final WorldGenLevel level, final ChunkGenerator chunkGenerator, final RandomSource random, final BlockPos origin) {
|
| 54 | TemplateFeature.TemplateEntry templateEntry = this.templates.getRandomOrThrow(random);
|
| 55 | Rotation rotation = Util.getRandom(templateEntry.rotations(), random);
|
| 56 | StructureTemplateManager structureTemplateManager = level.getLevel().getServer().getStructureTemplateManager();
|
| 57 | StructureTemplate template = structureTemplateManager.getOrCreate(templateEntry.template());
|
| 58 | Vec3i offsetX = this.getRotatedOffset(rotation, Direction.Axis.X, template);
|
| 59 | Vec3i offsetZ = this.getRotatedOffset(rotation, Direction.Axis.Z, template);
|
| 60 | BlockPos pos = origin.offset(offsetX).offset(offsetZ);
|
| 61 | StructurePlaceSettings settings = new StructurePlaceSettings().setRotation(rotation).setRandom(random);
|
| 62 | if (this.processors.isPresent()) {
|
| 63 | for (StructureProcessor processor : this.processors.get().value().list()) {
|
| 64 | settings.addProcessor(processor);
|
| 65 | }
|
| 66 | }
|
| 67 |
|
| 68 | return template.placeInWorld(level, pos, pos, settings, random, 3);
|
| 69 | }
|
| 70 |
|
| 71 | private Vec3i getRotatedOffset(final Rotation rotation, final Direction.Axis axis, final StructureTemplate template) {
|
| 72 | return rotation.rotate(axis.getNegative()).getUnitVec3i().multiply(template.getSize().get(axis) / 2);
|
| 73 | }
|
| 74 |
|
| 75 | public record TemplateEntry(Identifier template, List<Rotation> rotations) {
|
| 76 | public static final Codec<TemplateFeature.TemplateEntry> CODEC = RecordCodecBuilder.create(
|
| 77 | i -> i.group(
|
| 78 | Identifier.CODEC.fieldOf("id").forGetter(TemplateFeature.TemplateEntry::template),
|
| 79 | Rotation.CODEC.listOf().optionalFieldOf("rotations", List.of(Rotation.values())).forGetter(TemplateFeature.TemplateEntry::rotations)
|
| 80 | )
|
| 81 | .apply(i, TemplateFeature.TemplateEntry::new)
|
| 82 | );
|
| 83 |
|
| 84 | public static TemplateFeature.TemplateEntry of(final Identifier template) {
|
| 85 | return new TemplateFeature.TemplateEntry(template, List.of(Rotation.values()));
|
| 86 | }
|
| 87 | }
|
| 88 | }
|
| 89 |
|