GateBehavior .java

net.minecraft.world.entity.ai.behavior.GateBehavior (class, 138 lines): members: exitErasedMemories, RunningPolicy, runningPolicy, OrderPolicy, behaviors.

net.minecraft.world.entity.ai.behaviorclass138 linescommonextends LivingEntity>4.8 KB

Implements

API Surface (9)

Behavior.Status getStatus()
Set<MemoryModuleType<?>> getRequiredMemories()
boolean tryStart(final ServerLevel level, final E body, final long timestamp)
void tickOrStop(final ServerLevel level, final E body, final long timestamp)
void doStop(final ServerLevel level, final E body, final long timestamp)
String debugString()
void apply(final ShufflingList<?> list)
<E extends LivingEntity> void apply(final Stream<BehaviorControl<? super E>> behaviors, final ServerLevel level, final E body, final long timestamp)
<E extends LivingEntity> void apply(final Stream<BehaviorControl<? super E>> behaviors, final ServerLevel level, final E body, final long timestamp)

Fields & Constants (6)

entryCondition
exitErasedMemories
orderPolicy
runningPolicy
behaviors
status

Full Source

GateBehavior.java138 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.entity.ai.behavior;
2
3
import com.mojang.datafixers.util.Pair;
4
import java.util.HashSet;
5
import java.util.List;
6
import java.util.Map;
7
import java.util.Set;
8
import java.util.Map.Entry;
9
import java.util.function.Consumer;
10
import java.util.stream.Collectors;
11
import java.util.stream.Stream;
12
import net.minecraft.server.level.ServerLevel;
13
import net.minecraft.world.entity.LivingEntity;
14
import net.minecraft.world.entity.ai.memory.MemoryModuleType;
15
import net.minecraft.world.entity.ai.memory.MemoryStatus;
16
17
public class GateBehavior<E extends LivingEntity> implements BehaviorControl<E> {
18
	private final Map<MemoryModuleType<?>, MemoryStatus> entryCondition;
19
	private final Set<MemoryModuleType<?>> exitErasedMemories;
20
	private final GateBehavior.OrderPolicy orderPolicy;
21
	private final GateBehavior.RunningPolicy runningPolicy;
22
	private final ShufflingList<BehaviorControl<? super E>> behaviors = new ShufflingList<>();
23
	private Behavior.Status status = Behavior.Status.STOPPED;
24
25
	public GateBehavior(
26
		final Map<MemoryModuleType<?>, MemoryStatus> entryCondition,
27
		final Set<MemoryModuleType<?>> exitErasedMemories,
28
		final GateBehavior.OrderPolicy orderPolicy,
29
		final GateBehavior.RunningPolicy runningPolicy,
30
		final List<Pair<? extends BehaviorControl<? super E>, Integer>> behaviors
31
	) {
32
		this.entryCondition = entryCondition;
33
		this.exitErasedMemories = exitErasedMemories;
34
		this.orderPolicy = orderPolicy;
35
		this.runningPolicy = runningPolicy;
36
		behaviors.forEach(entry -> this.behaviors.add(entry.getFirst(), entry.getSecond()));
37
	}
38
39
	@Override
40
	public Behavior.Status getStatus() {
41
		return this.status;
42
	}
43
44
	@Override
45
	public Set<MemoryModuleType<?>> getRequiredMemories() {
46
		Set<MemoryModuleType<?>> memories = new HashSet<>(this.entryCondition.keySet());
47
48
		for (BehaviorControl<? super E> behavior : this.behaviors) {
49
			memories.addAll(behavior.getRequiredMemories());
50
		}
51
52
		return memories;
53
	}
54
55
	private boolean hasRequiredMemories(final E body) {
56
		for (Entry<MemoryModuleType<?>, MemoryStatus> entry : this.entryCondition.entrySet()) {
57
			MemoryModuleType<?> memoryType = entry.getKey();
58
			MemoryStatus requiredStatus = entry.getValue();
59
			if (!body.getBrain().checkMemory(memoryType, requiredStatus)) {
60
				return false;
61
			}
62
		}
63
64
		return true;
65
	}
66
67
	@Override
68
	public final boolean tryStart(final ServerLevel level, final E body, final long timestamp) {
69
		if (this.hasRequiredMemories(body)) {
70
			this.status = Behavior.Status.RUNNING;
71
			this.orderPolicy.apply(this.behaviors);
72
			this.runningPolicy.apply(this.behaviors.stream(), level, body, timestamp);
73
			return true;
74
		} else {
75
			return false;
76
		}
77
	}
78
79
	@Override
80
	public final void tickOrStop(final ServerLevel level, final E body, final long timestamp) {
81
		this.behaviors.stream().filter(goal -> goal.getStatus() == Behavior.Status.RUNNING).forEach(goal -> goal.tickOrStop(level, body, timestamp));
82
		if (this.behaviors.stream().noneMatch(g -> g.getStatus() == Behavior.Status.RUNNING)) {
83
			this.doStop(level, body, timestamp);
84
		}
85
	}
86
87
	@Override
88
	public final void doStop(final ServerLevel level, final E body, final long timestamp) {
89
		this.status = Behavior.Status.STOPPED;
90
		this.behaviors.stream().filter(goal -> goal.getStatus() == Behavior.Status.RUNNING).forEach(goal -> goal.doStop(level, body, timestamp));
91
		this.exitErasedMemories.forEach(body.getBrain()::eraseMemory);
92
	}
93
94
	@Override
95
	public String debugString() {
96
		Set<String> runningBehaviours = this.behaviors
97
			.stream()
98
			.filter(goal -> goal.getStatus() == Behavior.Status.RUNNING)
99
			.map(b -> b.getClass().getSimpleName())
100
			.collect(Collectors.toSet());
101
		return this.getClass().getSimpleName() + ": " + runningBehaviours;
102
	}
103
104
	public enum OrderPolicy {
105
		ORDERED(t -> {}),
106
		SHUFFLED(ShufflingList::shuffle);
107
108
		private final Consumer<ShufflingList<?>> consumer;
109
110
		OrderPolicy(final Consumer<ShufflingList<?>> consumer) {
111
			this.consumer = consumer;
112
		}
113
114
		public void apply(final ShufflingList<?> list) {
115
			this.consumer.accept(list);
116
		}
117
	}
118
119
	public enum RunningPolicy {
120
		RUN_ONE {
121
			@Override
122
			public <E extends LivingEntity> void apply(final Stream<BehaviorControl<? super E>> behaviors, final ServerLevel level, final E body, final long timestamp) {
123
				behaviors.filter(goal -> goal.getStatus() == Behavior.Status.STOPPED).filter(goal -> goal.tryStart(level, body, timestamp)).findFirst();
124
			}
125
		},
126
		TRY_ALL {
127
			@Override
128
			public <E extends LivingEntity> void apply(final Stream<BehaviorControl<? super E>> behaviors, final ServerLevel level, final E body, final long timestamp) {
129
				behaviors.filter(goal -> goal.getStatus() == Behavior.Status.STOPPED).forEach(goal -> goal.tryStart(level, body, timestamp));
130
			}
131
		};
132
133
		public abstract <E extends LivingEntity> void apply(
134
			final Stream<BehaviorControl<? super E>> behaviors, final ServerLevel level, final E body, final long timestamp
135
		);
136
	}
137
}
138

Decompiled Minecraft 26.3 source of GateBehavior (net/minecraft/world/entity/ai/behavior/GateBehavior.java) with Mojang official mappings, stored verbatim on the site.