| 1 | package net.minecraft.world.level.storage.loot.functions;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableList;
|
| 4 | import com.mojang.serialization.Codec;
|
| 5 | import com.mojang.serialization.MapCodec;
|
| 6 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 7 | import java.util.ArrayList;
|
| 8 | import java.util.Collection;
|
| 9 | import java.util.List;
|
| 10 | import java.util.Optional;
|
| 11 | import java.util.Set;
|
| 12 | import java.util.function.Predicate;
|
| 13 | import net.minecraft.core.Holder;
|
| 14 | import net.minecraft.core.component.DataComponentGetter;
|
| 15 | import net.minecraft.core.component.DataComponentMap;
|
| 16 | import net.minecraft.core.component.DataComponentType;
|
| 17 | import net.minecraft.core.component.TypedDataComponent;
|
| 18 | import net.minecraft.core.registries.BuiltInRegistries;
|
| 19 | import net.minecraft.util.Util;
|
| 20 | import net.minecraft.util.context.ContextKey;
|
| 21 | import net.minecraft.world.entity.Entity;
|
| 22 | import net.minecraft.world.item.ItemStack;
|
| 23 | import net.minecraft.world.level.block.entity.BlockEntity;
|
| 24 | import net.minecraft.world.level.storage.loot.LootContext;
|
| 25 | import net.minecraft.world.level.storage.loot.LootContextArg;
|
| 26 | import net.minecraft.world.level.storage.loot.predicates.LootItemCondition;
|
| 27 |
|
| 28 | public class CopyComponentsFunction extends LootItemConditionalFunction {
|
| 29 | private static final Codec<LootContextArg<DataComponentGetter>> GETTER_CODEC = LootContextArg.createArgCodec(
|
| 30 | builder -> builder.anyEntity(CopyComponentsFunction.DirectSource::new)
|
| 31 | .anyBlockEntity(CopyComponentsFunction.BlockEntitySource::new)
|
| 32 | .anyItemStack(CopyComponentsFunction.DirectSource::new)
|
| 33 | );
|
| 34 | public static final MapCodec<CopyComponentsFunction> MAP_CODEC = RecordCodecBuilder.mapCodec(
|
| 35 | i -> commonFields(i)
|
| 36 | .and(
|
| 37 | i.group(
|
| 38 | GETTER_CODEC.fieldOf("source").forGetter(f -> f.source),
|
| 39 | DataComponentType.CODEC.listOf().optionalFieldOf("include").forGetter(f -> f.include),
|
| 40 | DataComponentType.CODEC.listOf().optionalFieldOf("exclude").forGetter(f -> f.exclude)
|
| 41 | )
|
| 42 | )
|
| 43 | .apply(i, CopyComponentsFunction::new)
|
| 44 | );
|
| 45 | private final LootContextArg<DataComponentGetter> source;
|
| 46 | private final Optional<List<DataComponentType<?>>> include;
|
| 47 | private final Optional<List<DataComponentType<?>>> exclude;
|
| 48 | private final Predicate<DataComponentType<?>> bakedPredicate;
|
| 49 |
|
| 50 | private CopyComponentsFunction(
|
| 51 | final Optional<Holder<LootItemCondition>> condition,
|
| 52 | final LootContextArg<DataComponentGetter> source,
|
| 53 | final Optional<List<DataComponentType<?>>> include,
|
| 54 | final Optional<List<DataComponentType<?>>> exclude
|
| 55 | ) {
|
| 56 | super(condition);
|
| 57 | this.source = source;
|
| 58 | this.include = include.map(List::copyOf);
|
| 59 | this.exclude = exclude.map(List::copyOf);
|
| 60 | List<Predicate<DataComponentType<?>>> componentPredicates = new ArrayList<>(2);
|
| 61 | exclude.ifPresent(s -> componentPredicates.add(e -> !s.contains(e)));
|
| 62 | include.ifPresent(s -> componentPredicates.add(s::contains));
|
| 63 | this.bakedPredicate = Util.allOf(componentPredicates);
|
| 64 | }
|
| 65 |
|
| 66 | @Override
|
| 67 | public MapCodec<CopyComponentsFunction> codec() {
|
| 68 | return MAP_CODEC;
|
| 69 | }
|
| 70 |
|
| 71 | @Override
|
| 72 | public Set<ContextKey<?>> getReferencedContextParams() {
|
| 73 | return Set.of(this.source.contextParam());
|
| 74 | }
|
| 75 |
|
| 76 | @Override
|
| 77 | public ItemStack run(final ItemStack itemStack, final LootContext context) {
|
| 78 | DataComponentGetter data = this.source.get(context);
|
| 79 | if (data != null) {
|
| 80 | if (data instanceof DataComponentMap sourceComponents) {
|
| 81 | itemStack.applyComponents(sourceComponents.filter(this.bakedPredicate));
|
| 82 | } else {
|
| 83 | Collection<DataComponentType<?>> exclude = this.exclude.orElse(List.of());
|
| 84 | this.include.map(Collection::stream).orElse(BuiltInRegistries.DATA_COMPONENT_TYPE.listElements().map(Holder::value)).forEach(componentType -> {
|
| 85 | if (!exclude.contains(componentType)) {
|
| 86 | TypedDataComponent<?> value = data.getTyped(componentType);
|
| 87 | if (value != null) {
|
| 88 | itemStack.set(value);
|
| 89 | }
|
| 90 | }
|
| 91 | });
|
| 92 | }
|
| 93 | }
|
| 94 |
|
| 95 | return itemStack;
|
| 96 | }
|
| 97 |
|
| 98 | public static CopyComponentsFunction.Builder copyComponentsFromEntity(final ContextKey<? extends Entity> source) {
|
| 99 | return new CopyComponentsFunction.Builder(new CopyComponentsFunction.DirectSource<>(source));
|
| 100 | }
|
| 101 |
|
| 102 | public static CopyComponentsFunction.Builder copyComponentsFromBlockEntity(final ContextKey<? extends BlockEntity> source) {
|
| 103 | return new CopyComponentsFunction.Builder(new CopyComponentsFunction.BlockEntitySource(source));
|
| 104 | }
|
| 105 |
|
| 106 | private record BlockEntitySource(ContextKey<? extends BlockEntity> contextParam) implements LootContextArg.Getter<BlockEntity, DataComponentGetter> {
|
| 107 | public DataComponentGetter get(final BlockEntity blockEntity) {
|
| 108 | return blockEntity.collectComponents();
|
| 109 | }
|
| 110 | }
|
| 111 |
|
| 112 | public static class Builder extends LootItemConditionalFunction.Builder<CopyComponentsFunction.Builder> {
|
| 113 | private final LootContextArg<DataComponentGetter> source;
|
| 114 | private Optional<ImmutableList.Builder<DataComponentType<?>>> include = Optional.empty();
|
| 115 | private Optional<ImmutableList.Builder<DataComponentType<?>>> exclude = Optional.empty();
|
| 116 |
|
| 117 | private Builder(final LootContextArg<DataComponentGetter> source) {
|
| 118 | this.source = source;
|
| 119 | }
|
| 120 |
|
| 121 | public CopyComponentsFunction.Builder include(final DataComponentType<?> type) {
|
| 122 | if (this.include.isEmpty()) {
|
| 123 | this.include = Optional.of(ImmutableList.builder());
|
| 124 | }
|
| 125 |
|
| 126 | this.include.get().add(type);
|
| 127 | return this;
|
| 128 | }
|
| 129 |
|
| 130 | public CopyComponentsFunction.Builder exclude(final DataComponentType<?> type) {
|
| 131 | if (this.exclude.isEmpty()) {
|
| 132 | this.exclude = Optional.of(ImmutableList.builder());
|
| 133 | }
|
| 134 |
|
| 135 | this.exclude.get().add(type);
|
| 136 | return this;
|
| 137 | }
|
| 138 |
|
| 139 | protected CopyComponentsFunction.Builder getThis() {
|
| 140 | return this;
|
| 141 | }
|
| 142 |
|
| 143 | @Override
|
| 144 | public LootItemFunction build() {
|
| 145 | return new CopyComponentsFunction(
|
| 146 | this.getCondition(), this.source, this.include.map(ImmutableList.Builder::build), this.exclude.map(ImmutableList.Builder::build)
|
| 147 | );
|
| 148 | }
|
| 149 | }
|
| 150 |
|
| 151 | private record DirectSource<T extends DataComponentGetter>(ContextKey<? extends T> contextParam) implements LootContextArg.Getter<T, DataComponentGetter> {
|
| 152 | public DataComponentGetter get(final T value) {
|
| 153 | return value;
|
| 154 | }
|
| 155 | }
|
| 156 | }
|
| 157 |
|