| 1 | package net.minecraft.world.level.storage.loot.functions;
|
| 2 |
|
| 3 | import com.mojang.serialization.Codec;
|
| 4 | import com.mojang.serialization.MapCodec;
|
| 5 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 6 | import java.util.Optional;
|
| 7 | import net.minecraft.core.Holder;
|
| 8 | import net.minecraft.core.component.DataComponents;
|
| 9 | import net.minecraft.world.item.DyeColor;
|
| 10 | import net.minecraft.world.item.ItemStack;
|
| 11 | import net.minecraft.world.level.block.entity.BannerPattern;
|
| 12 | import net.minecraft.world.level.block.entity.BannerPatternLayers;
|
| 13 | import net.minecraft.world.level.storage.loot.LootContext;
|
| 14 | import net.minecraft.world.level.storage.loot.predicates.LootItemCondition;
|
| 15 |
|
| 16 | public class SetBannerPatternFunction extends LootItemConditionalFunction {
|
| 17 | public static final MapCodec<SetBannerPatternFunction> MAP_CODEC = RecordCodecBuilder.mapCodec(
|
| 18 | i -> commonFields(i)
|
| 19 | .and(i.group(BannerPatternLayers.CODEC.fieldOf("patterns").forGetter(f -> f.patterns), Codec.BOOL.fieldOf("append").forGetter(f -> f.append)))
|
| 20 | .apply(i, SetBannerPatternFunction::new)
|
| 21 | );
|
| 22 | private final BannerPatternLayers patterns;
|
| 23 | private final boolean append;
|
| 24 |
|
| 25 | private SetBannerPatternFunction(final Optional<Holder<LootItemCondition>> condition, final BannerPatternLayers patterns, final boolean append) {
|
| 26 | super(condition);
|
| 27 | this.patterns = patterns;
|
| 28 | this.append = append;
|
| 29 | }
|
| 30 |
|
| 31 | @Override
|
| 32 | protected ItemStack run(final ItemStack itemStack, final LootContext context) {
|
| 33 | if (this.append) {
|
| 34 | itemStack.update(
|
| 35 | DataComponents.BANNER_PATTERNS,
|
| 36 | BannerPatternLayers.EMPTY,
|
| 37 | this.patterns,
|
| 38 | (base, appended) -> new BannerPatternLayers.Builder().addAll(base).addAll(appended).build()
|
| 39 | );
|
| 40 | } else {
|
| 41 | itemStack.set(DataComponents.BANNER_PATTERNS, this.patterns);
|
| 42 | }
|
| 43 |
|
| 44 | return itemStack;
|
| 45 | }
|
| 46 |
|
| 47 | @Override
|
| 48 | public MapCodec<SetBannerPatternFunction> codec() {
|
| 49 | return MAP_CODEC;
|
| 50 | }
|
| 51 |
|
| 52 | public static SetBannerPatternFunction.Builder setBannerPattern(final boolean append) {
|
| 53 | return new SetBannerPatternFunction.Builder(append);
|
| 54 | }
|
| 55 |
|
| 56 | public static class Builder extends LootItemConditionalFunction.Builder<SetBannerPatternFunction.Builder> {
|
| 57 | private final BannerPatternLayers.Builder patterns = new BannerPatternLayers.Builder();
|
| 58 | private final boolean append;
|
| 59 |
|
| 60 | private Builder(final boolean append) {
|
| 61 | this.append = append;
|
| 62 | }
|
| 63 |
|
| 64 | protected SetBannerPatternFunction.Builder getThis() {
|
| 65 | return this;
|
| 66 | }
|
| 67 |
|
| 68 | @Override
|
| 69 | public LootItemFunction build() {
|
| 70 | return new SetBannerPatternFunction(this.getCondition(), this.patterns.build(), this.append);
|
| 71 | }
|
| 72 |
|
| 73 | public SetBannerPatternFunction.Builder addPattern(final Holder<BannerPattern> pattern, final DyeColor color) {
|
| 74 | this.patterns.add(pattern, color);
|
| 75 | return this;
|
| 76 | }
|
| 77 | }
|
| 78 | }
|
| 79 |
|