From 54c74d0138d59242c37435cba41c99d3c566f4f4 Mon Sep 17 00:00:00 2001 From: Auxilor Date: Wed, 2 Mar 2022 12:14:03 +0000 Subject: [PATCH] Added more goals, added shorthand goal syntax, added kotlin extension for entity controllers --- .../eco/core/entities/ai/EntityGoal.java | 14 ++++++++++- .../eco/core/entities/ai/TargetGoal.java | 14 ++++++++++- .../ai/target/TargetGoalNonTameRandom.java | 23 +++++++++++++++++++ .../ai/target/TargetGoalOwnerHurt.java | 12 ++++++++++ .../ai/target/TargetGoalOwnerHurtBy.java | 12 ++++++++++ .../target/TargetGoalResetUniversalAnger.java | 17 ++++++++++++++ .../com/willfp/eco/core/entities/Entities.kt | 12 ++++++++++ .../spigot/proxy/common/ai/TargetGoals.kt | 9 ++++++++ .../ai/target/NonTameRandomGoalFactory.kt | 22 ++++++++++++++++++ .../ai/target/OwnerHurtByGoalFactory.kt | 16 +++++++++++++ .../common/ai/target/OwnerHurtGoalFactory.kt | 16 +++++++++++++ .../target/ResetUniversalAngerGoalFactory.kt | 19 +++++++++++++++ 12 files changed, 184 insertions(+), 2 deletions(-) create mode 100644 eco-api/src/main/java/com/willfp/eco/core/entities/ai/target/TargetGoalNonTameRandom.java create mode 100644 eco-api/src/main/java/com/willfp/eco/core/entities/ai/target/TargetGoalOwnerHurt.java create mode 100644 eco-api/src/main/java/com/willfp/eco/core/entities/ai/target/TargetGoalOwnerHurtBy.java create mode 100644 eco-api/src/main/java/com/willfp/eco/core/entities/ai/target/TargetGoalResetUniversalAnger.java create mode 100644 eco-api/src/main/kotlin/com/willfp/eco/core/entities/Entities.kt create mode 100644 eco-core/core-nms/nms-common/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/common/ai/target/NonTameRandomGoalFactory.kt create mode 100644 eco-core/core-nms/nms-common/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/common/ai/target/OwnerHurtByGoalFactory.kt create mode 100644 eco-core/core-nms/nms-common/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/common/ai/target/OwnerHurtGoalFactory.kt create mode 100644 eco-core/core-nms/nms-common/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/common/ai/target/ResetUniversalAngerGoalFactory.kt diff --git a/eco-api/src/main/java/com/willfp/eco/core/entities/ai/EntityGoal.java b/eco-api/src/main/java/com/willfp/eco/core/entities/ai/EntityGoal.java index 9d3b1cc9..45c74d0f 100644 --- a/eco-api/src/main/java/com/willfp/eco/core/entities/ai/EntityGoal.java +++ b/eco-api/src/main/java/com/willfp/eco/core/entities/ai/EntityGoal.java @@ -1,6 +1,7 @@ package com.willfp.eco.core.entities.ai; import org.bukkit.entity.Mob; +import org.jetbrains.annotations.NotNull; /** * A goal for entity AI. @@ -8,5 +9,16 @@ import org.bukkit.entity.Mob; * @param The type of mob that the goal can be applied to. */ public interface EntityGoal { - + /** + * Add the entity goal to an entity. + * + * @param entity The entity. + * @param priority The priority. + * @return The entity, modified. + */ + default T addToEntity(@NotNull T entity, int priority) { + return EntityController.of(entity) + .addEntityGoal(priority, this) + .getEntity(); + } } diff --git a/eco-api/src/main/java/com/willfp/eco/core/entities/ai/TargetGoal.java b/eco-api/src/main/java/com/willfp/eco/core/entities/ai/TargetGoal.java index bd0bff4c..f80c6467 100644 --- a/eco-api/src/main/java/com/willfp/eco/core/entities/ai/TargetGoal.java +++ b/eco-api/src/main/java/com/willfp/eco/core/entities/ai/TargetGoal.java @@ -1,6 +1,7 @@ package com.willfp.eco.core.entities.ai; import org.bukkit.entity.Mob; +import org.jetbrains.annotations.NotNull; /** * A goal for entity target AI. @@ -8,5 +9,16 @@ import org.bukkit.entity.Mob; * @param The type of mob that the goal can be applied to. */ public interface TargetGoal { - + /** + * Add the target goal to an entity. + * + * @param entity The entity. + * @param priority The priority. + * @return The entity, modified. + */ + default T addToEntity(@NotNull T entity, int priority) { + return EntityController.of(entity) + .addTargetGoal(priority, this) + .getEntity(); + } } diff --git a/eco-api/src/main/java/com/willfp/eco/core/entities/ai/target/TargetGoalNonTameRandom.java b/eco-api/src/main/java/com/willfp/eco/core/entities/ai/target/TargetGoalNonTameRandom.java new file mode 100644 index 00000000..c7bd39f1 --- /dev/null +++ b/eco-api/src/main/java/com/willfp/eco/core/entities/ai/target/TargetGoalNonTameRandom.java @@ -0,0 +1,23 @@ +package com.willfp.eco.core.entities.ai.target; + +import com.willfp.eco.core.entities.ai.TargetGoal; +import org.bukkit.entity.LivingEntity; +import org.bukkit.entity.Tameable; +import org.jetbrains.annotations.NotNull; + +import java.util.function.Predicate; + +/** + * Target random non-tame entity. + * + * @param targetClass The types of entities to heal. + * @param checkVisibility If visibility should be checked. + * @param targetFilter The filter for targets to match. + */ +public record TargetGoalNonTameRandom( + @NotNull Class targetClass, + boolean checkVisibility, + @NotNull Predicate targetFilter +) implements TargetGoal { + +} diff --git a/eco-api/src/main/java/com/willfp/eco/core/entities/ai/target/TargetGoalOwnerHurt.java b/eco-api/src/main/java/com/willfp/eco/core/entities/ai/target/TargetGoalOwnerHurt.java new file mode 100644 index 00000000..571e56e6 --- /dev/null +++ b/eco-api/src/main/java/com/willfp/eco/core/entities/ai/target/TargetGoalOwnerHurt.java @@ -0,0 +1,12 @@ +package com.willfp.eco.core.entities.ai.target; + +import com.willfp.eco.core.entities.ai.TargetGoal; +import org.bukkit.entity.Tameable; + +/** + * Target last entity that hurt the owner. + */ +public record TargetGoalOwnerHurt( +) implements TargetGoal { + +} diff --git a/eco-api/src/main/java/com/willfp/eco/core/entities/ai/target/TargetGoalOwnerHurtBy.java b/eco-api/src/main/java/com/willfp/eco/core/entities/ai/target/TargetGoalOwnerHurtBy.java new file mode 100644 index 00000000..5c3d0103 --- /dev/null +++ b/eco-api/src/main/java/com/willfp/eco/core/entities/ai/target/TargetGoalOwnerHurtBy.java @@ -0,0 +1,12 @@ +package com.willfp.eco.core.entities.ai.target; + +import com.willfp.eco.core.entities.ai.TargetGoal; +import org.bukkit.entity.Tameable; + +/** + * Target entity that hurt the owner. + */ +public record TargetGoalOwnerHurtBy( +) implements TargetGoal { + +} diff --git a/eco-api/src/main/java/com/willfp/eco/core/entities/ai/target/TargetGoalResetUniversalAnger.java b/eco-api/src/main/java/com/willfp/eco/core/entities/ai/target/TargetGoalResetUniversalAnger.java new file mode 100644 index 00000000..cb57697a --- /dev/null +++ b/eco-api/src/main/java/com/willfp/eco/core/entities/ai/target/TargetGoalResetUniversalAnger.java @@ -0,0 +1,17 @@ +package com.willfp.eco.core.entities.ai.target; + +import com.willfp.eco.core.entities.ai.TargetGoal; +import org.bukkit.entity.Mob; + +/** + * Reset universal anger. + *

+ * Can only be applied to neutral mobs. + * + * @param triggerOthers If this should cause other nearby entities to trigger. + */ +public record TargetGoalResetUniversalAnger( + boolean triggerOthers +) implements TargetGoal { + +} diff --git a/eco-api/src/main/kotlin/com/willfp/eco/core/entities/Entities.kt b/eco-api/src/main/kotlin/com/willfp/eco/core/entities/Entities.kt new file mode 100644 index 00000000..58d37a59 --- /dev/null +++ b/eco-api/src/main/kotlin/com/willfp/eco/core/entities/Entities.kt @@ -0,0 +1,12 @@ +@file:JvmName("EntityExtensions") + +package com.willfp.eco.core.entities + +import com.willfp.eco.core.entities.ai.EntityController +import org.bukkit.entity.Mob + +/** + * @see EntityController.of + */ +val T.controller: EntityController + get() = EntityController.of(this) diff --git a/eco-core/core-nms/nms-common/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/common/ai/TargetGoals.kt b/eco-core/core-nms/nms-common/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/common/ai/TargetGoals.kt index fa6ab086..7d9d3a00 100644 --- a/eco-core/core-nms/nms-common/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/common/ai/TargetGoals.kt +++ b/eco-core/core-nms/nms-common/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/common/ai/TargetGoals.kt @@ -6,10 +6,16 @@ import com.willfp.eco.core.entities.ai.target.TargetGoalHurtBy import com.willfp.eco.core.entities.ai.target.TargetGoalNearestAttackable import com.willfp.eco.core.entities.ai.target.TargetGoalNearestAttackableWitch import com.willfp.eco.core.entities.ai.target.TargetGoalNearestHealableRaider +import com.willfp.eco.core.entities.ai.target.TargetGoalNonTameRandom +import com.willfp.eco.core.entities.ai.target.TargetGoalOwnerHurt +import com.willfp.eco.core.entities.ai.target.TargetGoalOwnerHurtBy import com.willfp.eco.internal.spigot.proxy.common.ai.target.HurtByGoalFactory import com.willfp.eco.internal.spigot.proxy.common.ai.target.NearestAttackableGoalFactory import com.willfp.eco.internal.spigot.proxy.common.ai.target.NearestAttackableWitchGoalFactory import com.willfp.eco.internal.spigot.proxy.common.ai.target.NearestHealableRaiderGoalFactory +import com.willfp.eco.internal.spigot.proxy.common.ai.target.NonTameRandomGoalFactory +import com.willfp.eco.internal.spigot.proxy.common.ai.target.OwnerHurtByGoalFactory +import com.willfp.eco.internal.spigot.proxy.common.ai.target.OwnerHurtGoalFactory import com.willfp.eco.internal.spigot.proxy.common.commonsProvider import net.minecraft.world.entity.PathfinderMob import net.minecraft.world.entity.ai.goal.Goal @@ -26,6 +32,9 @@ fun > T.getGoalFactory(): TargetGoalFactory? { is TargetGoalNearestAttackable -> NearestAttackableGoalFactory is TargetGoalNearestAttackableWitch -> NearestAttackableWitchGoalFactory is TargetGoalNearestHealableRaider -> NearestHealableRaiderGoalFactory + is TargetGoalNonTameRandom -> NonTameRandomGoalFactory + is TargetGoalOwnerHurtBy -> OwnerHurtByGoalFactory + is TargetGoalOwnerHurt -> OwnerHurtGoalFactory is CustomGoal<*> -> CustomGoalFactory else -> null } as TargetGoalFactory? diff --git a/eco-core/core-nms/nms-common/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/common/ai/target/NonTameRandomGoalFactory.kt b/eco-core/core-nms/nms-common/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/common/ai/target/NonTameRandomGoalFactory.kt new file mode 100644 index 00000000..8324948c --- /dev/null +++ b/eco-core/core-nms/nms-common/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/common/ai/target/NonTameRandomGoalFactory.kt @@ -0,0 +1,22 @@ +package com.willfp.eco.internal.spigot.proxy.common.ai.target + +import com.willfp.eco.core.entities.ai.target.TargetGoalNonTameRandom +import com.willfp.eco.internal.spigot.proxy.common.ai.TargetGoalFactory +import com.willfp.eco.internal.spigot.proxy.common.ai.toBukkitEntity +import com.willfp.eco.internal.spigot.proxy.common.ai.toNMSClass +import net.minecraft.world.entity.PathfinderMob +import net.minecraft.world.entity.TamableAnimal +import net.minecraft.world.entity.ai.goal.Goal +import net.minecraft.world.entity.ai.goal.target.NonTameRandomTargetGoal + +object NonTameRandomGoalFactory : TargetGoalFactory { + override fun create(apiGoal: TargetGoalNonTameRandom, entity: PathfinderMob): Goal? { + return NonTameRandomTargetGoal( + entity as? TamableAnimal ?: return null, + apiGoal.targetClass.toNMSClass(), + apiGoal.checkVisibility, + ) { + apiGoal.targetFilter.test(it.toBukkitEntity()) + } + } +} diff --git a/eco-core/core-nms/nms-common/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/common/ai/target/OwnerHurtByGoalFactory.kt b/eco-core/core-nms/nms-common/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/common/ai/target/OwnerHurtByGoalFactory.kt new file mode 100644 index 00000000..ebc97ae7 --- /dev/null +++ b/eco-core/core-nms/nms-common/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/common/ai/target/OwnerHurtByGoalFactory.kt @@ -0,0 +1,16 @@ +package com.willfp.eco.internal.spigot.proxy.common.ai.target + +import com.willfp.eco.core.entities.ai.target.TargetGoalOwnerHurtBy +import com.willfp.eco.internal.spigot.proxy.common.ai.TargetGoalFactory +import net.minecraft.world.entity.PathfinderMob +import net.minecraft.world.entity.TamableAnimal +import net.minecraft.world.entity.ai.goal.Goal +import net.minecraft.world.entity.ai.goal.target.OwnerHurtByTargetGoal + +object OwnerHurtByGoalFactory : TargetGoalFactory { + override fun create(apiGoal: TargetGoalOwnerHurtBy, entity: PathfinderMob): Goal? { + return OwnerHurtByTargetGoal( + entity as? TamableAnimal ?: return null + ) + } +} diff --git a/eco-core/core-nms/nms-common/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/common/ai/target/OwnerHurtGoalFactory.kt b/eco-core/core-nms/nms-common/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/common/ai/target/OwnerHurtGoalFactory.kt new file mode 100644 index 00000000..6960350a --- /dev/null +++ b/eco-core/core-nms/nms-common/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/common/ai/target/OwnerHurtGoalFactory.kt @@ -0,0 +1,16 @@ +package com.willfp.eco.internal.spigot.proxy.common.ai.target + +import com.willfp.eco.core.entities.ai.target.TargetGoalOwnerHurt +import com.willfp.eco.internal.spigot.proxy.common.ai.TargetGoalFactory +import net.minecraft.world.entity.PathfinderMob +import net.minecraft.world.entity.TamableAnimal +import net.minecraft.world.entity.ai.goal.Goal +import net.minecraft.world.entity.ai.goal.target.OwnerHurtTargetGoal + +object OwnerHurtGoalFactory : TargetGoalFactory { + override fun create(apiGoal: TargetGoalOwnerHurt, entity: PathfinderMob): Goal? { + return OwnerHurtTargetGoal( + entity as? TamableAnimal ?: return null + ) + } +} diff --git a/eco-core/core-nms/nms-common/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/common/ai/target/ResetUniversalAngerGoalFactory.kt b/eco-core/core-nms/nms-common/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/common/ai/target/ResetUniversalAngerGoalFactory.kt new file mode 100644 index 00000000..d809e206 --- /dev/null +++ b/eco-core/core-nms/nms-common/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/common/ai/target/ResetUniversalAngerGoalFactory.kt @@ -0,0 +1,19 @@ +package com.willfp.eco.internal.spigot.proxy.common.ai.target + +import com.willfp.eco.core.entities.ai.target.TargetGoalResetUniversalAnger +import com.willfp.eco.internal.spigot.proxy.common.ai.TargetGoalFactory +import net.minecraft.world.entity.NeutralMob +import net.minecraft.world.entity.PathfinderMob +import net.minecraft.world.entity.ai.goal.Goal +import net.minecraft.world.entity.ai.goal.target.ResetUniversalAngerTargetGoal + +object ResetUniversalAngerGoalFactory : TargetGoalFactory { + override fun create(apiGoal: TargetGoalResetUniversalAnger, entity: PathfinderMob): Goal? { + if (entity !is NeutralMob) return null + + return ResetUniversalAngerTargetGoal( + entity, + apiGoal.triggerOthers + ) + } +}