| 1 | package net.minecraft.world.level.storage.loot;
|
| 2 |
|
| 3 | import com.google.common.collect.Maps;
|
| 4 | import java.util.Map;
|
| 5 | import java.util.NoSuchElementException;
|
| 6 | import java.util.function.Consumer;
|
| 7 | import net.minecraft.resources.Identifier;
|
| 8 | import net.minecraft.server.level.ServerLevel;
|
| 9 | import net.minecraft.util.context.ContextKey;
|
| 10 | import net.minecraft.util.context.ContextKeySet;
|
| 11 | import net.minecraft.util.context.ContextMap;
|
| 12 | import net.minecraft.world.item.ItemStack;
|
| 13 | import org.jspecify.annotations.Nullable;
|
| 14 |
|
| 15 | public class LootParams {
|
| 16 | private final ServerLevel level;
|
| 17 | private final ContextMap params;
|
| 18 | private final Map<Identifier, LootParams.DynamicDrop> dynamicDrops;
|
| 19 | private final float luck;
|
| 20 |
|
| 21 | public LootParams(final ServerLevel level, final ContextMap params, final Map<Identifier, LootParams.DynamicDrop> dynamicDrops, final float luck) {
|
| 22 | this.level = level;
|
| 23 | this.params = params;
|
| 24 | this.dynamicDrops = dynamicDrops;
|
| 25 | this.luck = luck;
|
| 26 | }
|
| 27 |
|
| 28 | public ServerLevel getLevel() {
|
| 29 | return this.level;
|
| 30 | }
|
| 31 |
|
| 32 | public ContextMap contextMap() {
|
| 33 | return this.params;
|
| 34 | }
|
| 35 |
|
| 36 | public void addDynamicDrops(final Identifier location, final Consumer<ItemStack> output) {
|
| 37 | LootParams.DynamicDrop dynamicDrop = this.dynamicDrops.get(location);
|
| 38 | if (dynamicDrop != null) {
|
| 39 | dynamicDrop.add(output);
|
| 40 | }
|
| 41 | }
|
| 42 |
|
| 43 | public float getLuck() {
|
| 44 | return this.luck;
|
| 45 | }
|
| 46 |
|
| 47 | public static class Builder {
|
| 48 | private final ServerLevel level;
|
| 49 | private final ContextMap.Builder params = ContextMap.builder();
|
| 50 | private final Map<Identifier, LootParams.DynamicDrop> dynamicDrops = Maps.newHashMap();
|
| 51 | private float luck;
|
| 52 |
|
| 53 | public Builder(final ServerLevel level) {
|
| 54 | this.level = level;
|
| 55 | }
|
| 56 |
|
| 57 | public ServerLevel getLevel() {
|
| 58 | return this.level;
|
| 59 | }
|
| 60 |
|
| 61 | public <T> LootParams.Builder withParameter(final ContextKey<T> param, final T value) {
|
| 62 | this.params.set(param, value);
|
| 63 | return this;
|
| 64 | }
|
| 65 |
|
| 66 | public <T> LootParams.Builder withOptionalParameter(final ContextKey<T> param, final @Nullable T value) {
|
| 67 | this.params.set(param, value);
|
| 68 | return this;
|
| 69 | }
|
| 70 |
|
| 71 | public <T> T getParameter(final ContextKey<T> param) {
|
| 72 | T value = this.params.get(param);
|
| 73 | if (value == null) {
|
| 74 | throw new NoSuchElementException(param.name().toString());
|
| 75 | } else {
|
| 76 | return value;
|
| 77 | }
|
| 78 | }
|
| 79 |
|
| 80 | public <T> @Nullable T getOptionalParameter(final ContextKey<T> param) {
|
| 81 | return this.params.get(param);
|
| 82 | }
|
| 83 |
|
| 84 | public LootParams.Builder withDynamicDrop(final Identifier location, final LootParams.DynamicDrop dynamicDrop) {
|
| 85 | LootParams.DynamicDrop prev = this.dynamicDrops.put(location, dynamicDrop);
|
| 86 | if (prev != null) {
|
| 87 | throw new IllegalStateException("Duplicated dynamic drop '" + this.dynamicDrops + "'");
|
| 88 | } else {
|
| 89 | return this;
|
| 90 | }
|
| 91 | }
|
| 92 |
|
| 93 | public LootParams.Builder withLuck(final float luck) {
|
| 94 | this.luck = luck;
|
| 95 | return this;
|
| 96 | }
|
| 97 |
|
| 98 | public LootParams create(final ContextKeySet contextKeySet) {
|
| 99 | ContextMap keySet = this.params.buildAndValidate(contextKeySet);
|
| 100 | return new LootParams(this.level, keySet, this.dynamicDrops, this.luck);
|
| 101 | }
|
| 102 | }
|
| 103 |
|
| 104 | @FunctionalInterface
|
| 105 | public interface DynamicDrop {
|
| 106 | void add(Consumer<ItemStack> output);
|
| 107 | }
|
| 108 | }
|
| 109 |
|