Class AbstractGoal<O extends Agent>
- java.lang.Object
-
- org.spongepowered.api.entity.ai.goal.AbstractGoal<O>
-
- Type Parameters:
O
- The type of Agent
- All Implemented Interfaces:
Goal<O>
public abstract class AbstractGoal<O extends Agent> extends Object implements Goal<O>
An abstract implementation of aGoal
that aGoalExecutor
can run.It is required for anyone wanting to write their own logic that a Goal can run to utilize this class. If you desire to use the builtin AI included with Minecraft, use
BuilderProvider.provide(Class)
and pass a builder to it instead.At the beginning of every "AI" tick, all
Goal
s that are added to the parentGoalExecutor
are iterated through. If aGoal
is already marked as "in use", that goal'scontinueUpdating()
is checked fortrue
. IfcontinueUpdating()
isfalse
,reset()
is invoked, and theGoal
is "flagged" as unused for the parentGoalExecutor
. IfcontinueUpdating()
istrue
,update()
is invoked to perform any major logic.If an
Goal
is not currently flagged as "in use", thenshouldUpdate()
is invoked. IfshouldUpdate()
istrue
, theGoal
is marked as "in use", andstart()
is invoked. Afterstart()
is called,update()
will be invoked to be used for the first time in this "AI tick".Regardless whether the
Goal
was "in use" or not, if theGoal
is now "in use",continueUpdating()
is called to verify the validity of the task. IfcontinueUpdating()
isfalse
,reset()
is called to clean up theGoal
.
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
AbstractGoal(GoalType type)
Creates a newAbstractGoal
with the providedGoal
.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description abstract boolean
continueUpdating()
Optional<GoalExecutor<O>>
executor()
Gets theGoalExecutor
that is updating this goal, if any.abstract void
reset()
Performs any reset necessary for this goal during the current tick.abstract boolean
shouldUpdate()
abstract void
start()
Invoked when the goal is "started".GoalType
type()
Gets theGoalType
.abstract void
update()
Performs any customary logic for this goal to modify the parentAgent
in any way, including navigation, health, potion effects, etc.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.spongepowered.api.entity.ai.goal.Goal
canBeInterrupted, canRunConcurrentWith, owner
-
-
-
-
Constructor Detail
-
AbstractGoal
protected AbstractGoal(GoalType type)
Creates a newAbstractGoal
with the providedGoal
.- Parameters:
type
- The type
-
-
Method Detail
-
executor
public final Optional<GoalExecutor<O>> executor()
Description copied from interface:Goal
Gets theGoalExecutor
that is updating this goal, if any.- Specified by:
executor
in interfaceGoal<O extends Agent>
- Returns:
- The goal or
Optional.empty()
if not present
-
start
public abstract void start()
Invoked when the goal is "started". A "start" of a goal occurs at the "start" of the "AI" is "ticking". Depending on the case whereshouldUpdate()
returnsfalse
, anGoal
is NOT going to be called, so this method would not be called at the "start" of the tick to "prepare" forupdate()
.
-
shouldUpdate
public abstract boolean shouldUpdate()
Determines whether thisGoal
should be considered for "ticking" orstart()
,update()
, andreset()
are called for that "AI" tick.- Returns:
- Whether this goal should be marked for use in the current tick
-
update
public abstract void update()
Performs any customary logic for this goal to modify the parentAgent
in any way, including navigation, health, potion effects, etc. Only called whenshouldUpdate()
returnstrue
, and afterstart()
has completed. Likewise, ifcontinueUpdating()
-
continueUpdating
public abstract boolean continueUpdating()
Called to verify that thisGoal
needs toupdate()
in this tick. If this returnsfalse
, this goal is removed from use andreset()
is called.- Returns:
- Whether this task should update this "tick" or not
-
reset
public abstract void reset()
-
-