FeatureSorter .java

net.minecraft.world.level.biome.FeatureSorter (class, 118 lines): members: buildFeaturesPerStep, StepFeatureData, featureGetter. imports: Graph, Builder.

net.minecraft.world.level.biomeclass118 linescommon4.3 KB

API Surface (2)

<T> List<FeatureSorter.StepFeatureData> buildFeaturesPerStep(final List<T> featureSources, final Function<T, List<HolderSet<PlacedFeature>>> featureGetter, final boolean tryReducingError)
record StepFeatureData(List<PlacedFeature> features, ToIntFunction<PlacedFeature> indexMapping)

Fields & Constants (1)

featureGetter

String literals (2)

"Feature order cycle found"
"Feature order cycle found, involved sources: "

Code Graph: Dependencies (6)

Edges (calls / imports / inherits / references) resolved by graphify from the AST. Every chip opens the class source.

Full Source

FeatureSorter.java118 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.level.biome;
2
3
import com.google.common.collect.ImmutableList;
4
import com.google.common.collect.Lists;
5
import com.google.common.collect.ImmutableList.Builder;
6
import it.unimi.dsi.fastutil.objects.Reference2IntMap;
7
import it.unimi.dsi.fastutil.objects.Reference2IntOpenHashMap;
8
import java.util.ArrayList;
9
import java.util.Collections;
10
import java.util.Comparator;
11
import java.util.List;
12
import java.util.ListIterator;
13
import java.util.Map;
14
import java.util.Set;
15
import java.util.TreeMap;
16
import java.util.TreeSet;
17
import java.util.function.Function;
18
import java.util.function.ToIntFunction;
19
import java.util.stream.Collectors;
20
import net.minecraft.core.Holder;
21
import net.minecraft.core.HolderSet;
22
import net.minecraft.util.Graph;
23
import net.minecraft.util.Util;
24
import net.minecraft.world.level.levelgen.placement.PlacedFeature;
25
import org.apache.commons.lang3.mutable.MutableInt;
26
27
public class FeatureSorter {
28
	public static <T> List<FeatureSorter.StepFeatureData> buildFeaturesPerStep(
29
		final List<T> featureSources, final Function<T, List<HolderSet<PlacedFeature>>> featureGetter, final boolean tryReducingError
30
	) {
31
		Reference2IntMap<PlacedFeature> featureIndex = new Reference2IntOpenHashMap<>();
32
		MutableInt nextFeatureIndex = new MutableInt(0);
33
34
		record FeatureData(int featureIndex, int step, PlacedFeature feature) {
35
		}
36
37
		Comparator<FeatureData> featureDataComparator = Comparator.comparingInt(FeatureData::step).thenComparingInt(FeatureData::featureIndex);
38
		Map<FeatureData, Set<FeatureData>> edges = new TreeMap<>(featureDataComparator);
39
		int maxStep = 0;
40
41
		for (T featureSource : featureSources) {
42
			List<FeatureData> featureList = Lists.newArrayList();
43
			List<HolderSet<PlacedFeature>> featuresForStep = featureGetter.apply(featureSource);
44
			maxStep = Math.max(maxStep, featuresForStep.size());
45
46
			for (int i = 0; i < featuresForStep.size(); i++) {
47
				for (Holder<PlacedFeature> featureSupplier : featuresForStep.get(i)) {
48
					PlacedFeature feature = featureSupplier.value();
49
					featureList.add(new FeatureData(featureIndex.computeIfAbsent(feature, f -> nextFeatureIndex.getAndIncrement()), i, feature));
50
				}
51
			}
52
53
			for (int i = 0; i < featureList.size(); i++) {
54
				Set<FeatureData> data = edges.computeIfAbsent(featureList.get(i), k -> new TreeSet<>(featureDataComparator));
55
				if (i < featureList.size() - 1) {
56
					data.add(featureList.get(i + 1));
57
				}
58
			}
59
		}
60
61
		Set<FeatureData> discovered = new TreeSet<>(featureDataComparator);
62
		Set<FeatureData> currentlyVisiting = new TreeSet<>(featureDataComparator);
63
		List<FeatureData> sortedFeatures = Lists.newArrayList();
64
65
		for (FeatureData feature : edges.keySet()) {
66
			if (!currentlyVisiting.isEmpty()) {
67
				throw new IllegalStateException("You somehow broke the universe; DFS bork (iteration finished with non-empty in-progress vertex set");
68
			}
69
70
			if (!discovered.contains(feature) && Graph.depthFirstSearch(edges, discovered, currentlyVisiting, sortedFeatures::add, feature)) {
71
				if (!tryReducingError) {
72
					throw new IllegalStateException("Feature order cycle found");
73
				}
74
75
				List<T> reducedSources = new ArrayList<>(featureSources);
76
77
				int lastSize;
78
				do {
79
					lastSize = reducedSources.size();
80
					ListIterator<T> iterator = reducedSources.listIterator();
81
82
					while (iterator.hasNext()) {
83
						T source = iterator.next();
84
						iterator.remove();
85
86
						try {
87
							buildFeaturesPerStep(reducedSources, featureGetter, false);
88
						} catch (IllegalStateException e) {
89
							continue;
90
						}
91
92
						iterator.add(source);
93
					}
94
				} while (lastSize != reducedSources.size());
95
96
				throw new IllegalStateException("Feature order cycle found, involved sources: " + reducedSources);
97
			}
98
		}
99
100
		Collections.reverse(sortedFeatures);
101
		Builder<FeatureSorter.StepFeatureData> features = ImmutableList.builder();
102
103
		for (int step = 0; step < maxStep; step++) {
104
			int finalStep = step;
105
			List<PlacedFeature> featuresInStep = sortedFeatures.stream().filter(p -> p.step() == finalStep).map(FeatureData::feature).collect(Collectors.toList());
106
			features.add(new FeatureSorter.StepFeatureData(featuresInStep));
107
		}
108
109
		return features.build();
110
	}
111
112
	public record StepFeatureData(List<PlacedFeature> features, ToIntFunction<PlacedFeature> indexMapping) {
113
		private StepFeatureData(final List<PlacedFeature> features) {
114
			this(features, Util.createIndexIdentityLookup(features));
115
		}
116
	}
117
}
118

Decompiled Minecraft 26.3 source of FeatureSorter (net/minecraft/world/level/biome/FeatureSorter.java) with Mojang official mappings, stored verbatim on the site.