Skip to content

Commit

Permalink
Fixed nearest_attackable target goal
Browse files Browse the repository at this point in the history
  • Loading branch information
WillFP committed Sep 2, 2024
1 parent 4e6960f commit 5b9fffe
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,22 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;
import java.util.Set;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
* Allows an entity to attack the closest target within a given subset of specific target types.
*
* @param target The type of entities to attack.
* @param targets The type of entities to attack.
* @param checkVisibility If visibility should be checked.
* @param checkCanNavigate If navigation should be checked.
* @param reciprocalChance 1 in reciprocalChance chance of not activating on any tick.
* @param targetFilter The filter for targets to match.
*/
public record TargetGoalNearestAttackable(
@NotNull TestableEntity target,
@NotNull Set<TestableEntity> targets,
boolean checkVisibility,
boolean checkCanNavigate,
int reciprocalChance,
Expand All @@ -32,16 +35,16 @@ public record TargetGoalNearestAttackable(
/**
* Create a new target goal.
*
* @param target The type of entities to attack.
* @param targets The type of entities to attack.
* @param checkVisibility If visibility should be checked.
* @param checkCanNavigate If navigation should be checked.
* @param reciprocalChance 1 in reciprocalChance chance of not activating on any tick.
*/
public TargetGoalNearestAttackable(@NotNull final TestableEntity target,
public TargetGoalNearestAttackable(@NotNull final Set<TestableEntity> targets,
final boolean checkVisibility,
final boolean checkCanNavigate,
final int reciprocalChance) {
this(target, checkVisibility, checkCanNavigate, reciprocalChance, it -> true);
this(targets, checkVisibility, checkCanNavigate, reciprocalChance, it -> true);
}

/**
Expand All @@ -65,19 +68,23 @@ public TargetGoalNearestAttackable deserialize(@NotNull final Config config) {
return null;
}

Set<TestableEntity> targets = config.getStrings("target").stream()
.map(Entities::lookup)
.collect(Collectors.toSet());

if (config.has("targetFilter")) {
TestableEntity filter = Entities.lookup(config.getString("targetFilter"));

return new TargetGoalNearestAttackable(
Entities.lookup(config.getString("target")),
targets,
config.getBool("checkVisibility"),
config.getBool("checkCanNavigate"),
config.getInt("reciprocalChance"),
filter::matches
);
} else {
return new TargetGoalNearestAttackable(
Entities.lookup(config.getString("target")),
targets,
config.getBool("checkVisibility"),
config.getBool("checkCanNavigate"),
config.getInt("reciprocalChance")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
package com.willfp.eco.core.entities

import com.willfp.eco.core.entities.ai.EntityController
import com.willfp.eco.core.items.Items
import com.willfp.eco.core.items.TestableItem
import org.bukkit.entity.Mob
import org.bukkit.inventory.ItemStack

/** @see EntityController.getFor */
val <T : Mob> T.controller: EntityController<T>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ class EcoEntityController<T : Mob>(
priority, goal.getGoalFactory()?.create(goal, nms) ?: return this
)

nms.targetSelector

return this
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.willfp.eco.internal.spigot.proxy.common.ai.target

import com.willfp.eco.core.entities.ai.target.TargetGoalNearestAttackable
import com.willfp.eco.core.lookup.matches
import com.willfp.eco.internal.spigot.proxy.common.ai.TargetGoalFactory
import com.willfp.eco.internal.spigot.proxy.common.toBukkitEntity
import net.minecraft.world.entity.LivingEntity
Expand All @@ -17,7 +18,9 @@ object NearestAttackableGoalFactory : TargetGoalFactory<TargetGoalNearestAttacka
apiGoal.checkVisibility,
apiGoal.checkCanNavigate,
) {
apiGoal.targetFilter.test(it.toBukkitEntity()) && apiGoal.target.matches(it.toBukkitEntity())
val bukkit = it.toBukkitEntity()

apiGoal.targetFilter.test(bukkit) && apiGoal.targets.any { t -> t.matches(bukkit) }
}
}

Expand Down

0 comments on commit 5b9fffe

Please sign in to comment.