| 1 | package net.minecraft.world.level.storage.loot.functions;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableSet;
|
| 4 | import com.mojang.serialization.Codec;
|
| 5 | import com.mojang.serialization.MapCodec;
|
| 6 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 7 | import java.util.List;
|
| 8 | import java.util.Objects;
|
| 9 | import java.util.Optional;
|
| 10 | import java.util.Set;
|
| 11 | import java.util.stream.Collectors;
|
| 12 | import net.minecraft.core.Holder;
|
| 13 | import net.minecraft.core.component.DataComponents;
|
| 14 | import net.minecraft.core.registries.BuiltInRegistries;
|
| 15 | import net.minecraft.util.context.ContextKey;
|
| 16 | import net.minecraft.world.item.ItemStack;
|
| 17 | import net.minecraft.world.item.component.BlockItemStateProperties;
|
| 18 | import net.minecraft.world.level.block.Block;
|
| 19 | import net.minecraft.world.level.block.state.BlockState;
|
| 20 | import net.minecraft.world.level.block.state.properties.Property;
|
| 21 | import net.minecraft.world.level.storage.loot.LootContext;
|
| 22 | import net.minecraft.world.level.storage.loot.parameters.LootContextParams;
|
| 23 | import net.minecraft.world.level.storage.loot.predicates.LootItemCondition;
|
| 24 |
|
| 25 | public class CopyBlockState extends LootItemConditionalFunction {
|
| 26 | public static final MapCodec<CopyBlockState> MAP_CODEC = RecordCodecBuilder.mapCodec(
|
| 27 | i -> commonFields(i)
|
| 28 | .and(
|
| 29 | i.group(
|
| 30 | BuiltInRegistries.BLOCK.holderByNameCodec().fieldOf("block").forGetter(f -> f.block),
|
| 31 | Codec.STRING.listOf().fieldOf("properties").forGetter(f -> f.properties.stream().map(Property::getName).toList())
|
| 32 | )
|
| 33 | )
|
| 34 | .apply(i, CopyBlockState::new)
|
| 35 | );
|
| 36 | private final Holder<Block> block;
|
| 37 | private final Set<Property<?>> properties;
|
| 38 |
|
| 39 | private CopyBlockState(final Optional<Holder<LootItemCondition>> condition, final Holder<Block> block, final Set<Property<?>> properties) {
|
| 40 | super(condition);
|
| 41 | this.block = block;
|
| 42 | this.properties = properties;
|
| 43 | }
|
| 44 |
|
| 45 | private CopyBlockState(final Optional<Holder<LootItemCondition>> condition, final Holder<Block> block, final List<String> propertyNames) {
|
| 46 | this(condition, block, propertyNames.stream().map(block.value().getStateDefinition()::getProperty).filter(Objects::nonNull).collect(Collectors.toSet()));
|
| 47 | }
|
| 48 |
|
| 49 | @Override
|
| 50 | public MapCodec<CopyBlockState> codec() {
|
| 51 | return MAP_CODEC;
|
| 52 | }
|
| 53 |
|
| 54 | @Override
|
| 55 | public Set<ContextKey<?>> getReferencedContextParams() {
|
| 56 | return Set.of(LootContextParams.BLOCK_STATE);
|
| 57 | }
|
| 58 |
|
| 59 | @Override
|
| 60 | protected ItemStack run(final ItemStack itemStack, final LootContext context) {
|
| 61 | BlockState state = context.getOptional(LootContextParams.BLOCK_STATE);
|
| 62 | if (state != null) {
|
| 63 | itemStack.update(DataComponents.BLOCK_STATE, BlockItemStateProperties.EMPTY, itemState -> {
|
| 64 | for (Property<?> property : this.properties) {
|
| 65 | if (state.hasProperty(property)) {
|
| 66 | itemState = itemState.with(property, state);
|
| 67 | }
|
| 68 | }
|
| 69 |
|
| 70 | return itemState;
|
| 71 | });
|
| 72 | }
|
| 73 |
|
| 74 | return itemStack;
|
| 75 | }
|
| 76 |
|
| 77 | public static CopyBlockState.Builder copyState(final Block block) {
|
| 78 | return new CopyBlockState.Builder(block);
|
| 79 | }
|
| 80 |
|
| 81 | public static class Builder extends LootItemConditionalFunction.Builder<CopyBlockState.Builder> {
|
| 82 | private final Holder<Block> block;
|
| 83 | private final ImmutableSet.Builder<Property<?>> properties = ImmutableSet.builder();
|
| 84 |
|
| 85 | private Builder(final Block block) {
|
| 86 | this.block = block.builtInRegistryHolder();
|
| 87 | }
|
| 88 |
|
| 89 | public CopyBlockState.Builder copy(final Property<?> property) {
|
| 90 | if (!this.block.value().getStateDefinition().getProperties().contains(property)) {
|
| 91 | throw new IllegalStateException("Property " + property + " is not present on block " + this.block);
|
| 92 | }
|
| 93 |
|
| 94 | this.properties.add(property);
|
| 95 | return this;
|
| 96 | }
|
| 97 |
|
| 98 | protected CopyBlockState.Builder getThis() {
|
| 99 | return this;
|
| 100 | }
|
| 101 |
|
| 102 | @Override
|
| 103 | public LootItemFunction build() {
|
| 104 | return new CopyBlockState(this.getCondition(), this.block, this.properties.build());
|
| 105 | }
|
| 106 | }
|
| 107 | }
|
| 108 |
|