EnvironmentAttributeValue .java

net.minecraft.world.level.storage.loot.providers.number.ints.EnvironmentAttributeValue (record): members: attribute, ATTRIBUTE_CODEC, getAsInt, MAP_CODEC.

net.minecraft.world.level.storage.loot.providers.number.intsrecord33 linescommon1.5 KB

Implements

API Surface (2)

MapCodec<EnvironmentAttributeValue> codec()
int getIntUnsafe(final LootContext context)

Fields & Constants (2)

ATTRIBUTE_CODEC
MAP_CODEC

String literals (1)

" cannot be converted to an integer"

Code Graph: Dependencies (5)

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

Full Source

EnvironmentAttributeValue.java33 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.level.storage.loot.providers.number.ints;
2
3
import com.mojang.serialization.Codec;
4
import com.mojang.serialization.DataResult;
5
import com.mojang.serialization.MapCodec;
6
import net.minecraft.world.attribute.EnvironmentAttribute;
7
import net.minecraft.world.attribute.EnvironmentAttributes;
8
import net.minecraft.world.level.storage.loot.LootContext;
9
import net.minecraft.world.level.storage.loot.providers.number.EnvironmentAttributeProvider;
10
11
public record EnvironmentAttributeValue(EnvironmentAttribute<?> attribute) implements ContextIntProvider, EnvironmentAttributeProvider {
12
	private static final Codec<EnvironmentAttribute<?>> ATTRIBUTE_CODEC = EnvironmentAttributes.CODEC
13
		.validate(
14
			attribute -> attribute.type().toInt() == null ? DataResult.error(() -> attribute + " cannot be converted to an integer") : DataResult.success(attribute)
15
		);
16
	public static final MapCodec<EnvironmentAttributeValue> MAP_CODEC = EnvironmentAttributeProvider.mapCodec(ATTRIBUTE_CODEC, EnvironmentAttributeValue::new);
17
18
	@Override
19
	public MapCodec<EnvironmentAttributeValue> codec() {
20
		return MAP_CODEC;
21
	}
22
23
	@Override
24
	public int getIntUnsafe(final LootContext context) throws ArithmeticException {
25
		return getAsInt(context, this.attribute);
26
	}
27
28
	private static <Value> int getAsInt(final LootContext context, final EnvironmentAttribute<Value> attribute) {
29
		Value value = context.getLevel().environmentAttributes().getValue(context, attribute);
30
		return attribute.type().toInt(value);
31
	}
32
}
33

Decompiled Minecraft 26.3 source of EnvironmentAttributeValue (net/minecraft/world/level/storage/loot/providers/number/ints/EnvironmentAttributeValue.java) with Mojang official mappings, stored verbatim on the site.