Commit c0967ef6 by Jonathan Thomas

Protect changing goalSelector to a single server thread, to prevent crash

parent 6faf3371
Pipeline #12025 passed with stage
in 26 seconds
package com.owlmaddie.goals; package com.owlmaddie.goals;
import com.owlmaddie.network.ServerPackets;
import net.minecraft.entity.ai.goal.Goal; import net.minecraft.entity.ai.goal.Goal;
import net.minecraft.entity.ai.goal.GoalSelector; import net.minecraft.entity.ai.goal.GoalSelector;
import net.minecraft.entity.ai.goal.PrioritizedGoal;
import net.minecraft.entity.mob.MobEntity; import net.minecraft.entity.mob.MobEntity;
import net.minecraft.server.world.ServerWorld; import net.minecraft.server.world.ServerWorld;
import net.minecraft.entity.ai.goal.PrioritizedGoal;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
...@@ -37,9 +38,12 @@ public class EntityBehaviorManager { ...@@ -37,9 +38,12 @@ public class EntityBehaviorManager {
List<Goal> goals = entityGoals.computeIfAbsent(entityId, k -> new ArrayList<>()); List<Goal> goals = entityGoals.computeIfAbsent(entityId, k -> new ArrayList<>());
goals.add(goal); goals.add(goal);
// Ensure that the task is synced with the server thread
ServerPackets.serverInstance.execute(() -> {
GoalSelector goalSelector = GoalUtils.getGoalSelector(entity); GoalSelector goalSelector = GoalUtils.getGoalSelector(entity);
goalSelector.add(priority.getPriority(), goal); goalSelector.add(priority.getPriority(), goal);
LOGGER.debug("Goal of type {} added to entity UUID: {}", goal.getClass().getSimpleName(), entityId); LOGGER.debug("Goal of type {} added to entity UUID: {}", goal.getClass().getSimpleName(), entityId);
});
} }
public static void removeGoal(MobEntity entity, Class<? extends Goal> goalClass) { public static void removeGoal(MobEntity entity, Class<? extends Goal> goalClass) {
...@@ -47,6 +51,8 @@ public class EntityBehaviorManager { ...@@ -47,6 +51,8 @@ public class EntityBehaviorManager {
List<Goal> goals = entityGoals.get(entityId); List<Goal> goals = entityGoals.get(entityId);
if (goals != null) { if (goals != null) {
// Ensure that the task is synced with the server thread
ServerPackets.serverInstance.execute(() -> {
GoalSelector goalSelector = GoalUtils.getGoalSelector(entity); GoalSelector goalSelector = GoalUtils.getGoalSelector(entity);
goals.removeIf(goal -> { goals.removeIf(goal -> {
if (goalClass.isInstance(goal)) { if (goalClass.isInstance(goal)) {
...@@ -56,10 +62,13 @@ public class EntityBehaviorManager { ...@@ -56,10 +62,13 @@ public class EntityBehaviorManager {
} }
return false; return false;
}); });
});
} }
} }
private static void moveConflictingGoals(MobEntity entity, GoalPriority newGoalPriority) { private static void moveConflictingGoals(MobEntity entity, GoalPriority newGoalPriority) {
// Ensure that the task is synced with the server thread
ServerPackets.serverInstance.execute(() -> {
GoalSelector goalSelector = GoalUtils.getGoalSelector(entity); GoalSelector goalSelector = GoalUtils.getGoalSelector(entity);
// Retrieve the existing goals // Retrieve the existing goals
...@@ -90,5 +99,6 @@ public class EntityBehaviorManager { ...@@ -90,5 +99,6 @@ public class EntityBehaviorManager {
// Increment the priority and re-add the goal // Increment the priority and re-add the goal
goalSelector.add(goalToModify.getPriority() + 1, goalToModify.getGoal()); goalSelector.add(goalToModify.getPriority() + 1, goalToModify.getGoal());
} }
});
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment