From 2c51a6900f39b34e02fcae1ff4ec70824efff47f Mon Sep 17 00:00:00 2001 From: Auxilor Date: Tue, 1 Mar 2022 20:40:45 +0000 Subject: [PATCH] Added remaining entity goals --- .../goals/entity/EntityGoalRestrictSun.java | 11 +++ .../EntityGoalStrollThroughVillage.java | 14 +++ .../ai/goals/entity/EntityGoalTempt.java | 21 +++++ .../goals/entity/EntityGoalTryFindWater.java | 11 +++ .../ai/goals/entity/EntityGoalUseItem.java | 24 +++++ .../EntityGoalWaterAvoidingRandomFlying.java | 14 +++ .../EntityGoalWaterAvoidingRandomStroll.java | 16 ++++ .../spigot/proxy/v1_17_R1/ai/EntityGoals.kt | 92 +++++++++++++++++++ 8 files changed, 203 insertions(+) create mode 100644 eco-api/src/main/java/com/willfp/eco/core/entities/ai/goals/entity/EntityGoalRestrictSun.java create mode 100644 eco-api/src/main/java/com/willfp/eco/core/entities/ai/goals/entity/EntityGoalStrollThroughVillage.java create mode 100644 eco-api/src/main/java/com/willfp/eco/core/entities/ai/goals/entity/EntityGoalTempt.java create mode 100644 eco-api/src/main/java/com/willfp/eco/core/entities/ai/goals/entity/EntityGoalTryFindWater.java create mode 100644 eco-api/src/main/java/com/willfp/eco/core/entities/ai/goals/entity/EntityGoalUseItem.java create mode 100644 eco-api/src/main/java/com/willfp/eco/core/entities/ai/goals/entity/EntityGoalWaterAvoidingRandomFlying.java create mode 100644 eco-api/src/main/java/com/willfp/eco/core/entities/ai/goals/entity/EntityGoalWaterAvoidingRandomStroll.java diff --git a/eco-api/src/main/java/com/willfp/eco/core/entities/ai/goals/entity/EntityGoalRestrictSun.java b/eco-api/src/main/java/com/willfp/eco/core/entities/ai/goals/entity/EntityGoalRestrictSun.java new file mode 100644 index 00000000..0b725f6d --- /dev/null +++ b/eco-api/src/main/java/com/willfp/eco/core/entities/ai/goals/entity/EntityGoalRestrictSun.java @@ -0,0 +1,11 @@ +package com.willfp.eco.core.entities.ai.goals.entity; + +import com.willfp.eco.core.entities.ai.goals.EntityGoal; + +/** + * Restrict sun. + */ +public record EntityGoalRestrictSun( +) implements EntityGoal { + +} diff --git a/eco-api/src/main/java/com/willfp/eco/core/entities/ai/goals/entity/EntityGoalStrollThroughVillage.java b/eco-api/src/main/java/com/willfp/eco/core/entities/ai/goals/entity/EntityGoalStrollThroughVillage.java new file mode 100644 index 00000000..f0f7ef5f --- /dev/null +++ b/eco-api/src/main/java/com/willfp/eco/core/entities/ai/goals/entity/EntityGoalStrollThroughVillage.java @@ -0,0 +1,14 @@ +package com.willfp.eco.core.entities.ai.goals.entity; + +import com.willfp.eco.core.entities.ai.goals.EntityGoal; + +/** + * Stroll through village. + * + * @param searchRange The search range. + */ +public record EntityGoalStrollThroughVillage( + int searchRange +) implements EntityGoal { + +} diff --git a/eco-api/src/main/java/com/willfp/eco/core/entities/ai/goals/entity/EntityGoalTempt.java b/eco-api/src/main/java/com/willfp/eco/core/entities/ai/goals/entity/EntityGoalTempt.java new file mode 100644 index 00000000..5201eba2 --- /dev/null +++ b/eco-api/src/main/java/com/willfp/eco/core/entities/ai/goals/entity/EntityGoalTempt.java @@ -0,0 +1,21 @@ +package com.willfp.eco.core.entities.ai.goals.entity; + +import com.willfp.eco.core.entities.ai.goals.EntityGoal; +import org.bukkit.inventory.ItemStack; + +import java.util.Collection; + +/** + * Be tempted by holding items. + * + * @param speed The speed at which the entity follows the item. + * @param items The items that the entity will be attracted by. + * @param canBeScared If the entity can be scared and lose track of the item. + */ +public record EntityGoalTempt( + double speed, + Collection items, + boolean canBeScared +) implements EntityGoal { + +} diff --git a/eco-api/src/main/java/com/willfp/eco/core/entities/ai/goals/entity/EntityGoalTryFindWater.java b/eco-api/src/main/java/com/willfp/eco/core/entities/ai/goals/entity/EntityGoalTryFindWater.java new file mode 100644 index 00000000..432e1ec5 --- /dev/null +++ b/eco-api/src/main/java/com/willfp/eco/core/entities/ai/goals/entity/EntityGoalTryFindWater.java @@ -0,0 +1,11 @@ +package com.willfp.eco.core.entities.ai.goals.entity; + +import com.willfp.eco.core.entities.ai.goals.EntityGoal; + +/** + * Try to find water. + */ +public record EntityGoalTryFindWater( +) implements EntityGoal { + +} diff --git a/eco-api/src/main/java/com/willfp/eco/core/entities/ai/goals/entity/EntityGoalUseItem.java b/eco-api/src/main/java/com/willfp/eco/core/entities/ai/goals/entity/EntityGoalUseItem.java new file mode 100644 index 00000000..8a3b5306 --- /dev/null +++ b/eco-api/src/main/java/com/willfp/eco/core/entities/ai/goals/entity/EntityGoalUseItem.java @@ -0,0 +1,24 @@ +package com.willfp.eco.core.entities.ai.goals.entity; + +import com.willfp.eco.core.entities.ai.goals.EntityGoal; +import org.bukkit.Sound; +import org.bukkit.entity.LivingEntity; +import org.bukkit.inventory.ItemStack; +import org.jetbrains.annotations.NotNull; + +import java.util.function.Predicate; + +/** + * Use item. + * + * @param item The item. + * @param sound The sound to play on use. + * @param condition The condition when to use the item. + */ +public record EntityGoalUseItem( + @NotNull ItemStack item, + @NotNull Sound sound, + @NotNull Predicate condition +) implements EntityGoal { + +} diff --git a/eco-api/src/main/java/com/willfp/eco/core/entities/ai/goals/entity/EntityGoalWaterAvoidingRandomFlying.java b/eco-api/src/main/java/com/willfp/eco/core/entities/ai/goals/entity/EntityGoalWaterAvoidingRandomFlying.java new file mode 100644 index 00000000..d2bedc40 --- /dev/null +++ b/eco-api/src/main/java/com/willfp/eco/core/entities/ai/goals/entity/EntityGoalWaterAvoidingRandomFlying.java @@ -0,0 +1,14 @@ +package com.willfp.eco.core.entities.ai.goals.entity; + +import com.willfp.eco.core.entities.ai.goals.EntityGoal; + +/** + * Fly randomly while avoiding water. + * + * @param speed The speed. + */ +public record EntityGoalWaterAvoidingRandomFlying( + double speed +) implements EntityGoal { + +} diff --git a/eco-api/src/main/java/com/willfp/eco/core/entities/ai/goals/entity/EntityGoalWaterAvoidingRandomStroll.java b/eco-api/src/main/java/com/willfp/eco/core/entities/ai/goals/entity/EntityGoalWaterAvoidingRandomStroll.java new file mode 100644 index 00000000..372a47dc --- /dev/null +++ b/eco-api/src/main/java/com/willfp/eco/core/entities/ai/goals/entity/EntityGoalWaterAvoidingRandomStroll.java @@ -0,0 +1,16 @@ +package com.willfp.eco.core.entities.ai.goals.entity; + +import com.willfp.eco.core.entities.ai.goals.EntityGoal; + +/** + * Stroll randomly while avoiding water. + * + * @param speed The speed. + * @param chance The chance to stroll every tick, as a percentage. + */ +public record EntityGoalWaterAvoidingRandomStroll( + double speed, + double chance +) implements EntityGoal { + +} diff --git a/eco-core/core-nms/v1_17_R1/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/v1_17_R1/ai/EntityGoals.kt b/eco-core/core-nms/v1_17_R1/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/v1_17_R1/ai/EntityGoals.kt index f0692c7a..85d04f67 100644 --- a/eco-core/core-nms/v1_17_R1/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/v1_17_R1/ai/EntityGoals.kt +++ b/eco-core/core-nms/v1_17_R1/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/v1_17_R1/ai/EntityGoals.kt @@ -26,6 +26,14 @@ import com.willfp.eco.core.entities.ai.goals.entity.EntityGoalRandomSwimming import com.willfp.eco.core.entities.ai.goals.entity.EntityGoalRangedAttack import com.willfp.eco.core.entities.ai.goals.entity.EntityGoalRangedBowAttack import com.willfp.eco.core.entities.ai.goals.entity.EntityGoalRangedCrossbowAttack +import com.willfp.eco.core.entities.ai.goals.entity.EntityGoalRestrictSun +import com.willfp.eco.core.entities.ai.goals.entity.EntityGoalStrollThroughVillage +import com.willfp.eco.core.entities.ai.goals.entity.EntityGoalTempt +import com.willfp.eco.core.entities.ai.goals.entity.EntityGoalTryFindWater +import com.willfp.eco.core.entities.ai.goals.entity.EntityGoalUseItem +import com.willfp.eco.core.entities.ai.goals.entity.EntityGoalWaterAvoidingRandomFlying +import com.willfp.eco.core.entities.ai.goals.entity.EntityGoalWaterAvoidingRandomStroll +import net.minecraft.sounds.SoundEvent import net.minecraft.world.entity.PathfinderMob import net.minecraft.world.entity.ai.goal.AvoidEntityGoal import net.minecraft.world.entity.ai.goal.BreakDoorGoal @@ -53,8 +61,18 @@ import net.minecraft.world.entity.ai.goal.RandomSwimmingGoal import net.minecraft.world.entity.ai.goal.RangedAttackGoal import net.minecraft.world.entity.ai.goal.RangedBowAttackGoal import net.minecraft.world.entity.ai.goal.RangedCrossbowAttackGoal +import net.minecraft.world.entity.ai.goal.RestrictSunGoal +import net.minecraft.world.entity.ai.goal.StrollThroughVillageGoal +import net.minecraft.world.entity.ai.goal.TemptGoal +import net.minecraft.world.entity.ai.goal.TryFindWaterGoal +import net.minecraft.world.entity.ai.goal.UseItemGoal +import net.minecraft.world.entity.ai.goal.WaterAvoidingRandomFlyingGoal +import net.minecraft.world.entity.ai.goal.WaterAvoidingRandomStrollGoal import net.minecraft.world.entity.monster.RangedAttackMob import net.minecraft.world.entity.player.Player +import net.minecraft.world.item.crafting.Ingredient +import org.bukkit.craftbukkit.v1_17_R1.inventory.CraftItemStack +import org.bukkit.craftbukkit.v1_17_R1.util.CraftNamespacedKey fun T.getImplementation(): EcoEntityGoal { @Suppress("UNCHECKED_CAST") @@ -84,6 +102,13 @@ fun T.getImplementation(): EcoEntityGoal { is EntityGoalRangedAttack -> RangedAttackImpl is EntityGoalRangedBowAttack -> RangedBowAttackImpl is EntityGoalRangedCrossbowAttack -> RangedCrossbowAttackImpl + is EntityGoalRestrictSun -> RestrictSunImpl + is EntityGoalStrollThroughVillage -> StrollThroughVillageImpl + is EntityGoalTempt -> TemptImpl + is EntityGoalTryFindWater -> TryFindWaterImpl + is EntityGoalUseItem -> UseItemImpl + is EntityGoalWaterAvoidingRandomFlying -> WaterAvoidingRandomFlyingImpl + is EntityGoalWaterAvoidingRandomStroll -> WaterAvoidingRandomStrollImpl else -> throw IllegalArgumentException("Unknown API goal!") } as EcoEntityGoal } @@ -335,3 +360,70 @@ object RangedCrossbowAttackImpl : EcoEntityGoal } } +object RestrictSunImpl : EcoEntityGoal { + override fun generateNMSGoal(apiGoal: EntityGoalRestrictSun, entity: PathfinderMob): Goal { + return RestrictSunGoal( + entity + ) + } +} + +object StrollThroughVillageImpl : EcoEntityGoal { + override fun generateNMSGoal(apiGoal: EntityGoalStrollThroughVillage, entity: PathfinderMob): Goal { + return StrollThroughVillageGoal( + entity, + apiGoal.searchRange + ) + } +} + +object TemptImpl : EcoEntityGoal { + override fun generateNMSGoal(apiGoal: EntityGoalTempt, entity: PathfinderMob): Goal { + return TemptGoal( + entity, + apiGoal.speed, + Ingredient.of(*apiGoal.items.map { CraftItemStack.asNMSCopy(it) }.toTypedArray()), + apiGoal.canBeScared + ) + } +} + +object TryFindWaterImpl : EcoEntityGoal { + override fun generateNMSGoal(apiGoal: EntityGoalTryFindWater, entity: PathfinderMob): Goal { + return TryFindWaterGoal( + entity + ) + } +} + +object UseItemImpl : EcoEntityGoal { + override fun generateNMSGoal(apiGoal: EntityGoalUseItem, entity: PathfinderMob): Goal { + return UseItemGoal( + entity, + CraftItemStack.asNMSCopy(apiGoal.item), + SoundEvent(CraftNamespacedKey.toMinecraft(apiGoal.sound.key)), + ) { + apiGoal.condition.test(it.toBukkitEntity()) + } + } +} + +object WaterAvoidingRandomFlyingImpl : EcoEntityGoal { + override fun generateNMSGoal(apiGoal: EntityGoalWaterAvoidingRandomFlying, entity: PathfinderMob): Goal { + return WaterAvoidingRandomFlyingGoal( + entity, + apiGoal.speed + ) + } +} + +object WaterAvoidingRandomStrollImpl : EcoEntityGoal { + override fun generateNMSGoal(apiGoal: EntityGoalWaterAvoidingRandomStroll, entity: PathfinderMob): Goal { + return WaterAvoidingRandomStrollGoal( + entity, + apiGoal.speed, + apiGoal.chance.toFloat() / 100 + ) + } +} +