SPIGOT-3531: Refactor pathfinder API

This commit is contained in:
Mikołaj Nowak 2025-01-14 18:15:25 +01:00
parent 6e82ba38c8
commit 562de896b7
No known key found for this signature in database
GPG key ID: 9AD71794C4063890
4 changed files with 79 additions and 86 deletions

View file

@ -103,7 +103,7 @@ public interface Mob extends LivingEntity, Lootable {
* *
* @param predicate a predicate indicating which goals should be removed * @param predicate a predicate indicating which goals should be removed
*/ */
public void removeGoals(@NotNull Predicate<Goal> predicate); public void removeGoalIf(@NotNull Predicate<Goal> predicate);
/** /**
* Disable all goals of a given type. * Disable all goals of a given type.

View file

@ -0,0 +1,73 @@
package org.bukkit.entity.ai;
import java.util.Set;
import org.bukkit.NamespacedKey;
import org.bukkit.entity.Mob;
import org.jetbrains.annotations.NotNull;
/**
* Custom goal definition.
*
* <p>In order to add custom goals to mobs, plugins should implement this
* interface.
*
* @see GoalFactory#custom(NamespacedKey, int, CustomGoalDefinition)
*/
public interface CustomGoalDefinition {
/**
* Action executed when a mob starts following this goal.
*
* @param mob a mob which started following this goal
*/
default void start(@NotNull Mob mob) {}
/**
* Action executed when a mob stops following this goal.
*
* @param mob a mob which stopped following this goal
*/
default void stop(@NotNull Mob mob) {}
/**
* Check requirements needed to start following this goal.
*
* @param mob a mob which is trying to start following this goal
* @return whether the mob can start following this goal
*/
boolean canUse(@NotNull Mob mob);
/**
* Check requirements needed to continue following this goal.
*
* @param mob a mob which is following this goal
* @return whether the mob should continue following this goal
*/
default boolean canContinueToUse(@NotNull Mob mob) {
return canUse(mob);
}
/**
* Action taken by a mob every tick while it's following this goal.
*
* @param mob a mob which is following this goal
*/
void tick(@NotNull Mob mob);
/**
* Check whether this goal can be interrupted.
*
* @return whether this goal can be interrupted
*/
default boolean isInterruptible() {
return true;
}
/**
* Get types of this goal.
*
* @return goal types of this goal
*/
@NotNull
Set<Goal.Type> getTypes();
}

View file

@ -4,13 +4,14 @@ import static org.bukkit.NamespacedKey.BUKKIT;
import static org.bukkit.NamespacedKey.minecraft; import static org.bukkit.NamespacedKey.minecraft;
import java.util.Set; import java.util.Set;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Keyed;
import org.bukkit.NamespacedKey; import org.bukkit.NamespacedKey;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
/** /**
* Mob AI's goal. * Mob AI's goal.
*/ */
public interface Goal { public interface Goal extends Keyed {
/** /**
* Attack target entity with arrows. * Attack target entity with arrows.
@ -265,24 +266,6 @@ public interface Goal {
*/ */
NamespacedKey OTHER = new NamespacedKey(BUKKIT, "other"); NamespacedKey OTHER = new NamespacedKey(BUKKIT, "other");
/**
* Get goal key.
*
* <p>Note: some vanilla goals are not supported in the API.
* {@link Goal#OTHER} will be returned in such cases.
*
* @return goal key
*/
@NotNull
NamespacedKey getId();
/**
* Set goal id.
*
* @param id new goal id
*/
void setId(@NotNull NamespacedKey id);
/** /**
* Check whether this goal is currently enabled. * Check whether this goal is currently enabled.
* *
@ -310,7 +293,7 @@ public interface Goal {
* *
* @return whether this goal is active * @return whether this goal is active
*/ */
boolean isRunning(); boolean isActive();
/** /**
* Get goal types associated with this goal. * Get goal types associated with this goal.

View file

@ -1,6 +1,5 @@
package org.bukkit.entity.ai; package org.bukkit.entity.ai;
import java.util.Set;
import java.util.function.BiPredicate; import java.util.function.BiPredicate;
import java.util.function.BooleanSupplier; import java.util.function.BooleanSupplier;
import java.util.function.Function; import java.util.function.Function;
@ -905,71 +904,9 @@ public interface GoalFactory {
* *
* @param goalId goal id * @param goalId goal id
* @param priority goal priority * @param priority goal priority
* @param customGoalRegistration goal definition * @param customGoalDefinition goal definition
* @return created goal * @return created goal
*/ */
@NotNull @NotNull
Goal custom(@NotNull NamespacedKey goalId, int priority, @NotNull CustomGoalRegistration customGoalRegistration); Goal custom(@NotNull NamespacedKey goalId, int priority, @NotNull CustomGoalDefinition customGoalDefinition);
/**
* Custom goal definition.
*/
interface CustomGoalRegistration {
/**
* Action executed when a mob starts following this goal.
*
* @param mob a mob which started following this goal
*/
default void start(@NotNull Mob mob) {}
/**
* Action executed when a mob stops following this goal.
*
* @param mob a mob which stopped following this goal
*/
default void stop(@NotNull Mob mob) {}
/**
* Check requirements needed to start following this goal.
*
* @param mob a mob which is trying to start following this goal
* @return whether the mob can start following this goal
*/
boolean canUse(@NotNull Mob mob);
/**
* Check requirements needed to continue following this goal.
*
* @param mob a mob which is following this goal
* @return whether the mob should continue following this goal
*/
default boolean canContinueToUse(@NotNull Mob mob) {
return canUse(mob);
}
/**
* Action taken by a mob every tick while it's following this goal.
*
* @param mob a mob which is following this goal
*/
void action(@NotNull Mob mob);
/**
* Check whether this goal can be interrupted.
*
* @return whether this goal can be interrupted
*/
default boolean isInterruptible() {
return true;
}
/**
* Get types of this goal.
*
* @return goal types of this goal
*/
@NotNull
Set<Goal.Type> getTypes();
}
} }