AdventureModePredicate .java

net.minecraft.world.item.AdventureModePredicate (class, 142 lines): members: checksBlockEntity, CAN_BREAK_HEADER, CAN_PLACE_HEADER, lastCheckedBlock, test.

net.minecraft.world.itemclass142 linescommon5.0 KB

API Surface (5)

boolean test(final BlockInWorld blockInWorld)
void addToTooltip(final Consumer<Component> consumer)
boolean equals(final Object obj)
int hashCode()
String toString()

Fields & Constants (11)

LOGGER
CODEC
STREAM_CODEC
CAN_BREAK_HEADER
CAN_PLACE_HEADER
UNKNOWN_USE
predicates
cachedTooltip
lastCheckedBlock
lastResult
checksBlockEntity

String literals (4)

"item.canBreak"
"item.canPlace"
"item.canUse.unknown"
"AdventureModePredicate{predicates="

Full Source

AdventureModePredicate.java142 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.item;
2
3
import com.mojang.logging.LogUtils;
4
import com.mojang.serialization.Codec;
5
import java.util.List;
6
import java.util.Objects;
7
import java.util.function.Consumer;
8
import net.minecraft.ChatFormatting;
9
import net.minecraft.advancements.predicates.BlockPredicate;
10
import net.minecraft.core.RegistryAccess;
11
import net.minecraft.nbt.CompoundTag;
12
import net.minecraft.network.RegistryFriendlyByteBuf;
13
import net.minecraft.network.chat.Component;
14
import net.minecraft.network.codec.ByteBufCodecs;
15
import net.minecraft.network.codec.StreamCodec;
16
import net.minecraft.util.ExtraCodecs;
17
import net.minecraft.util.ProblemReporter;
18
import net.minecraft.world.level.block.entity.BlockEntity;
19
import net.minecraft.world.level.block.state.pattern.BlockInWorld;
20
import net.minecraft.world.level.storage.TagValueOutput;
21
import org.jspecify.annotations.Nullable;
22
import org.slf4j.Logger;
23
24
public class AdventureModePredicate {
25
	private static final Logger LOGGER = LogUtils.getLogger();
26
	public static final Codec<AdventureModePredicate> CODEC = ExtraCodecs.compactListCodec(
27
			BlockPredicate.CODEC, ExtraCodecs.nonEmptyList(BlockPredicate.CODEC.listOf())
28
		)
29
		.xmap(AdventureModePredicate::new, p -> p.predicates);
30
	public static final StreamCodec<RegistryFriendlyByteBuf, AdventureModePredicate> STREAM_CODEC = StreamCodec.composite(
31
		BlockPredicate.STREAM_CODEC.apply(ByteBufCodecs.list()), predicate -> predicate.predicates, AdventureModePredicate::new
32
	);
33
	public static final Component CAN_BREAK_HEADER = Component.translatable("item.canBreak").withStyle(ChatFormatting.GRAY);
34
	public static final Component CAN_PLACE_HEADER = Component.translatable("item.canPlace").withStyle(ChatFormatting.GRAY);
35
	private static final Component UNKNOWN_USE = Component.translatable("item.canUse.unknown").withStyle(ChatFormatting.GRAY);
36
	private final List<BlockPredicate> predicates;
37
	private @Nullable List<Component> cachedTooltip;
38
	private @Nullable BlockInWorld lastCheckedBlock;
39
	private boolean lastResult;
40
	private boolean checksBlockEntity;
41
42
	public AdventureModePredicate(final List<BlockPredicate> predicates) {
43
		this.predicates = predicates;
44
	}
45
46
	private static boolean areSameBlocks(final BlockInWorld blockInWorld, final @Nullable BlockInWorld cachedBlock, final boolean checkBlockEntity) {
47
		if (cachedBlock == null || blockInWorld.getState() != cachedBlock.getState()) {
48
			return false;
49
		}
50
51
		if (!checkBlockEntity) {
52
			return true;
53
		}
54
55
		if (blockInWorld.getEntity() == null && cachedBlock.getEntity() == null) {
56
			return true;
57
		}
58
59
		if (blockInWorld.getEntity() != null && cachedBlock.getEntity() != null) {
60
			try (ProblemReporter.ScopedCollector reporter = new ProblemReporter.ScopedCollector(LOGGER)) {
61
				RegistryAccess registryAccess = blockInWorld.getLevel().registryAccess();
62
				CompoundTag inWorldTag = saveBlockEntity(blockInWorld.getEntity(), registryAccess, reporter);
63
				CompoundTag cachedTag = saveBlockEntity(cachedBlock.getEntity(), registryAccess, reporter);
64
				return Objects.equals(inWorldTag, cachedTag);
65
			}
66
		} else {
67
			return false;
68
		}
69
	}
70
71
	private static CompoundTag saveBlockEntity(final BlockEntity blockEntity, final RegistryAccess registryAccess, final ProblemReporter reporter) {
72
		TagValueOutput inWorldOutput = TagValueOutput.createWithContext(reporter.forChild(blockEntity.problemPath()), registryAccess);
73
		blockEntity.saveWithId(inWorldOutput);
74
		return inWorldOutput.buildResult();
75
	}
76
77
	public boolean test(final BlockInWorld blockInWorld) {
78
		if (areSameBlocks(blockInWorld, this.lastCheckedBlock, this.checksBlockEntity)) {
79
			return this.lastResult;
80
		}
81
82
		this.lastCheckedBlock = blockInWorld;
83
		this.checksBlockEntity = false;
84
85
		for (BlockPredicate predicate : this.predicates) {
86
			if (predicate.matches(blockInWorld)) {
87
				this.checksBlockEntity = this.checksBlockEntity | predicate.requiresNbt();
88
				this.lastResult = true;
89
				return true;
90
			}
91
		}
92
93
		this.lastResult = false;
94
		return false;
95
	}
96
97
	private List<Component> tooltip() {
98
		if (this.cachedTooltip == null) {
99
			this.cachedTooltip = computeTooltip(this.predicates);
100
		}
101
102
		return this.cachedTooltip;
103
	}
104
105
	public void addToTooltip(final Consumer<Component> consumer) {
106
		this.tooltip().forEach(consumer);
107
	}
108
109
	private static List<Component> computeTooltip(final List<BlockPredicate> predicates) {
110
		for (BlockPredicate predicate : predicates) {
111
			if (predicate.blocks().isEmpty()) {
112
				return List.of(UNKNOWN_USE);
113
			}
114
		}
115
116
		return predicates.stream()
117
			.flatMap(predicatex -> predicatex.blocks().orElseThrow().stream())
118
			.distinct()
119
			.map(block -> block.value().getName().withStyle(ChatFormatting.DARK_GRAY))
120
			.toList();
121
	}
122
123
	@Override
124
	public boolean equals(final Object obj) {
125
		if (this == obj) {
126
			return true;
127
		} else {
128
			return obj instanceof AdventureModePredicate predicate ? this.predicates.equals(predicate.predicates) : false;
129
		}
130
	}
131
132
	@Override
133
	public int hashCode() {
134
		return this.predicates.hashCode();
135
	}
136
137
	@Override
138
	public String toString() {
139
		return "AdventureModePredicate{predicates=" + this.predicates + "}";
140
	}
141
}
142

Decompiled Minecraft 26.3 source of AdventureModePredicate (net/minecraft/world/item/AdventureModePredicate.java) with Mojang official mappings, stored verbatim on the site.