Added remaining entity goals

This commit is contained in:
Auxilor
2022-03-01 20:40:45 +00:00
parent e09e9b4e8d
commit 2c51a6900f
8 changed files with 203 additions and 0 deletions

View File

@@ -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 {
}

View File

@@ -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 {
}

View File

@@ -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<ItemStack> items,
boolean canBeScared
) implements EntityGoal {
}

View File

@@ -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 {
}

View File

@@ -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<LivingEntity> condition
) implements EntityGoal {
}

View File

@@ -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 {
}

View File

@@ -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 {
}

View File

@@ -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 : EntityGoal> T.getImplementation(): EcoEntityGoal<T> {
@Suppress("UNCHECKED_CAST")
@@ -84,6 +102,13 @@ fun <T : EntityGoal> T.getImplementation(): EcoEntityGoal<T> {
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<T>
}
@@ -335,3 +360,70 @@ object RangedCrossbowAttackImpl : EcoEntityGoal<EntityGoalRangedCrossbowAttack>
}
}
object RestrictSunImpl : EcoEntityGoal<EntityGoalRestrictSun> {
override fun generateNMSGoal(apiGoal: EntityGoalRestrictSun, entity: PathfinderMob): Goal {
return RestrictSunGoal(
entity
)
}
}
object StrollThroughVillageImpl : EcoEntityGoal<EntityGoalStrollThroughVillage> {
override fun generateNMSGoal(apiGoal: EntityGoalStrollThroughVillage, entity: PathfinderMob): Goal {
return StrollThroughVillageGoal(
entity,
apiGoal.searchRange
)
}
}
object TemptImpl : EcoEntityGoal<EntityGoalTempt> {
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<EntityGoalTryFindWater> {
override fun generateNMSGoal(apiGoal: EntityGoalTryFindWater, entity: PathfinderMob): Goal {
return TryFindWaterGoal(
entity
)
}
}
object UseItemImpl : EcoEntityGoal<EntityGoalUseItem> {
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<EntityGoalWaterAvoidingRandomFlying> {
override fun generateNMSGoal(apiGoal: EntityGoalWaterAvoidingRandomFlying, entity: PathfinderMob): Goal {
return WaterAvoidingRandomFlyingGoal(
entity,
apiGoal.speed
)
}
}
object WaterAvoidingRandomStrollImpl : EcoEntityGoal<EntityGoalWaterAvoidingRandomStroll> {
override fun generateNMSGoal(apiGoal: EntityGoalWaterAvoidingRandomStroll, entity: PathfinderMob): Goal {
return WaterAvoidingRandomStrollGoal(
entity,
apiGoal.speed,
apiGoal.chance.toFloat() / 100
)
}
}