| 1 | package net.minecraft.world.level.levelgen.structure.pools;
|
| 2 |
|
| 3 | import com.google.common.annotations.VisibleForTesting;
|
| 4 | import com.google.common.collect.ImmutableList;
|
| 5 | import com.google.common.collect.Lists;
|
| 6 | import com.mojang.datafixers.util.Pair;
|
| 7 | import com.mojang.serialization.Codec;
|
| 8 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 9 | import it.unimi.dsi.fastutil.objects.ObjectArrayList;
|
| 10 | import java.util.List;
|
| 11 | import java.util.function.Function;
|
| 12 | import net.minecraft.core.BlockPos;
|
| 13 | import net.minecraft.core.Holder;
|
| 14 | import net.minecraft.core.registries.Registries;
|
| 15 | import net.minecraft.core.registries.codec.RegistryCodecs;
|
| 16 | import net.minecraft.util.RandomSource;
|
| 17 | import net.minecraft.util.StringRepresentable;
|
| 18 | import net.minecraft.util.Util;
|
| 19 | import net.minecraft.world.level.block.Rotation;
|
| 20 | import net.minecraft.world.level.levelgen.Heightmap;
|
| 21 | import net.minecraft.world.level.levelgen.structure.templatesystem.GravityProcessor;
|
| 22 | import net.minecraft.world.level.levelgen.structure.templatesystem.StructureProcessor;
|
| 23 | import net.minecraft.world.level.levelgen.structure.templatesystem.StructureTemplateManager;
|
| 24 | import org.apache.commons.lang3.mutable.MutableObject;
|
| 25 |
|
| 26 | public class StructureTemplatePool {
|
| 27 | private static final int SIZE_UNSET = Integer.MIN_VALUE;
|
| 28 | private static final MutableObject<Codec<Holder<StructureTemplatePool>>> CODEC_REFERENCE = new MutableObject<>();
|
| 29 | public static final Codec<StructureTemplatePool> DIRECT_CODEC = RecordCodecBuilder.create(
|
| 30 | i -> i.group(
|
| 31 | Codec.lazyInitialized(CODEC_REFERENCE).fieldOf("fallback").forGetter(StructureTemplatePool::getFallback),
|
| 32 | Codec.mapPair(StructurePoolElement.CODEC.fieldOf("element"), Codec.intRange(1, 150).fieldOf("weight"))
|
| 33 | .codec()
|
| 34 | .listOf()
|
| 35 | .fieldOf("elements")
|
| 36 | .forGetter(p -> p.rawTemplates)
|
| 37 | )
|
| 38 | .apply(i, StructureTemplatePool::new)
|
| 39 | );
|
| 40 | public static final Codec<Holder<StructureTemplatePool>> CODEC = Util.make(
|
| 41 | RegistryCodecs.holder(Registries.TEMPLATE_POOL, DIRECT_CODEC), CODEC_REFERENCE::setValue
|
| 42 | );
|
| 43 | private final List<Pair<StructurePoolElement, Integer>> rawTemplates;
|
| 44 | private final ObjectArrayList<StructurePoolElement> templates;
|
| 45 | private final Holder<StructureTemplatePool> fallback;
|
| 46 | private int maxSize = Integer.MIN_VALUE;
|
| 47 |
|
| 48 | public StructureTemplatePool(final Holder<StructureTemplatePool> fallback, final List<Pair<StructurePoolElement, Integer>> templates) {
|
| 49 | this.rawTemplates = templates;
|
| 50 | this.templates = new ObjectArrayList<>();
|
| 51 |
|
| 52 | for (Pair<StructurePoolElement, Integer> templateDef : templates) {
|
| 53 | StructurePoolElement element = templateDef.getFirst();
|
| 54 |
|
| 55 | for (int i = 0; i < templateDef.getSecond(); i++) {
|
| 56 | this.templates.add(element);
|
| 57 | }
|
| 58 | }
|
| 59 |
|
| 60 | this.fallback = fallback;
|
| 61 | }
|
| 62 |
|
| 63 | public StructureTemplatePool(
|
| 64 | final Holder<StructureTemplatePool> fallback,
|
| 65 | final List<Pair<Function<StructureTemplatePool.Projection, ? extends StructurePoolElement>, Integer>> templates,
|
| 66 | final StructureTemplatePool.Projection projection
|
| 67 | ) {
|
| 68 | this.rawTemplates = Lists.newArrayList();
|
| 69 | this.templates = new ObjectArrayList<>();
|
| 70 |
|
| 71 | for (Pair<Function<StructureTemplatePool.Projection, ? extends StructurePoolElement>, Integer> templateDef : templates) {
|
| 72 | StructurePoolElement element = templateDef.getFirst().apply(projection);
|
| 73 | this.rawTemplates.add(Pair.of(element, templateDef.getSecond()));
|
| 74 |
|
| 75 | for (int i = 0; i < templateDef.getSecond(); i++) {
|
| 76 | this.templates.add(element);
|
| 77 | }
|
| 78 | }
|
| 79 |
|
| 80 | this.fallback = fallback;
|
| 81 | }
|
| 82 |
|
| 83 | public int getMaxSize(final StructureTemplateManager manager) {
|
| 84 | if (this.maxSize == Integer.MIN_VALUE) {
|
| 85 | this.maxSize = this.templates
|
| 86 | .stream()
|
| 87 | .filter(t -> t != EmptyPoolElement.INSTANCE)
|
| 88 | .mapToInt(t -> t.getBoundingBox(manager, BlockPos.ZERO, Rotation.NONE).getYSpan())
|
| 89 | .max()
|
| 90 | .orElse(0);
|
| 91 | }
|
| 92 |
|
| 93 | return this.maxSize;
|
| 94 | }
|
| 95 |
|
| 96 | @VisibleForTesting
|
| 97 | public List<Pair<StructurePoolElement, Integer>> getTemplates() {
|
| 98 | return this.rawTemplates;
|
| 99 | }
|
| 100 |
|
| 101 | public Holder<StructureTemplatePool> getFallback() {
|
| 102 | return this.fallback;
|
| 103 | }
|
| 104 |
|
| 105 | public StructurePoolElement getRandomTemplate(final RandomSource random) {
|
| 106 | return this.templates.isEmpty() ? EmptyPoolElement.INSTANCE : this.templates.get(random.nextInt(this.templates.size()));
|
| 107 | }
|
| 108 |
|
| 109 | public List<StructurePoolElement> getShuffledTemplates(final RandomSource random) {
|
| 110 | return Util.shuffledCopy(this.templates, random);
|
| 111 | }
|
| 112 |
|
| 113 | public int size() {
|
| 114 | return this.templates.size();
|
| 115 | }
|
| 116 |
|
| 117 | public enum Projection implements StringRepresentable {
|
| 118 | TERRAIN_MATCHING("terrain_matching", ImmutableList.of(new GravityProcessor(Heightmap.Types.WORLD_SURFACE_WG, -1))),
|
| 119 | RIGID("rigid", ImmutableList.of());
|
| 120 |
|
| 121 | public static final StringRepresentable.EnumCodec<StructureTemplatePool.Projection> CODEC = StringRepresentable.fromEnum(
|
| 122 | StructureTemplatePool.Projection::values
|
| 123 | );
|
| 124 | private final String name;
|
| 125 | private final ImmutableList<StructureProcessor> processors;
|
| 126 |
|
| 127 | Projection(final String name, final ImmutableList<StructureProcessor> processors) {
|
| 128 | this.name = name;
|
| 129 | this.processors = processors;
|
| 130 | }
|
| 131 |
|
| 132 | public String getName() {
|
| 133 | return this.name;
|
| 134 | }
|
| 135 |
|
| 136 | public static StructureTemplatePool.Projection byName(final String name) {
|
| 137 | return CODEC.byName(name);
|
| 138 | }
|
| 139 |
|
| 140 | public ImmutableList<StructureProcessor> getProcessors() {
|
| 141 | return this.processors;
|
| 142 | }
|
| 143 |
|
| 144 | @Override
|
| 145 | public String getSerializedName() {
|
| 146 | return this.name;
|
| 147 | }
|
| 148 | }
|
| 149 | }
|
| 150 |
|