Improved CustomGoal

This commit is contained in:
Auxilor
2022-03-03 10:07:40 +00:00
parent f32110687f
commit 0d0800dfb5
3 changed files with 120 additions and 7 deletions

View File

@@ -3,6 +3,10 @@ package com.willfp.eco.core.entities.ai;
import org.bukkit.entity.Mob;
import org.jetbrains.annotations.NotNull;
import java.util.Collection;
import java.util.EnumSet;
import java.util.Set;
/**
* Base interface for all custom goals.
* <p>
@@ -10,7 +14,12 @@ import org.jetbrains.annotations.NotNull;
*
* @param <T> The type of mob that this goal can be applied to.
*/
public interface CustomGoal<T extends Mob> extends EntityGoal<T>, TargetGoal<T> {
public abstract class CustomGoal<T extends Mob> implements EntityGoal<T>, TargetGoal<T> {
/**
* The flags for the goal.
*/
private final Set<GoalFlag> flags = EnumSet.noneOf(GoalFlag.class);
/**
* Initialize the goal with a mob.
* <p>
@@ -18,7 +27,7 @@ public interface CustomGoal<T extends Mob> extends EntityGoal<T>, TargetGoal<T>
*
* @param mob The mob.
*/
void initialize(@NotNull T mob);
public abstract void initialize(@NotNull T mob);
/**
* Get if the goal can be used.
@@ -26,7 +35,7 @@ public interface CustomGoal<T extends Mob> extends EntityGoal<T>, TargetGoal<T>
*
* @return If the goal can be used.
*/
boolean canUse();
public abstract boolean canUse();
/**
* Tick the goal.
@@ -35,7 +44,7 @@ public interface CustomGoal<T extends Mob> extends EntityGoal<T>, TargetGoal<T>
* <p>
* Runs after {@link this#start()}.
*/
default void tick() {
public void tick() {
// Override when needed.
}
@@ -44,7 +53,7 @@ public interface CustomGoal<T extends Mob> extends EntityGoal<T>, TargetGoal<T>
* <p>
* Runs once {@link this#canUse()} returns true.
*/
default void start() {
public void start() {
// Override when needed.
}
@@ -53,12 +62,50 @@ public interface CustomGoal<T extends Mob> extends EntityGoal<T>, TargetGoal<T>
* <p>
* Runs once {@link this#canUse()} returns false.
*/
default void stop() {
public void stop() {
// Override when needed.
}
/**
* Get if the goal can continue to be used.
*
* @return If the goal can continue to be used.
*/
public boolean canContinueToUse() {
return this.canUse();
}
/**
* Get if the goal is interruptable.
*
* @return If interruptable.
*/
public boolean isInterruptable() {
return true;
}
/**
* Get the goal flags.
*
* @return The flags.
*/
public EnumSet<GoalFlag> getFlags() {
return EnumSet.copyOf(this.flags);
}
/**
* Set the flags for the goal.
*
* @param flags The flags.
*/
public void setFlags(@NotNull final Collection<GoalFlag> flags) {
this.flags.clear();
this.flags.addAll(flags);
}
@Override
default T addToEntity(@NotNull T entity, int priority) {
public T addToEntity(@NotNull final T entity,
final int priority) {
throw new UnsupportedOperationException(
"Shorthand syntax is not supported for custom goals by default as they can be both entity and target goals."
);

View File

@@ -0,0 +1,26 @@
package com.willfp.eco.core.entities.ai;
/**
* Flags for ai goals.
*/
public enum GoalFlag {
/**
* Move.
*/
MOVE,
/**
* Look around.
*/
LOOK,
/**
* Jump.
*/
JUMP,
/**
* Target.
*/
TARGET
}