All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.minecraft.server.PathfinderGoalSelector Maven / Gradle / Ivy

package net.minecraft.server;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.bukkit.craftbukkit.util.UnsafeList;

import java.util.Iterator;
import java.util.List;

public class PathfinderGoalSelector {
	
	private static final Logger logger = LogManager.getLogger();  // from -> a
	private final MethodProfiler profiler;  // from -> d
	
	// from -> b
	public final List tasks = new UnsafeList<>();
	
	// from -> c
	public final List executingTasks = new UnsafeList<>();
	
	public int tickRate = 3; // from -> f
	public int tickCount; // from -> e
	
	public PathfinderGoalSelector(MethodProfiler methodprofiler) {
		this.profiler = methodprofiler;
	}
	
	/**
	 * Add a new PathfinderGoal. Args : priority, task
	 */
	// from -> a()
	public void addTask(int priority, PathfinderGoal goal) {
		tasks.add(new PathfinderGoalSelectorItem(priority, goal));
	}
	
	/**
	 * removes the indicated task from the entity's AI tasks.
	 */
	// from -> a()
	public void removeTask(PathfinderGoal goal) {
		Iterator iterator = this.tasks.iterator();
		
		while (iterator.hasNext()) {
			PathfinderGoalSelector.PathfinderGoalSelectorItem path = iterator.next();
			PathfinderGoal pathfindergoal1 = path.goal;
			
			if (pathfindergoal1 == goal) {
				if (this.executingTasks.contains(path)) {
					pathfindergoal1.d();
					this.executingTasks.remove(path);
				}
				
				iterator.remove();
			}
		}
		
	}
	
	// from -> a()
	public void onUpdateTasks() {
		Iterator iterator;
		PathfinderGoalSelector.PathfinderGoalSelectorItem path;
		
		if (this.tickCount++ % this.tickRate == 0) {
			iterator = this.tasks.iterator();
			
			while (iterator.hasNext()) {
				path = iterator.next();
				boolean isExecuting = this.executingTasks.contains(path);
				
				if (isExecuting) {
					if (this.canUse(path) && this.canContinue(path)) {
						continue;
					}
					
					path.goal.d();
					this.executingTasks.remove(path);
				}
				
				if (this.canUse(path) && path.goal.a()) {
					path.goal.c();
					this.executingTasks.add(path);
				}
			}
		} else {
			iterator = this.executingTasks.iterator();
			
			while (iterator.hasNext()) {
				path = iterator.next();
				if (!this.canContinue(path)) {
					path.goal.d();
					iterator.remove();
				}
			}
		}
		
		iterator = this.executingTasks.iterator();
		
		while (iterator.hasNext()) {
			path = iterator.next();
			path.goal.e();
		}
	}
	
	/**
	 * Determine if a specific AI Task should continue being executed.
	 */
	// from -> a()
	private boolean canContinue(PathfinderGoalSelector.PathfinderGoalSelectorItem path) {
		return path.goal.b();
	}
	
	/**
	 * Determine if a specific AI Task can be executed, which means that all running higher (= lower int value) priority
	 * tasks are compatible with it or all lower priority tasks can be interrupted.
	 */
	// from -> a()
	private boolean canUse(PathfinderGoalSelector.PathfinderGoalSelectorItem path) {
		Iterator iterator = this.tasks.iterator();
		
		while (iterator.hasNext()) {
			PathfinderGoalSelector.PathfinderGoalSelectorItem path2 = iterator.next();
			
			if (path2 != path) {
				if (path.priority >= path2.priority) {
					if (!this.areTasksCompatible(path, path2) && this.executingTasks.contains(path2)) {
						((UnsafeList.Itr) iterator).valid = false; // CraftBukkit - mark iterator for reuse
						return false;
					}
				} else if (!path2.goal.i() && this.executingTasks.contains(path2)) {
					((UnsafeList.Itr) iterator).valid = false; // CraftBukkit - mark iterator for reuse
					return false;
				}
			}
		}
		
		return true;
	}
	
	/**
	 * Returns whether two EntityAITaskEntries can be executed concurrently
	 */
	// from -> a()
	private boolean areTasksCompatible(PathfinderGoalSelector.PathfinderGoalSelectorItem path, PathfinderGoalSelector.PathfinderGoalSelectorItem path2) {
		return (path.goal.getMutexBits() & path2.goal.getMutexBits()) == 0;
	}
	
	static class PathfinderGoalSelectorItem {
		
		public PathfinderGoal goal;  // from -> a
		public int priority;  // from -> b
		
		public PathfinderGoalSelectorItem(int priority, PathfinderGoal goal) {
			this.priority = priority;
			this.goal = goal;
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy