BlockStatePredicate .java

net.minecraft.world.level.block.state.predicate.BlockStatePredicate (class): members: applies, forBlock, where, definition, ANY, properties, test.

net.minecraft.world.level.block.state.predicateclass58 linescommon1.9 KB

Implements

API Surface (4)

BlockStatePredicate forBlock(final Block block)
boolean test(final @Nullable BlockState input)
<T extends Comparable<T>> boolean applies(final BlockState input, final Property<T> key, final Predicate<Object> predicate)
<V extends Comparable<V>> BlockStatePredicate where(final Property<V> property, final Predicate<Object> predicate)

Fields & Constants (3)

ANY
definition
properties

String literals (1)

" cannot support property "

Code Graph: Dependencies (4)

Edges (calls / imports / inherits / references) resolved by graphify from the AST. Every chip opens the class source.

Full Source

BlockStatePredicate.java58 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.level.block.state.predicate;
2
3
import com.google.common.collect.Maps;
4
import java.util.Map;
5
import java.util.Map.Entry;
6
import java.util.function.Predicate;
7
import net.minecraft.world.level.block.Block;
8
import net.minecraft.world.level.block.state.BlockState;
9
import net.minecraft.world.level.block.state.StateDefinition;
10
import net.minecraft.world.level.block.state.properties.Property;
11
import org.jspecify.annotations.Nullable;
12
13
public class BlockStatePredicate implements Predicate<BlockState> {
14
	public static final Predicate<BlockState> ANY = input -> true;
15
	private final StateDefinition<Block, BlockState> definition;
16
	private final Map<Property<?>, Predicate<Object>> properties = Maps.newHashMap();
17
18
	private BlockStatePredicate(final StateDefinition<Block, BlockState> definition) {
19
		this.definition = definition;
20
	}
21
22
	public static BlockStatePredicate forBlock(final Block block) {
23
		return new BlockStatePredicate(block.getStateDefinition());
24
	}
25
26
	public boolean test(final @Nullable BlockState input) {
27
		if (input != null && input.getBlock().equals(this.definition.getOwner())) {
28
			if (this.properties.isEmpty()) {
29
				return true;
30
			}
31
32
			for (Entry<Property<?>, Predicate<Object>> entry : this.properties.entrySet()) {
33
				if (!this.applies(input, entry.getKey(), entry.getValue())) {
34
					return false;
35
				}
36
			}
37
38
			return true;
39
		} else {
40
			return false;
41
		}
42
	}
43
44
	protected <T extends Comparable<T>> boolean applies(final BlockState input, final Property<T> key, final Predicate<Object> predicate) {
45
		T value = input.getValue(key);
46
		return predicate.test(value);
47
	}
48
49
	public <V extends Comparable<V>> BlockStatePredicate where(final Property<V> property, final Predicate<Object> predicate) {
50
		if (!this.definition.getProperties().contains(property)) {
51
			throw new IllegalArgumentException(this.definition + " cannot support property " + property);
52
		}
53
54
		this.properties.put(property, predicate);
55
		return this;
56
	}
57
}
58

Decompiled Minecraft 26.3 source of BlockStatePredicate (net/minecraft/world/level/block/state/predicate/BlockStatePredicate.java) with Mojang official mappings, stored verbatim on the site.