mirror of
https://github.com/Winds-Studio/Leaf.git
synced 2025-12-19 15:09:25 +00:00
2011 lines
96 KiB
Diff
2011 lines
96 KiB
Diff
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
|
From: Taiyou06 <kaandindar21@gmail.com>
|
|
Date: Sat, 29 Mar 2025 13:40:46 +0100
|
|
Subject: [PATCH] Async target finding
|
|
|
|
|
|
diff --git a/ca/spottedleaf/moonrise/patches/chunk_system/level/entity/ChunkEntitySlices.java b/ca/spottedleaf/moonrise/patches/chunk_system/level/entity/ChunkEntitySlices.java
|
|
index b6af8da084c83ee38bb3ecea6a98feb0c1c74d2a..561723eeb1dd80f7fdd9aff33f6e1c4c16def0ff 100644
|
|
--- a/ca/spottedleaf/moonrise/patches/chunk_system/level/entity/ChunkEntitySlices.java
|
|
+++ b/ca/spottedleaf/moonrise/patches/chunk_system/level/entity/ChunkEntitySlices.java
|
|
@@ -40,8 +40,10 @@ public final class ChunkEntitySlices {
|
|
|
|
private final EntityCollectionBySection allEntities;
|
|
private final EntityCollectionBySection hardCollidingEntities;
|
|
- private final Reference2ObjectOpenHashMap<Class<? extends Entity>, EntityCollectionBySection> entitiesByClass;
|
|
- private final Reference2ObjectOpenHashMap<EntityType<?>, EntityCollectionBySection> entitiesByType;
|
|
+ // Leaf start - Async target finding
|
|
+ private final Reference2ObjectMap<Class<? extends Entity>, EntityCollectionBySection> entitiesByClass;
|
|
+ private final Reference2ObjectMap<EntityType<?>, EntityCollectionBySection> entitiesByType;
|
|
+ // Leaf end - Async target finding
|
|
private final EntityList entities = new EntityList();
|
|
|
|
public FullChunkStatus status;
|
|
@@ -67,9 +69,15 @@ public final class ChunkEntitySlices {
|
|
|
|
this.allEntities = new EntityCollectionBySection(this);
|
|
this.hardCollidingEntities = new EntityCollectionBySection(this);
|
|
- this.entitiesByClass = new Reference2ObjectOpenHashMap<>();
|
|
- this.entitiesByType = new Reference2ObjectOpenHashMap<>();
|
|
-
|
|
+ // Leaf start - Async target finding
|
|
+ if (org.dreeam.leaf.config.modules.async.AsyncTargetFinding.enabled) {
|
|
+ this.entitiesByClass = it.unimi.dsi.fastutil.objects.Reference2ObjectMaps.synchronize(new Reference2ObjectOpenHashMap<>());
|
|
+ this.entitiesByType = it.unimi.dsi.fastutil.objects.Reference2ObjectMaps.synchronize(new Reference2ObjectOpenHashMap<>());
|
|
+ } else {
|
|
+ this.entitiesByClass = new Reference2ObjectOpenHashMap<>();
|
|
+ this.entitiesByType = new Reference2ObjectOpenHashMap<>();
|
|
+ }
|
|
+ // Leaf end - Async target finding
|
|
this.status = status;
|
|
this.chunkData = chunkData;
|
|
}
|
|
@@ -248,14 +256,26 @@ public final class ChunkEntitySlices {
|
|
this.hardCollidingEntities.addEntity(entity, sectionIndex);
|
|
}
|
|
|
|
- for (final Iterator<Reference2ObjectMap.Entry<Class<? extends Entity>, EntityCollectionBySection>> iterator =
|
|
- this.entitiesByClass.reference2ObjectEntrySet().fastIterator(); iterator.hasNext();) {
|
|
- final Reference2ObjectMap.Entry<Class<? extends Entity>, EntityCollectionBySection> entry = iterator.next();
|
|
+ // Leaf start - Async target finding
|
|
+ if (org.dreeam.leaf.config.modules.async.AsyncTargetFinding.enabled) {
|
|
+ synchronized (this.entitiesByClass) {
|
|
+ for (final var entry : this.entitiesByClass.reference2ObjectEntrySet()) {
|
|
+ if (entry.getKey().isInstance(entity)) {
|
|
+ entry.getValue().addEntity(entity, sectionIndex);
|
|
+ }
|
|
+ }
|
|
+ }
|
|
+ } else {
|
|
+ for (final Iterator<Reference2ObjectMap.Entry<Class<? extends Entity>, EntityCollectionBySection>> iterator =
|
|
+ this.entitiesByClass.reference2ObjectEntrySet().iterator(); iterator.hasNext(); ) {
|
|
+ final Reference2ObjectMap.Entry<Class<? extends Entity>, EntityCollectionBySection> entry = iterator.next();
|
|
|
|
- if (entry.getKey().isInstance(entity)) {
|
|
- entry.getValue().addEntity(entity, sectionIndex);
|
|
+ if (entry.getKey().isInstance(entity)) {
|
|
+ entry.getValue().addEntity(entity, sectionIndex);
|
|
+ }
|
|
}
|
|
}
|
|
+ // Leaf end - Async target finding
|
|
|
|
EntityCollectionBySection byType = this.entitiesByType.get(entity.getType());
|
|
if (byType != null) {
|
|
@@ -282,14 +302,27 @@ public final class ChunkEntitySlices {
|
|
this.hardCollidingEntities.removeEntity(entity, sectionIndex);
|
|
}
|
|
|
|
- for (final Iterator<Reference2ObjectMap.Entry<Class<? extends Entity>, EntityCollectionBySection>> iterator =
|
|
- this.entitiesByClass.reference2ObjectEntrySet().fastIterator(); iterator.hasNext();) {
|
|
- final Reference2ObjectMap.Entry<Class<? extends Entity>, EntityCollectionBySection> entry = iterator.next();
|
|
+ // Leaf start - Async target finding
|
|
+ if (org.dreeam.leaf.config.modules.async.AsyncTargetFinding.enabled) {
|
|
+ synchronized (this.entitiesByClass) {
|
|
+ for (final var entry : this.entitiesByClass.reference2ObjectEntrySet()) {
|
|
+ if (entry.getKey().isInstance(entity)) {
|
|
+ entry.getValue().removeEntity(entity, sectionIndex);
|
|
+ }
|
|
+ }
|
|
+ }
|
|
+ } else {
|
|
+ for (final Iterator<Reference2ObjectMap.Entry<Class<? extends Entity>, EntityCollectionBySection>> iterator =
|
|
+ this.entitiesByClass.reference2ObjectEntrySet().iterator(); iterator.hasNext();) {
|
|
+ final Reference2ObjectMap.Entry<Class<? extends Entity>, EntityCollectionBySection> entry = iterator.next();
|
|
|
|
- if (entry.getKey().isInstance(entity)) {
|
|
- entry.getValue().removeEntity(entity, sectionIndex);
|
|
+ if (entry.getKey().isInstance(entity)) {
|
|
+ entry.getValue().removeEntity(entity, sectionIndex);
|
|
+ }
|
|
}
|
|
}
|
|
+ // Leaf end - Async target finding
|
|
+
|
|
|
|
final EntityCollectionBySection byType = this.entitiesByType.get(entity.getType());
|
|
byType.removeEntity(entity, sectionIndex);
|
|
diff --git a/net/minecraft/server/MinecraftServer.java b/net/minecraft/server/MinecraftServer.java
|
|
index fd1de4607b64860332432c8781f60d9933065d95..f5f0e4f5d7a4b34514e102020d2a7be313292f7f 100644
|
|
--- a/net/minecraft/server/MinecraftServer.java
|
|
+++ b/net/minecraft/server/MinecraftServer.java
|
|
@@ -292,6 +292,7 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop<TickTa
|
|
public gg.pufferfish.pufferfish.util.AsyncExecutor mobSpawnExecutor = new gg.pufferfish.pufferfish.util.AsyncExecutor("Leaf Async Mob Spawn Thread"); // Pufferfish - optimize mob spawning // Leaf - Fix Pufferfish and Purpur patches - Unify thread name
|
|
public final Set<net.minecraft.world.entity.Entity> entitiesWithScheduledTasks = java.util.concurrent.ConcurrentHashMap.newKeySet(); // SparklyPaper - skip EntityScheduler's executeTick checks if there isn't any tasks to be run (concurrent because plugins may schedule tasks async)
|
|
public java.util.concurrent.Semaphore serverLevelTickingSemaphore = null; // SparklyPaper - parallel world ticking
|
|
+ @Nullable public org.dreeam.leaf.async.ai.AsyncGoalThread asyncGoalThread; // Leaf - Async target finding
|
|
|
|
public static <S extends MinecraftServer> S spin(Function<Thread, S> threadFunction) {
|
|
ca.spottedleaf.dataconverter.minecraft.datatypes.MCTypeRegistry.init(); // Paper - rewrite data converter system
|
|
diff --git a/net/minecraft/server/dedicated/DedicatedServer.java b/net/minecraft/server/dedicated/DedicatedServer.java
|
|
index 33dd16a26edd2974f04d9a868d3e58e8e3060032..eb0589b203bcf72cd24bb37f2c448c23cb8d6f2b 100644
|
|
--- a/net/minecraft/server/dedicated/DedicatedServer.java
|
|
+++ b/net/minecraft/server/dedicated/DedicatedServer.java
|
|
@@ -255,6 +255,11 @@ public class DedicatedServer extends MinecraftServer implements ServerInterface
|
|
DedicatedServer.LOGGER.info("Using " + serverLevelTickingSemaphore.availablePermits() + " permits for parallel world ticking"); // SparklyPaper - parallel world ticking
|
|
}
|
|
// Leaf end - SparklyPaper - parallel world ticking mod (make configurable)
|
|
+ // Leaf start - Async target finding
|
|
+ if (org.dreeam.leaf.config.modules.async.AsyncTargetFinding.enabled) {
|
|
+ asyncGoalThread = new org.dreeam.leaf.async.ai.AsyncGoalThread(this);
|
|
+ }
|
|
+ // Leaf end - Async target finding
|
|
com.destroystokyo.paper.VersionHistoryManager.INSTANCE.getClass(); // Paper - load version history now
|
|
|
|
// Gale start - Pufferfish - SIMD support
|
|
diff --git a/net/minecraft/server/level/ServerLevel.java b/net/minecraft/server/level/ServerLevel.java
|
|
index fe81b2acfb51ed3335bde6f27ecfc53e339d2c7e..53ea97c91684e9a0a3ed9f94ac15c242a43434ec 100644
|
|
--- a/net/minecraft/server/level/ServerLevel.java
|
|
+++ b/net/minecraft/server/level/ServerLevel.java
|
|
@@ -177,7 +177,16 @@ public class ServerLevel extends Level implements ServerEntityGetter, WorldGenLe
|
|
private static final Logger LOGGER = LogUtils.getLogger();
|
|
private static final int EMPTY_TIME_NO_TICK = 300;
|
|
private static final int MAX_SCHEDULED_TICKS_PER_TICK = 65536;
|
|
- final List<ServerPlayer> players = Lists.newArrayList();
|
|
+ // Leaf start - Async target finding
|
|
+ final List<ServerPlayer> players;
|
|
+ {
|
|
+ if (org.dreeam.leaf.config.modules.async.AsyncTargetFinding.enabled) {
|
|
+ this.players = Lists.newCopyOnWriteArrayList();
|
|
+ } else {
|
|
+ this.players = Lists.newArrayList();
|
|
+ }
|
|
+ }
|
|
+ // Leaf end - Async target finding
|
|
public final ServerChunkCache chunkSource;
|
|
private final MinecraftServer server;
|
|
public final net.minecraft.world.level.storage.PrimaryLevelData serverLevelData; // CraftBukkit - type
|
|
@@ -219,6 +228,7 @@ public class ServerLevel extends Level implements ServerEntityGetter, WorldGenLe
|
|
private final alternate.current.wire.WireHandler wireHandler = new alternate.current.wire.WireHandler(this); // Paper - optimize redstone (Alternate Current)
|
|
public boolean hasRidableMoveEvent = false; // Purpur - Ridables
|
|
final List<ServerPlayer> realPlayers; // Leaves - skip
|
|
+ public final @Nullable org.dreeam.leaf.async.ai.AsyncGoalExecutor asyncGoalExecutor; // Leaf - Async target finding
|
|
|
|
public LevelChunk getChunkIfLoaded(int x, int z) {
|
|
return this.chunkSource.getChunkAtIfLoadedImmediately(x, z); // Paper - Use getChunkIfLoadedImmediately
|
|
@@ -712,6 +722,13 @@ public class ServerLevel extends Level implements ServerEntityGetter, WorldGenLe
|
|
this.preciseTime = this.serverLevelData.getDayTime(); // Purpur - Configurable daylight cycle
|
|
this.realPlayers = Lists.newArrayList(); // Leaves - skip
|
|
this.tickExecutor = java.util.concurrent.Executors.newSingleThreadExecutor(new org.dreeam.leaf.async.world.SparklyPaperServerLevelTickExecutorThreadFactory(getWorld().getName())); // SparklyPaper - parallel world ticking
|
|
+ // Leaf start - Async target finding
|
|
+ if (org.dreeam.leaf.config.modules.async.AsyncTargetFinding.enabled) {
|
|
+ this.asyncGoalExecutor = new org.dreeam.leaf.async.ai.AsyncGoalExecutor(this);
|
|
+ } else {
|
|
+ this.asyncGoalExecutor = null;
|
|
+ }
|
|
+ // Leaf end - Async target finding
|
|
}
|
|
|
|
// Leaf start - SparklyPaper - parallel world ticking - Shutdown handling for async reads
|
|
diff --git a/net/minecraft/world/entity/Mob.java b/net/minecraft/world/entity/Mob.java
|
|
index 9bee3c8e13b1d5d66139ed793fcd9bb154987cd0..38b9931def2d407a3faa855db564e1d9ec1e636c 100644
|
|
--- a/net/minecraft/world/entity/Mob.java
|
|
+++ b/net/minecraft/world/entity/Mob.java
|
|
@@ -144,6 +144,12 @@ public abstract class Mob extends LivingEntity implements EquipmentUser, Leashab
|
|
private float restrictRadius = -1.0F;
|
|
public boolean aware = true; // CraftBukkit
|
|
public int ticksSinceLastInteraction; // Purpur - Entity lifespan
|
|
+ // Leaf start - Async target finding
|
|
+ public boolean tickingTarget;
|
|
+ public final org.dreeam.leaf.async.ai.Waker getGoalCtx() {
|
|
+ return tickingTarget ? this.targetSelector.ctx : this.goalSelector.ctx;
|
|
+ }
|
|
+ // Leaf end - Async target finding
|
|
|
|
protected Mob(EntityType<? extends Mob> entityType, Level level) {
|
|
super(entityType, level);
|
|
@@ -231,6 +237,11 @@ public abstract class Mob extends LivingEntity implements EquipmentUser, Leashab
|
|
if (this.targetSelector.inactiveTick(this.activatedPriority, true)) { // Pufferfish - pass activated priority
|
|
this.targetSelector.tick();
|
|
}
|
|
+ // Leaf start - Async target finding
|
|
+ if (org.dreeam.leaf.config.modules.async.AsyncTargetFinding.enabled) {
|
|
+ ((ServerLevel) this.level()).asyncGoalExecutor.tickMob(this);
|
|
+ }
|
|
+ // Leaf end - Async target finding
|
|
}
|
|
// Paper end
|
|
|
|
@@ -911,6 +922,11 @@ public abstract class Mob extends LivingEntity implements EquipmentUser, Leashab
|
|
if (this.goalSelector.inactiveTick(this.activatedPriority, false)) // Pufferfish - use this to alternate ticking
|
|
this.goalSelector.tick();
|
|
}
|
|
+ // Leaf start - Async target finding
|
|
+ if (org.dreeam.leaf.config.modules.async.AsyncTargetFinding.enabled) {
|
|
+ ((ServerLevel) this.level()).asyncGoalExecutor.tickMob(this);
|
|
+ }
|
|
+ // Leaf end - Async target finding
|
|
|
|
this.navigation.tick();
|
|
this.customServerAiStep((ServerLevel)this.level());
|
|
diff --git a/net/minecraft/world/entity/ai/goal/AvoidEntityGoal.java b/net/minecraft/world/entity/ai/goal/AvoidEntityGoal.java
|
|
index 4b24bb47a53a46586a642f3fb2656b1b8b670bf2..22f052d02ccf22f22894a0b236af1b95c8d65e97 100644
|
|
--- a/net/minecraft/world/entity/ai/goal/AvoidEntityGoal.java
|
|
+++ b/net/minecraft/world/entity/ai/goal/AvoidEntityGoal.java
|
|
@@ -67,15 +67,24 @@ public class AvoidEntityGoal<T extends LivingEntity> extends Goal {
|
|
|
|
@Override
|
|
public boolean canUse() {
|
|
+ // Leaf start - Async target finding
|
|
+ if (org.dreeam.leaf.config.modules.async.AsyncTargetFinding.searchEntity) {
|
|
+ if (!poll()) {
|
|
+ getNearestEntityAsync();
|
|
+ return false;
|
|
+ }
|
|
+ } else {
|
|
this.toAvoid = getServerLevel(this.mob)
|
|
.getNearestEntity(
|
|
this.mob.level().getEntitiesOfClass(this.avoidClass, this.mob.getBoundingBox().inflate(this.maxDist, 3.0, this.maxDist), livingEntity -> true),
|
|
this.avoidEntityTargeting,
|
|
this.mob,
|
|
this.mob.getX(),
|
|
- this.mob.getY(),
|
|
+ this.mob.getEyeY(), // Leaf - Async target finding
|
|
this.mob.getZ()
|
|
);
|
|
+ }
|
|
+ // Leaf end - Async target finding
|
|
if (this.toAvoid == null) {
|
|
return false;
|
|
} else {
|
|
@@ -91,6 +100,36 @@ public class AvoidEntityGoal<T extends LivingEntity> extends Goal {
|
|
}
|
|
}
|
|
|
|
+ // Leaf start - Async target finding
|
|
+ private boolean poll() {
|
|
+ if (!(this.mob.getGoalCtx().result() instanceof LivingEntity target)) return false;
|
|
+ var serverLevel = getServerLevel(this.mob);
|
|
+ if (serverLevel == null || !target.isAlive() || !this.avoidEntityTargeting.test(serverLevel, this.mob, target)) return false;
|
|
+ this.toAvoid = (T) target;
|
|
+ return true;
|
|
+ }
|
|
+
|
|
+ private void getNearestEntityAsync() {
|
|
+ final var mob = this.mob;
|
|
+ final var ctx = mob.getGoalCtx();
|
|
+ if (!ctx.state) return;
|
|
+ final double x = mob.getX();
|
|
+ final double y = mob.getEyeY();
|
|
+ final double z = mob.getZ();
|
|
+ final var serverLevel = getServerLevel(mob);
|
|
+ final var avoidClass = this.avoidClass;
|
|
+ final var bound = mob.getBoundingBox().inflate(this.maxDist, 3.0, this.maxDist);
|
|
+ ctx.wake = () -> serverLevel.getNearestEntity(
|
|
+ serverLevel.getEntitiesOfClass(avoidClass, bound, livingEntity -> true),
|
|
+ avoidEntityTargeting,
|
|
+ mob,
|
|
+ x,
|
|
+ y,
|
|
+ z
|
|
+ );
|
|
+ }
|
|
+ // Leaf end - Async target finding
|
|
+
|
|
@Override
|
|
public boolean canContinueToUse() {
|
|
return !this.pathNav.isDone();
|
|
diff --git a/net/minecraft/world/entity/ai/goal/BegGoal.java b/net/minecraft/world/entity/ai/goal/BegGoal.java
|
|
index 28ef40e8a645989ea181297069cf2bbe571f3082..661ff2379dca47162941f7ed2b6fb966a65f4ffc 100644
|
|
--- a/net/minecraft/world/entity/ai/goal/BegGoal.java
|
|
+++ b/net/minecraft/world/entity/ai/goal/BegGoal.java
|
|
@@ -27,8 +27,43 @@ public class BegGoal extends Goal {
|
|
this.setFlags(EnumSet.of(Goal.Flag.LOOK));
|
|
}
|
|
|
|
+ // Leaf start - Async target finding
|
|
+ protected boolean poll() {
|
|
+ if (!(this.wolf.getGoalCtx().result() instanceof Player target)) return false;
|
|
+ if (target == null) return false;
|
|
+ ServerLevel serverLevel = getServerLevel(this.wolf);
|
|
+ if (serverLevel == null || !target.isAlive() || !playerHoldingInteresting(target)) return false;
|
|
+ this.player = target;
|
|
+ return true;
|
|
+ }
|
|
+
|
|
+ private void findTargetAsync() {
|
|
+ final Wolf wolf = this.wolf;
|
|
+ final var ctx = wolf.getGoalCtx();
|
|
+ if (!ctx.state) return;
|
|
+ final ServerLevel serverLevel = getServerLevel(wolf);
|
|
+ final TargetingConditions begTargeting = this.begTargeting;
|
|
+ ctx.wake = () -> {
|
|
+ var player = serverLevel.getNearestPlayer(begTargeting, wolf);
|
|
+ if (player != null && playerHoldingInteresting(player)) {
|
|
+ return player;
|
|
+ }
|
|
+ return null;
|
|
+ };
|
|
+ }
|
|
+ // Leaf end - Async target finding
|
|
+
|
|
@Override
|
|
public boolean canUse() {
|
|
+ // Leaf start - Async target finding
|
|
+ if (poll()) {
|
|
+ return true;
|
|
+ }
|
|
+ if (org.dreeam.leaf.config.modules.async.AsyncTargetFinding.searchEntity) {
|
|
+ findTargetAsync();
|
|
+ return false;
|
|
+ }
|
|
+ // Leaf end - Async target finding
|
|
this.player = this.level.getNearestPlayer(this.begTargeting, this.wolf);
|
|
return this.player != null && this.playerHoldingInteresting(this.player);
|
|
}
|
|
@@ -59,10 +94,10 @@ public class BegGoal extends Goal {
|
|
this.lookTime--;
|
|
}
|
|
|
|
- private boolean playerHoldingInteresting(Player player) {
|
|
+ private static boolean playerHoldingInteresting(Player player) { // Leaf start - Async target finding - static
|
|
for (InteractionHand interactionHand : InteractionHand.values()) {
|
|
ItemStack itemInHand = player.getItemInHand(interactionHand);
|
|
- if (itemInHand.is(Items.BONE) || this.wolf.isFood(itemInHand)) {
|
|
+ if (itemInHand.is(Items.BONE) || itemInHand.is(net.minecraft.tags.ItemTags.WOLF_FOOD)) { // Leaf end - Async target finding
|
|
return true;
|
|
}
|
|
}
|
|
diff --git a/net/minecraft/world/entity/ai/goal/CatLieOnBedGoal.java b/net/minecraft/world/entity/ai/goal/CatLieOnBedGoal.java
|
|
index 4c46cd105cde3cbcde65a02ff691c3a8edd56c76..892e22b80d8c98ea2954b4024ba434da5a1abffa 100644
|
|
--- a/net/minecraft/world/entity/ai/goal/CatLieOnBedGoal.java
|
|
+++ b/net/minecraft/world/entity/ai/goal/CatLieOnBedGoal.java
|
|
@@ -52,6 +52,13 @@ public class CatLieOnBedGoal extends MoveToBlockGoal {
|
|
|
|
@Override
|
|
protected boolean isValidTarget(LevelReader level, BlockPos pos) {
|
|
- return level.isEmptyBlock(pos.above()) && level.getBlockState(pos).is(BlockTags.BEDS);
|
|
+ return level.isEmptyBlock(pos.above()) && level.getBlockState(pos).is(BlockTags.BEDS); // Leaf - Async target finding - diff on change
|
|
}
|
|
+
|
|
+ // Leaf start - Async target finding
|
|
+ @Override
|
|
+ protected TypeToCheck typeToCheck() {
|
|
+ return TypeToCheck.CatLie;
|
|
+ }
|
|
+ // Leaf end - Async target finding
|
|
}
|
|
diff --git a/net/minecraft/world/entity/ai/goal/CatSitOnBlockGoal.java b/net/minecraft/world/entity/ai/goal/CatSitOnBlockGoal.java
|
|
index 9954f49bc364969c7ccb37f4186fa2ab8710f6ae..79e71a41245028042b8ac5a56cd39bd0940c37f5 100644
|
|
--- a/net/minecraft/world/entity/ai/goal/CatSitOnBlockGoal.java
|
|
+++ b/net/minecraft/world/entity/ai/goal/CatSitOnBlockGoal.java
|
|
@@ -44,14 +44,21 @@ public class CatSitOnBlockGoal extends MoveToBlockGoal {
|
|
|
|
@Override
|
|
protected boolean isValidTarget(LevelReader level, BlockPos pos) {
|
|
- if (!level.isEmptyBlock(pos.above())) {
|
|
+ if (!level.isEmptyBlock(pos.above())) { // Leaf - Async target finding - diff on change
|
|
return false;
|
|
} else {
|
|
- BlockState blockState = level.getBlockState(pos);
|
|
+ BlockState blockState = level.getBlockState(pos); // Leaf - Async target finding - diff on change
|
|
return blockState.is(Blocks.CHEST)
|
|
? ChestBlockEntity.getOpenCount(level, pos) < 1
|
|
: blockState.is(Blocks.FURNACE) && blockState.getValue(FurnaceBlock.LIT)
|
|
|| blockState.is(BlockTags.BEDS, state -> state.getOptionalValue(BedBlock.PART).map(bedPart -> bedPart != BedPart.HEAD).orElse(true));
|
|
}
|
|
}
|
|
+
|
|
+ // Leaf start - Async target finding
|
|
+ @Override
|
|
+ protected TypeToCheck typeToCheck() {
|
|
+ return TypeToCheck.CatSit;
|
|
+ }
|
|
+ // Leaf end - Async target finding
|
|
}
|
|
diff --git a/net/minecraft/world/entity/ai/goal/FollowBoatGoal.java b/net/minecraft/world/entity/ai/goal/FollowBoatGoal.java
|
|
index 1fe4d1877ed52017d4d22ddb9b77f78e2b93dff9..70c75c0b3be9de58e73663c194fd18e8cc2989e7 100644
|
|
--- a/net/minecraft/world/entity/ai/goal/FollowBoatGoal.java
|
|
+++ b/net/minecraft/world/entity/ai/goal/FollowBoatGoal.java
|
|
@@ -23,8 +23,47 @@ public class FollowBoatGoal extends Goal {
|
|
this.mob = mob;
|
|
}
|
|
|
|
+ // Leaf start - Async target finding
|
|
+ private boolean poll() {
|
|
+ if (!(this.mob.getGoalCtx().result() instanceof Player target)) return false;
|
|
+ var serverLevel = getServerLevel(this.mob);
|
|
+ return serverLevel != null && target.isAlive() && !target.isSpectator() && (Mth.abs(target.xxa) > 0.0F || Mth.abs(target.zza) > 0.0F);
|
|
+ }
|
|
+
|
|
+ private void findTargetAsync() {
|
|
+ final PathfinderMob mob = this.mob;
|
|
+ final var ctx = mob.getGoalCtx();
|
|
+ if (!ctx.state) return;
|
|
+ final var bound = mob.getBoundingBox().inflate(5.0);
|
|
+ final var serverLevel = getServerLevel(mob);
|
|
+ ctx.wake = () -> {
|
|
+ List<AbstractBoat> entitiesOfClass = serverLevel.getEntitiesOfClass(AbstractBoat.class, bound);
|
|
+ for (AbstractBoat abstractBoat : entitiesOfClass) {
|
|
+ Entity controllingPassenger = abstractBoat.getControllingPassenger();
|
|
+ if (controllingPassenger instanceof Player player
|
|
+ && (Mth.abs(player.xxa) > 0.0F || Mth.abs(player.zza) > 0.0F)) {
|
|
+ return player;
|
|
+
|
|
+ }
|
|
+ }
|
|
+ return null;
|
|
+ };
|
|
+ }
|
|
+ // Leaf end - Async target finding
|
|
@Override
|
|
public boolean canUse() {
|
|
+ // Leaf start - Async target finding
|
|
+ if (poll()) {
|
|
+ return true;
|
|
+ }
|
|
+ if (this.following != null && (Mth.abs(this.following.xxa) > 0.0F || Mth.abs(this.following.zza) > 0.0F)) {
|
|
+ return true;
|
|
+ }
|
|
+ if (org.dreeam.leaf.config.modules.async.AsyncTargetFinding.searchEntity) {
|
|
+ findTargetAsync();
|
|
+ return false;
|
|
+ }
|
|
+ // Leaf end - Async target finding
|
|
List<AbstractBoat> entitiesOfClass = this.mob.level().getEntitiesOfClass(AbstractBoat.class, this.mob.getBoundingBox().inflate(5.0));
|
|
boolean flag = false;
|
|
|
|
@@ -37,7 +76,7 @@ public class FollowBoatGoal extends Goal {
|
|
}
|
|
}
|
|
|
|
- return this.following != null && (Mth.abs(this.following.xxa) > 0.0F || Mth.abs(this.following.zza) > 0.0F) || flag;
|
|
+ return flag; // Leaf - Async target finding - move above
|
|
}
|
|
|
|
@Override
|
|
diff --git a/net/minecraft/world/entity/ai/goal/FollowMobGoal.java b/net/minecraft/world/entity/ai/goal/FollowMobGoal.java
|
|
index c9cf985173d3da9e84ce178f161e2fd5fb8ce472..66f69163ed8d36dcd0dec5603e72a88e8812d3e5 100644
|
|
--- a/net/minecraft/world/entity/ai/goal/FollowMobGoal.java
|
|
+++ b/net/minecraft/world/entity/ai/goal/FollowMobGoal.java
|
|
@@ -38,6 +38,15 @@ public class FollowMobGoal extends Goal {
|
|
|
|
@Override
|
|
public boolean canUse() {
|
|
+ // Leaf start - Async target finding
|
|
+ if (poll()) {
|
|
+ return true;
|
|
+ }
|
|
+ if (org.dreeam.leaf.config.modules.async.AsyncTargetFinding.searchEntity) {
|
|
+ getFollowingMobAsync();
|
|
+ return false;
|
|
+ }
|
|
+ // Leaf end - Async target finding
|
|
List<Mob> entitiesOfClass = this.mob.level().getEntitiesOfClass(Mob.class, this.mob.getBoundingBox().inflate(this.areaSize), this.followPredicate);
|
|
if (!entitiesOfClass.isEmpty()) {
|
|
for (Mob mob : entitiesOfClass) {
|
|
@@ -51,6 +60,36 @@ public class FollowMobGoal extends Goal {
|
|
return false;
|
|
}
|
|
|
|
+ // Leaf start - Async target finding
|
|
+ private boolean poll() {
|
|
+ if (!(this.mob.getGoalCtx().result() instanceof Mob target)) return false;
|
|
+ var serverLevel = getServerLevel(this.mob);
|
|
+ if (serverLevel == null || !target.isAlive() || target.isInvisible()) return false;
|
|
+ this.followingMob = target;
|
|
+ return true;
|
|
+ }
|
|
+
|
|
+ private void getFollowingMobAsync() {
|
|
+ final var mob = this.mob;
|
|
+ final var ctx = this.mob.getGoalCtx();
|
|
+ if (!ctx.state) return;
|
|
+ final var serverLevel = getServerLevel(mob);
|
|
+ final var bound = this.mob.getBoundingBox().inflate(this.areaSize);
|
|
+ final var followPredicate = this.followPredicate;
|
|
+ ctx.wake = () -> {
|
|
+ List<Mob> entitiesOfClass = serverLevel.getEntitiesOfClass(Mob.class, bound, followPredicate);
|
|
+ if (!entitiesOfClass.isEmpty()) {
|
|
+ for (final Mob follow : entitiesOfClass) {
|
|
+ if (!follow.isInvisible()) {
|
|
+ return follow;
|
|
+ }
|
|
+ }
|
|
+ }
|
|
+ return null;
|
|
+ };
|
|
+ }
|
|
+ // Leaf end - Async target finding
|
|
+
|
|
@Override
|
|
public boolean canContinueToUse() {
|
|
return this.followingMob != null && !this.navigation.isDone() && this.mob.distanceToSqr(this.followingMob) > this.stopDistance * this.stopDistance;
|
|
diff --git a/net/minecraft/world/entity/ai/goal/FollowParentGoal.java b/net/minecraft/world/entity/ai/goal/FollowParentGoal.java
|
|
index 3093f03d4f298bf39fec8bad2b6c22518774aea8..4eec8eda6ea1698529e0392bc75c07be5980f5fc 100644
|
|
--- a/net/minecraft/world/entity/ai/goal/FollowParentGoal.java
|
|
+++ b/net/minecraft/world/entity/ai/goal/FollowParentGoal.java
|
|
@@ -19,11 +19,56 @@ public class FollowParentGoal extends Goal {
|
|
this.speedModifier = speedModifier;
|
|
}
|
|
|
|
+ // Leaf start - Async target finding
|
|
+ protected boolean poll() {
|
|
+ if (!(this.animal.getGoalCtx().result() instanceof Animal target)) return false;
|
|
+ var serverLevel = getServerLevel(animal);
|
|
+ if (serverLevel == null || !target.isAlive() || animal.distanceToSqr(target) < 9.0) return false;
|
|
+ this.parent = animal;
|
|
+ return true;
|
|
+ }
|
|
+
|
|
+ private void findTargetAsync() {
|
|
+ final Animal animal = this.animal;
|
|
+ final var ctx = animal.getGoalCtx();
|
|
+ if (!ctx.state) return;
|
|
+ final var targetType = animal.getClass();
|
|
+ final var bound = animal.getBoundingBox().inflate(8.0, 4.0, 8.0);
|
|
+ final var serverLevel = getServerLevel(animal);
|
|
+ final var pos = animal.position();
|
|
+ ctx.wake = () -> {
|
|
+ List<? extends Animal> entitiesOfClass = serverLevel.getEntitiesOfClass(targetType, bound);
|
|
+ Animal target = null;
|
|
+ double d = Double.MAX_VALUE;
|
|
+
|
|
+ for (Animal animal1 : entitiesOfClass) {
|
|
+ if (animal1.getAge() >= 0) {
|
|
+ double d1 = animal1.distanceToSqr(pos);
|
|
+ if (!(d1 > d)) {
|
|
+ d = d1;
|
|
+ target = animal1;
|
|
+ }
|
|
+ }
|
|
+ }
|
|
+ return target;
|
|
+ };
|
|
+ }
|
|
+ // Leaf end - Async target finding
|
|
+
|
|
@Override
|
|
public boolean canUse() {
|
|
if (this.animal.getAge() >= 0) {
|
|
return false;
|
|
} else {
|
|
+ // Leaf start - Async target finding
|
|
+ if (poll()) {
|
|
+ return true;
|
|
+ }
|
|
+ if (org.dreeam.leaf.config.modules.async.AsyncTargetFinding.searchEntity) {
|
|
+ findTargetAsync();
|
|
+ return false;
|
|
+ }
|
|
+ // Leaf end - Async target finding
|
|
List<? extends Animal> entitiesOfClass = this.animal
|
|
.level()
|
|
.getEntitiesOfClass((Class<? extends Animal>)this.animal.getClass(), this.animal.getBoundingBox().inflate(8.0, 4.0, 8.0));
|
|
@@ -43,6 +88,7 @@ public class FollowParentGoal extends Goal {
|
|
if (animal == null) {
|
|
return false;
|
|
} else if (d < 9.0) {
|
|
+ // Leaf - Async target finding - diff on change
|
|
return false;
|
|
} 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..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 {
|
|
private final ca.spottedleaf.moonrise.common.set.OptimizedSmallEnumSet<net.minecraft.world.entity.ai.goal.Goal.Flag> goalTypes = new ca.spottedleaf.moonrise.common.set.OptimizedSmallEnumSet<>(Goal.Flag.class); // Paper - remove streams from GoalSelector
|
|
private int curRate; // Paper - EAR 2
|
|
|
|
+ // Leaf start - Async target finding
|
|
+ private boolean availableGoalsDirty = true;
|
|
+ private WrappedGoal @org.jetbrains.annotations.Nullable[] ctxGoals = null;
|
|
+ private int ctxIndex = 0;
|
|
+ public int ctxState = -1;
|
|
+ public final org.dreeam.leaf.async.ai.Waker ctx = new org.dreeam.leaf.async.ai.Waker();
|
|
+ // Leaf end - Async target finding
|
|
+
|
|
public void addGoal(int priority, Goal goal) {
|
|
this.availableGoals.add(new WrappedGoal(priority, goal));
|
|
+ availableGoalsDirty = true; // Leaf - Async target finding
|
|
}
|
|
|
|
@VisibleForTesting
|
|
public void removeAllGoals(Predicate<Goal> filter) {
|
|
this.availableGoals.removeIf(wrappedGoal -> filter.test(wrappedGoal.getGoal()));
|
|
+ availableGoalsDirty = true; // Leaf - Async target finding
|
|
}
|
|
|
|
// Paper start - EAR 2
|
|
@@ -63,18 +73,19 @@ public class GoalSelector {
|
|
}
|
|
|
|
this.availableGoals.removeIf(wrappedGoal1 -> wrappedGoal1.getGoal() == goal);
|
|
+ availableGoalsDirty = true; // Leaf - Async target finding
|
|
}
|
|
|
|
// Paper start - Perf: optimize goal types
|
|
private static boolean goalContainsAnyFlags(WrappedGoal goal, ca.spottedleaf.moonrise.common.set.OptimizedSmallEnumSet<Goal.Flag> flags) {
|
|
- return goal.getFlags().hasCommonElements(flags);
|
|
+ return goal.getFlags().hasCommonElements(flags); // Leaf - Async target finding - inline diff
|
|
}
|
|
|
|
private static boolean goalCanBeReplacedForAllFlags(WrappedGoal goal, Map<Goal.Flag, WrappedGoal> flag) {
|
|
long flagIterator = goal.getFlags().getBackingSet();
|
|
int wrappedGoalSize = goal.getFlags().size();
|
|
for (int i = 0; i < wrappedGoalSize; ++i) {
|
|
- final Goal.Flag flag1 = GOAL_FLAG_VALUES[Long.numberOfTrailingZeros(flagIterator)];
|
|
+ final Goal.Flag flag1 = GOAL_FLAG_VALUES[Long.numberOfTrailingZeros(flagIterator)]; // Leaf - Async target finding - inline diff
|
|
flagIterator ^= ca.spottedleaf.concurrentutil.util.IntegerUtil.getTrailingBit(flagIterator);
|
|
// Paper end - Perf: optimize goal types
|
|
if (!flag.getOrDefault(flag1, NO_GOAL).canBeReplacedBy(goal)) {
|
|
@@ -85,7 +96,136 @@ public class GoalSelector {
|
|
return true;
|
|
}
|
|
|
|
+ // Leaf start - Async target finding
|
|
+ public final boolean poll() {
|
|
+ if (ctxState == -1 || this.ctxGoals == null || ctx.wake != null || ctxGoals.length == 0) {
|
|
+ return false;
|
|
+ }
|
|
+ if (ctxState == 0) {
|
|
+ while (ctxIndex < this.ctxGoals.length) {
|
|
+ WrappedGoal goal = this.ctxGoals[ctxIndex];
|
|
+ // entity tempt
|
|
+ if (goal.isRunning() && (goalContainsAnyFlags(goal, this.goalTypes) || !goal.canContinueToUse())) {
|
|
+ ctx.state = false;
|
|
+ if (ctx.wake != null) {
|
|
+ return true;
|
|
+ }
|
|
+ goal.stop();
|
|
+ }
|
|
+
|
|
+ ctxIndex++;
|
|
+ ctx.state = true;
|
|
+ }
|
|
+
|
|
+ for (Goal.Flag flag : GOAL_FLAG_VALUES) {
|
|
+ var goal = this.lockedFlags.get(flag);
|
|
+ if (goal != null && !goal.isRunning()) {
|
|
+ this.lockedFlags.remove(flag);
|
|
+ }
|
|
+ }
|
|
+
|
|
+ ctxIndex = 0;
|
|
+ ctx.state = true;
|
|
+ ctxState = 1;
|
|
+ }
|
|
+ if (ctxState == 1) {
|
|
+ while (ctxIndex < this.ctxGoals.length) {
|
|
+ WrappedGoal goal = this.ctxGoals[ctxIndex];
|
|
+ var flags = goal.getFlags();
|
|
+ // entity and block
|
|
+ if (!goal.isRunning() && !flags.hasCommonElements(this.goalTypes)) {
|
|
+ // inline
|
|
+ boolean result = true;
|
|
+ long flagIterator1 = flags.getBackingSet();
|
|
+ int wrappedGoalSize1 = flags.size();
|
|
+ for (int i1 = 0; i1 < wrappedGoalSize1; ++i1) {
|
|
+ final Goal.Flag flag1 = GOAL_FLAG_VALUES[Long.numberOfTrailingZeros(flagIterator1)];
|
|
+ flagIterator1 ^= ca.spottedleaf.concurrentutil.util.IntegerUtil.getTrailingBit(flagIterator1);
|
|
+ if (!this.lockedFlags.getOrDefault(flag1, NO_GOAL).canBeReplacedBy(goal)) {
|
|
+ result = false;
|
|
+ break;
|
|
+ }
|
|
+ }
|
|
+ if (result) {
|
|
+ if (goal.canUse()) {
|
|
+ long flagIterator = flags.getBackingSet();
|
|
+ int wrappedGoalSize = flags.size();
|
|
+ for (int i = 0; i < wrappedGoalSize; ++i) {
|
|
+ final Goal.Flag flag = GOAL_FLAG_VALUES[Long.numberOfTrailingZeros(flagIterator)];
|
|
+ flagIterator ^= ca.spottedleaf.concurrentutil.util.IntegerUtil.getTrailingBit(flagIterator);
|
|
+ WrappedGoal wrappedGoal1 = this.lockedFlags.getOrDefault(flag, NO_GOAL);
|
|
+ wrappedGoal1.stop();
|
|
+ this.lockedFlags.put(flag, goal);
|
|
+ }
|
|
+
|
|
+ goal.start();
|
|
+ }
|
|
+ ctx.state = false;
|
|
+ if (ctx.wake != null) {
|
|
+ return true;
|
|
+ }
|
|
+ }
|
|
+ }
|
|
+ // entity alert other
|
|
+ if (!ctx.state) {
|
|
+ switch (goal.getGoal()) {
|
|
+ case net.minecraft.world.entity.ai.goal.target.HurtByTargetGoal t -> t.poll();
|
|
+ case net.minecraft.world.entity.ai.goal.target.ResetUniversalAngerTargetGoal<?> t -> t.poll();
|
|
+ default -> {}
|
|
+ }
|
|
+ }
|
|
+ ctxIndex++;
|
|
+ ctx.state = true;
|
|
+ }
|
|
+
|
|
+ ctxIndex = 0;
|
|
+ ctx.state = true;
|
|
+ ctxState = 2;
|
|
+ }
|
|
+ if (ctxState == 2) {
|
|
+ while (ctxIndex < this.ctxGoals.length) {
|
|
+ WrappedGoal goal = this.ctxGoals[ctxIndex];
|
|
+ if (goal.isRunning()) {
|
|
+ goal.tick();
|
|
+ }
|
|
+ ctxIndex++;
|
|
+ }
|
|
+
|
|
+ ctxState = -1;
|
|
+ ctxIndex = 0;
|
|
+ ctx.state = true;
|
|
+ }
|
|
+ if (ctxState == 3) {
|
|
+ while (ctxIndex < this.ctxGoals.length) {
|
|
+ WrappedGoal goal = this.ctxGoals[ctxIndex];
|
|
+ if (goal.isRunning() && goal.requiresUpdateEveryTick()) {
|
|
+ goal.tick();
|
|
+ }
|
|
+ ctxIndex++;
|
|
+ }
|
|
+
|
|
+ ctxState = -1;
|
|
+ ctxIndex = 0;
|
|
+ ctx.state = true;
|
|
+ }
|
|
+ return false;
|
|
+ }
|
|
+ // Leaf end - Async target finding
|
|
+
|
|
public void tick() {
|
|
+ // Leaf start - Async target finding
|
|
+ if (org.dreeam.leaf.config.modules.async.AsyncTargetFinding.enabled) {
|
|
+ if (this.ctxState == -1) {
|
|
+ if (this.availableGoalsDirty || this.ctxGoals == null) {
|
|
+ this.ctxGoals = this.availableGoals.toArray(new WrappedGoal[0]);
|
|
+ this.availableGoalsDirty = false;
|
|
+ }
|
|
+ this.ctxState = 0;
|
|
+ }
|
|
+ return;
|
|
+ }
|
|
+ // Leaf end - Async target finding
|
|
+
|
|
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,24 @@ public class GoalSelector {
|
|
}
|
|
|
|
public void tickRunningGoals(boolean tickAllRunning) {
|
|
+ // Leaf start - Async target finding
|
|
+ if (org.dreeam.leaf.config.modules.async.AsyncTargetFinding.enabled) {
|
|
+ if (ctxState == -1) {
|
|
+ if (availableGoalsDirty || this.ctxGoals == null) {
|
|
+ this.ctxGoals = this.availableGoals.toArray(new WrappedGoal[0]);
|
|
+ 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;
|
|
+ }
|
|
+ // Leaf end - Async target finding
|
|
for (WrappedGoal wrappedGoal : this.availableGoals) {
|
|
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..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 {
|
|
this.setFlags(EnumSet.of(Goal.Flag.MOVE));
|
|
}
|
|
|
|
+ // Leaf start - Async target finding
|
|
+ private @javax.annotation.Nullable Llama poll() {
|
|
+ if (!(this.llama.getGoalCtx().result() instanceof Llama target)) return null;
|
|
+ var serverLevel = getServerLevel(this.llama);
|
|
+ if (serverLevel == null || !target.isAlive()) return null;
|
|
+ return target;
|
|
+ }
|
|
+
|
|
+ private void findTargetAsync() {
|
|
+ final Llama llama = this.llama;
|
|
+ final var ctx = llama.getGoalCtx();
|
|
+ if (!ctx.state) return;
|
|
+ final var bound = llama.getBoundingBox().inflate(9.0, 4.0, 9.0);
|
|
+ final var serverLevel = getServerLevel(llama);
|
|
+ final var pos = llama.position();
|
|
+ ctx.wake = () -> {
|
|
+ List<Entity> entities = serverLevel.getEntities(llama, bound, entity1 -> {
|
|
+ EntityType<?> type = entity1.getType();
|
|
+ return type == EntityType.LLAMA || type == EntityType.TRADER_LLAMA;
|
|
+ });
|
|
+ Llama target = null;
|
|
+ double d = Double.MAX_VALUE;
|
|
+
|
|
+ for (Entity entity : entities) {
|
|
+ Llama llama1 = (Llama) entity;
|
|
+ if (llama1.inCaravan() && !llama1.hasCaravanTail()) {
|
|
+ double d1 = llama1.distanceToSqr(pos);
|
|
+ if (!(d1 > d)) {
|
|
+ d = d1;
|
|
+ target = llama1;
|
|
+ }
|
|
+ }
|
|
+ }
|
|
+
|
|
+ if (target == null) {
|
|
+ for (Entity entityx : entities) {
|
|
+ Llama llama1 = (Llama) entityx;
|
|
+ if (llama1.isLeashed() && !llama1.hasCaravanTail()) {
|
|
+ double d1 = llama1.distanceToSqr(pos);
|
|
+ if (!(d1 > d)) {
|
|
+ d = d1;
|
|
+ target = llama1;
|
|
+ }
|
|
+ }
|
|
+ }
|
|
+ }
|
|
+ return target;
|
|
+ };
|
|
+ }
|
|
+ // Leaf end - Async target finding
|
|
+
|
|
@Override
|
|
public boolean canUse() {
|
|
if (!this.llama.level().purpurConfig.llamaJoinCaravans || !this.llama.shouldJoinCaravan) return false; // Purpur - Llama API // Purpur - Config to disable Llama caravans
|
|
if (!this.llama.isLeashed() && !this.llama.inCaravan()) {
|
|
+ // Leaf start - Async target finding
|
|
+ Llama llama = poll();
|
|
+ double d = Double.MAX_VALUE;
|
|
+ if (org.dreeam.leaf.config.modules.async.AsyncTargetFinding.searchEntity) {
|
|
+ if (llama == null) {
|
|
+ findTargetAsync();
|
|
+ return false;
|
|
+ } else {
|
|
+ d = this.llama.distanceToSqr(llama);
|
|
+ }
|
|
+ } else {
|
|
+ // Leaf end - Async target finding
|
|
List<Entity> entities = this.llama.level().getEntities(this.llama, this.llama.getBoundingBox().inflate(9.0, 4.0, 9.0), entity1 -> {
|
|
EntityType<?> type = entity1.getType();
|
|
return type == EntityType.LLAMA || type == EntityType.TRADER_LLAMA;
|
|
});
|
|
- Llama llama = null;
|
|
- double d = Double.MAX_VALUE;
|
|
+ // Llama llama = null; // Leaf - Async target finding
|
|
+ // double d = Double.MAX_VALUE; // Leaf - Async target finding
|
|
|
|
for (Entity entity : entities) {
|
|
Llama llama1 = (Llama)entity;
|
|
- if (llama1.inCaravan() && !llama1.hasCaravanTail()) {
|
|
+ if (llama1.inCaravan() && !llama1.hasCaravanTail()) { // Leaf - Async target finding - diff on change
|
|
double d1 = this.llama.distanceToSqr(llama1);
|
|
if (!(d1 > d)) {
|
|
d = d1;
|
|
@@ -45,7 +108,7 @@ public class LlamaFollowCaravanGoal extends Goal {
|
|
if (llama == null) {
|
|
for (Entity entityx : entities) {
|
|
Llama llama1 = (Llama)entityx;
|
|
- if (llama1.isLeashed() && !llama1.hasCaravanTail()) {
|
|
+ if (llama1.isLeashed() && !llama1.hasCaravanTail()) { // Leaf - Async target finding - diff on change
|
|
double d1 = this.llama.distanceToSqr(llama1);
|
|
if (!(d1 > d)) {
|
|
d = d1;
|
|
@@ -54,6 +117,7 @@ public class LlamaFollowCaravanGoal extends Goal {
|
|
}
|
|
}
|
|
}
|
|
+ } // Leaf - Async target finding
|
|
|
|
if (llama == null) {
|
|
return false;
|
|
diff --git a/net/minecraft/world/entity/ai/goal/LookAtPlayerGoal.java b/net/minecraft/world/entity/ai/goal/LookAtPlayerGoal.java
|
|
index 6463c3c9b08d6058f2843c225b08a40fc30a960b..126bd98bc5980a2f1177bd7c74918b86a1f5bdce 100644
|
|
--- a/net/minecraft/world/entity/ai/goal/LookAtPlayerGoal.java
|
|
+++ b/net/minecraft/world/entity/ai/goal/LookAtPlayerGoal.java
|
|
@@ -48,32 +48,79 @@ public class LookAtPlayerGoal extends Goal {
|
|
|
|
@Override
|
|
public boolean canUse() {
|
|
+ // Leaf start - Async target finding
|
|
+ if (poll()) {
|
|
+ return true;
|
|
+ }
|
|
if (this.mob.getRandom().nextFloat() >= this.probability) {
|
|
return false;
|
|
+ }
|
|
+ if (this.mob.getTarget() != null) {
|
|
+ this.lookAt = this.mob.getTarget();
|
|
+ }
|
|
+ if (org.dreeam.leaf.config.modules.async.AsyncTargetFinding.searchEntity) {
|
|
+ getLookAsync();
|
|
+ return false;
|
|
+ }
|
|
+ ServerLevel serverLevel = getServerLevel(this.mob);
|
|
+ if (this.lookAtType == Player.class) {
|
|
+ this.lookAt = serverLevel.getNearestPlayer(this.lookAtContext, this.mob, this.mob.getX(), this.mob.getEyeY(), this.mob.getZ());
|
|
} else {
|
|
- if (this.mob.getTarget() != null) {
|
|
- this.lookAt = this.mob.getTarget();
|
|
- }
|
|
+ this.lookAt = serverLevel.getNearestEntity(
|
|
+ this.mob
|
|
+ .level()
|
|
+ .getEntitiesOfClass(this.lookAtType, this.mob.getBoundingBox().inflate(this.lookDistance, 3.0, this.lookDistance), livingEntity -> true),
|
|
+ this.lookAtContext,
|
|
+ this.mob,
|
|
+ this.mob.getX(),
|
|
+ this.mob.getEyeY(),
|
|
+ this.mob.getZ()
|
|
+ );
|
|
+ }
|
|
|
|
- ServerLevel serverLevel = getServerLevel(this.mob);
|
|
- if (this.lookAtType == Player.class) {
|
|
- this.lookAt = serverLevel.getNearestPlayer(this.lookAtContext, this.mob, this.mob.getX(), this.mob.getEyeY(), this.mob.getZ());
|
|
+ return this.lookAt != null;
|
|
+ }
|
|
+
|
|
+ protected boolean poll() {
|
|
+ if (!(this.mob.getGoalCtx().result() instanceof LivingEntity target)) return false;
|
|
+ if (this.mob.getTarget() != null) {
|
|
+ this.lookAt = this.mob.getTarget();
|
|
+ return true;
|
|
+ }
|
|
+ ServerLevel serverLevel = getServerLevel(this.mob);
|
|
+ if (!target.isAlive() || !this.lookAtContext.test(serverLevel, this.mob, target)) return false;
|
|
+ this.lookAt = target;
|
|
+ return true;
|
|
+ }
|
|
+
|
|
+ protected void getLookAsync() {
|
|
+ final var mob = this.mob;
|
|
+ final var ctx = mob.getGoalCtx();
|
|
+ if (!ctx.state) return;
|
|
+ final double x = mob.getX();
|
|
+ final double y = mob.getEyeY();
|
|
+ final double z = mob.getZ();
|
|
+ final var serverLevel = getServerLevel(mob);
|
|
+ final var bound = mob.getBoundingBox().inflate(this.lookDistance, 3.0, this.lookDistance);
|
|
+ final var lookAtContext = this.lookAtContext;
|
|
+ final var lookAtType = this.lookAtType;
|
|
+ ctx.wake = () -> {
|
|
+ if (lookAtType == Player.class) {
|
|
+ return serverLevel.getNearestPlayer(lookAtContext, mob, x, y, z);
|
|
} else {
|
|
- this.lookAt = serverLevel.getNearestEntity(
|
|
- this.mob
|
|
- .level()
|
|
- .getEntitiesOfClass(this.lookAtType, this.mob.getBoundingBox().inflate(this.lookDistance, 3.0, this.lookDistance), livingEntity -> true),
|
|
- this.lookAtContext,
|
|
- this.mob,
|
|
- this.mob.getX(),
|
|
- this.mob.getEyeY(),
|
|
- this.mob.getZ()
|
|
+ return serverLevel.getNearestEntity(
|
|
+ serverLevel
|
|
+ .getEntitiesOfClass(lookAtType, bound, livingEntity -> true),
|
|
+ lookAtContext,
|
|
+ mob,
|
|
+ x,
|
|
+ y,
|
|
+ z
|
|
);
|
|
}
|
|
-
|
|
- return this.lookAt != null;
|
|
- }
|
|
+ };
|
|
}
|
|
+ // Leaf end - Async target finding
|
|
|
|
@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..146a5efa6d4f978ee30a71a4a52340a8d470f590 100644
|
|
--- a/net/minecraft/world/entity/ai/goal/MoveToBlockGoal.java
|
|
+++ b/net/minecraft/world/entity/ai/goal/MoveToBlockGoal.java
|
|
@@ -41,14 +41,67 @@ public abstract class MoveToBlockGoal extends Goal {
|
|
this.setFlags(EnumSet.of(Goal.Flag.MOVE, Goal.Flag.JUMP));
|
|
}
|
|
|
|
+ // Leaf start - Async target finding
|
|
+ protected boolean poll() {
|
|
+ if (!(this.mob.getGoalCtx().result() instanceof BlockPos blockPos1)) return false;
|
|
+ if (!this.mob.level().hasChunkAt(blockPos1)
|
|
+ || !this.mob.isWithinRestriction(blockPos1)
|
|
+ || !this.isValidTarget(this.mob.level(), blockPos1)) {
|
|
+ return false;
|
|
+ }
|
|
+ this.blockPos = blockPos1;
|
|
+ this.mob.movingTarget = blockPos1 == BlockPos.ZERO ? null : blockPos1;
|
|
+ return true;
|
|
+ }
|
|
+
|
|
+ protected void getBlockAsync() {
|
|
+ final var mob = this.mob;
|
|
+ final var ctx = mob.getGoalCtx();
|
|
+ if (!ctx.state) return;
|
|
+ final var serverLevel = getServerLevel(mob);
|
|
+ final TypeToCheck ty = this.typeToCheck();
|
|
+ final net.minecraft.world.level.block.Block toRemove;
|
|
+ if (this instanceof RemoveBlockGoal removeBlockGoal) {
|
|
+ toRemove = removeBlockGoal.blockToRemove;
|
|
+ } else {
|
|
+ toRemove = null;
|
|
+ }
|
|
+ final int verticalSearchStart = this.verticalSearchStart;
|
|
+ final int searchRange = this.searchRange;
|
|
+ final int verticalSearchRange = this.verticalSearchRange;
|
|
+ final BlockPos blockPos = mob.blockPosition();
|
|
+ final float restrictRadius = mob.getRestrictRadius();
|
|
+ final BlockPos restrictCenter = mob.getRestrictCenter();
|
|
+ ctx.wake = () -> findNearestBlockAsync(ty, toRemove, mob, serverLevel, verticalSearchStart, searchRange, verticalSearchRange, blockPos, restrictRadius, restrictCenter);
|
|
+ }
|
|
+
|
|
+ protected enum TypeToCheck {
|
|
+ CatLie,
|
|
+ CatSit,
|
|
+ Drowned,
|
|
+ FoxEat,
|
|
+ RaidGarden,
|
|
+ RemoveBlock,
|
|
+ Strider,
|
|
+ TurtleToWater,
|
|
+ TurtleLay,
|
|
+ Unknown,
|
|
+ }
|
|
+ // Leaf end - Async target finding
|
|
+
|
|
@Override
|
|
public boolean canUse() {
|
|
+ // Leaf start - Async target finding
|
|
+ if (poll()) {
|
|
+ return true;
|
|
+ }
|
|
+ // Leaf end - Async target finding
|
|
if (this.nextStartTick > 0) {
|
|
this.nextStartTick--;
|
|
return false;
|
|
} else {
|
|
this.nextStartTick = this.nextStartTick(this.mob);
|
|
- return this.findNearestBlock();
|
|
+ return this.findNearestBlockAsync(); // Leaf - Async target finding
|
|
}
|
|
}
|
|
|
|
@@ -133,5 +186,133 @@ public abstract class MoveToBlockGoal extends Goal {
|
|
return false;
|
|
}
|
|
|
|
+ // Leaf start - Async target finding
|
|
+ protected boolean findNearestBlockAsync() {
|
|
+ if (!org.dreeam.leaf.config.modules.async.AsyncTargetFinding.searchBlock) {
|
|
+ return findNearestBlock();
|
|
+ }
|
|
+ final var mob = this.mob;
|
|
+ final var ctx = mob.getGoalCtx();
|
|
+ if (!ctx.state) return false;
|
|
+ final var serverLevel = getServerLevel(mob);
|
|
+ final TypeToCheck ty = this.typeToCheck();
|
|
+ final net.minecraft.world.level.block.Block toRemove;
|
|
+ if (this instanceof RemoveBlockGoal removeBlockGoal) {
|
|
+ toRemove = removeBlockGoal.blockToRemove;
|
|
+ } else {
|
|
+ toRemove = null;
|
|
+ }
|
|
+ final int verticalSearchStart = this.verticalSearchStart;
|
|
+ final int searchRange = this.searchRange;
|
|
+ final int verticalSearchRange = this.verticalSearchRange;
|
|
+ final BlockPos blockPos = mob.blockPosition();
|
|
+ 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 static @javax.annotation.Nullable BlockPos findNearestBlockAsync(
|
|
+ final TypeToCheck ty,
|
|
+ @org.jetbrains.annotations.Nullable final net.minecraft.world.level.block.Block toRemove,
|
|
+ final PathfinderMob mob,
|
|
+ final net.minecraft.server.level.ServerLevel serverLevel,
|
|
+ final int verticalSearchStart,
|
|
+ final int searchRange,
|
|
+ final int verticalSearchRange,
|
|
+ final BlockPos blockPos,
|
|
+ final float restrictRadius,
|
|
+ final BlockPos restrictCenter
|
|
+ ) {
|
|
+ BlockPos.MutableBlockPos mutableBlockPos = new BlockPos.MutableBlockPos();
|
|
+ for (int i2 = verticalSearchStart; i2 <= verticalSearchRange; i2 = i2 > 0 ? -i2 : 1 - i2) {
|
|
+ for (int i3 = 0; i3 < searchRange; i3++) {
|
|
+ for (int i4 = 0; i4 <= i3; i4 = i4 > 0 ? -i4 : 1 - i4) {
|
|
+ for (int i5 = i4 < i3 && i4 > -i3 ? i3 : 0; i5 <= i3; i5 = i5 > 0 ? -i5 : 1 - i5) {
|
|
+ mutableBlockPos.setWithOffset(blockPos, i4, i2 - 1, i5);
|
|
+ if (!serverLevel.hasChunkAt(mutableBlockPos)) continue;
|
|
+ if (isWithinRestriction(restrictRadius, restrictCenter, mutableBlockPos)
|
|
+ && isValidTargetAsync(ty, toRemove, mob, serverLevel, mutableBlockPos)) {
|
|
+ return mutableBlockPos.immutable();
|
|
+ }
|
|
+ }
|
|
+ }
|
|
+ }
|
|
+ }
|
|
+ return null;
|
|
+ }
|
|
+
|
|
+ private static boolean isWithinRestriction(float restrictRadius, BlockPos restrictCenter, BlockPos pos) {
|
|
+ return restrictRadius == -1.0F || restrictCenter.distSqr(pos) < restrictRadius * restrictRadius;
|
|
+ }
|
|
+ // Leaf end - Async target finding
|
|
+
|
|
protected abstract boolean isValidTarget(LevelReader level, BlockPos pos);
|
|
+
|
|
+ // Leaf start - Async target finding
|
|
+ protected TypeToCheck typeToCheck() {
|
|
+ return TypeToCheck.Unknown;
|
|
+ }
|
|
+
|
|
+ private static boolean isValidTargetAsync(
|
|
+ TypeToCheck type,
|
|
+ @org.jetbrains.annotations.Nullable net.minecraft.world.level.block.Block blockToRemoveTy,
|
|
+ PathfinderMob mob,
|
|
+ LevelReader level,
|
|
+ BlockPos pos
|
|
+ ) {
|
|
+ switch (type) {
|
|
+ case CatLie -> {
|
|
+ return level.isEmptyBlock(pos.above()) && level.getBlockState(pos).is(net.minecraft.tags.BlockTags.BEDS);
|
|
+ }
|
|
+ case CatSit -> {
|
|
+ if (!level.isEmptyBlock(pos.above())) {
|
|
+ return false;
|
|
+ } else {
|
|
+ var blockState = level.getBlockState(pos);
|
|
+ return blockState.is(net.minecraft.world.level.block.Blocks.CHEST)
|
|
+ ? net.minecraft.world.level.block.entity.ChestBlockEntity.getOpenCount(level, pos) < 1
|
|
+ : blockState.is(net.minecraft.world.level.block.Blocks.FURNACE) && blockState.getValue(net.minecraft.world.level.block.FurnaceBlock.LIT)
|
|
+ || blockState.is(net.minecraft.tags.BlockTags.BEDS, state -> state.getOptionalValue(net.minecraft.world.level.block.BedBlock.PART).map(bedPart -> bedPart != net.minecraft.world.level.block.state.properties.BedPart.HEAD).orElse(true));
|
|
+ }
|
|
+ }
|
|
+ case Drowned -> {
|
|
+ BlockPos blockPos = pos.above();
|
|
+ return level.isEmptyBlock(blockPos) && level.isEmptyBlock(blockPos.above()) && level.getBlockState(pos).entityCanStandOn(level, pos, mob);
|
|
+ }
|
|
+ case FoxEat -> {
|
|
+ var blockState = level.getBlockState(pos);
|
|
+ return blockState.is(net.minecraft.world.level.block.Blocks.SWEET_BERRY_BUSH) && blockState.getValue(net.minecraft.world.level.block.SweetBerryBushBlock.AGE) >= 2 || net.minecraft.world.level.block.CaveVines.hasGlowBerries(blockState);
|
|
+ }
|
|
+ case RaidGarden -> {
|
|
+ var blockState = level.getBlockState(pos);
|
|
+ if (blockState.is(net.minecraft.world.level.block.Blocks.FARMLAND)) {
|
|
+ blockState = level.getBlockState(pos.above());
|
|
+ return blockState.getBlock() instanceof net.minecraft.world.level.block.CarrotBlock carrot && carrot.isMaxAge(blockState);
|
|
+ } else {
|
|
+ return false;
|
|
+ }
|
|
+
|
|
+ }
|
|
+ case RemoveBlock -> {
|
|
+ var chunk = level.getChunkIfLoadedImmediately(pos.getX() >> 4, pos.getZ() >> 4); // Paper - Prevent AI rules from loading chunks
|
|
+ return chunk != null
|
|
+ && chunk.getBlockState(pos).is(blockToRemoveTy)
|
|
+ && chunk.getBlockState(pos.above()).isAir()
|
|
+ && chunk.getBlockState(pos.above(2)).isAir();
|
|
+ }
|
|
+ case Strider -> {
|
|
+ return level.getBlockState(pos).is(net.minecraft.world.level.block.Blocks.LAVA) && level.getBlockState(pos.above()).isPathfindable(net.minecraft.world.level.pathfinder.PathComputationType.LAND);
|
|
+ }
|
|
+ case TurtleToWater -> {
|
|
+ return level.getBlockState(pos).is(net.minecraft.world.level.block.Blocks.WATER);
|
|
+ }
|
|
+ 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
|
|
+ }
|
|
}
|
|
diff --git a/net/minecraft/world/entity/ai/goal/OfferFlowerGoal.java b/net/minecraft/world/entity/ai/goal/OfferFlowerGoal.java
|
|
index 3c274d917bca9de87abfb842f5f332e112a7a2d7..a3f96a6dac65a315842a4ae5de6c5ea04b7bd697 100644
|
|
--- a/net/minecraft/world/entity/ai/goal/OfferFlowerGoal.java
|
|
+++ b/net/minecraft/world/entity/ai/goal/OfferFlowerGoal.java
|
|
@@ -19,10 +19,20 @@ public class OfferFlowerGoal extends Goal {
|
|
|
|
@Override
|
|
public boolean canUse() {
|
|
+ // Leaf start - Async target finding
|
|
if (!this.golem.level().isDay()) {
|
|
return false;
|
|
- } else if (this.golem.getRandom().nextInt(8000) != 0) {
|
|
+ }
|
|
+ if (poll()) {
|
|
+ return true;
|
|
+ }
|
|
+ if (this.golem.getRandom().nextInt(8000) != 0) {
|
|
return false;
|
|
+ }
|
|
+ if (org.dreeam.leaf.config.modules.async.AsyncTargetFinding.searchEntity) {
|
|
+ getVillagerAsync();
|
|
+ return false;
|
|
+ // Leaf end - Async target finding
|
|
} else {
|
|
this.villager = getServerLevel(this.golem)
|
|
.getNearestEntity(
|
|
@@ -38,6 +48,36 @@ public class OfferFlowerGoal extends Goal {
|
|
}
|
|
}
|
|
|
|
+
|
|
+ // Leaf start - Async target finding
|
|
+ protected boolean poll() {
|
|
+ if (!(this.golem.getGoalCtx().result() instanceof Villager target)) return false;
|
|
+ var serverLevel = getServerLevel(this.golem);
|
|
+ if (!target.isAlive() || !OFFER_TARGER_CONTEXT.test(serverLevel, this.golem, target)) return false;
|
|
+ this.villager = target;
|
|
+ return true;
|
|
+ }
|
|
+
|
|
+ protected void getVillagerAsync() {
|
|
+ final var golem = this.golem;
|
|
+ final var ctx = golem.getGoalCtx();
|
|
+ if (!ctx.state) return;
|
|
+ final double x = golem.getX();
|
|
+ final double y = golem.getEyeY();
|
|
+ final double z = golem.getZ();
|
|
+ final var serverLevel = getServerLevel(golem);
|
|
+ final var bound = golem.getBoundingBox().inflate(6.0, 2.0, 6.0);
|
|
+ ctx.wake = () -> serverLevel.getNearestEntity(
|
|
+ serverLevel.getEntitiesOfClass(Villager.class, bound, livingEntity -> true),
|
|
+ OFFER_TARGER_CONTEXT,
|
|
+ golem,
|
|
+ x,
|
|
+ y,
|
|
+ z
|
|
+ );
|
|
+ }
|
|
+ // Leaf end - Async target finding
|
|
+
|
|
@Override
|
|
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 c67a88c9c77ece7c85ffb169ac96da4f28291228..a1a967780f904e05cfc5d804ecd637aa60f44438 100644
|
|
--- a/net/minecraft/world/entity/ai/goal/RemoveBlockGoal.java
|
|
+++ b/net/minecraft/world/entity/ai/goal/RemoveBlockGoal.java
|
|
@@ -37,10 +37,17 @@ public class RemoveBlockGoal extends MoveToBlockGoal {
|
|
public boolean canUse() {
|
|
if (!getServerLevel(this.removerMob).purpurConfig.zombieBypassMobGriefing == !getServerLevel(this.removerMob).getGameRules().getBoolean(GameRules.RULE_MOBGRIEFING)) { // Purpur - Add mobGriefing bypass to everything affected
|
|
return false;
|
|
- } else if (this.nextStartTick > 0) {
|
|
+ }
|
|
+ // Leaf start - Async target finding
|
|
+ if (poll()) {
|
|
+ this.nextStartTick = reducedTickDelay(20);
|
|
+ return true;
|
|
+ }
|
|
+ // Leaf end - Async target finding
|
|
+ if (this.nextStartTick > 0) {
|
|
this.nextStartTick--;
|
|
return false;
|
|
- } 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
|
|
return chunk != null
|
|
- && chunk.getBlockState(pos).is(this.blockToRemove)
|
|
+ && chunk.getBlockState(pos).is(this.blockToRemove) // Leaf - Async target finding - diff on change
|
|
&& chunk.getBlockState(pos.above()).isAir()
|
|
&& chunk.getBlockState(pos.above(2)).isAir();
|
|
}
|
|
+
|
|
+ // Leaf start - Async target finding
|
|
+ @Override
|
|
+ protected TypeToCheck typeToCheck() {
|
|
+ return TypeToCheck.RemoveBlock;
|
|
+ }
|
|
+ // 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..234b6e4712a289fd858e3bc2d79df897a28281f7 100644
|
|
--- a/net/minecraft/world/entity/ai/goal/TemptGoal.java
|
|
+++ b/net/minecraft/world/entity/ai/goal/TemptGoal.java
|
|
@@ -36,12 +36,43 @@ public class TemptGoal extends Goal {
|
|
this.targetingConditions = TEMPT_TARGETING.copy().selector((entity, level) -> this.shouldFollow(entity));
|
|
}
|
|
|
|
+ // Leaf start - Async target finding
|
|
+ private boolean poll() {
|
|
+ if (!(this.mob.getGoalCtx().result() instanceof Player target)) return false;
|
|
+ var serverLevel = getServerLevel(this.mob);
|
|
+ if (!target.isAlive() || !this.targetingConditions.range(this.mob.getAttributeValue(Attributes.TEMPT_RANGE)).test(serverLevel, this.mob, target)) return false;
|
|
+ this.player = target;
|
|
+ return true;
|
|
+ }
|
|
+
|
|
+ private void getNearestPlayerAsync() {
|
|
+ final var mob = this.mob;
|
|
+ final var ctx = mob.getGoalCtx();
|
|
+ if (!ctx.state) return;
|
|
+ final var serverLevel = getServerLevel(mob);
|
|
+ final var conditions = this.targetingConditions
|
|
+ .range(mob.getAttributeValue(Attributes.TEMPT_RANGE))
|
|
+ .copy();
|
|
+ ctx.wake = () -> serverLevel.getNearestPlayer(conditions, mob);
|
|
+ }
|
|
+ // Leaf end - Async target finding
|
|
@Override
|
|
public boolean canUse() {
|
|
if (this.calmDown > 0) {
|
|
this.calmDown--;
|
|
return false;
|
|
} else {
|
|
+ // Leaf start - Async target finding
|
|
+ if (org.dreeam.leaf.config.modules.async.AsyncTargetFinding.searchEntity) {
|
|
+ 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
|
|
diff --git a/net/minecraft/world/entity/ai/goal/target/DefendVillageTargetGoal.java b/net/minecraft/world/entity/ai/goal/target/DefendVillageTargetGoal.java
|
|
index 4644f3f7af89623ca6218c0dd24bb6cd67db553b..a5a09f33ff1d4dec3ab2ea9f0fd6da0da30d524d 100644
|
|
--- a/net/minecraft/world/entity/ai/goal/target/DefendVillageTargetGoal.java
|
|
+++ b/net/minecraft/world/entity/ai/goal/target/DefendVillageTargetGoal.java
|
|
@@ -24,8 +24,47 @@ public class DefendVillageTargetGoal extends TargetGoal {
|
|
this.setFlags(EnumSet.of(Goal.Flag.TARGET));
|
|
}
|
|
|
|
+ // Leaf start - Async target finding
|
|
+ protected boolean poll() {
|
|
+ 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;
|
|
+ this.potentialTarget = target;
|
|
+ return true;
|
|
+ }
|
|
+
|
|
+ private void findTargetAsync() {
|
|
+ final IronGolem mob = this.golem;
|
|
+ final var ctx = mob.getGoalCtx();
|
|
+ if (!ctx.state) return;
|
|
+ AABB bound = this.golem.getBoundingBox().inflate(10.0, 8.0, 10.0);
|
|
+ final ServerLevel serverLevel = getServerLevel(mob);
|
|
+ ctx.wake = () -> {
|
|
+ List<Villager> nearbyEntities = serverLevel.getNearbyEntities(Villager.class, attackTargeting, mob, bound);
|
|
+ List<Player> nearbyPlayers = serverLevel.getNearbyPlayers(attackTargeting, mob, bound);
|
|
+ for (Villager villager : nearbyEntities) {
|
|
+ for (Player player : nearbyPlayers) {
|
|
+ int playerReputation = villager.getPlayerReputation(player);
|
|
+ if (playerReputation <= -100) {
|
|
+ return player;
|
|
+ }
|
|
+ }
|
|
+ }
|
|
+ return null;
|
|
+ };
|
|
+ }
|
|
+ // Leaf end - Async target finding
|
|
+
|
|
@Override
|
|
public boolean canUse() {
|
|
+ // Leaf start - Async target finding
|
|
+ if (org.dreeam.leaf.config.modules.async.AsyncTargetFinding.searchEntity) {
|
|
+ if (!poll()) {
|
|
+ this.findTargetAsync();
|
|
+ return false;
|
|
+ }
|
|
+ } else {
|
|
+ // Leaf end - Async target finding
|
|
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);
|
|
@@ -41,9 +80,10 @@ public class DefendVillageTargetGoal extends TargetGoal {
|
|
}
|
|
}
|
|
}
|
|
+ } // Leaf - Async target finding
|
|
|
|
return this.potentialTarget != null
|
|
- && (!(this.potentialTarget instanceof Player) || !this.potentialTarget.isSpectator() && !((Player)this.potentialTarget).isCreative());
|
|
+ && (!(this.potentialTarget instanceof Player) || !this.potentialTarget.isSpectator() && !((Player)this.potentialTarget).isCreative()); // Leaf - Async target finding - diff on change
|
|
}
|
|
|
|
@Override
|
|
diff --git a/net/minecraft/world/entity/ai/goal/target/HurtByTargetGoal.java b/net/minecraft/world/entity/ai/goal/target/HurtByTargetGoal.java
|
|
index 25fe78116ce01eeefe5c958423734195d27302eb..bfe73d837c346b7550da2a3f521c25390f9f9530 100644
|
|
--- a/net/minecraft/world/entity/ai/goal/target/HurtByTargetGoal.java
|
|
+++ b/net/minecraft/world/entity/ai/goal/target/HurtByTargetGoal.java
|
|
@@ -73,6 +73,46 @@ public class HurtByTargetGoal extends TargetGoal {
|
|
protected void alertOthers() {
|
|
double followDistance = this.getFollowDistance();
|
|
AABB aabb = AABB.unitCubeFromLowerCorner(this.mob.position()).inflate(followDistance, 10.0, followDistance);
|
|
+
|
|
+ // Leaf start - Async target finding
|
|
+ if (org.dreeam.leaf.config.modules.async.AsyncTargetFinding.alertOther) {
|
|
+ final var self = this.mob;
|
|
+ final var ctx = self.getGoalCtx();
|
|
+ if (!ctx.state) return;
|
|
+ final var serverLevel = getServerLevel(self);
|
|
+ final var toIgnoreAlert = this.toIgnoreAlert;
|
|
+ ctx.wake = () -> {
|
|
+ var toAlert = new java.util.ArrayList<Mob>();
|
|
+ List<? extends Mob> entitiesOfClass = serverLevel
|
|
+ .getEntitiesOfClass(self.getClass(), aabb, EntitySelector.NO_SPECTATORS);
|
|
+ for (Mob mob : entitiesOfClass) {
|
|
+ if (self == mob
|
|
+ || mob.getTarget() != null
|
|
+ || (self instanceof TamableAnimal && ((TamableAnimal) self).getOwner() != ((TamableAnimal) mob).getOwner())
|
|
+ || mob.isAlliedTo(self.getLastHurtByMob())) {
|
|
+ continue;
|
|
+ }
|
|
+ if (toIgnoreAlert == null) {
|
|
+ toAlert.add(mob);
|
|
+ continue;
|
|
+ }
|
|
+ boolean flag = false;
|
|
+ for (Class<?> clazz : toIgnoreAlert) {
|
|
+ if (mob.getClass() == clazz) {
|
|
+ flag = true;
|
|
+ break;
|
|
+ }
|
|
+ }
|
|
+ if (!flag) {
|
|
+ toAlert.add(mob);
|
|
+ }
|
|
+ }
|
|
+ return toAlert;
|
|
+ };
|
|
+ return;
|
|
+ }
|
|
+ // Leaf end - Async target finding
|
|
+
|
|
List<? extends Mob> entitiesOfClass = this.mob
|
|
.level()
|
|
.getEntitiesOfClass((Class<? extends Mob>)this.mob.getClass(), aabb, EntitySelector.NO_SPECTATORS);
|
|
@@ -87,7 +127,7 @@ public class HurtByTargetGoal extends TargetGoal {
|
|
|
|
mob = (Mob)var5.next();
|
|
if (this.mob != mob
|
|
- && mob.getTarget() == null
|
|
+ && mob.getTarget() == null // Leaf - Async target finding - diff on change
|
|
&& (!(this.mob instanceof TamableAnimal) || ((TamableAnimal)this.mob).getOwner() == ((TamableAnimal)mob).getOwner())
|
|
&& !mob.isAlliedTo(this.mob.getLastHurtByMob())) {
|
|
if (this.toIgnoreAlert == null) {
|
|
@@ -96,7 +136,7 @@ public class HurtByTargetGoal extends TargetGoal {
|
|
|
|
boolean flag = false;
|
|
|
|
- for (Class<?> clazz : this.toIgnoreAlert) {
|
|
+ for (Class<?> clazz : this.toIgnoreAlert) { // Leaf - Async target finding - diff on change
|
|
if (mob.getClass() == clazz) {
|
|
flag = true;
|
|
break;
|
|
@@ -113,6 +153,36 @@ public class HurtByTargetGoal extends TargetGoal {
|
|
}
|
|
}
|
|
|
|
+ // Leaf start - Async target finding
|
|
+ public void poll() {
|
|
+ if (!(this.mob.getGoalCtx().result() instanceof List<?> toAlert)) return;
|
|
+ LivingEntity lastHurtByMob = this.mob.getLastHurtByMob();
|
|
+ if (lastHurtByMob == null || (lastHurtByMob.getType() == EntityType.PLAYER && getServerLevel(this.mob).getGameRules().getBoolean(GameRules.RULE_UNIVERSAL_ANGER))) {
|
|
+ return;
|
|
+ }
|
|
+ for (Class<?> clazz : this.toIgnoreDamage) {
|
|
+ if (clazz.isAssignableFrom(lastHurtByMob.getClass())) {
|
|
+ return;
|
|
+ }
|
|
+ }
|
|
+ if (this.mob.getTarget() == null) {
|
|
+ return;
|
|
+ }
|
|
+ if (!canContinueToUse()) {
|
|
+ return;
|
|
+ }
|
|
+ if (!this.canAttack(lastHurtByMob, HURT_BY_TARGETING)) {
|
|
+ return;
|
|
+ }
|
|
+ for (var obj : toAlert) {
|
|
+ Mob mob = (Mob) obj;
|
|
+ if (mob.getTarget() == null) {
|
|
+ alertOther(mob, lastHurtByMob);
|
|
+ }
|
|
+ }
|
|
+ }
|
|
+ // Leaf end - Async target finding
|
|
+
|
|
protected void alertOther(Mob mob, LivingEntity target) {
|
|
mob.setTarget(target, org.bukkit.event.entity.EntityTargetEvent.TargetReason.TARGET_ATTACKED_NEARBY_ENTITY, true); // 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 85eae0a14f7a417dfd8c911079d05354a98e5834..4eb56c456f9b71b3d6d8f7d8e089d08ac68a236f 100644
|
|
--- a/net/minecraft/world/entity/ai/goal/target/NearestAttackableTargetGoal.java
|
|
+++ b/net/minecraft/world/entity/ai/goal/target/NearestAttackableTargetGoal.java
|
|
@@ -41,12 +41,54 @@ public class NearestAttackableTargetGoal<T extends LivingEntity> extends TargetG
|
|
this.targetConditions = TargetingConditions.forCombat().range(this.getFollowDistance()).selector(selector);
|
|
}
|
|
|
|
+ // Leaf start - Async target finding
|
|
+ protected boolean poll() {
|
|
+ if (!(this.mob.getGoalCtx().result() instanceof LivingEntity target)) return false;
|
|
+ ServerLevel serverLevel = getServerLevel(this.mob);
|
|
+ if (serverLevel == null || !target.isAlive() || !this.getTargetConditions().test(serverLevel, this.mob, target)) return false;
|
|
+ this.target = target;
|
|
+ return true;
|
|
+ }
|
|
+
|
|
+ protected void findTargetAsync() {
|
|
+ // Leaf start - Async target finding
|
|
+ if (!org.dreeam.leaf.config.modules.async.AsyncTargetFinding.searchEntity) {
|
|
+ this.findTarget();
|
|
+ return;
|
|
+ }
|
|
+ this.target = null;
|
|
+ // Leaf end - Async target finding
|
|
+ final Mob mob = this.mob;
|
|
+ final var ctx = mob.getGoalCtx();
|
|
+ if (!ctx.state) return;
|
|
+ final double x = mob.getX();
|
|
+ final double y = mob.getEyeY();
|
|
+ final double z = mob.getZ();
|
|
+ final TargetingConditions targetConditions = this.getTargetConditions().copy();
|
|
+ final Class<T> targetType = this.targetType;
|
|
+ final AABB targetSearch = getTargetSearchArea(this.getFollowDistance());
|
|
+ final ServerLevel serverLevel = getServerLevel(mob);
|
|
+ ctx.wake = () -> {
|
|
+ if (targetType != Player.class && targetType != ServerPlayer.class) {
|
|
+ return serverLevel.getNearestEntity(mob.level().getEntitiesOfClass(targetType, targetSearch, entity -> entity != null && entity != mob && entity.isAlive()), targetConditions, mob, x, y, z);
|
|
+ } else {
|
|
+ return serverLevel.getNearestPlayer(targetConditions, mob, x, y, z);
|
|
+ }
|
|
+ };
|
|
+ }
|
|
+ // Leaf end - Async target finding
|
|
+
|
|
@Override
|
|
public boolean canUse() {
|
|
+ // Leaf start - Async target finding
|
|
+ if (poll()) {
|
|
+ return true;
|
|
+ }
|
|
+ // Leaf end - Async target finding
|
|
if (this.randomInterval > 0 && this.mob.getRandom().nextInt(this.randomInterval) != 0) {
|
|
return false;
|
|
} else {
|
|
- this.findTarget();
|
|
+ this.findTargetAsync(); // Leaf - Async target finding
|
|
return this.target != null;
|
|
}
|
|
}
|
|
@@ -57,6 +99,7 @@ public class NearestAttackableTargetGoal<T extends LivingEntity> extends TargetG
|
|
|
|
protected void findTarget() {
|
|
ServerLevel serverLevel = getServerLevel(this.mob);
|
|
+
|
|
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..84c7b89e7c894c0f544cf0ffcf9dff3f6a5919cc 100644
|
|
--- a/net/minecraft/world/entity/ai/goal/target/NearestHealableRaiderTargetGoal.java
|
|
+++ b/net/minecraft/world/entity/ai/goal/target/NearestHealableRaiderTargetGoal.java
|
|
@@ -23,12 +23,20 @@ public class NearestHealableRaiderTargetGoal<T extends LivingEntity> extends Nea
|
|
|
|
@Override
|
|
public boolean canUse() {
|
|
- if (this.cooldown > 0 || !this.mob.getRandom().nextBoolean()) {
|
|
+ // Leaf start - Async target finding
|
|
+ if (this.cooldown > 0) {
|
|
+ return false;
|
|
+ }
|
|
+ if (poll()) {
|
|
+ return ((Raider) this.mob).hasActiveRaid();
|
|
+ }
|
|
+ if (/*this.cooldown > 0 || */ !this.mob.getRandom().nextBoolean()) {
|
|
+ // Leaf end - Async target finding
|
|
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
|
|
+++ b/net/minecraft/world/entity/ai/goal/target/NonTameRandomTargetGoal.java
|
|
@@ -20,6 +20,15 @@ public class NonTameRandomTargetGoal<T extends LivingEntity> extends NearestAtta
|
|
|
|
@Override
|
|
public boolean canContinueToUse() {
|
|
- return this.targetConditions != null ? this.targetConditions.test(getServerLevel(this.mob), this.mob, this.target) : super.canContinueToUse();
|
|
+ // Leaf start - Async target finding
|
|
+ if (this.target == null || !this.target.isAlive() || this.target.isRemoved()) {
|
|
+ return false;
|
|
+ }
|
|
+ var serverLevel = getServerLevel(this.mob);
|
|
+ if (serverLevel == null) {
|
|
+ return false;
|
|
+ }
|
|
+ return this.getTargetConditions().test(serverLevel, this.mob, this.target) && super.canContinueToUse();
|
|
+ // Leaf end - Async target finding
|
|
}
|
|
}
|
|
diff --git a/net/minecraft/world/entity/ai/goal/target/ResetUniversalAngerTargetGoal.java b/net/minecraft/world/entity/ai/goal/target/ResetUniversalAngerTargetGoal.java
|
|
index 61a7bb5f1760d47a13b6aafc123bcf3dff420860..08a09dea0dd377adca22b4432cf8f57c3122ec02 100644
|
|
--- a/net/minecraft/world/entity/ai/goal/target/ResetUniversalAngerTargetGoal.java
|
|
+++ b/net/minecraft/world/entity/ai/goal/target/ResetUniversalAngerTargetGoal.java
|
|
@@ -37,6 +37,27 @@ public class ResetUniversalAngerTargetGoal<T extends Mob & NeutralMob> extends G
|
|
this.lastHurtByPlayerTimestamp = this.mob.getLastHurtByMobTimestamp();
|
|
this.mob.forgetCurrentTargetAndRefreshUniversalAnger();
|
|
if (this.alertOthersOfSameType) {
|
|
+ // Leaf start - Async target finding
|
|
+ if (org.dreeam.leaf.config.modules.async.AsyncTargetFinding.alertOther) {
|
|
+ final var mob = this.mob;
|
|
+ final var ctx = mob.getGoalCtx();
|
|
+ if (!ctx.state) return;
|
|
+ final var serverLevel = getServerLevel(mob);
|
|
+ final double followRange = this.mob.getAttributeValue(Attributes.FOLLOW_RANGE);
|
|
+ final AABB aabb = AABB.unitCubeFromLowerCorner(this.mob.position()).inflate(followRange, 10.0, followRange);
|
|
+ ctx.wake = () -> {
|
|
+ var entities = serverLevel.getEntitiesOfClass(mob.getClass(), aabb, EntitySelector.NO_SPECTATORS);
|
|
+ List<NeutralMob> toStop = new java.util.ArrayList<>(entities.size());
|
|
+ for (Mob entity : entities) {
|
|
+ if (entity != mob) {
|
|
+ toStop.add((NeutralMob) entity);
|
|
+ }
|
|
+ }
|
|
+ return toStop;
|
|
+ };
|
|
+ return;
|
|
+ }
|
|
+ // Leaf end - Async target finding
|
|
this.getNearbyMobsOfSameType()
|
|
.stream()
|
|
.filter(mob -> mob != this.mob)
|
|
@@ -47,7 +68,19 @@ public class ResetUniversalAngerTargetGoal<T extends Mob & NeutralMob> extends G
|
|
super.start();
|
|
}
|
|
|
|
+ // Leaf start - Async target finding
|
|
+ public void poll() {
|
|
+ if (!(this.mob.getGoalCtx().result() instanceof List toStop)) return;
|
|
+ for (var neutralMob : toStop) {
|
|
+ if (EntitySelector.NO_SPECTATORS.test((net.minecraft.world.entity.Entity) neutralMob)) {
|
|
+ ((NeutralMob) neutralMob).forgetCurrentTargetAndRefreshUniversalAnger();
|
|
+ }
|
|
+ }
|
|
+ }
|
|
+ // Leaf end - Async target finding
|
|
+
|
|
private List<? extends Mob> getNearbyMobsOfSameType() {
|
|
+ // Leaf - Async target finding - diff on change
|
|
double attributeValue = this.mob.getAttributeValue(Attributes.FOLLOW_RANGE);
|
|
AABB aabb = AABB.unitCubeFromLowerCorner(this.mob.position()).inflate(attributeValue, 10.0, attributeValue);
|
|
return this.mob.level().getEntitiesOfClass((Class<? extends Mob>)this.mob.getClass(), aabb, EntitySelector.NO_SPECTATORS);
|
|
diff --git a/net/minecraft/world/entity/ai/sensing/Sensing.java b/net/minecraft/world/entity/ai/sensing/Sensing.java
|
|
index 002d3c0d8b1107a275020d5c582c37e9a5c536ee..6fa0b8defbd1d06b3bf5d9b32ffd08f32c1fb707 100644
|
|
--- a/net/minecraft/world/entity/ai/sensing/Sensing.java
|
|
+++ b/net/minecraft/world/entity/ai/sensing/Sensing.java
|
|
@@ -33,6 +33,17 @@ public class Sensing {
|
|
}
|
|
|
|
public void tick() {
|
|
+ // Leaf start - Async target finding
|
|
+ if (org.dreeam.leaf.config.modules.async.AsyncTargetFinding.enabled) {
|
|
+ synchronized (this) {
|
|
+ tickInternal();
|
|
+ }
|
|
+ } else {
|
|
+ tickInternal();
|
|
+ }
|
|
+ }
|
|
+ private void tickInternal() {
|
|
+ // Leaf end - Async target finding
|
|
if (this.expiring == null) { // Gale - Petal - reduce line of sight updates
|
|
this.seen.clear();
|
|
// Gale start - Petal - reduce line of sight updates
|
|
@@ -63,6 +74,17 @@ public class Sensing {
|
|
}
|
|
|
|
public boolean hasLineOfSight(Entity entity) {
|
|
+ // Leaf start - Async target finding
|
|
+ if (org.dreeam.leaf.config.modules.async.AsyncTargetFinding.enabled) {
|
|
+ synchronized (this) {
|
|
+ return hasLineOfSightInternal(entity);
|
|
+ }
|
|
+ } else {
|
|
+ return hasLineOfSightInternal(entity);
|
|
+ }
|
|
+ }
|
|
+ private boolean hasLineOfSightInternal(Entity entity) {
|
|
+ // Leaf end - Async target finding
|
|
int id = entity.getId();
|
|
// Gale start - Petal - reduce line of sight cache lookups - merge sets
|
|
int cached = this.seen.get(id);
|
|
diff --git a/net/minecraft/world/entity/animal/Fox.java b/net/minecraft/world/entity/animal/Fox.java
|
|
index 90452f0945e761077608692877677f522d38bccd..0a6ca6eb0775a7bc9e3070b70444c170a6504eca 100644
|
|
--- a/net/minecraft/world/entity/animal/Fox.java
|
|
+++ b/net/minecraft/world/entity/animal/Fox.java
|
|
@@ -849,13 +849,18 @@ public class Fox extends Animal implements VariantHolder<Fox.Variant> {
|
|
return false;
|
|
} else {
|
|
ServerLevel serverLevel = getServerLevel(Fox.this.level());
|
|
+ // Leaf start - Async target finding
|
|
+ if (serverLevel == null) {
|
|
+ return false;
|
|
+ }
|
|
+ // Leaf end - Async target finding
|
|
|
|
for (UUID uuid : Fox.this.getTrustedUUIDs()) {
|
|
if (serverLevel.getEntity(uuid) instanceof LivingEntity livingEntity) {
|
|
this.trustedLastHurt = livingEntity;
|
|
this.trustedLastHurtBy = livingEntity.getLastHurtByMob();
|
|
int lastHurtByMobTimestamp = livingEntity.getLastHurtByMobTimestamp();
|
|
- return lastHurtByMobTimestamp != this.timestamp && this.canAttack(this.trustedLastHurtBy, this.targetConditions);
|
|
+ return lastHurtByMobTimestamp != this.timestamp && this.canAttack(this.trustedLastHurtBy, this.getTargetConditions()); // Leaf - Async target finding
|
|
}
|
|
}
|
|
|
|
@@ -1033,7 +1038,7 @@ public class Fox extends Animal implements VariantHolder<Fox.Variant> {
|
|
@Override
|
|
protected boolean isValidTarget(LevelReader level, BlockPos pos) {
|
|
BlockState blockState = level.getBlockState(pos);
|
|
- return blockState.is(Blocks.SWEET_BERRY_BUSH) && blockState.getValue(SweetBerryBushBlock.AGE) >= 2 || CaveVines.hasGlowBerries(blockState);
|
|
+ return blockState.is(Blocks.SWEET_BERRY_BUSH) && blockState.getValue(SweetBerryBushBlock.AGE) >= 2 || CaveVines.hasGlowBerries(blockState); // Leaf - Async target finding - diff on change
|
|
}
|
|
|
|
@Override
|
|
@@ -1097,6 +1102,13 @@ public class Fox extends Animal implements VariantHolder<Fox.Variant> {
|
|
Fox.this.setSitting(false);
|
|
super.start();
|
|
}
|
|
+
|
|
+ // Leaf start - Async target finding
|
|
+ @Override
|
|
+ protected TypeToCheck typeToCheck() {
|
|
+ return TypeToCheck.FoxEat;
|
|
+ }
|
|
+ // Leaf end - Async target finding
|
|
}
|
|
|
|
class FoxFloatGoal extends FloatGoal {
|
|
diff --git a/net/minecraft/world/entity/animal/Panda.java b/net/minecraft/world/entity/animal/Panda.java
|
|
index 0a6a3060f3690ab2d8439d66e6fd6f0c5dc20688..0fa38df5e2e11765a1d43ccc14b47730c83ae482 100644
|
|
--- a/net/minecraft/world/entity/animal/Panda.java
|
|
+++ b/net/minecraft/world/entity/animal/Panda.java
|
|
@@ -991,9 +991,18 @@ public class Panda extends Animal {
|
|
|
|
@Override
|
|
public boolean canUse() {
|
|
+ // Leaf start - Async target finding
|
|
+ if (poll()) {
|
|
+ return true;
|
|
+ }
|
|
if (this.mob.getRandom().nextFloat() >= this.probability) {
|
|
return false;
|
|
} else {
|
|
+ if (org.dreeam.leaf.config.modules.async.AsyncTargetFinding.searchEntity) {
|
|
+ getLookAsync();
|
|
+ return false;
|
|
+ }
|
|
+ // Leaf end - Async target finding
|
|
if (this.lookAt == null) {
|
|
ServerLevel serverLevel = getServerLevel(this.mob);
|
|
if (this.lookAtType == Player.class) {
|
|
diff --git a/net/minecraft/world/entity/animal/Rabbit.java b/net/minecraft/world/entity/animal/Rabbit.java
|
|
index da5b32a17283e540615373097acc511d928aeff5..6211c6e41161852c06330f61b21969ac9a49cad8 100644
|
|
--- a/net/minecraft/world/entity/animal/Rabbit.java
|
|
+++ b/net/minecraft/world/entity/animal/Rabbit.java
|
|
@@ -642,7 +642,13 @@ public class Rabbit extends Animal implements VariantHolder<Rabbit.Variant> {
|
|
this.wantsToRaid = this.rabbit.wantsMoreFood();
|
|
}
|
|
|
|
- return super.canUse();
|
|
+ // Leaf start - Async target finding
|
|
+ if (this.wantsToRaid && !this.canRaid) {
|
|
+ return super.canUse();
|
|
+ } else {
|
|
+ return false;
|
|
+ }
|
|
+ // Leaf end - Async target finding
|
|
}
|
|
|
|
@Override
|
|
@@ -685,7 +691,7 @@ public class Rabbit extends Animal implements VariantHolder<Rabbit.Variant> {
|
|
@Override
|
|
protected boolean isValidTarget(LevelReader level, BlockPos pos) {
|
|
BlockState blockState = level.getBlockState(pos);
|
|
- if (blockState.is(Blocks.FARMLAND) && this.wantsToRaid && !this.canRaid) {
|
|
+ if (blockState.is(Blocks.FARMLAND) && this.wantsToRaid && !this.canRaid) { // Leaf - Async target finding - diff on change
|
|
blockState = level.getBlockState(pos.above());
|
|
if (blockState.getBlock() instanceof CarrotBlock && ((CarrotBlock)blockState.getBlock()).isMaxAge(blockState)) {
|
|
this.canRaid = true;
|
|
@@ -695,6 +701,13 @@ public class Rabbit extends Animal implements VariantHolder<Rabbit.Variant> {
|
|
|
|
return false;
|
|
}
|
|
+
|
|
+ // Leaf start - Async target finding
|
|
+ @Override
|
|
+ protected TypeToCheck typeToCheck() {
|
|
+ return TypeToCheck.RaidGarden;
|
|
+ }
|
|
+ // Leaf end - Async target finding
|
|
}
|
|
|
|
public static enum Variant implements StringRepresentable {
|
|
diff --git a/net/minecraft/world/entity/animal/Turtle.java b/net/minecraft/world/entity/animal/Turtle.java
|
|
index 10477fea8fcd70bf0c1ba4b6e1113625be690e68..a7c1f02e3e7346555d7f822acfb15b6dea312dd5 100644
|
|
--- a/net/minecraft/world/entity/animal/Turtle.java
|
|
+++ b/net/minecraft/world/entity/animal/Turtle.java
|
|
@@ -527,8 +527,15 @@ public class Turtle extends Animal {
|
|
|
|
@Override
|
|
protected boolean isValidTarget(LevelReader level, BlockPos pos) {
|
|
- return level.getBlockState(pos).is(Blocks.WATER);
|
|
+ return level.getBlockState(pos).is(Blocks.WATER); // Leaf - Async target finding - diff on change
|
|
}
|
|
+
|
|
+ // Leaf start - Async target finding
|
|
+ @Override
|
|
+ protected TypeToCheck typeToCheck() {
|
|
+ return TypeToCheck.TurtleToWater;
|
|
+ }
|
|
+ // Leaf end - Async target finding
|
|
}
|
|
|
|
static class TurtleLayEggGoal extends MoveToBlockGoal {
|
|
@@ -584,8 +591,15 @@ public class Turtle extends Animal {
|
|
|
|
@Override
|
|
protected boolean isValidTarget(LevelReader level, BlockPos pos) {
|
|
- return level.isEmptyBlock(pos.above()) && TurtleEggBlock.isSand(level, pos);
|
|
+ return level.isEmptyBlock(pos.above()) && TurtleEggBlock.isSand(level, pos); // Leaf - Async target finding - diff on change
|
|
+ }
|
|
+
|
|
+ // Leaf start - Async target finding
|
|
+ @Override
|
|
+ protected TypeToCheck typeToCheck() {
|
|
+ return TypeToCheck.TurtleLay;
|
|
}
|
|
+ // Leaf end - Async target finding
|
|
}
|
|
|
|
static class TurtleMoveControl extends org.purpurmc.purpur.controller.MoveControllerWASD { // Purpur - Ridables
|
|
diff --git a/net/minecraft/world/entity/animal/Wolf.java b/net/minecraft/world/entity/animal/Wolf.java
|
|
index 7cb292de6b27fa4ba3c5fce526a4e939c576789f..187707845c4ed8cc3be439a12633ba9c446d075f 100644
|
|
--- a/net/minecraft/world/entity/animal/Wolf.java
|
|
+++ b/net/minecraft/world/entity/animal/Wolf.java
|
|
@@ -672,7 +672,7 @@ public class Wolf extends TamableAnimal implements NeutralMob, VariantHolder<Hol
|
|
|
|
@Override
|
|
public boolean isFood(ItemStack stack) {
|
|
- return stack.is(ItemTags.WOLF_FOOD);
|
|
+ return stack.is(ItemTags.WOLF_FOOD); // Leaf - Async target finding - diff on change
|
|
}
|
|
|
|
@Override
|
|
diff --git a/net/minecraft/world/entity/monster/Drowned.java b/net/minecraft/world/entity/monster/Drowned.java
|
|
index d44ed0d6a672a0b1eb0a8781e3e094096a2b753d..04f20d4ff0e5fa8ddd5234549fba742fe219e0d1 100644
|
|
--- a/net/minecraft/world/entity/monster/Drowned.java
|
|
+++ b/net/minecraft/world/entity/monster/Drowned.java
|
|
@@ -394,7 +394,7 @@ public class Drowned extends Zombie implements RangedAttackMob {
|
|
@Override
|
|
protected boolean isValidTarget(LevelReader level, BlockPos pos) {
|
|
BlockPos blockPos = pos.above();
|
|
- return level.isEmptyBlock(blockPos) && level.isEmptyBlock(blockPos.above()) && level.getBlockState(pos).entityCanStandOn(level, pos, this.drowned);
|
|
+ return level.isEmptyBlock(blockPos) && level.isEmptyBlock(blockPos.above()) && level.getBlockState(pos).entityCanStandOn(level, pos, this.drowned); // Leaf - Async target finding - diff on change
|
|
}
|
|
|
|
@Override
|
|
@@ -408,6 +408,13 @@ public class Drowned extends Zombie implements RangedAttackMob {
|
|
public void stop() {
|
|
super.stop();
|
|
}
|
|
+
|
|
+ // Leaf start - Async target finding
|
|
+ @Override
|
|
+ protected TypeToCheck typeToCheck() {
|
|
+ return TypeToCheck.Drowned;
|
|
+ }
|
|
+ // Leaf end - Async target finding
|
|
}
|
|
|
|
static class DrownedGoToWaterGoal extends Goal {
|
|
diff --git a/net/minecraft/world/entity/monster/EnderMan.java b/net/minecraft/world/entity/monster/EnderMan.java
|
|
index c7897532163d4fdf5a82982f7d24a47dd61e3dfa..b3eb6409e94dbfa18a356e2715efd2a5aa1e9b24 100644
|
|
--- a/net/minecraft/world/entity/monster/EnderMan.java
|
|
+++ b/net/minecraft/world/entity/monster/EnderMan.java
|
|
@@ -597,10 +597,34 @@ public class EnderMan extends Monster implements NeutralMob {
|
|
|
|
@Override
|
|
public boolean canUse() {
|
|
+ // Leaf start - Async target finding
|
|
+ if (org.dreeam.leaf.config.modules.async.AsyncTargetFinding.searchEntity) {
|
|
+ if (poll()) {
|
|
+ return true;
|
|
+ }
|
|
+ final EnderMan enderman = this.enderman;
|
|
+ final var ctx = enderman.getGoalCtx();
|
|
+ if (!ctx.state) return false;
|
|
+ final var level = getServerLevel(this.enderman);
|
|
+ final var cond = this.startAggroTargetConditions.range(this.getFollowDistance()).copy();
|
|
+ ctx.wake = () -> level.getNearestPlayer(cond, enderman);
|
|
+ return false;
|
|
+ }
|
|
+ // Leaf end - Async target finding
|
|
this.pendingTarget = getServerLevel(this.enderman).getNearestPlayer(this.startAggroTargetConditions.range(this.getFollowDistance()), this.enderman);
|
|
return this.pendingTarget != null;
|
|
}
|
|
|
|
+ // Leaf start - Async target finding
|
|
+ protected boolean poll() {
|
|
+ if (!(this.mob.getGoalCtx().result() instanceof Player player)) return false;
|
|
+ var serverLevel = getServerLevel(this.enderman);
|
|
+ if (!this.startAggroTargetConditions.range(this.getFollowDistance()).test(serverLevel, enderman, player)) return false;
|
|
+ this.pendingTarget = player;
|
|
+ return true;
|
|
+ }
|
|
+ // Leaf end - Async target finding
|
|
+
|
|
@Override
|
|
public void start() {
|
|
this.aggroTime = this.adjustedTickDelay(5);
|
|
diff --git a/net/minecraft/world/entity/monster/Strider.java b/net/minecraft/world/entity/monster/Strider.java
|
|
index ae4ee948971e931e4fdc4ec2187f5182195c626c..041079bf35822e75b5464a6336f517b38cb19363 100644
|
|
--- a/net/minecraft/world/entity/monster/Strider.java
|
|
+++ b/net/minecraft/world/entity/monster/Strider.java
|
|
@@ -570,8 +570,15 @@ public class Strider extends Animal implements ItemSteerable, Saddleable {
|
|
|
|
@Override
|
|
protected boolean isValidTarget(LevelReader level, BlockPos pos) {
|
|
- return level.getBlockState(pos).is(Blocks.LAVA) && level.getBlockState(pos.above()).isPathfindable(PathComputationType.LAND);
|
|
+ return level.getBlockState(pos).is(Blocks.LAVA) && level.getBlockState(pos.above()).isPathfindable(PathComputationType.LAND); // Leaf - Async target finding - diff on change
|
|
}
|
|
+
|
|
+ // Leaf start - Async target finding
|
|
+ @Override
|
|
+ protected TypeToCheck typeToCheck() {
|
|
+ return TypeToCheck.Strider;
|
|
+ }
|
|
+ // Leaf end - Async target finding
|
|
}
|
|
|
|
static class StriderPathNavigation extends GroundPathNavigation {
|
|
diff --git a/net/minecraft/world/level/Level.java b/net/minecraft/world/level/Level.java
|
|
index 41b67fda739ca98383f9f795d97b01969e85e609..f679ed1fb96e135b044359e55d69bfa803a6b215 100644
|
|
--- a/net/minecraft/world/level/Level.java
|
|
+++ b/net/minecraft/world/level/Level.java
|
|
@@ -1817,9 +1817,11 @@ public abstract class Level implements LevelAccessor, AutoCloseable, ca.spottedl
|
|
|
|
@Override
|
|
public List<Entity> getEntities(@Nullable Entity entity, AABB boundingBox, Predicate<? super Entity> predicate) {
|
|
- if (org.dreeam.leaf.config.modules.async.SparklyPaperParallelWorldTicking.enabled) // Leaf start - SparklyPaper - parallel world ticking mod (make configurable)
|
|
- ca.spottedleaf.moonrise.common.util.TickThread.ensureTickThread((ServerLevel)this, boundingBox, "Cannot getEntities asynchronously"); // SparklyPaper - parallel world ticking (additional concurrency issues logs)
|
|
- List<Entity> list = Lists.newArrayList();
|
|
+ // Leaf start - Async target finding
|
|
+ // if (org.dreeam.leaf.config.modules.async.SparklyPaperParallelWorldTicking.enabled) // Leaf start - SparklyPaper - parallel world ticking mod (make configurable)
|
|
+ // ca.spottedleaf.moonrise.common.util.TickThread.ensureTickThread((ServerLevel)this, boundingBox, "Cannot getEntities asynchronously"); // SparklyPaper - parallel world ticking (additional concurrency issues logs)
|
|
+ // List<Entity> list = Lists.newArrayList(); // Leaf - Async target finding - unused
|
|
+ // Leaf end - Async target finding
|
|
|
|
// Paper start - rewrite chunk system
|
|
final List<Entity> ret = new java.util.ArrayList<>();
|