package net.minecraft.world.level.block.state.predicate; import com.google.common.collect.Maps; import java.util.Map; import java.util.Map.Entry; import java.util.function.Predicate; import net.minecraft.world.level.block.Block; import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.block.state.StateDefinition; import net.minecraft.world.level.block.state.properties.Property; import org.jetbrains.annotations.Nullable; public class BlockStatePredicate implements Predicate { public static final Predicate ANY = blockState -> true; private final StateDefinition definition; private final Map, Predicate> properties = Maps., Predicate>newHashMap(); private BlockStatePredicate(StateDefinition definition) { this.definition = definition; } public static BlockStatePredicate forBlock(Block block) { return new BlockStatePredicate(block.getStateDefinition()); } public boolean test(@Nullable BlockState state) { if (state != null && state.getBlock().equals(this.definition.getOwner())) { if (this.properties.isEmpty()) { return true; } else { for (Entry, Predicate> entry : this.properties.entrySet()) { if (!this.applies(state, (Property)entry.getKey(), (Predicate)entry.getValue())) { return false; } } return true; } } else { return false; } } protected > boolean applies(BlockState state, Property property, Predicate valuePredicate) { T comparable = state.getValue(property); return valuePredicate.test(comparable); } public > BlockStatePredicate where(Property property, Predicate valuePredicate) { if (!this.definition.getProperties().contains(property)) { throw new IllegalArgumentException(this.definition + " cannot support property " + property); } else { this.properties.put(property, valuePredicate); return this; } } }