| 1 | package net.minecraft.world.level.levelgen.structure.pools;
|
| 2 |
|
| 3 | import com.google.common.annotations.VisibleForTesting;
|
| 4 | import com.google.common.collect.Lists;
|
| 5 | import com.mojang.datafixers.util.Either;
|
| 6 | import com.mojang.serialization.Codec;
|
| 7 | import com.mojang.serialization.DataResult;
|
| 8 | import com.mojang.serialization.DynamicOps;
|
| 9 | import com.mojang.serialization.MapCodec;
|
| 10 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 11 | import java.util.Comparator;
|
| 12 | import java.util.List;
|
| 13 | import java.util.Optional;
|
| 14 | import java.util.function.Function;
|
| 15 | import net.minecraft.core.BlockPos;
|
| 16 | import net.minecraft.core.Holder;
|
| 17 | import net.minecraft.core.Vec3i;
|
| 18 | import net.minecraft.nbt.CompoundTag;
|
| 19 | import net.minecraft.resources.Identifier;
|
| 20 | import net.minecraft.util.RandomSource;
|
| 21 | import net.minecraft.util.Util;
|
| 22 | import net.minecraft.world.level.StructureManager;
|
| 23 | import net.minecraft.world.level.WorldGenLevel;
|
| 24 | import net.minecraft.world.level.block.Blocks;
|
| 25 | import net.minecraft.world.level.block.Rotation;
|
| 26 | import net.minecraft.world.level.block.state.properties.StructureMode;
|
| 27 | import net.minecraft.world.level.chunk.ChunkGenerator;
|
| 28 | import net.minecraft.world.level.levelgen.structure.BoundingBox;
|
| 29 | import net.minecraft.world.level.levelgen.structure.templatesystem.BlockIgnoreProcessor;
|
| 30 | import net.minecraft.world.level.levelgen.structure.templatesystem.JigsawReplacementProcessor;
|
| 31 | import net.minecraft.world.level.levelgen.structure.templatesystem.LiquidSettings;
|
| 32 | import net.minecraft.world.level.levelgen.structure.templatesystem.StructurePlaceSettings;
|
| 33 | import net.minecraft.world.level.levelgen.structure.templatesystem.StructureProcessorList;
|
| 34 | import net.minecraft.world.level.levelgen.structure.templatesystem.StructureProcessorType;
|
| 35 | import net.minecraft.world.level.levelgen.structure.templatesystem.StructureTemplate;
|
| 36 | import net.minecraft.world.level.levelgen.structure.templatesystem.StructureTemplateManager;
|
| 37 |
|
| 38 | public class SinglePoolElement extends StructurePoolElement {
|
| 39 | private static final Comparator<StructureTemplate.JigsawBlockInfo> HIGHEST_SELECTION_PRIORITY_FIRST = Comparator.comparingInt(
|
| 40 | StructureTemplate.JigsawBlockInfo::selectionPriority
|
| 41 | )
|
| 42 | .reversed();
|
| 43 | private static final Codec<Either<Identifier, StructureTemplate>> TEMPLATE_CODEC = Codec.of(
|
| 44 | SinglePoolElement::encodeTemplate, Identifier.CODEC.map(Either::left)
|
| 45 | );
|
| 46 | public static final MapCodec<SinglePoolElement> CODEC = RecordCodecBuilder.mapCodec(
|
| 47 | i -> i.group(templateCodec(), processorsCodec(), projectionCodec(), overrideLiquidSettingsCodec()).apply(i, SinglePoolElement::new)
|
| 48 | );
|
| 49 | protected final Either<Identifier, StructureTemplate> template;
|
| 50 | protected final Holder<StructureProcessorList> processors;
|
| 51 | protected final Optional<LiquidSettings> overrideLiquidSettings;
|
| 52 |
|
| 53 | private static <T> DataResult<T> encodeTemplate(final Either<Identifier, StructureTemplate> template, final DynamicOps<T> ops, final T prefix) {
|
| 54 | Optional<Identifier> location = template.left();
|
| 55 | return location.isEmpty() ? DataResult.error(() -> "Can not serialize a runtime pool element") : Identifier.CODEC.encode(location.get(), ops, prefix);
|
| 56 | }
|
| 57 |
|
| 58 | protected static <E extends SinglePoolElement> RecordCodecBuilder<E, Holder<StructureProcessorList>> processorsCodec() {
|
| 59 | return StructureProcessorType.LIST_CODEC.fieldOf("processors").forGetter(t -> t.processors);
|
| 60 | }
|
| 61 |
|
| 62 | protected static <E extends SinglePoolElement> RecordCodecBuilder<E, Optional<LiquidSettings>> overrideLiquidSettingsCodec() {
|
| 63 | return LiquidSettings.CODEC.optionalFieldOf("override_liquid_settings").forGetter(t -> t.overrideLiquidSettings);
|
| 64 | }
|
| 65 |
|
| 66 | protected static <E extends SinglePoolElement> RecordCodecBuilder<E, Either<Identifier, StructureTemplate>> templateCodec() {
|
| 67 | return TEMPLATE_CODEC.fieldOf("location").forGetter(t -> t.template);
|
| 68 | }
|
| 69 |
|
| 70 | protected SinglePoolElement(
|
| 71 | final Either<Identifier, StructureTemplate> template,
|
| 72 | final Holder<StructureProcessorList> processors,
|
| 73 | final StructureTemplatePool.Projection projection,
|
| 74 | final Optional<LiquidSettings> overrideLiquidSettings
|
| 75 | ) {
|
| 76 | super(projection);
|
| 77 | this.template = template;
|
| 78 | this.processors = processors;
|
| 79 | this.overrideLiquidSettings = overrideLiquidSettings;
|
| 80 | }
|
| 81 |
|
| 82 | @Override
|
| 83 | public Vec3i getSize(final StructureTemplateManager structureTemplateManager, final Rotation rotation) {
|
| 84 | StructureTemplate template = this.getTemplate(structureTemplateManager);
|
| 85 | return template.getSize(rotation);
|
| 86 | }
|
| 87 |
|
| 88 | private StructureTemplate getTemplate(final StructureTemplateManager structureTemplateManager) {
|
| 89 | return this.template.map(structureTemplateManager::getOrCreate, Function.identity());
|
| 90 | }
|
| 91 |
|
| 92 | public List<StructureTemplate.StructureBlockInfo> getDataMarkers(
|
| 93 | final StructureTemplateManager structureTemplateManager, final BlockPos position, final Rotation rotation, final boolean absolute
|
| 94 | ) {
|
| 95 | StructureTemplate template = this.getTemplate(structureTemplateManager);
|
| 96 | List<StructureTemplate.StructureBlockInfo> structureBlocks = template.filterBlocks(
|
| 97 | position, new StructurePlaceSettings().setRotation(rotation), Blocks.STRUCTURE_BLOCK, absolute
|
| 98 | );
|
| 99 | List<StructureTemplate.StructureBlockInfo> dataMarkers = Lists.newArrayList();
|
| 100 |
|
| 101 | for (StructureTemplate.StructureBlockInfo info : structureBlocks) {
|
| 102 | CompoundTag nbt = info.nbt();
|
| 103 | if (nbt != null) {
|
| 104 | StructureMode mode = nbt.read("mode", StructureMode.LEGACY_CODEC).orElseThrow();
|
| 105 | if (mode == StructureMode.DATA) {
|
| 106 | dataMarkers.add(info);
|
| 107 | }
|
| 108 | }
|
| 109 | }
|
| 110 |
|
| 111 | return dataMarkers;
|
| 112 | }
|
| 113 |
|
| 114 | @Override
|
| 115 | public List<StructureTemplate.JigsawBlockInfo> getShuffledJigsawBlocks(
|
| 116 | final StructureTemplateManager structureTemplateManager, final BlockPos position, final Rotation rotation, final RandomSource random
|
| 117 | ) {
|
| 118 | List<StructureTemplate.JigsawBlockInfo> jigsaws = this.getTemplate(structureTemplateManager).getJigsaws(position, rotation);
|
| 119 | Util.shuffle(jigsaws, random);
|
| 120 | sortBySelectionPriority(jigsaws);
|
| 121 | return jigsaws;
|
| 122 | }
|
| 123 |
|
| 124 | @VisibleForTesting
|
| 125 | static void sortBySelectionPriority(final List<StructureTemplate.JigsawBlockInfo> blocks) {
|
| 126 | blocks.sort(HIGHEST_SELECTION_PRIORITY_FIRST);
|
| 127 | }
|
| 128 |
|
| 129 | @Override
|
| 130 | public BoundingBox getBoundingBox(final StructureTemplateManager structureTemplateManager, final BlockPos position, final Rotation rotation) {
|
| 131 | StructureTemplate template = this.getTemplate(structureTemplateManager);
|
| 132 | return template.getBoundingBox(new StructurePlaceSettings().setRotation(rotation), position);
|
| 133 | }
|
| 134 |
|
| 135 | @Override
|
| 136 | public boolean place(
|
| 137 | final StructureTemplateManager structureTemplateManager,
|
| 138 | final WorldGenLevel level,
|
| 139 | final StructureManager structureManager,
|
| 140 | final ChunkGenerator generator,
|
| 141 | final BlockPos position,
|
| 142 | final BlockPos referencePos,
|
| 143 | final Rotation rotation,
|
| 144 | final BoundingBox chunkBB,
|
| 145 | final RandomSource random,
|
| 146 | final LiquidSettings liquidSettings,
|
| 147 | final boolean keepJigsaws
|
| 148 | ) {
|
| 149 | StructureTemplate template = this.getTemplate(structureTemplateManager);
|
| 150 | StructurePlaceSettings settings = this.getSettings(rotation, chunkBB, liquidSettings, keepJigsaws);
|
| 151 | if (!template.placeInWorld(level, position, referencePos, settings, random, 18)) {
|
| 152 | return false;
|
| 153 | }
|
| 154 |
|
| 155 | for (StructureTemplate.StructureBlockInfo dataMarker : StructureTemplate.processBlockInfos(
|
| 156 | level, position, referencePos, settings, this.getDataMarkers(structureTemplateManager, position, rotation, false)
|
| 157 | )) {
|
| 158 | this.handleDataMarker(level, dataMarker, position, rotation, random, chunkBB);
|
| 159 | }
|
| 160 |
|
| 161 | return true;
|
| 162 | }
|
| 163 |
|
| 164 | protected StructurePlaceSettings getSettings(
|
| 165 | final Rotation rotation, final BoundingBox chunkBB, final LiquidSettings liquidSettings, final boolean keepJigsaws
|
| 166 | ) {
|
| 167 | StructurePlaceSettings settings = new StructurePlaceSettings();
|
| 168 | settings.setBoundingBox(chunkBB);
|
| 169 | settings.setRotation(rotation);
|
| 170 | settings.setKnownShape(true);
|
| 171 | settings.setIgnoreEntities(false);
|
| 172 | settings.addProcessor(BlockIgnoreProcessor.STRUCTURE_BLOCK);
|
| 173 | settings.setFinalizeEntities(true);
|
| 174 | settings.setLiquidSettings(this.overrideLiquidSettings.orElse(liquidSettings));
|
| 175 | if (!keepJigsaws) {
|
| 176 | settings.addProcessor(JigsawReplacementProcessor.INSTANCE);
|
| 177 | }
|
| 178 |
|
| 179 | this.processors.value().list().forEach(settings::addProcessor);
|
| 180 | this.getProjection().getProcessors().forEach(settings::addProcessor);
|
| 181 | return settings;
|
| 182 | }
|
| 183 |
|
| 184 | @Override
|
| 185 | public StructurePoolElementType<?> getType() {
|
| 186 | return StructurePoolElementType.SINGLE;
|
| 187 | }
|
| 188 |
|
| 189 | @Override
|
| 190 | public String toString() {
|
| 191 | return "Single[" + this.template + "]";
|
| 192 | }
|
| 193 |
|
| 194 | @VisibleForTesting
|
| 195 | public Identifier getTemplateLocation() {
|
| 196 | return this.template.orThrow();
|
| 197 | }
|
| 198 | }
|
| 199 |
|