9
0
mirror of https://github.com/Winds-Studio/Leaf.git synced 2026-01-04 15:41:40 +00:00

readd some fixes in async target finding

This commit is contained in:
hayanesuru
2025-06-22 01:50:49 +09:00
parent 5ef030ab1e
commit cb21caa1c2

View File

@@ -581,7 +581,7 @@ index 3093f03d4f298bf39fec8bad2b6c22518774aea8..4eec8eda6ea1698529e0392bc75c07be
} else {
this.parent = animal;
diff --git a/net/minecraft/world/entity/ai/goal/GoalSelector.java b/net/minecraft/world/entity/ai/goal/GoalSelector.java
index e82e32407cec6109b9c3b0106295217f4a3f4aa2..0998aa502b5a361558f1812bf93c2e01c8bb0ba8 100644
index e82e32407cec6109b9c3b0106295217f4a3f4aa2..127609e53f566413f93b983719604858802d8a6d 100644
--- a/net/minecraft/world/entity/ai/goal/GoalSelector.java
+++ b/net/minecraft/world/entity/ai/goal/GoalSelector.java
@@ -26,13 +26,23 @@ public class GoalSelector {
@@ -767,7 +767,7 @@ index e82e32407cec6109b9c3b0106295217f4a3f4aa2..0998aa502b5a361558f1812bf93c2e01
for (WrappedGoal wrappedGoal : this.availableGoals) {
if (wrappedGoal.isRunning() && (goalContainsAnyFlags(wrappedGoal, this.goalTypes) || !wrappedGoal.canContinueToUse())) { // Paper - Perf: optimize goal types by removing streams
wrappedGoal.stop();
@@ -116,6 +256,18 @@ public class GoalSelector {
@@ -116,6 +256,24 @@ public class GoalSelector {
}
public void tickRunningGoals(boolean tickAllRunning) {
@@ -779,6 +779,12 @@ index e82e32407cec6109b9c3b0106295217f4a3f4aa2..0998aa502b5a361558f1812bf93c2e01
+ availableGoalsDirty = false;
+ }
+ ctxState = tickAllRunning ? 2 : 3;
+ } else {
+ for (WrappedGoal wrappedGoal : java.util.Objects.requireNonNull(this.ctxGoals)) {
+ if (wrappedGoal.isRunning() && (tickAllRunning || wrappedGoal.requiresUpdateEveryTick())) {
+ wrappedGoal.tick();
+ }
+ }
+ }
+ return;
+ }
@@ -787,7 +793,7 @@ index e82e32407cec6109b9c3b0106295217f4a3f4aa2..0998aa502b5a361558f1812bf93c2e01
if (wrappedGoal.isRunning() && (tickAllRunning || wrappedGoal.requiresUpdateEveryTick())) {
wrappedGoal.tick();
diff --git a/net/minecraft/world/entity/ai/goal/LlamaFollowCaravanGoal.java b/net/minecraft/world/entity/ai/goal/LlamaFollowCaravanGoal.java
index be59d0c27a83b329ec3f97c029cfb9c114e22472..888fd1919954acf3ec3c5d5a115dffc2e0fd3caf 100644
index be59d0c27a83b329ec3f97c029cfb9c114e22472..83a0e5ffad44a628b60a6a2cc136559b0a0377f3 100644
--- a/net/minecraft/world/entity/ai/goal/LlamaFollowCaravanGoal.java
+++ b/net/minecraft/world/entity/ai/goal/LlamaFollowCaravanGoal.java
@@ -20,20 +20,83 @@ public class LlamaFollowCaravanGoal extends Goal {
@@ -852,7 +858,7 @@ index be59d0c27a83b329ec3f97c029cfb9c114e22472..888fd1919954acf3ec3c5d5a115dffc2
+ // Leaf start - Async target finding
+ Llama llama = poll();
+ double d = Double.MAX_VALUE;
+ if (org.dreeam.leaf.config.modules.async.AsyncTargetFinding.enabled) {
+ if (org.dreeam.leaf.config.modules.async.AsyncTargetFinding.searchEntity) {
+ if (llama == null) {
+ findTargetAsync();
+ return false;
@@ -997,10 +1003,10 @@ index 6463c3c9b08d6058f2843c225b08a40fc30a960b..126bd98bc5980a2f1177bd7c74918b86
@Override
public boolean canContinueToUse() {
diff --git a/net/minecraft/world/entity/ai/goal/MoveToBlockGoal.java b/net/minecraft/world/entity/ai/goal/MoveToBlockGoal.java
index 3f080b15543bf8c5fa0774b62d7f12e13b82511a..efd999c235271ac6b0935933db939cad51691a42 100644
index 3f080b15543bf8c5fa0774b62d7f12e13b82511a..89851bac25a4a1d3c499ba379bdadd357e079ea8 100644
--- a/net/minecraft/world/entity/ai/goal/MoveToBlockGoal.java
+++ b/net/minecraft/world/entity/ai/goal/MoveToBlockGoal.java
@@ -41,8 +41,60 @@ public abstract class MoveToBlockGoal extends Goal {
@@ -41,14 +41,73 @@ public abstract class MoveToBlockGoal extends Goal {
this.setFlags(EnumSet.of(Goal.Flag.MOVE, Goal.Flag.JUMP));
}
@@ -1017,10 +1023,15 @@ index 3f080b15543bf8c5fa0774b62d7f12e13b82511a..efd999c235271ac6b0935933db939cad
+ return true;
+ }
+
+ protected void getBlockAsync() {
+ protected boolean findNearestBlockAsync() {
+ // Leaf start - Async target finding
+ if (!org.dreeam.leaf.config.modules.async.AsyncTargetFinding.searchBlock) {
+ return findNearestBlock();
+ }
+ // Leaf end - Async target finding
+ final var mob = this.mob;
+ final var ctx = mob.getGoalCtx();
+ if (!ctx.state) return;
+ if (!ctx.state) return false;
+ final var serverLevel = getServerLevel(mob);
+ final TypeToCheck ty = this.typeToCheck();
+ final net.minecraft.world.level.block.Block toRemove;
@@ -1036,6 +1047,7 @@ index 3f080b15543bf8c5fa0774b62d7f12e13b82511a..efd999c235271ac6b0935933db939cad
+ final float restrictRadius = mob.getRestrictRadius();
+ final BlockPos restrictCenter = mob.getRestrictCenter();
+ ctx.wake = () -> findNearestBlockAsync(ty, toRemove, mob, serverLevel, verticalSearchStart, searchRange, verticalSearchRange, blockPos, restrictRadius, restrictCenter);
+ return false;
+ }
+
+ protected enum TypeToCheck {
@@ -1048,6 +1060,7 @@ index 3f080b15543bf8c5fa0774b62d7f12e13b82511a..efd999c235271ac6b0935933db939cad
+ Strider,
+ TurtleToWater,
+ TurtleLay,
+ Unknown,
+ }
+ // Leaf end - Async target finding
+
@@ -1061,20 +1074,14 @@ index 3f080b15543bf8c5fa0774b62d7f12e13b82511a..efd999c235271ac6b0935933db939cad
if (this.nextStartTick > 0) {
this.nextStartTick--;
return false;
@@ -109,6 +161,12 @@ public abstract class MoveToBlockGoal extends Goal {
} else {
this.nextStartTick = this.nextStartTick(this.mob);
- return this.findNearestBlock();
+ return this.findNearestBlockAsync(); // Leaf - Async target finding
}
}
protected boolean findNearestBlock() {
+ // Leaf start - Async target finding
+ if (org.dreeam.leaf.config.modules.async.AsyncTargetFinding.searchBlock) {
+ getBlockAsync();
+ return false;
+ }
+ // Leaf end - Async target finding
int i = this.searchRange;
int i1 = this.verticalSearchRange;
BlockPos blockPos = this.mob.blockPosition();
@@ -133,5 +191,105 @@ public abstract class MoveToBlockGoal extends Goal {
@@ -133,5 +192,108 @@ public abstract class MoveToBlockGoal extends Goal {
return false;
}
@@ -1117,7 +1124,9 @@ index 3f080b15543bf8c5fa0774b62d7f12e13b82511a..efd999c235271ac6b0935933db939cad
protected abstract boolean isValidTarget(LevelReader level, BlockPos pos);
+
+ // Leaf start - Async target finding
+ protected abstract TypeToCheck typeToCheck();
+ protected TypeToCheck typeToCheck() {
+ return TypeToCheck.Unknown;
+ }
+
+ private static boolean isValidTargetAsync(
+ TypeToCheck type,
@@ -1175,6 +1184,7 @@ index 3f080b15543bf8c5fa0774b62d7f12e13b82511a..efd999c235271ac6b0935933db939cad
+ case TurtleLay -> {
+ return level.isEmptyBlock(pos.above()) && net.minecraft.world.level.block.TurtleEggBlock.isSand(level, pos);
+ }
+ case Unknown -> throw new IllegalStateException();
+ case null -> throw new IllegalStateException();
+ }
+ // Leaf end - Async target finding
@@ -1244,10 +1254,10 @@ index 4ba5f7da27f7f6842790c0093bc0f25e138fa982..d0e3e030bc5eacc304c523afcabd193e
public boolean canContinueToUse() {
return this.tick > 0;
diff --git a/net/minecraft/world/entity/ai/goal/RemoveBlockGoal.java b/net/minecraft/world/entity/ai/goal/RemoveBlockGoal.java
index 16ec032d84f128fc44a836843fafef303f52b699..4fe7961d031af0b2aa6b79d9f104ceb883edf301 100644
index 16ec032d84f128fc44a836843fafef303f52b699..e3bb4c5850e25405a243aaf57e2e8b1c64381d1d 100644
--- a/net/minecraft/world/entity/ai/goal/RemoveBlockGoal.java
+++ b/net/minecraft/world/entity/ai/goal/RemoveBlockGoal.java
@@ -37,7 +37,14 @@ public class RemoveBlockGoal extends MoveToBlockGoal {
@@ -37,10 +37,17 @@ public class RemoveBlockGoal extends MoveToBlockGoal {
public boolean canUse() {
if (!getServerLevel(this.removerMob).getGameRules().getBoolean(GameRules.RULE_MOBGRIEFING, getServerLevel(this.removerMob).purpurConfig.zombieMobGriefingOverride)) { // Purpur - Add mobGriefing override to everything affected
return false;
@@ -1262,7 +1272,11 @@ index 16ec032d84f128fc44a836843fafef303f52b699..4fe7961d031af0b2aa6b79d9f104ceb8
+ if (this.nextStartTick > 0) {
this.nextStartTick--;
return false;
} else if (this.findNearestBlock()) {
- } else if (this.findNearestBlock()) {
+ } else if (this.findNearestBlockAsync()) { // Leaf - Async target finding
this.nextStartTick = reducedTickDelay(20);
return true;
} else {
@@ -151,8 +158,15 @@ public class RemoveBlockGoal extends MoveToBlockGoal {
protected boolean isValidTarget(LevelReader level, BlockPos pos) {
ChunkAccess chunk = level.getChunkIfLoadedImmediately(pos.getX() >> 4, pos.getZ() >> 4); // Paper - Prevent AI rules from loading chunks
@@ -1281,10 +1295,18 @@ index 16ec032d84f128fc44a836843fafef303f52b699..4fe7961d031af0b2aa6b79d9f104ceb8
+ // Leaf end - Async target finding
}
diff --git a/net/minecraft/world/entity/ai/goal/TemptGoal.java b/net/minecraft/world/entity/ai/goal/TemptGoal.java
index f88f618d34fb343b31de3af1a875d6633703df71..4bbd83c702a818b48313698919dc9a85392707f6 100644
index f88f618d34fb343b31de3af1a875d6633703df71..dc90441edfa1cdd5c5e218811b0dc595e63f4e9c 100644
--- a/net/minecraft/world/entity/ai/goal/TemptGoal.java
+++ b/net/minecraft/world/entity/ai/goal/TemptGoal.java
@@ -36,12 +36,51 @@ public class TemptGoal extends Goal {
@@ -7,6 +7,7 @@ import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.PathfinderMob;
import net.minecraft.world.entity.ai.attributes.Attributes;
import net.minecraft.world.entity.ai.targeting.TargetingConditions;
+import net.minecraft.world.entity.animal.horse.Llama;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
@@ -36,14 +37,43 @@ public class TemptGoal extends Goal {
this.targetingConditions = TEMPT_TARGETING.copy().selector((entity, level) -> this.shouldFollow(entity));
}
@@ -1314,33 +1336,27 @@ index f88f618d34fb343b31de3af1a875d6633703df71..4bbd83c702a818b48313698919dc9a85
this.calmDown--;
return false;
} else {
- this.player = getServerLevel(this.mob)
- .getNearestPlayer(this.targetingConditions.range(this.mob.getAttributeValue(Attributes.TEMPT_RANGE)), this.mob);
+ // Leaf start - Async target finding
+ if (org.dreeam.leaf.config.modules.async.AsyncTargetFinding.searchEntity) {
+ if (poll()) {
+ if (this.player != null) {
+ org.bukkit.event.entity.EntityTargetLivingEntityEvent event = org.bukkit.craftbukkit.event.CraftEventFactory.callEntityTargetLivingEvent(this.mob, this.player, org.bukkit.event.entity.EntityTargetEvent.TargetReason.TEMPT);
+ if (event.isCancelled()) {
+ return false;
+ }
+ this.player = (event.getTarget() == null) ? null : ((org.bukkit.craftbukkit.entity.CraftLivingEntity) event.getTarget()).getHandle();
+ }
+ if (this.player != null) {
+ return true;
+ }
+ } else {
+ if (!poll()) {
+ getNearestPlayerAsync();
+ return false;
+ }
+ } else {
+ this.player = getServerLevel(this.mob)
+ .getNearestPlayer(this.targetingConditions.range(this.mob.getAttributeValue(Attributes.TEMPT_RANGE)), this.mob);
+ }
+ // Leaf end - Async target finding
this.player = getServerLevel(this.mob)
.getNearestPlayer(this.targetingConditions.range(this.mob.getAttributeValue(Attributes.TEMPT_RANGE)), this.mob);
// CraftBukkit start
if (this.player != null) {
org.bukkit.event.entity.EntityTargetLivingEntityEvent event = org.bukkit.craftbukkit.event.CraftEventFactory.callEntityTargetLivingEvent(this.mob, this.player, org.bukkit.event.entity.EntityTargetEvent.TargetReason.TEMPT);
diff --git a/net/minecraft/world/entity/ai/goal/target/DefendVillageTargetGoal.java b/net/minecraft/world/entity/ai/goal/target/DefendVillageTargetGoal.java
index fb160a59c873d5c3f2c3d31966ca1a653f1b384d..9054eb373de510f7949f7070cfe69ad2fe25ea3b 100644
index fb160a59c873d5c3f2c3d31966ca1a653f1b384d..96ccafe979c5c15e26c12f6ca98ad1e81537bdce 100644
--- a/net/minecraft/world/entity/ai/goal/target/DefendVillageTargetGoal.java
+++ b/net/minecraft/world/entity/ai/goal/target/DefendVillageTargetGoal.java
@@ -24,8 +24,49 @@ public class DefendVillageTargetGoal extends TargetGoal {
@@ -24,13 +24,52 @@ public class DefendVillageTargetGoal extends TargetGoal {
this.setFlags(EnumSet.of(Goal.Flag.TARGET));
}
@@ -1349,7 +1365,6 @@ index fb160a59c873d5c3f2c3d31966ca1a653f1b384d..9054eb373de510f7949f7070cfe69ad2
+ if (!(this.mob.getGoalCtx().result() instanceof LivingEntity target)) return false;
+ ServerLevel serverLevel = getServerLevel(this.mob);
+ if (serverLevel == null || !target.isAlive() || !attackTargeting.test(serverLevel, golem, target)) return false;
+ if ((target instanceof Player player && (player.isSpectator() || player.isCreative()))) return false;
+ this.potentialTarget = target;
+ return true;
+ }
@@ -1379,26 +1394,30 @@ index fb160a59c873d5c3f2c3d31966ca1a653f1b384d..9054eb373de510f7949f7070cfe69ad2
@Override
public boolean canUse() {
+ // Leaf start - Async target finding
+ if (poll()) {
+ return true;
+ }
+ if (org.dreeam.leaf.config.modules.async.AsyncTargetFinding.searchEntity) {
+ this.findTargetAsync();
+ return false;
+ }
+ // Leaf end - Async target finding
+ if (!poll()) {
+ this.findTargetAsync();
+ return false;
+ }
+ } else {
AABB aabb = this.golem.getBoundingBox().inflate(10.0, 8.0, 10.0);
ServerLevel serverLevel = getServerLevel(this.golem);
List<? extends LivingEntity> nearbyEntities = serverLevel.getNearbyEntities(Villager.class, this.attackTargeting, this.golem, aabb);
@@ -42,7 +83,7 @@ public class DefendVillageTargetGoal extends TargetGoal {
List<Player> nearbyPlayers = serverLevel.getNearbyPlayers(this.attackTargeting, this.golem, aabb);
+ // Async target finding - diff
for (LivingEntity livingEntity : nearbyEntities) {
Villager villager = (Villager)livingEntity;
@@ -41,6 +80,8 @@ public class DefendVillageTargetGoal extends TargetGoal {
}
}
}
+ }
+ // Leaf end - Async target finding
- return this.potentialTarget != null && !(this.potentialTarget instanceof Player player1 && (player1.isSpectator() || player1.isCreative()));
+ return this.potentialTarget != null && !(this.potentialTarget instanceof Player player1 && (player1.isSpectator() || player1.isCreative())); // Leaf - Async target finding - diff on change
return this.potentialTarget != null && !(this.potentialTarget instanceof Player player1 && (player1.isSpectator() || player1.isCreative()));
}
@Override
diff --git a/net/minecraft/world/entity/ai/goal/target/HurtByTargetGoal.java b/net/minecraft/world/entity/ai/goal/target/HurtByTargetGoal.java
index a8ec1d5f4b0fb0ff26a234235b7d8d9c4b4a2a98..412fc8e049ba3763314ec2a56dce378cb0e4cc5f 100644
--- a/net/minecraft/world/entity/ai/goal/target/HurtByTargetGoal.java
@@ -1506,10 +1525,10 @@ index a8ec1d5f4b0fb0ff26a234235b7d8d9c4b4a2a98..412fc8e049ba3763314ec2a56dce378c
mob.setTarget(target, org.bukkit.event.entity.EntityTargetEvent.TargetReason.TARGET_ATTACKED_NEARBY_ENTITY); // CraftBukkit - reason
}
diff --git a/net/minecraft/world/entity/ai/goal/target/NearestAttackableTargetGoal.java b/net/minecraft/world/entity/ai/goal/target/NearestAttackableTargetGoal.java
index 1cd8143c938237ce035fa866d4f2ed1e2cd1ebca..7fac18f9d41030f0e9a566fd1923ea79edde32b6 100644
index 1cd8143c938237ce035fa866d4f2ed1e2cd1ebca..d44aec876818421eb8a54007e2bdabd7d354f22b 100644
--- a/net/minecraft/world/entity/ai/goal/target/NearestAttackableTargetGoal.java
+++ b/net/minecraft/world/entity/ai/goal/target/NearestAttackableTargetGoal.java
@@ -41,8 +41,43 @@ public class NearestAttackableTargetGoal<T extends LivingEntity> extends TargetG
@@ -41,12 +41,52 @@ public class NearestAttackableTargetGoal<T extends LivingEntity> extends TargetG
this.targetConditions = TargetingConditions.forCombat().range(this.getFollowDistance()).selector(selector);
}
@@ -1522,7 +1541,12 @@ index 1cd8143c938237ce035fa866d4f2ed1e2cd1ebca..7fac18f9d41030f0e9a566fd1923ea79
+ return true;
+ }
+
+ private void findTargetAsync() {
+ protected void findTargetAsync() {
+ if (!org.dreeam.leaf.config.modules.async.AsyncTargetFinding.searchEntity) {
+ findTarget();
+ return;
+ }
+ this.target = null;
+ final Mob mob = this.mob;
+ final var ctx = mob.getGoalCtx();
+ if (!ctx.state) return;
@@ -1553,27 +1577,24 @@ index 1cd8143c938237ce035fa866d4f2ed1e2cd1ebca..7fac18f9d41030f0e9a566fd1923ea79
if (this.randomInterval > 0 && this.mob.getRandom().nextInt(this.randomInterval) != 0) {
return false;
} else {
@@ -57,6 +92,15 @@ public class NearestAttackableTargetGoal<T extends LivingEntity> extends TargetG
- this.findTarget();
+ this.findTargetAsync(); // Async target finding
return this.target != null;
}
}
@@ -57,6 +97,7 @@ public class NearestAttackableTargetGoal<T extends LivingEntity> extends TargetG
protected void findTarget() {
ServerLevel serverLevel = getServerLevel(this.mob);
+
+ // Leaf start - Async target finding
+ if (org.dreeam.leaf.config.modules.async.AsyncTargetFinding.searchEntity) {
+ this.findTargetAsync();
+ this.target = null;
+ return;
+ }
+ // Leaf end - Async target finding
+
if (this.targetType != Player.class && this.targetType != ServerPlayer.class) {
this.target = serverLevel.getNearestEntity(
this.mob.level().getEntitiesOfClass(this.targetType, this.getTargetSearchArea(this.getFollowDistance()), entity -> true),
diff --git a/net/minecraft/world/entity/ai/goal/target/NearestHealableRaiderTargetGoal.java b/net/minecraft/world/entity/ai/goal/target/NearestHealableRaiderTargetGoal.java
index 4604a603c4ddd0a9242e859aaa5a511c2d4c4f84..7274dfb52ca4a08cdebcd04294cedc73460593e5 100644
index 4604a603c4ddd0a9242e859aaa5a511c2d4c4f84..84c7b89e7c894c0f544cf0ffcf9dff3f6a5919cc 100644
--- a/net/minecraft/world/entity/ai/goal/target/NearestHealableRaiderTargetGoal.java
+++ b/net/minecraft/world/entity/ai/goal/target/NearestHealableRaiderTargetGoal.java
@@ -23,7 +23,15 @@ public class NearestHealableRaiderTargetGoal<T extends LivingEntity> extends Nea
@@ -23,12 +23,20 @@ public class NearestHealableRaiderTargetGoal<T extends LivingEntity> extends Nea
@Override
public boolean canUse() {
@@ -1590,6 +1611,12 @@ index 4604a603c4ddd0a9242e859aaa5a511c2d4c4f84..7274dfb52ca4a08cdebcd04294cedc73
return false;
} else if (!((Raider)this.mob).hasActiveRaid()) {
return false;
} else {
- this.findTarget();
+ this.findTargetAsync(); // Leaf - Async target finding
return this.target != null;
}
}
diff --git a/net/minecraft/world/entity/ai/goal/target/NonTameRandomTargetGoal.java b/net/minecraft/world/entity/ai/goal/target/NonTameRandomTargetGoal.java
index abf57494950f55bbd75f335f26736cb9e703c197..efd2418f56c36e7850edde483a2a4906dd622441 100644
--- a/net/minecraft/world/entity/ai/goal/target/NonTameRandomTargetGoal.java