mirror of
https://github.com/Winds-Studio/Leaf.git
synced 2025-12-19 15:09:25 +00:00
244 lines
13 KiB
Diff
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 13bcd8653d766cd0b754a22e9aab261fbc62b0a5..13542755864904b343b0aeea957e2a76e977a51f 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 0fd51020ca9480be2855eb58a7d4d43511829512..52a2b993bbd1ad4851b3273af6ecbc069beb5b84 100644
|
|
--- a/net/minecraft/server/level/ServerChunkCache.java
|
|
+++ b/net/minecraft/server/level/ServerChunkCache.java
|
|
@@ -878,14 +878,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 7003b532182737a745491e397a967b72e6b308aa..3ed9652510976770f5661dd7b317f27f046700d4 100644
|
|
--- a/net/minecraft/world/entity/animal/Dolphin.java
|
|
+++ b/net/minecraft/world/entity/animal/Dolphin.java
|
|
@@ -500,6 +500,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;
|
|
@@ -519,6 +523,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 treasurePos = this.dolphin.getTreasurePos();
|
|
return !BlockPos.containing(treasurePos.getX(), this.dolphin.getY(), treasurePos.getZ()).closerToCenterThan(this.dolphin.position(), 4.0)
|
|
&& !this.stuck
|
|
@@ -532,6 +541,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.setTreasurePos(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.setTreasurePos(blockPos1);
|
|
@@ -544,6 +569,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 treasurePos = this.dolphin.getTreasurePos();
|
|
if (BlockPos.containing(treasurePos.getX(), this.dolphin.getY(), treasurePos.getZ()).closerToCenterThan(this.dolphin.position(), 4.0) || this.stuck
|
|
)
|
|
@@ -554,6 +585,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
|
|
Level level = this.dolphin.level();
|
|
if (this.dolphin.closeToNextPos() || this.dolphin.getNavigation().isDone()) {
|
|
Vec3 vec3 = Vec3.atCenterOf(this.dolphin.getTreasurePos());
|
|
diff --git a/net/minecraft/world/entity/projectile/EyeOfEnder.java b/net/minecraft/world/entity/projectile/EyeOfEnder.java
|
|
index 01a9bad80a30a7879a69b800258b616b4d986108..d4f49e40461a165ebd6635e9fec8fe56d7f1acf6 100644
|
|
--- a/net/minecraft/world/entity/projectile/EyeOfEnder.java
|
|
+++ b/net/minecraft/world/entity/projectile/EyeOfEnder.java
|
|
@@ -26,6 +26,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);
|
|
@@ -112,6 +113,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 02f2c38b5f9a503097dea44d5c79518b03b63f9a..a7aa09d7c9d5f95706349e426cd54a79e963c6f9 100644
|
|
--- a/net/minecraft/world/item/EnderEyeItem.java
|
|
+++ b/net/minecraft/world/item/EnderEyeItem.java
|
|
@@ -103,14 +103,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)) {
|
|
@@ -124,7 +157,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;
|