| 1 | package net.minecraft.world.level.storage.loot.functions;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableList;
|
| 4 | import com.mojang.datafixers.DataFixUtils;
|
| 5 | import com.mojang.serialization.MapCodec;
|
| 6 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 7 | import java.util.List;
|
| 8 | import java.util.Optional;
|
| 9 | import java.util.Set;
|
| 10 | import java.util.function.UnaryOperator;
|
| 11 | import net.minecraft.core.Holder;
|
| 12 | import net.minecraft.core.component.DataComponents;
|
| 13 | import net.minecraft.network.chat.Component;
|
| 14 | import net.minecraft.network.chat.ComponentSerialization;
|
| 15 | import net.minecraft.util.context.ContextKey;
|
| 16 | import net.minecraft.world.item.ItemStack;
|
| 17 | import net.minecraft.world.item.component.ItemLore;
|
| 18 | import net.minecraft.world.level.storage.loot.LootContext;
|
| 19 | import net.minecraft.world.level.storage.loot.predicates.LootItemCondition;
|
| 20 | import org.jspecify.annotations.Nullable;
|
| 21 |
|
| 22 | public class SetLoreFunction extends LootItemConditionalFunction {
|
| 23 | public static final MapCodec<SetLoreFunction> MAP_CODEC = RecordCodecBuilder.mapCodec(
|
| 24 | i -> commonFields(i)
|
| 25 | .and(
|
| 26 | i.group(
|
| 27 | ComponentSerialization.CODEC.sizeLimitedListOf(256).fieldOf("lore").forGetter(f -> f.lore),
|
| 28 | ListOperation.codec(256).forGetter(f -> f.mode),
|
| 29 | LootContext.EntityTarget.CODEC.optionalFieldOf("entity").forGetter(f -> f.resolutionContext)
|
| 30 | )
|
| 31 | )
|
| 32 | .apply(i, SetLoreFunction::new)
|
| 33 | );
|
| 34 | private final List<Component> lore;
|
| 35 | private final ListOperation mode;
|
| 36 | private final Optional<LootContext.EntityTarget> resolutionContext;
|
| 37 |
|
| 38 | public SetLoreFunction(
|
| 39 | final Optional<Holder<LootItemCondition>> condition,
|
| 40 | final List<Component> lore,
|
| 41 | final ListOperation mode,
|
| 42 | final Optional<LootContext.EntityTarget> resolutionContext
|
| 43 | ) {
|
| 44 | super(condition);
|
| 45 | this.lore = List.copyOf(lore);
|
| 46 | this.mode = mode;
|
| 47 | this.resolutionContext = resolutionContext;
|
| 48 | }
|
| 49 |
|
| 50 | @Override
|
| 51 | public MapCodec<SetLoreFunction> codec() {
|
| 52 | return MAP_CODEC;
|
| 53 | }
|
| 54 |
|
| 55 | @Override
|
| 56 | public Set<ContextKey<?>> getReferencedContextParams() {
|
| 57 | return DataFixUtils.orElse(this.resolutionContext.map(target -> Set.of(target.contextParam())), Set.of());
|
| 58 | }
|
| 59 |
|
| 60 | @Override
|
| 61 | public ItemStack run(final ItemStack itemStack, final LootContext context) {
|
| 62 | itemStack.update(DataComponents.LORE, ItemLore.EMPTY, oldLore -> new ItemLore(this.updateLore(oldLore, context)));
|
| 63 | return itemStack;
|
| 64 | }
|
| 65 |
|
| 66 | private List<Component> updateLore(final @Nullable ItemLore itemLore, final LootContext context) {
|
| 67 | if (itemLore == null && this.lore.isEmpty()) {
|
| 68 | return List.of();
|
| 69 | }
|
| 70 |
|
| 71 | UnaryOperator<Component> resolver = SetNameFunction.createResolver(context, this.resolutionContext.orElse(null));
|
| 72 | List<Component> resolvedLines = this.lore.stream().map(resolver).toList();
|
| 73 | return this.mode.apply(itemLore.lines(), resolvedLines, 256);
|
| 74 | }
|
| 75 |
|
| 76 | public static SetLoreFunction.Builder setLore() {
|
| 77 | return new SetLoreFunction.Builder();
|
| 78 | }
|
| 79 |
|
| 80 | public static class Builder extends LootItemConditionalFunction.Builder<SetLoreFunction.Builder> {
|
| 81 | private Optional<LootContext.EntityTarget> resolutionContext = Optional.empty();
|
| 82 | private final ImmutableList.Builder<Component> lore = ImmutableList.builder();
|
| 83 | private ListOperation mode = ListOperation.Append.INSTANCE;
|
| 84 |
|
| 85 | public SetLoreFunction.Builder setMode(final ListOperation mode) {
|
| 86 | this.mode = mode;
|
| 87 | return this;
|
| 88 | }
|
| 89 |
|
| 90 | public SetLoreFunction.Builder setResolutionContext(final LootContext.EntityTarget resolutionContext) {
|
| 91 | this.resolutionContext = Optional.of(resolutionContext);
|
| 92 | return this;
|
| 93 | }
|
| 94 |
|
| 95 | public SetLoreFunction.Builder addLine(final Component line) {
|
| 96 | this.lore.add(line);
|
| 97 | return this;
|
| 98 | }
|
| 99 |
|
| 100 | protected SetLoreFunction.Builder getThis() {
|
| 101 | return this;
|
| 102 | }
|
| 103 |
|
| 104 | @Override
|
| 105 | public LootItemFunction build() {
|
| 106 | return new SetLoreFunction(this.getCondition(), this.lore.build(), this.mode, this.resolutionContext);
|
| 107 | }
|
| 108 | }
|
| 109 | }
|
| 110 |
|