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 a Goal that a GoalExecutor 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 Goals that are added to the parent GoalExecutor are iterated through. If a Goal is already marked as "in use", that goal's continueUpdating() is checked for true. If continueUpdating() is false, reset() is invoked, and the Goal is "flagged" as unused for the parent GoalExecutor. If continueUpdating() is true, update() is invoked to perform any major logic.

If an Goal is not currently flagged as "in use", then shouldUpdate() is invoked. If shouldUpdate() is true, the Goal is marked as "in use", and start() is invoked. After start() 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 the Goal is now "in use", continueUpdating() is called to verify the validity of the task. If continueUpdating() is false, reset() is called to clean up the Goal.

  • Constructor Details

    • AbstractGoal

      protected AbstractGoal(GoalType type)
      Creates a new AbstractGoal with the provided Goal.
      Parameters:
      type - The type
  • Method Details

    • type

      public final GoalType type()
      Description copied from interface: Goal
      Gets the GoalType.
      Specified by:
      type in interface Goal<O extends Agent>
      Returns:
      The type
    • executor

      public final Optional<GoalExecutor<O>> executor()
      Description copied from interface: Goal
      Gets the GoalExecutor that is updating this goal, if any.
      Specified by:
      executor in interface Goal<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 where shouldUpdate() returns false, an Goal is NOT going to be called, so this method would not be called at the "start" of the tick to "prepare" for update().
    • shouldUpdate

      public abstract boolean shouldUpdate()
      Determines whether this Goal should be considered for "ticking" or start(), update(), and reset() 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 parent Agent in any way, including navigation, health, potion effects, etc. Only called when shouldUpdate() returns true, and after start() has completed. Likewise, if continueUpdating()
    • continueUpdating

      public abstract boolean continueUpdating()
      Called to verify that this Goal needs to update() in this tick. If this returns false, this goal is removed from use and reset() is called.
      Returns:
      Whether this task should update this "tick" or not
    • reset

      public abstract void reset()
      Performs any reset necessary for this goal during the current tick.

      Note that this may be called during any state during start() or update() such that the goal is removed from use for the current "AI" tick.