| 1 | package net.minecraft.world.attribute;
|
| 2 |
|
| 3 | import com.google.common.annotations.VisibleForTesting;
|
| 4 | import it.unimi.dsi.fastutil.objects.Reference2ObjectOpenHashMap;
|
| 5 | import java.util.ArrayList;
|
| 6 | import java.util.Collection;
|
| 7 | import java.util.HashMap;
|
| 8 | import java.util.List;
|
| 9 | import java.util.Map;
|
| 10 | import java.util.Objects;
|
| 11 | import java.util.stream.Stream;
|
| 12 | import net.minecraft.SharedConstants;
|
| 13 | import net.minecraft.core.Holder;
|
| 14 | import net.minecraft.core.HolderLookup;
|
| 15 | import net.minecraft.core.RegistryAccess;
|
| 16 | import net.minecraft.core.registries.Registries;
|
| 17 | import net.minecraft.util.Mth;
|
| 18 | import net.minecraft.world.clock.ClockManager;
|
| 19 | import net.minecraft.world.level.Level;
|
| 20 | import net.minecraft.world.level.LevelAccessor;
|
| 21 | import net.minecraft.world.level.biome.Biome;
|
| 22 | import net.minecraft.world.level.biome.BiomeManager;
|
| 23 | import net.minecraft.world.level.dimension.DimensionType;
|
| 24 | import net.minecraft.world.phys.Vec3;
|
| 25 | import net.minecraft.world.timeline.Timeline;
|
| 26 | import org.jspecify.annotations.Nullable;
|
| 27 |
|
| 28 | public class EnvironmentAttributeSystem implements EnvironmentAttributeReader {
|
| 29 | private final Map<EnvironmentAttribute<?>, EnvironmentAttributeSystem.ValueSampler<?>> attributeSamplers = new Reference2ObjectOpenHashMap<>();
|
| 30 |
|
| 31 | private EnvironmentAttributeSystem(final Map<EnvironmentAttribute<?>, List<EnvironmentAttributeLayer<?>>> layersByAttribute) {
|
| 32 | layersByAttribute.forEach(
|
| 33 | (attribute, layers) -> this.attributeSamplers
|
| 34 | .put((EnvironmentAttribute<?>)attribute, this.bakeLayerSampler((EnvironmentAttribute<?>)attribute, (List<? extends EnvironmentAttributeLayer<?>>)layers))
|
| 35 | );
|
| 36 | }
|
| 37 |
|
| 38 | private <Value> EnvironmentAttributeSystem.ValueSampler<Value> bakeLayerSampler(
|
| 39 | final EnvironmentAttribute<Value> attribute, final List<? extends EnvironmentAttributeLayer<?>> untypedLayers
|
| 40 | ) {
|
| 41 | List<EnvironmentAttributeLayer<Value>> layers = new ArrayList<>((Collection<? extends EnvironmentAttributeLayer<Value>>)untypedLayers);
|
| 42 | Value constantBaseValue = attribute.defaultValue();
|
| 43 |
|
| 44 | while (!layers.isEmpty()) {
|
| 45 | if (!(layers.getFirst() instanceof EnvironmentAttributeLayer.Constant<Value> constantLayer)) {
|
| 46 | break;
|
| 47 | }
|
| 48 |
|
| 49 | constantBaseValue = constantLayer.applyConstant(constantBaseValue);
|
| 50 | layers.removeFirst();
|
| 51 | }
|
| 52 |
|
| 53 | boolean isAffectedByPosition = layers.stream().anyMatch(layer -> layer instanceof EnvironmentAttributeLayer.Positional);
|
| 54 | return new EnvironmentAttributeSystem.ValueSampler<>(attribute, constantBaseValue, List.copyOf(layers), isAffectedByPosition);
|
| 55 | }
|
| 56 |
|
| 57 | public static EnvironmentAttributeSystem.Builder builder() {
|
| 58 | return new EnvironmentAttributeSystem.Builder();
|
| 59 | }
|
| 60 |
|
| 61 | private static void addStaticLayers(final EnvironmentAttributeSystem.Builder builder, final LevelAccessor level) {
|
| 62 | RegistryAccess registries = level.registryAccess();
|
| 63 | BiomeManager biomeManager = level.getBiomeManager();
|
| 64 | addDimensionLayer(builder, level.dimensionType());
|
| 65 | addBiomeLayer(builder, registries.lookupOrThrow(Registries.BIOME), biomeManager);
|
| 66 | }
|
| 67 |
|
| 68 | private static void addDynamicLayers(final EnvironmentAttributeSystem.Builder builder, final Level level) {
|
| 69 | ClockManager clockManager = level.clockManager();
|
| 70 | level.dimensionType().timelines().forEach(timeline -> builder.addTimelineLayer((Holder<Timeline>)timeline, clockManager));
|
| 71 | if (level.canHaveWeather()) {
|
| 72 | WeatherAttributes.addBuiltinLayers(builder, WeatherAttributes.WeatherAccess.from(level));
|
| 73 | }
|
| 74 | }
|
| 75 |
|
| 76 | private static void addDimensionLayer(final EnvironmentAttributeSystem.Builder builder, final DimensionType dimensionType) {
|
| 77 | builder.addConstantLayer(dimensionType.attributes());
|
| 78 | }
|
| 79 |
|
| 80 | private static void addBiomeLayer(final EnvironmentAttributeSystem.Builder builder, final HolderLookup<Biome> biomes, final BiomeManager biomeManager) {
|
| 81 | Stream<EnvironmentAttribute<?>> attributesProvidedByBiomes = biomes.listElements()
|
| 82 | .flatMap(biome -> biome.value().getAttributes().keySet().stream())
|
| 83 | .distinct();
|
| 84 | attributesProvidedByBiomes.forEach(attribute -> addBiomeLayerForAttribute(builder, (EnvironmentAttribute<?>)attribute, biomeManager));
|
| 85 | }
|
| 86 |
|
| 87 | private static <Value> void addBiomeLayerForAttribute(
|
| 88 | final EnvironmentAttributeSystem.Builder builder, final EnvironmentAttribute<Value> attribute, final BiomeManager biomeManager
|
| 89 | ) {
|
| 90 | builder.addPositionalLayer(
|
| 91 | attribute,
|
| 92 | (baseValue, pos, biomeWeights) -> {
|
| 93 | if (biomeWeights != null && attribute.isSpatiallyInterpolated()) {
|
| 94 | return biomeWeights.applyAttributeLayer(attribute, baseValue);
|
| 95 | }
|
| 96 |
|
| 97 | Holder<Biome> biome = attribute.isFullResolutionBiomes()
|
| 98 | ? biomeManager.getBiome(Mth.floor(pos.x), Mth.floor(pos.y), Mth.floor(pos.z))
|
| 99 | : biomeManager.getNoiseBiomeAtPosition(pos.x, pos.y, pos.z);
|
| 100 | return biome.value().getAttributes().applyModifier(attribute, baseValue);
|
| 101 | }
|
| 102 | );
|
| 103 | }
|
| 104 |
|
| 105 | public void invalidateTickCache() {
|
| 106 | this.attributeSamplers.values().forEach(EnvironmentAttributeSystem.ValueSampler::invalidateTickCache);
|
| 107 | }
|
| 108 |
|
| 109 | private <Value> EnvironmentAttributeSystem.@Nullable ValueSampler<Value> getValueSampler(final EnvironmentAttribute<Value> attribute) {
|
| 110 | return (EnvironmentAttributeSystem.ValueSampler<Value>)this.attributeSamplers.get(attribute);
|
| 111 | }
|
| 112 |
|
| 113 | @Override
|
| 114 | public <Value> Value getDimensionValue(final EnvironmentAttribute<Value> attribute) {
|
| 115 | if (SharedConstants.IS_RUNNING_IN_IDE && attribute.isPositional()) {
|
| 116 | throw new IllegalStateException("Position must always be provided for positional attribute " + attribute);
|
| 117 | }
|
| 118 |
|
| 119 | EnvironmentAttributeSystem.ValueSampler<Value> sampler = this.getValueSampler(attribute);
|
| 120 | return sampler == null ? attribute.defaultValue() : sampler.getDimensionValue();
|
| 121 | }
|
| 122 |
|
| 123 | @Override
|
| 124 | public <Value> Value getValue(final EnvironmentAttribute<Value> attribute, final Vec3 pos, final @Nullable SpatialAttributeInterpolator biomeInterpolator) {
|
| 125 | EnvironmentAttributeSystem.ValueSampler<Value> sampler = this.getValueSampler(attribute);
|
| 126 | return sampler == null ? attribute.defaultValue() : sampler.getValue(pos, biomeInterpolator);
|
| 127 | }
|
| 128 |
|
| 129 | @VisibleForTesting
|
| 130 | <Value> Value getConstantBaseValue(final EnvironmentAttribute<Value> attribute) {
|
| 131 | EnvironmentAttributeSystem.ValueSampler<Value> sampler = this.getValueSampler(attribute);
|
| 132 | return sampler != null ? sampler.baseValue : attribute.defaultValue();
|
| 133 | }
|
| 134 |
|
| 135 | @VisibleForTesting
|
| 136 | boolean isAffectedByPosition(final EnvironmentAttribute<?> attribute) {
|
| 137 | EnvironmentAttributeSystem.ValueSampler<?> sampler = this.getValueSampler(attribute);
|
| 138 | return sampler != null && sampler.isAffectedByPosition;
|
| 139 | }
|
| 140 |
|
| 141 | public static class Builder {
|
| 142 | private final Map<EnvironmentAttribute<?>, List<EnvironmentAttributeLayer<?>>> layersByAttribute = new HashMap<>();
|
| 143 |
|
| 144 | private Builder() {
|
| 145 | }
|
| 146 |
|
| 147 | public EnvironmentAttributeSystem.Builder addDefaultLayers(final Level level) {
|
| 148 | this.addStaticLayers(level);
|
| 149 | this.addDynamicLayers(level);
|
| 150 | return this;
|
| 151 | }
|
| 152 |
|
| 153 | public EnvironmentAttributeSystem.Builder addStaticLayers(final LevelAccessor level) {
|
| 154 | EnvironmentAttributeSystem.addStaticLayers(this, level);
|
| 155 | return this;
|
| 156 | }
|
| 157 |
|
| 158 | public EnvironmentAttributeSystem.Builder addDynamicLayers(final Level level) {
|
| 159 | EnvironmentAttributeSystem.addDynamicLayers(this, level);
|
| 160 | return this;
|
| 161 | }
|
| 162 |
|
| 163 | public EnvironmentAttributeSystem.Builder addConstantLayer(final EnvironmentAttributeMap attributeMap) {
|
| 164 | for (EnvironmentAttribute<?> attribute : attributeMap.keySet()) {
|
| 165 | this.addConstantEntry(attribute, attributeMap);
|
| 166 | }
|
| 167 |
|
| 168 | return this;
|
| 169 | }
|
| 170 |
|
| 171 | private <Value> EnvironmentAttributeSystem.Builder addConstantEntry(final EnvironmentAttribute<Value> attribute, final EnvironmentAttributeMap attributeMap) {
|
| 172 | EnvironmentAttributeMap.Entry<Value, ?> entry = attributeMap.get(attribute);
|
| 173 | if (entry == null) {
|
| 174 | throw new IllegalArgumentException("Missing attribute " + attribute);
|
| 175 | } else {
|
| 176 | return this.addConstantLayer(attribute, entry::applyModifier);
|
| 177 | }
|
| 178 | }
|
| 179 |
|
| 180 | public <Value> EnvironmentAttributeSystem.Builder addConstantLayer(
|
| 181 | final EnvironmentAttribute<Value> attribute, final EnvironmentAttributeLayer.Constant<Value> layer
|
| 182 | ) {
|
| 183 | return this.addLayer(attribute, layer);
|
| 184 | }
|
| 185 |
|
| 186 | public <Value> EnvironmentAttributeSystem.Builder addTimeBasedLayer(
|
| 187 | final EnvironmentAttribute<Value> attribute, final EnvironmentAttributeLayer.TimeBased<Value> layer
|
| 188 | ) {
|
| 189 | return this.addLayer(attribute, layer);
|
| 190 | }
|
| 191 |
|
| 192 | public <Value> EnvironmentAttributeSystem.Builder addPositionalLayer(
|
| 193 | final EnvironmentAttribute<Value> attribute, final EnvironmentAttributeLayer.Positional<Value> layer
|
| 194 | ) {
|
| 195 | return this.addLayer(attribute, layer);
|
| 196 | }
|
| 197 |
|
| 198 | private <Value> EnvironmentAttributeSystem.Builder addLayer(final EnvironmentAttribute<Value> attribute, final EnvironmentAttributeLayer<Value> layer) {
|
| 199 | this.layersByAttribute.computeIfAbsent(attribute, t -> new ArrayList<>()).add(layer);
|
| 200 | return this;
|
| 201 | }
|
| 202 |
|
| 203 | public EnvironmentAttributeSystem.Builder addTimelineLayer(final Holder<Timeline> timeline, final ClockManager clockManager) {
|
| 204 | for (EnvironmentAttribute<?> attribute : timeline.value().attributes()) {
|
| 205 | this.addTimelineLayerForAttribute(timeline, attribute, clockManager);
|
| 206 | }
|
| 207 |
|
| 208 | return this;
|
| 209 | }
|
| 210 |
|
| 211 | private <Value> void addTimelineLayerForAttribute(
|
| 212 | final Holder<Timeline> timeline, final EnvironmentAttribute<Value> attribute, final ClockManager clockManager
|
| 213 | ) {
|
| 214 | this.addTimeBasedLayer(attribute, timeline.value().createTrackSampler(attribute, clockManager));
|
| 215 | }
|
| 216 |
|
| 217 | public EnvironmentAttributeSystem build() {
|
| 218 | return new EnvironmentAttributeSystem(this.layersByAttribute);
|
| 219 | }
|
| 220 | }
|
| 221 |
|
| 222 | private static class ValueSampler<Value> {
|
| 223 | private final EnvironmentAttribute<Value> attribute;
|
| 224 | private final Value baseValue;
|
| 225 | private final List<EnvironmentAttributeLayer<Value>> layers;
|
| 226 | private final boolean isAffectedByPosition;
|
| 227 | private @Nullable Value cachedTickValue;
|
| 228 | private int cacheTickId;
|
| 229 |
|
| 230 | private ValueSampler(
|
| 231 | final EnvironmentAttribute<Value> attribute, final Value baseValue, final List<EnvironmentAttributeLayer<Value>> layers, final boolean isAffectedByPosition
|
| 232 | ) {
|
| 233 | this.attribute = attribute;
|
| 234 | this.baseValue = baseValue;
|
| 235 | this.layers = layers;
|
| 236 | this.isAffectedByPosition = isAffectedByPosition;
|
| 237 | }
|
| 238 |
|
| 239 | public void invalidateTickCache() {
|
| 240 | this.cachedTickValue = null;
|
| 241 | this.cacheTickId++;
|
| 242 | }
|
| 243 |
|
| 244 | public Value getDimensionValue() {
|
| 245 | if (this.cachedTickValue != null) {
|
| 246 | return this.cachedTickValue;
|
| 247 | }
|
| 248 |
|
| 249 | Value result = this.computeValueNotPositional();
|
| 250 | this.cachedTickValue = result;
|
| 251 | return result;
|
| 252 | }
|
| 253 |
|
| 254 | public Value getValue(final Vec3 pos, final @Nullable SpatialAttributeInterpolator biomeInterpolator) {
|
| 255 | return !this.isAffectedByPosition ? this.getDimensionValue() : this.computeValuePositional(pos, biomeInterpolator);
|
| 256 | }
|
| 257 |
|
| 258 | private Value computeValuePositional(final Vec3 pos, final @Nullable SpatialAttributeInterpolator biomeInterpolator) {
|
| 259 | Value result = this.baseValue;
|
| 260 |
|
| 261 | for (EnvironmentAttributeLayer<Value> layer : this.layers) {
|
| 262 | result = (Value)(switch (layer) {
|
| 263 | case EnvironmentAttributeLayer.Constant<Value> constantLayer -> constantLayer.applyConstant(result);
|
| 264 | case EnvironmentAttributeLayer.TimeBased<Value> timeBasedLayer -> timeBasedLayer.applyTimeBased(result, this.cacheTickId);
|
| 265 | case EnvironmentAttributeLayer.Positional<Value> positionalLayer -> positionalLayer.applyPositional(result, Objects.requireNonNull(pos), biomeInterpolator);
|
| 266 | default -> throw new MatchException(null, null);
|
| 267 | });
|
| 268 | }
|
| 269 |
|
| 270 | return this.attribute.sanitizeValue(result);
|
| 271 | }
|
| 272 |
|
| 273 | private Value computeValueNotPositional() {
|
| 274 | Value result = this.baseValue;
|
| 275 |
|
| 276 | for (EnvironmentAttributeLayer<Value> layer : this.layers) {
|
| 277 | result = (Value)(switch (layer) {
|
| 278 | case EnvironmentAttributeLayer.Constant<Value> constantLayer -> constantLayer.applyConstant(result);
|
| 279 | case EnvironmentAttributeLayer.TimeBased<Value> timeBasedLayer -> timeBasedLayer.applyTimeBased(result, this.cacheTickId);
|
| 280 | case EnvironmentAttributeLayer.Positional<Value> ignored -> result;
|
| 281 | default -> throw new MatchException(null, null);
|
| 282 | });
|
| 283 | }
|
| 284 |
|
| 285 | return this.attribute.sanitizeValue(result);
|
| 286 | }
|
| 287 | }
|
| 288 | }
|
| 289 |
|