1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package com.owlmaddie.goals;
import com.owlmaddie.network.ServerPackets;
import net.minecraft.entity.ai.goal.Goal;
import net.minecraft.entity.ai.goal.GoalSelector;
import net.minecraft.entity.ai.goal.PrioritizedGoal;
import net.minecraft.entity.mob.MobEntity;
import net.minecraft.server.world.ServerWorld;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.function.Predicate;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
/**
* The {@code EntityBehaviorManager} class now directly interacts with the goal selectors of entities
* to manage goals, while avoiding concurrent modification issues.
*/
public class EntityBehaviorManager {
public static final Logger LOGGER = LoggerFactory.getLogger("creaturechat");
public static void addGoal(MobEntity entity, Goal goal, GoalPriority priority) {
if (!(entity.getWorld() instanceof ServerWorld)) {
LOGGER.debug("Attempted to add a goal in a non-server world. Aborting.");
return;
}
ServerPackets.serverInstance.execute(() -> {
GoalSelector goalSelector = GoalUtils.getGoalSelector(entity);
// First clear any existing goals of the same type to avoid duplicates
clearAndRemove(g -> goal.getClass().equals(g.getClass()), goalSelector);
// Handle potential priority conflicts before adding the new goal
moveConflictingGoals(goalSelector, priority);
// Now add the new goal at the specified priority
goalSelector.add(priority.getPriority(), goal);
LOGGER.debug("Goal of type {} added with priority {}", goal.getClass().getSimpleName(), priority);
});
}
public static void clearAndRemove(Predicate<Goal> predicate, GoalSelector goalSelector) {
// Collect all goals that need to be removed
List<PrioritizedGoal> toBeRemoved = goalSelector.getGoals().stream()
.filter(prioritizedGoal -> predicate.test(prioritizedGoal.getGoal()))
.collect(Collectors.toList());
// Stop if running and remove each goal
toBeRemoved.forEach(prioritizedGoal -> {
goalSelector.remove(prioritizedGoal.getGoal()); // Remove the goal
});
}
public static void removeGoal(MobEntity entity, Class<? extends Goal> goalClass) {
ServerPackets.serverInstance.execute(() -> {
GoalSelector goalSelector = GoalUtils.getGoalSelector(entity);
// First clear any existing goals of the same type to avoid duplicates
clearAndRemove(g -> goalClass.equals(g.getClass()), goalSelector);
LOGGER.debug("All goals of type {} removed.", goalClass.getSimpleName());
});
}
public static void moveConflictingGoals(GoalSelector goalSelector, GoalPriority newGoalPriority) {
// Collect all prioritized goals currently in the selector.
List<PrioritizedGoal> sortedGoals = goalSelector.getGoals().stream()
.sorted(Comparator.comparingInt(PrioritizedGoal::getPriority))
.collect(Collectors.toList());
// Check if there is an existing goal at the new priority level.
boolean conflictExists = sortedGoals.stream()
.anyMatch(pg -> pg.getPriority() == newGoalPriority.getPriority());
// If there is a conflict, we need to shift priorities of this and all higher priorities.
if (conflictExists) {
int shiftPriority = newGoalPriority.getPriority();
for (PrioritizedGoal pg : sortedGoals) {
if (pg.getPriority() >= shiftPriority) {
// Remove the goal and increment its priority.
goalSelector.remove(pg.getGoal());
goalSelector.add(shiftPriority + 1, pg.getGoal());
shiftPriority++; // Update the shift priority for the next possible conflict.
}
}
LOGGER.debug("Moved conflicting goals starting from priority {}", newGoalPriority);
} else {
LOGGER.debug("No conflicting goal at priority {}, no action taken.", newGoalPriority);
}
}
}