package net.minecraft.commands.arguments.blocks;

import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.level.block.entity.BlockEntity;
import java.util.Iterator;
import net.minecraft.nbt.Tag;
import net.minecraft.nbt.NbtUtils;
import javax.annotation.Nullable;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.level.block.state.properties.Property;
import java.util.Set;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.pattern.BlockInWorld;
import java.util.function.Predicate;

public class BlockInput implements Predicate<BlockInWorld> {
    private final BlockState state;
    private final Set<Property<?>> properties;
    @Nullable
    private final CompoundTag tag;
    
    public BlockInput(final BlockState byj, final Set<Property<?>> set, @Nullable final CompoundTag jt) {
        this.state = byj;
        this.properties = set;
        this.tag = jt;
    }
    
    public BlockState getState() {
        return this.state;
    }
    
    @Override
    public boolean test(final BlockInWorld byn) {
        final BlockState byj3 = byn.getState();
        if (byj3.getBlock() != this.state.getBlock()) {
            return false;
        }
        for (final Property<?> bzm5 : this.properties) {
            if (byj3.getValue(bzm5) != this.state.getValue(bzm5)) {
                return false;
            }
        }
        if (this.tag != null) {
            final BlockEntity bwl4 = byn.getEntity();
            return bwl4 != null && NbtUtils.compareNbt(this.tag, bwl4.save(new CompoundTag()), true);
        }
        return true;
    }
    
    public boolean place(final ServerLevel xd, final BlockPos fk, final int integer) {
        if (!xd.setBlock(fk, this.state, integer)) {
            return false;
        }
        if (this.tag != null) {
            final BlockEntity bwl5 = xd.getBlockEntity(fk);
            if (bwl5 != null) {
                final CompoundTag jt6 = this.tag.copy();
                jt6.putInt("x", fk.getX());
                jt6.putInt("y", fk.getY());
                jt6.putInt("z", fk.getZ());
                bwl5.load(jt6);
            }
        }
        return true;
    }
}
