| 1 | package net.minecraft.world.level.storage.loot.parameters;
|
| 2 |
|
| 3 | import com.google.common.collect.Sets;
|
| 4 | import com.mojang.serialization.Codec;
|
| 5 | import java.util.HashSet;
|
| 6 | import java.util.Set;
|
| 7 | import java.util.function.Consumer;
|
| 8 | import net.minecraft.core.Registry;
|
| 9 | import net.minecraft.core.registries.BuiltInRegistries;
|
| 10 | import net.minecraft.core.registries.Registries;
|
| 11 | import net.minecraft.resources.Identifier;
|
| 12 | import net.minecraft.resources.ResourceKey;
|
| 13 | import net.minecraft.util.context.ContextKey;
|
| 14 | import net.minecraft.util.context.ContextKeySet;
|
| 15 |
|
| 16 | public class LootContextParamSets {
|
| 17 | public static final Codec<ContextKeySet> CODEC = BuiltInRegistries.CONTEXT_KEY_SET.byNameCodec();
|
| 18 | public static final ContextKeySet EMPTY = register("empty", var0 -> {});
|
| 19 | public static final ContextKeySet ALL_PARAMS = register(
|
| 20 | "generic",
|
| 21 | builder -> builder.required(LootContextParams.THIS_ENTITY)
|
| 22 | .required(LootContextParams.LAST_DAMAGE_PLAYER)
|
| 23 | .required(LootContextParams.DAMAGE_SOURCE)
|
| 24 | .required(LootContextParams.ATTACKING_ENTITY)
|
| 25 | .required(LootContextParams.DIRECT_ATTACKING_ENTITY)
|
| 26 | .required(LootContextParams.ORIGIN)
|
| 27 | .required(LootContextParams.BLOCK_STATE)
|
| 28 | .required(LootContextParams.BLOCK_ENTITY)
|
| 29 | .required(LootContextParams.TOOL)
|
| 30 | .required(LootContextParams.EXPLOSION_RADIUS)
|
| 31 | .required(LootContextParams.ADDITIONAL_COST_COMPONENT_ALLOWED)
|
| 32 | .required(LootContextParams.CONTAINER)
|
| 33 | .required(LootContextParams.INTERACTING_ENTITY)
|
| 34 | .required(LootContextParams.TARGET_ENTITY)
|
| 35 | .required(LootContextParams.ENCHANTMENT_ACTIVE)
|
| 36 | .required(LootContextParams.ENCHANTMENT_LEVEL)
|
| 37 | );
|
| 38 | public static final ContextKeySet CHEST = register("chest", builder -> builder.required(LootContextParams.ORIGIN).optional(LootContextParams.THIS_ENTITY));
|
| 39 | public static final ContextKeySet COMMAND = register("command", builder -> builder.required(LootContextParams.ORIGIN).optional(LootContextParams.THIS_ENTITY));
|
| 40 | public static final ContextKeySet COMMAND_SLOT_SOURCE = register(
|
| 41 | "command_slot_source", builder -> builder.required(LootContextParams.ORIGIN).required(LootContextParams.CONTAINER).optional(LootContextParams.THIS_ENTITY)
|
| 42 | );
|
| 43 | public static final ContextKeySet COMMAND_COMPUTE_DEFAULT = register(
|
| 44 | "command_compute_default", builder -> builder.optional(LootContextParams.THIS_ENTITY).required(LootContextParams.ORIGIN)
|
| 45 | );
|
| 46 | public static final ContextKeySet COMMAND_COMPUTE_POSITION = register(
|
| 47 | "command_compute_position",
|
| 48 | builder -> builder.optional(LootContextParams.THIS_ENTITY)
|
| 49 | .required(LootContextParams.ORIGIN)
|
| 50 | .required(LootContextParams.BLOCK_STATE)
|
| 51 | .optional(LootContextParams.BLOCK_ENTITY)
|
| 52 | );
|
| 53 | public static final ContextKeySet COMMAND_COMPUTE_ENTITY = register(
|
| 54 | "command_compute_entity",
|
| 55 | builder -> builder.optional(LootContextParams.THIS_ENTITY).required(LootContextParams.ORIGIN).required(LootContextParams.TARGET_ENTITY)
|
| 56 | );
|
| 57 | public static final ContextKeySet SELECTOR = register(
|
| 58 | "selector", builder -> builder.required(LootContextParams.ORIGIN).required(LootContextParams.THIS_ENTITY)
|
| 59 | );
|
| 60 | public static final ContextKeySet VILLAGER_TRADE = register(
|
| 61 | "villager_trade",
|
| 62 | builder -> builder.required(LootContextParams.ORIGIN).required(LootContextParams.THIS_ENTITY).required(LootContextParams.ADDITIONAL_COST_COMPONENT_ALLOWED)
|
| 63 | );
|
| 64 | public static final ContextKeySet FISHING = register(
|
| 65 | "fishing", builder -> builder.required(LootContextParams.ORIGIN).required(LootContextParams.TOOL).optional(LootContextParams.THIS_ENTITY)
|
| 66 | );
|
| 67 | public static final ContextKeySet ENTITY = register(
|
| 68 | "entity",
|
| 69 | builder -> builder.required(LootContextParams.THIS_ENTITY)
|
| 70 | .required(LootContextParams.ORIGIN)
|
| 71 | .required(LootContextParams.DAMAGE_SOURCE)
|
| 72 | .optional(LootContextParams.ATTACKING_ENTITY)
|
| 73 | .optional(LootContextParams.DIRECT_ATTACKING_ENTITY)
|
| 74 | .optional(LootContextParams.LAST_DAMAGE_PLAYER)
|
| 75 | );
|
| 76 | public static final ContextKeySet EQUIPMENT = register(
|
| 77 | "equipment", builder -> builder.required(LootContextParams.ORIGIN).required(LootContextParams.THIS_ENTITY)
|
| 78 | );
|
| 79 | public static final ContextKeySet ARCHAEOLOGY = register(
|
| 80 | "archaeology", builder -> builder.required(LootContextParams.ORIGIN).required(LootContextParams.THIS_ENTITY).required(LootContextParams.TOOL)
|
| 81 | );
|
| 82 | public static final ContextKeySet GIFT = register("gift", builder -> builder.required(LootContextParams.ORIGIN).required(LootContextParams.THIS_ENTITY));
|
| 83 | public static final ContextKeySet PIGLIN_BARTER = register("barter", builder -> builder.required(LootContextParams.THIS_ENTITY));
|
| 84 | public static final ContextKeySet VAULT = register(
|
| 85 | "vault", builder -> builder.required(LootContextParams.ORIGIN).optional(LootContextParams.THIS_ENTITY).optional(LootContextParams.TOOL)
|
| 86 | );
|
| 87 | public static final ContextKeySet ADVANCEMENT_REWARD = register(
|
| 88 | "advancement_reward", builder -> builder.required(LootContextParams.THIS_ENTITY).required(LootContextParams.ORIGIN)
|
| 89 | );
|
| 90 | public static final ContextKeySet ADVANCEMENT_ENTITY = register(
|
| 91 | "advancement_entity", builder -> builder.required(LootContextParams.THIS_ENTITY).required(LootContextParams.ORIGIN)
|
| 92 | );
|
| 93 | public static final ContextKeySet ADVANCEMENT_LOCATION = register(
|
| 94 | "advancement_location",
|
| 95 | builder -> builder.required(LootContextParams.THIS_ENTITY)
|
| 96 | .required(LootContextParams.ORIGIN)
|
| 97 | .required(LootContextParams.TOOL)
|
| 98 | .required(LootContextParams.BLOCK_STATE)
|
| 99 | .optional(LootContextParams.BLOCK_ENTITY)
|
| 100 | );
|
| 101 | public static final ContextKeySet BLOCK_USE = register(
|
| 102 | "block_use", builder -> builder.required(LootContextParams.THIS_ENTITY).required(LootContextParams.ORIGIN).required(LootContextParams.BLOCK_STATE)
|
| 103 | );
|
| 104 | public static final ContextKeySet BLOCK = register(
|
| 105 | "block",
|
| 106 | builder -> builder.required(LootContextParams.BLOCK_STATE)
|
| 107 | .required(LootContextParams.ORIGIN)
|
| 108 | .required(LootContextParams.TOOL)
|
| 109 | .optional(LootContextParams.THIS_ENTITY)
|
| 110 | .optional(LootContextParams.BLOCK_ENTITY)
|
| 111 | .optional(LootContextParams.EXPLOSION_RADIUS)
|
| 112 | );
|
| 113 | public static final ContextKeySet SHEARING = register(
|
| 114 | "shearing", builder -> builder.required(LootContextParams.ORIGIN).required(LootContextParams.THIS_ENTITY).required(LootContextParams.TOOL)
|
| 115 | );
|
| 116 | public static final ContextKeySet ENTITY_INTERACT = register(
|
| 117 | "entity_interact",
|
| 118 | builder -> builder.required(LootContextParams.TARGET_ENTITY).optional(LootContextParams.INTERACTING_ENTITY).required(LootContextParams.TOOL)
|
| 119 | );
|
| 120 | public static final ContextKeySet BLOCK_INTERACT = register(
|
| 121 | "block_interact",
|
| 122 | builder -> builder.required(LootContextParams.BLOCK_STATE)
|
| 123 | .required(LootContextParams.ORIGIN)
|
| 124 | .optional(LootContextParams.BLOCK_ENTITY)
|
| 125 | .optional(LootContextParams.INTERACTING_ENTITY)
|
| 126 | .optional(LootContextParams.TOOL)
|
| 127 | );
|
| 128 | public static final ContextKeySet CONTAINER_PROCESS = register(
|
| 129 | "container_process",
|
| 130 | builder -> builder.required(LootContextParams.BLOCK_ENTITY)
|
| 131 | .required(LootContextParams.BLOCK_STATE)
|
| 132 | .required(LootContextParams.CONTAINER)
|
| 133 | .required(LootContextParams.ORIGIN)
|
| 134 | );
|
| 135 | public static final ContextKeySet ENCHANTED_DAMAGE = register(
|
| 136 | "enchanted_damage",
|
| 137 | builder -> builder.required(LootContextParams.THIS_ENTITY)
|
| 138 | .required(LootContextParams.ENCHANTMENT_LEVEL)
|
| 139 | .required(LootContextParams.ORIGIN)
|
| 140 | .required(LootContextParams.DAMAGE_SOURCE)
|
| 141 | .optional(LootContextParams.DIRECT_ATTACKING_ENTITY)
|
| 142 | .optional(LootContextParams.ATTACKING_ENTITY)
|
| 143 | );
|
| 144 | public static final ContextKeySet ENCHANTED_ITEM = register(
|
| 145 | "enchanted_item", builder -> builder.required(LootContextParams.TOOL).required(LootContextParams.ENCHANTMENT_LEVEL)
|
| 146 | );
|
| 147 | public static final ContextKeySet ENCHANTED_LOCATION = register(
|
| 148 | "enchanted_location",
|
| 149 | builder -> builder.required(LootContextParams.THIS_ENTITY)
|
| 150 | .required(LootContextParams.ENCHANTMENT_LEVEL)
|
| 151 | .required(LootContextParams.ORIGIN)
|
| 152 | .required(LootContextParams.ENCHANTMENT_ACTIVE)
|
| 153 | );
|
| 154 | public static final ContextKeySet ENCHANTED_ENTITY = register(
|
| 155 | "enchanted_entity",
|
| 156 | builder -> builder.required(LootContextParams.THIS_ENTITY).required(LootContextParams.ENCHANTMENT_LEVEL).required(LootContextParams.ORIGIN)
|
| 157 | );
|
| 158 | public static final ContextKeySet HIT_BLOCK = register(
|
| 159 | "hit_block",
|
| 160 | builder -> builder.required(LootContextParams.THIS_ENTITY)
|
| 161 | .required(LootContextParams.ENCHANTMENT_LEVEL)
|
| 162 | .required(LootContextParams.ORIGIN)
|
| 163 | .required(LootContextParams.BLOCK_STATE)
|
| 164 | );
|
| 165 |
|
| 166 | private static ContextKeySet register(final String name, final Consumer<ContextKeySet.Builder> consumer) {
|
| 167 | ResourceKey<ContextKeySet> key = ResourceKey.create(Registries.CONTEXT_KEY_SET, Identifier.withDefaultNamespace(name));
|
| 168 | return register(consumer, key);
|
| 169 | }
|
| 170 |
|
| 171 | private static ContextKeySet register(final Consumer<ContextKeySet.Builder> consumer, final ResourceKey<ContextKeySet> key) {
|
| 172 | ContextKeySet.Builder builder = new ContextKeySet.Builder();
|
| 173 | consumer.accept(builder);
|
| 174 | return Registry.register(BuiltInRegistries.CONTEXT_KEY_SET, key, builder.build());
|
| 175 | }
|
| 176 |
|
| 177 | public static ContextKeySet bootstrap(final Registry<ContextKeySet> registry) {
|
| 178 | return ALL_PARAMS;
|
| 179 | }
|
| 180 |
|
| 181 | public static void validate() {
|
| 182 | Set<ContextKey<?>> allParams = new HashSet<>();
|
| 183 | BuiltInRegistries.CONTEXT_KEY_SET.forEach(paramSet -> {
|
| 184 | if (paramSet != ALL_PARAMS) {
|
| 185 | allParams.addAll(paramSet.allowed());
|
| 186 | }
|
| 187 | });
|
| 188 | Set<ContextKey<?>> missingFromAllParams = Sets.difference(allParams, ALL_PARAMS.required());
|
| 189 | if (!missingFromAllParams.isEmpty()) {
|
| 190 | throw new IllegalStateException("Missing parameters from 'all_params': " + missingFromAllParams);
|
| 191 | }
|
| 192 | }
|
| 193 | }
|
| 194 |
|