LootContext .java

net.minecraft.world.level.storage.loot.LootContext (class): members: createVisitedEntry, pushVisitedElement, BlockEntityTarget, popVisitedElement, getLuck.

net.minecraft.world.level.storage.lootclass209 linescommon6.5 KB

API Surface (24)

boolean hasParameter(final ContextKey<?> key)
void addDynamicDrops(final Identifier location, final Consumer<ItemStack> output)
boolean hasVisitedElement(final LootContext.VisitedEntry<?> element)
boolean pushVisitedElement(final LootContext.VisitedEntry<?> element)
void popVisitedElement(final LootContext.VisitedEntry<?> element)
HolderGetter.Provider getResolver()
RandomSource getRandom()
float getLuck()
ServerLevel getLevel()
LootContext.VisitedEntry<LootTable> createVisitedEntry(final LootTable table)
LootContext.VisitedEntry<LootItemCondition> createVisitedEntry(final LootItemCondition predicate)
LootContext.VisitedEntry<LootItemFunction> createVisitedEntry(final LootItemFunction modifier)
LootContext.VisitedEntry<SlotSource> createVisitedEntry(final SlotSource slotSource)
ContextKey<? extends BlockEntity> contextParam()
String getSerializedName()
LootContext.Builder withOptionalRandomSeed(final long seed)
LootContext.Builder withOptionalRandomSource(final RandomSource randomSource)
ServerLevel getLevel()
LootContext create(final Optional<Identifier> randomSequenceKey)
ContextKey<? extends Entity> contextParam()
LootContext.EntityTarget getByName(final String name)
String getSerializedName()
ContextKey<? extends ItemInstance> contextParam()
String getSerializedName()

Fields & Constants (5)

params
random
lootDataResolver
visitedElements
StringRepresentable

String literals (9)

"block_entity"
"this"
"attacker"
"direct_attacker"
"attacking_player"
"target_entity"
"interacting_entity"
"Invalid entity target "
"tool"

Full Source

