| 1 | package net.minecraft.data.loot;
|
| 2 |
|
| 3 | import com.google.common.collect.Maps;
|
| 4 | import java.util.HashMap;
|
| 5 | import java.util.HashSet;
|
| 6 | import java.util.List;
|
| 7 | import java.util.Locale;
|
| 8 | import java.util.Map;
|
| 9 | import java.util.Optional;
|
| 10 | import java.util.Set;
|
| 11 | import java.util.stream.Collectors;
|
| 12 | import net.fabricmc.fabric.api.datagen.v1.loot.FabricEntityLootSubProvider;
|
| 13 | import net.minecraft.advancements.predicates.DamageSourcePredicate;
|
| 14 | import net.minecraft.advancements.predicates.DataComponentMatchers;
|
| 15 | import net.minecraft.advancements.predicates.EnchantmentPredicate;
|
| 16 | import net.minecraft.advancements.predicates.ItemPredicate;
|
| 17 | import net.minecraft.advancements.predicates.MinMaxBounds;
|
| 18 | import net.minecraft.advancements.predicates.TagPredicate;
|
| 19 | import net.minecraft.advancements.predicates.entity.EntityEquipmentPredicate;
|
| 20 | import net.minecraft.advancements.predicates.entity.EntityFlagsPredicate;
|
| 21 | import net.minecraft.advancements.predicates.entity.EntityPredicate;
|
| 22 | import net.minecraft.advancements.predicates.entity.SheepPredicate;
|
| 23 | import net.minecraft.core.Holder;
|
| 24 | import net.minecraft.core.HolderGetter;
|
| 25 | import net.minecraft.core.component.DataComponentExactPredicate;
|
| 26 | import net.minecraft.core.component.DataComponents;
|
| 27 | import net.minecraft.core.component.predicates.DataComponentPredicates;
|
| 28 | import net.minecraft.core.component.predicates.EnchantmentsPredicate;
|
| 29 | import net.minecraft.core.registries.Registries;
|
| 30 | import net.minecraft.resources.ResourceKey;
|
| 31 | import net.minecraft.tags.DamageTypeTags;
|
| 32 | import net.minecraft.tags.EnchantmentTags;
|
| 33 | import net.minecraft.world.damagesource.DamageType;
|
| 34 | import net.minecraft.world.entity.EntityType;
|
| 35 | import net.minecraft.world.entity.EntityTypes;
|
| 36 | import net.minecraft.world.entity.animal.frog.FrogVariant;
|
| 37 | import net.minecraft.world.flag.FeatureFlagSet;
|
| 38 | import net.minecraft.world.item.DyeColor;
|
| 39 | import net.minecraft.world.item.Item;
|
| 40 | import net.minecraft.world.item.enchantment.Enchantment;
|
| 41 | import net.minecraft.world.level.block.ColorCollection;
|
| 42 | import net.minecraft.world.level.storage.loot.LootContext;
|
| 43 | import net.minecraft.world.level.storage.loot.LootPool;
|
| 44 | import net.minecraft.world.level.storage.loot.LootTable;
|
| 45 | import net.minecraft.world.level.storage.loot.entries.AlternativesEntry;
|
| 46 | import net.minecraft.world.level.storage.loot.entries.NestedLootTable;
|
| 47 | import net.minecraft.world.level.storage.loot.predicates.AnyOfCondition;
|
| 48 | import net.minecraft.world.level.storage.loot.predicates.DamageSourceCondition;
|
| 49 | import net.minecraft.world.level.storage.loot.predicates.LootItemCondition;
|
| 50 | import net.minecraft.world.level.storage.loot.predicates.LootItemEntityPropertyCondition;
|
| 51 |
|
| 52 | /**
|
| 53 | * Access widened by fabric-data-generation-api-v1 to accessible
|
| 54 | */
|
| 55 | public abstract class EntityLootSubProvider implements LootTableSubProvider, FabricEntityLootSubProvider {
|
| 56 | protected final LootTableSubProvider.Context output;
|
| 57 | protected final HolderGetter<Item> items;
|
| 58 | protected final HolderGetter<Enchantment> enchantments;
|
| 59 | protected final HolderGetter<EntityType<?>> entityTypes;
|
| 60 | protected final HolderGetter<FrogVariant> frogVariants;
|
| 61 | protected final HolderGetter<DamageType> damageTypes;
|
| 62 | protected final HolderGetter<LootTable> lootTables;
|
| 63 | private final FeatureFlagSet allowed;
|
| 64 | private final FeatureFlagSet required;
|
| 65 | private final Map<EntityType<?>, Map<ResourceKey<LootTable>, LootTable.Builder>> map = Maps.newHashMap();
|
| 66 |
|
| 67 | protected EntityLootSubProvider(final FeatureFlagSet enabledFeatures, final LootTableSubProvider.Context output) {
|
| 68 | this(enabledFeatures, enabledFeatures, output);
|
| 69 | }
|
| 70 |
|
| 71 | protected EntityLootSubProvider(final FeatureFlagSet allowed, final FeatureFlagSet required, final LootTableSubProvider.Context output) {
|
| 72 | this.allowed = allowed;
|
| 73 | this.required = required;
|
| 74 | this.output = output;
|
| 75 | this.items = output.lookup(Registries.ITEM);
|
| 76 | this.enchantments = output.lookup(Registries.ENCHANTMENT);
|
| 77 | this.entityTypes = output.lookup(Registries.ENTITY_TYPE);
|
| 78 | this.frogVariants = output.lookup(Registries.FROG_VARIANT);
|
| 79 | this.damageTypes = output.lookup(Registries.DAMAGE_TYPE);
|
| 80 | this.lootTables = output.lookup(Registries.LOOT_TABLE);
|
| 81 | }
|
| 82 |
|
| 83 | protected DamageSourcePredicate.Builder projectileDamage() {
|
| 84 | return DamageSourcePredicate.Builder.damageType().tag(TagPredicate.is(this.damageTypes, DamageTypeTags.IS_PROJECTILE));
|
| 85 | }
|
| 86 |
|
| 87 | protected final AnyOfCondition.Builder shouldSmeltLoot() {
|
| 88 | return AnyOfCondition.anyOf(
|
| 89 | LootItemEntityPropertyCondition.hasProperties(
|
| 90 | LootContext.EntityTarget.THIS, EntityPredicate.Builder.entity().flags(EntityFlagsPredicate.Builder.flags().setOnFire(true))
|
| 91 | ),
|
| 92 | LootItemEntityPropertyCondition.hasProperties(
|
| 93 | LootContext.EntityTarget.DIRECT_ATTACKER,
|
| 94 | EntityPredicate.Builder.entity()
|
| 95 | .equipment(
|
| 96 | EntityEquipmentPredicate.Builder.equipment()
|
| 97 | .mainhand(
|
| 98 | ItemPredicate.Builder.item()
|
| 99 | .withComponents(
|
| 100 | DataComponentMatchers.Builder.components()
|
| 101 | .partial(
|
| 102 | DataComponentPredicates.ENCHANTMENTS,
|
| 103 | EnchantmentsPredicate.enchantments(
|
| 104 | List.of(new EnchantmentPredicate(this.enchantments.getOrThrow(EnchantmentTags.SMELTS_LOOT), MinMaxBounds.Ints.ANY))
|
| 105 | )
|
| 106 | )
|
| 107 | .build()
|
| 108 | )
|
| 109 | )
|
| 110 | )
|
| 111 | )
|
| 112 | );
|
| 113 | }
|
| 114 |
|
| 115 | public static LootPool.Builder createSheepDispatchPool(final ColorCollection<Holder<LootTable>> tableNames) {
|
| 116 | AlternativesEntry.Builder variants = AlternativesEntry.alternatives();
|
| 117 |
|
| 118 | for (DyeColor color : DyeColor.VALUES) {
|
| 119 | variants = variants.otherwise(
|
| 120 | NestedLootTable.lootTableReference(tableNames.pick(color))
|
| 121 | .when(
|
| 122 | LootItemEntityPropertyCondition.hasProperties(
|
| 123 | LootContext.EntityTarget.THIS,
|
| 124 | EntityPredicate.Builder.entity().components(DataComponentExactPredicate.expect(DataComponents.SHEEP_COLOR, color)).sheep(SheepPredicate.hasWool())
|
| 125 | )
|
| 126 | )
|
| 127 | );
|
| 128 | }
|
| 129 |
|
| 130 | return LootPool.lootPool().add(variants);
|
| 131 | }
|
| 132 |
|
| 133 | public abstract void generate();
|
| 134 |
|
| 135 | @Override
|
| 136 | public void run() {
|
| 137 | this.generate();
|
| 138 | Set<ResourceKey<LootTable>> seen = new HashSet<>();
|
| 139 | this.output
|
| 140 | .listContextElements(Registries.ENTITY_TYPE)
|
| 141 | .forEach(
|
| 142 | holder -> {
|
| 143 | EntityType<?> type = holder.value();
|
| 144 | if (type.isEnabled(this.allowed)) {
|
| 145 | Optional<ResourceKey<LootTable>> defaultLootTable = type.getDefaultLootTable();
|
| 146 | if (defaultLootTable.isPresent()) {
|
| 147 | Map<ResourceKey<LootTable>, LootTable.Builder> builders = this.map.remove(type);
|
| 148 | if (type.isEnabled(this.required) && (builders == null || !builders.containsKey(defaultLootTable.get()))) {
|
| 149 | throw new IllegalStateException(String.format(Locale.ROOT, "Missing loottable '%s' for '%s'", defaultLootTable.get(), holder.key().identifier()));
|
| 150 | }
|
| 151 |
|
| 152 | if (builders != null) {
|
| 153 | builders.forEach((id, builder) -> {
|
| 154 | if (!seen.add((ResourceKey<LootTable>)id)) {
|
| 155 | throw new IllegalStateException(String.format(Locale.ROOT, "Duplicate loottable '%s' for '%s'", id, holder.key().identifier()));
|
| 156 | }
|
| 157 |
|
| 158 | this.output.accept((ResourceKey<LootTable>)id, builder);
|
| 159 | });
|
| 160 | }
|
| 161 | } else {
|
| 162 | Map<ResourceKey<LootTable>, LootTable.Builder> builders = this.map.remove(type);
|
| 163 | if (builders != null) {
|
| 164 | throw new IllegalStateException(
|
| 165 | String.format(
|
| 166 | Locale.ROOT,
|
| 167 | "Weird loottables '%s' for '%s', not a LivingEntity so should not have loot",
|
| 168 | builders.keySet().stream().map(r -> r.identifier().toString()).collect(Collectors.joining(",")),
|
| 169 | holder.key().identifier()
|
| 170 | )
|
| 171 | );
|
| 172 | }
|
| 173 | }
|
| 174 | }
|
| 175 | }
|
| 176 | );
|
| 177 | if (!this.map.isEmpty()) {
|
| 178 | throw new IllegalStateException("Created loot tables for entities not supported by datapack: " + this.map.keySet());
|
| 179 | }
|
| 180 | }
|
| 181 |
|
| 182 | protected LootItemCondition.Builder killedByFrog() {
|
| 183 | return DamageSourceCondition.hasDamageSource(
|
| 184 | DamageSourcePredicate.Builder.damageType().source(EntityPredicate.Builder.entity().of(this.entityTypes, EntityTypes.FROG))
|
| 185 | );
|
| 186 | }
|
| 187 |
|
| 188 | protected LootItemCondition.Builder killedByFrogVariant(final ResourceKey<FrogVariant> variant) {
|
| 189 | return DamageSourceCondition.hasDamageSource(
|
| 190 | DamageSourcePredicate.Builder.damageType()
|
| 191 | .source(
|
| 192 | EntityPredicate.Builder.entity()
|
| 193 | .of(this.entityTypes, EntityTypes.FROG)
|
| 194 | .components(DataComponentExactPredicate.expect(DataComponents.FROG_VARIANT, this.frogVariants.getOrThrow(variant)))
|
| 195 | )
|
| 196 | );
|
| 197 | }
|
| 198 |
|
| 199 | /**
|
| 200 | * Access widened by fabric-data-generation-api-v1 to accessible
|
| 201 | */
|
| 202 | public void add(final EntityType<?> type, final LootTable.Builder builder) {
|
| 203 | this.add(type, type.getDefaultLootTable().orElseThrow(() -> new IllegalStateException("Entity " + type + " has no loot table")), builder);
|
| 204 | }
|
| 205 |
|
| 206 | /**
|
| 207 | * Access widened by fabric-data-generation-api-v1 to accessible
|
| 208 | */
|
| 209 | public void add(final EntityType<?> type, final ResourceKey<LootTable> lootTable, final LootTable.Builder builder) {
|
| 210 | this.map.computeIfAbsent(type, k -> new HashMap<>()).put(lootTable, builder);
|
| 211 | }
|
| 212 | }
|
| 213 |
|