9
0
mirror of https://github.com/Winds-Studio/Leaf.git synced 2025-12-19 15:09:25 +00:00
Files
Leaf/leaf-server/minecraft-patches/features/0177-Asynchronous-locator.patch
2025-06-28 10:37:38 +08:00

244 lines
13 KiB
Diff

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: HaHaWTH <102713261+HaHaWTH@users.noreply.github.com>
Date: Wed, 23 Oct 2024 23:54:00 +0800
Subject: [PATCH] Asynchronous locator
Original license: MIT
Original project: https://github.com/thebrightspark/AsyncLocator
diff --git a/net/minecraft/server/commands/LocateCommand.java b/net/minecraft/server/commands/LocateCommand.java
index 2723d5377567241921fef61952e474c1c0ee9bbf..548a33b857aef542279255368031a095037b76bb 100644
--- a/net/minecraft/server/commands/LocateCommand.java
+++ b/net/minecraft/server/commands/LocateCommand.java
@@ -109,6 +109,34 @@ public class LocateCommand {
BlockPos blockPos = BlockPos.containing(source.getPosition());
ServerLevel level = source.getLevel();
Stopwatch stopwatch = Stopwatch.createStarted(Util.TICKER);
+ // Leaf start - Asynchronous locator
+ if (org.dreeam.leaf.config.modules.async.AsyncLocator.enabled) {
+ BlockPos originPos = BlockPos.containing(source.getPosition());
+ org.dreeam.leaf.async.locate.AsyncLocator.locate(source.getLevel(), holderSet, originPos, 100, false)
+ .thenOnServerThread(pair -> {
+ stopwatch.stop();
+ if (pair != null) {
+ showLocateResult(
+ source,
+ structure,
+ originPos,
+ pair,
+ "commands.locate.structure.success",
+ false,
+ stopwatch.elapsed()
+ );
+ } else {
+ source.sendFailure(
+ Component.literal(
+ ERROR_STRUCTURE_NOT_FOUND.create(structure.asPrintable()).getMessage()
+ )
+ );
+ }
+ });
+
+ return 0;
+ }
+ // Leaf end - Asynchronous locator
Pair<BlockPos, Holder<Structure>> pair = level.getChunkSource().getGenerator().findNearestMapStructure(level, holderSet, blockPos, 100, false);
stopwatch.stop();
if (pair == null) {
diff --git a/net/minecraft/server/level/ServerChunkCache.java b/net/minecraft/server/level/ServerChunkCache.java
index 6d5a15122079f2d1568ceb7086db21ad454f58e6..ecab2befa1f2f993ea4b4d088529745c2a37b73d 100644
--- a/net/minecraft/server/level/ServerChunkCache.java
+++ b/net/minecraft/server/level/ServerChunkCache.java
@@ -848,14 +848,25 @@ public class ServerChunkCache extends ChunkSource implements ca.spottedleaf.moon
@Override
public boolean pollTask() {
+ // Leaf start - Async Locator
// Paper start - rewrite chunk system
- final ServerChunkCache serverChunkCache = ServerChunkCache.this;
- if (serverChunkCache.runDistanceManagerUpdates()) {
- return true;
+ java.util.function.Supplier<Boolean> supplier = () -> {
+ final ServerChunkCache serverChunkCache = ServerChunkCache.this;
+ if (serverChunkCache.runDistanceManagerUpdates()) {
+ return true;
+ } else {
+ return super.pollTask() | ((ca.spottedleaf.moonrise.patches.chunk_system.level.ChunkSystemServerLevel) serverChunkCache.level).moonrise$getChunkTaskScheduler().executeMainThreadTask();
+ }
+ };
+ if (org.dreeam.leaf.config.modules.async.AsyncLocator.enabled && Thread.currentThread() instanceof org.dreeam.leaf.async.locate.AsyncLocator.AsyncLocatorThread) {
+ return MinecraftServer.getServer().scheduleWithResult((java.util.concurrent.CompletableFuture<Boolean> future) -> {
+ future.complete(supplier.get());
+ }).join();
} else {
- return super.pollTask() | ((ca.spottedleaf.moonrise.patches.chunk_system.level.ChunkSystemServerLevel)serverChunkCache.level).moonrise$getChunkTaskScheduler().executeMainThreadTask();
+ return supplier.get();
}
// Paper end - rewrite chunk system
+ // Leaf end - Async Locator
}
}
}
diff --git a/net/minecraft/world/entity/animal/Dolphin.java b/net/minecraft/world/entity/animal/Dolphin.java
index 87ba416479df56bad5d13c01e96e92e45b7802a3..2508645b62e7f935dee00fe87b3a6446dbd22cf2 100644
--- a/net/minecraft/world/entity/animal/Dolphin.java
+++ b/net/minecraft/world/entity/animal/Dolphin.java
@@ -486,6 +486,10 @@ public class Dolphin extends AgeableWaterCreature {
static class DolphinSwimToTreasureGoal extends Goal {
private final Dolphin dolphin;
private boolean stuck;
+ // Leaf start - Asynchronous locator
+ @Nullable
+ private org.dreeam.leaf.async.locate.AsyncLocator.LocateTask<?> asyncLocator$locateTask;
+ // Leaf end - Asynchronous locator
DolphinSwimToTreasureGoal(Dolphin dolphin) {
this.dolphin = dolphin;
@@ -505,6 +509,11 @@ public class Dolphin extends AgeableWaterCreature {
@Override
public boolean canContinueToUse() {
+ // Leaf start - Asynchronous locator
+ if (org.dreeam.leaf.config.modules.async.AsyncLocator.enabled && this.asyncLocator$locateTask != null) {
+ return true;
+ }
+ // Leaf end - Asynchronous locator
BlockPos blockPos = this.dolphin.treasurePos;
return blockPos != null
&& !BlockPos.containing(blockPos.getX(), this.dolphin.getY(), blockPos.getZ()).closerToCenterThan(this.dolphin.position(), 4.0)
@@ -519,6 +528,22 @@ public class Dolphin extends AgeableWaterCreature {
this.stuck = false;
this.dolphin.getNavigation().stop();
BlockPos blockPos = this.dolphin.blockPosition();
+ // Leaf start - Asynchronous locator
+ if (org.dreeam.leaf.config.modules.async.AsyncLocator.enabled) {
+ asyncLocator$locateTask = org.dreeam.leaf.async.locate.AsyncLocator.locate(serverLevel, StructureTags.DOLPHIN_LOCATED, blockPos, 50, false)
+ .thenOnServerThread(pos -> {
+ asyncLocator$locateTask = null;
+ if (pos != null) {
+ this.dolphin.treasurePos = pos;
+ serverLevel.broadcastEntityEvent(this.dolphin, (byte) 38);
+ } else {
+ this.stuck = true;
+ }
+ });
+
+ return;
+ }
+ // Leaf end - Asynchronous locator
BlockPos blockPos1 = serverLevel.findNearestMapStructure(StructureTags.DOLPHIN_LOCATED, blockPos, 50, false);
if (blockPos1 != null) {
this.dolphin.treasurePos = blockPos1;
@@ -531,6 +556,12 @@ public class Dolphin extends AgeableWaterCreature {
@Override
public void stop() {
+ // Leaf start - Asynchronous locator
+ if (org.dreeam.leaf.config.modules.async.AsyncLocator.enabled && this.asyncLocator$locateTask != null) {
+ this.asyncLocator$locateTask.cancel();
+ this.asyncLocator$locateTask = null;
+ }
+ // Leaf end - Asynchronous locator
BlockPos blockPos = this.dolphin.treasurePos;
if (blockPos == null
|| BlockPos.containing(blockPos.getX(), this.dolphin.getY(), blockPos.getZ()).closerToCenterThan(this.dolphin.position(), 4.0)
@@ -541,6 +572,11 @@ public class Dolphin extends AgeableWaterCreature {
@Override
public void tick() {
+ // Leaf start - Asynchronous locator
+ if (org.dreeam.leaf.config.modules.async.AsyncLocator.enabled && this.asyncLocator$locateTask != null) {
+ return;
+ }
+ // Leaf end - Asynchronous locator
if (this.dolphin.treasurePos != null) {
Level level = this.dolphin.level();
if (this.dolphin.closeToNextPos() || this.dolphin.getNavigation().isDone()) {
diff --git a/net/minecraft/world/entity/projectile/EyeOfEnder.java b/net/minecraft/world/entity/projectile/EyeOfEnder.java
index 59941c605085d93357211939114ecf1b88aef05d..74ffcc5417dd20635a52a3e799436eaf18076a0e 100644
--- a/net/minecraft/world/entity/projectile/EyeOfEnder.java
+++ b/net/minecraft/world/entity/projectile/EyeOfEnder.java
@@ -29,6 +29,7 @@ public class EyeOfEnder extends Entity implements ItemSupplier {
public double tz;
public int life;
public boolean surviveAfterDeath;
+ public boolean asyncLocator$locateTaskOngoing = false; // Leaf - Asynchronous locator
public EyeOfEnder(EntityType<? extends EyeOfEnder> entityType, Level level) {
super(entityType, level);
@@ -116,6 +117,11 @@ public class EyeOfEnder extends Entity implements ItemSupplier {
@Override
public void tick() {
super.tick();
+ // Leaf start - Asynchronous locator
+ if (org.dreeam.leaf.config.modules.async.AsyncLocator.enabled && this.asyncLocator$locateTaskOngoing) {
+ return;
+ }
+ // Leaf end - Asynchronous locator
Vec3 deltaMovement = this.getDeltaMovement();
double d = this.getX() + deltaMovement.x;
double d1 = this.getY() + deltaMovement.y;
diff --git a/net/minecraft/world/item/EnderEyeItem.java b/net/minecraft/world/item/EnderEyeItem.java
index 68f33de233b716055f791fd87fe3be981580375c..bd60cc8f7e37cba981792412a8ce7f71ea8b33f5 100644
--- a/net/minecraft/world/item/EnderEyeItem.java
+++ b/net/minecraft/world/item/EnderEyeItem.java
@@ -105,14 +105,47 @@ public class EnderEyeItem extends Item {
} else {
player.startUsingItem(hand);
if (level instanceof ServerLevel serverLevel) {
- BlockPos blockPos = serverLevel.findNearestMapStructure(StructureTags.EYE_OF_ENDER_LOCATED, player.blockPosition(), 100, false);
+ // Leaf start - Asynchronous locator
+ BlockPos blockPos;
+ if (org.dreeam.leaf.config.modules.async.AsyncLocator.enabled) {
+ blockPos = BlockPos.ZERO;
+ } else {
+ blockPos = serverLevel.findNearestMapStructure(StructureTags.EYE_OF_ENDER_LOCATED, player.blockPosition(), 100, false);
+ }
+ // Leaf end - Asynchronous locator
if (blockPos == null) {
return InteractionResult.CONSUME;
}
EyeOfEnder eyeOfEnder = new EyeOfEnder(level, player.getX(), player.getY(0.5), player.getZ());
+
+ // Leaf start - Asynchronous locator
+ final boolean isAsyncLocatorEnabled = org.dreeam.leaf.config.modules.async.AsyncLocator.enabled;
+
+ if (isAsyncLocatorEnabled) {
+ eyeOfEnder.asyncLocator$locateTaskOngoing = true;
+ org.dreeam.leaf.async.locate.AsyncLocator.locate(
+ serverLevel,
+ StructureTags.EYE_OF_ENDER_LOCATED,
+ player.blockPosition(),
+ 100,
+ false
+ ).thenOnServerThread(pos -> {
+ eyeOfEnder.asyncLocator$locateTaskOngoing = false;
+ if (pos != null) {
+ eyeOfEnder.signalTo(pos);
+ CriteriaTriggers.USED_ENDER_EYE.trigger((ServerPlayer) player, pos);
+ player.awardStat(Stats.ITEM_USED.get(this));
+ } else {
+ // Set the entity's life to long enough that it dies
+ eyeOfEnder.life = Integer.MAX_VALUE - 100;
+ }
+ });
+ }
+ // Leaf end - Asynchronous locator
+
eyeOfEnder.setItem(itemInHand);
- eyeOfEnder.signalTo(blockPos);
+ if (!isAsyncLocatorEnabled) eyeOfEnder.signalTo(blockPos); // Leaf - Asynchronous locator
level.gameEvent(GameEvent.PROJECTILE_SHOOT, eyeOfEnder.position(), GameEvent.Context.of(player));
// CraftBukkit start
if (!level.addFreshEntity(eyeOfEnder)) {
@@ -126,7 +159,7 @@ public class EnderEyeItem extends Item {
float f = Mth.lerp(level.random.nextFloat(), 0.33F, 0.5F);
level.playSound(null, player.getX(), player.getY(), player.getZ(), SoundEvents.ENDER_EYE_LAUNCH, SoundSource.NEUTRAL, 1.0F, f);
itemInHand.consume(1, player);
- player.awardStat(Stats.ITEM_USED.get(this));
+ if (!isAsyncLocatorEnabled) player.awardStat(Stats.ITEM_USED.get(this)); // Leaf - Asynchronous locator
}
return InteractionResult.SUCCESS_SERVER;