LootContext.java209 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.level.storage.loot;
2
3
import com.google.common.collect.Sets;
4
import java.util.Optional;
5
import java.util.Set;
6
import java.util.function.Consumer;
7
import net.minecraft.core.HolderGetter;
8
import net.minecraft.resources.Identifier;
9
import net.minecraft.server.MinecraftServer;
10
import net.minecraft.server.level.ServerLevel;
11
import net.minecraft.util.RandomSource;
12
import net.minecraft.util.StringRepresentable;
13
import net.minecraft.util.context.ContextKey;
14
import net.minecraft.world.entity.Entity;
15
import net.minecraft.world.item.ItemInstance;
16
import net.minecraft.world.item.ItemStack;
17
import net.minecraft.world.item.slot.SlotSource;
18
import net.minecraft.world.level.block.entity.BlockEntity;
19
import net.minecraft.world.level.storage.loot.functions.LootItemFunction;
20
import net.minecraft.world.level.storage.loot.parameters.LootContextParams;
21
import net.minecraft.world.level.storage.loot.predicates.LootItemCondition;
22
import org.jspecify.annotations.Nullable;
23
24
public class LootContext {
25
	private final LootParams params;
26
	private final RandomSource random;
27
	private final HolderGetter.Provider lootDataResolver;
28
	private final Set<LootContext.VisitedEntry<?>> visitedElements = Sets.newLinkedHashSet();
29
30
	private LootContext(final LootParams params, final RandomSource random, final HolderGetter.Provider lootDataResolver) {
31
		this.params = params;
32
		this.random = random;
33
		this.lootDataResolver = lootDataResolver;
34
	}
35
36
	public boolean hasParameter(final ContextKey<?> key) {
37
		return this.params.contextMap().has(key);
38
	}
39
40
	public <T> @Nullable T getOptional(final ContextKey<T> key) {
41
		return this.params.contextMap().get(key);
42
	}
43
44
	public void addDynamicDrops(final Identifier location, final Consumer<ItemStack> output) {
45
		this.params.addDynamicDrops(location, output);
46
	}
47
48
	public boolean hasVisitedElement(final LootContext.VisitedEntry<?> element) {
49
		return this.visitedElements.contains(element);
50
	}
51
52
	public boolean pushVisitedElement(final LootContext.VisitedEntry<?> element) {
53
		return this.visitedElements.add(element);
54
	}
55
56
	public void popVisitedElement(final LootContext.VisitedEntry<?> element) {
57
		this.visitedElements.remove(element);
58
	}
59
60
	public HolderGetter.Provider getResolver() {
61
		return this.lootDataResolver;
62
	}
63
64
	public RandomSource getRandom() {
65
		return this.random;
66
	}
67
68
	public float getLuck() {
69
		return this.params.getLuck();
70
	}
71
72
	public ServerLevel getLevel() {
73
		return this.params.getLevel();
74
	}
75
76
	public static LootContext.VisitedEntry<LootTable> createVisitedEntry(final LootTable table) {
77
		return new LootContext.VisitedEntry<>(LootDataType.TABLE, table);
78
	}
79
80
	public static LootContext.VisitedEntry<LootItemCondition> createVisitedEntry(final LootItemCondition predicate) {
81
		return new LootContext.VisitedEntry<>(LootDataType.PREDICATE, predicate);
82
	}
83
84
	public static LootContext.VisitedEntry<LootItemFunction> createVisitedEntry(final LootItemFunction modifier) {
85
		return new LootContext.VisitedEntry<>(LootDataType.MODIFIER, modifier);
86
	}
87
88
	public static LootContext.VisitedEntry<SlotSource> createVisitedEntry(final SlotSource slotSource) {
89
		return new LootContext.VisitedEntry<>(LootDataType.SLOT_SOURCE, slotSource);
90
	}
91
92
	public enum BlockEntityTarget implements StringRepresentable, LootContextArg.SimpleGetter<BlockEntity> {
93
		BLOCK_ENTITY("block_entity", LootContextParams.BLOCK_ENTITY);
94
95
		private final String name;
96
		private final ContextKey<? extends BlockEntity> param;
97
98
		BlockEntityTarget(final String name, final ContextKey<? extends BlockEntity> param) {
99
			this.name = name;
100
			this.param = param;
101
		}
102
103
		@Override
104
		public ContextKey<? extends BlockEntity> contextParam() {
105
			return this.param;
106
		}
107
108
		@Override
109
		public String getSerializedName() {
110
			return this.name;
111
		}
112
	}
113
114
	public static class Builder {
115
		private final LootParams params;
116
		private @Nullable RandomSource random;
117
118
		public Builder(final LootParams params) {
119
			this.params = params;
120
		}
121
122
		public LootContext.Builder withOptionalRandomSeed(final long seed) {
123
			if (seed != 0L) {
124
				this.random = RandomSource.create(seed);
125
			}
126
127
			return this;
128
		}
129
130
		public LootContext.Builder withOptionalRandomSource(final RandomSource randomSource) {
131
			this.random = randomSource;
132
			return this;
133
		}
134
135
		public ServerLevel getLevel() {
136
			return this.params.getLevel();
137
		}
138
139
		public LootContext create(final Optional<Identifier> randomSequenceKey) {
140
			ServerLevel level = this.getLevel();
141
			MinecraftServer server = level.getServer();
142
			RandomSource random = Optional.ofNullable(this.random).or(() -> randomSequenceKey.map(server::getRandomSequence)).orElseGet(level::getRandom);
143
			return new LootContext(this.params, random, server.reloadableRegistries().lookup());
144
		}
145
	}
146
147
	public enum EntityTarget implements StringRepresentable, LootContextArg.SimpleGetter<Entity> {
148
		THIS("this", LootContextParams.THIS_ENTITY),
149
		ATTACKER("attacker", LootContextParams.ATTACKING_ENTITY),
150
		DIRECT_ATTACKER("direct_attacker", LootContextParams.DIRECT_ATTACKING_ENTITY),
151
		ATTACKING_PLAYER("attacking_player", LootContextParams.LAST_DAMAGE_PLAYER),
152
		TARGET_ENTITY("target_entity", LootContextParams.TARGET_ENTITY),
153
		INTERACTING_ENTITY("interacting_entity", LootContextParams.INTERACTING_ENTITY);
154
155
		public static final StringRepresentable.EnumCodec<LootContext.EntityTarget> CODEC = StringRepresentable.fromEnum(LootContext.EntityTarget::values);
156
		private final String name;
157
		private final ContextKey<? extends Entity> param;
158
159
		EntityTarget(final String name, final ContextKey<? extends Entity> param) {
160
			this.name = name;
161
			this.param = param;
162
		}
163
164
		@Override
165
		public ContextKey<? extends Entity> contextParam() {
166
			return this.param;
167
		}
168
169
		public static LootContext.EntityTarget getByName(final String name) {
170
			LootContext.EntityTarget target = CODEC.byName(name);
171
			if (target != null) {
172
				return target;
173
			} else {
174
				throw new IllegalArgumentException("Invalid entity target " + name);
175
			}
176
		}
177
178
		@Override
179
		public String getSerializedName() {
180
			return this.name;
181
		}
182
	}
183
184
	public enum ItemStackTarget implements StringRepresentable, LootContextArg.SimpleGetter<ItemInstance> {
185
		TOOL("tool", LootContextParams.TOOL);
186
187
		private final String name;
188
		private final ContextKey<? extends ItemInstance> param;
189
190
		ItemStackTarget(final String name, final ContextKey<? extends ItemInstance> param) {
191
			this.name = name;
192
			this.param = param;
193
		}
194
195
		@Override
196
		public ContextKey<? extends ItemInstance> contextParam() {
197
			return this.param;
198
		}
199
200
		@Override
201
		public String getSerializedName() {
202
			return this.name;
203
		}
204
	}
205
206
	public record VisitedEntry<T extends Validatable>(LootDataType<T> type, T value) {
207
	}
208
}
209

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