mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-12-03 03:31:32 +00:00
Stop blocking the main thread when a rise-out-of-ground entity spawns
Ambient spawning calls the spawner on the main thread, where a next-tick future
can never complete. Run the below-ground check through the region future, which
runs inline on the owning thread.
Upstream: 0c760d925
This commit is contained in:
1 parent
9b86b34187
commit
e004ae1775
2 files changed
+48
-2
No files matched your search
@@ -83,6 +83,7 @@ import static art.arcane.iris.util.common.data.registry.Particles.ITEM;
|
|||||||
*/
|
*/
|
||||||
public final class BukkitEntitySpawner {
|
public final class BukkitEntitySpawner {
|
||||||
private static final long RISE_START_DEPTH_BLOCKS = 5;
|
private static final long RISE_START_DEPTH_BLOCKS = 5;
|
||||||
|
private static final int CHUNK_SHIFT = 4;
|
||||||
private static final int RISE_MAX_TICKS = 100;
|
private static final int RISE_MAX_TICKS = 100;
|
||||||
private static final int RISE_NO_DAMAGE_TICKS = 100000;
|
private static final int RISE_NO_DAMAGE_TICKS = 100000;
|
||||||
private static final double RISE_STEP_BLOCKS = 0.1;
|
private static final double RISE_STEP_BLOCKS = 0.1;
|
||||||
@@ -173,10 +174,10 @@ public final class BukkitEntitySpawner {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** Players watching an entity that rises out of the ground see it start five blocks below the spot. */
|
/** Players watching an entity that rises out of the ground see it start five blocks below the spot. */
|
||||||
private static Location startBelowGroundWhenWatched(Location at) {
|
static Location startBelowGroundWhenWatched(Location at) {
|
||||||
AtomicReference<Location> start = new AtomicReference<>(at);
|
AtomicReference<Location> start = new AtomicReference<>(at);
|
||||||
try {
|
try {
|
||||||
J.sfut(() -> {
|
J.runRegionFuture(at.getWorld(), at.getBlockX() >> CHUNK_SHIFT, at.getBlockZ() >> CHUNK_SHIFT, () -> {
|
||||||
if (Chunks.hasPlayersNearby(start.get())) {
|
if (Chunks.hasPlayersNearby(start.get())) {
|
||||||
Location b = start.get().clone();
|
Location b = start.get().clone();
|
||||||
start.set(new Location(b.getWorld(), b.getX(), b.getY() - RISE_START_DEPTH_BLOCKS, b.getZ()));
|
start.set(new Location(b.getWorld(), b.getX(), b.getY() - RISE_START_DEPTH_BLOCKS, b.getZ()));
|
||||||
|
|||||||
+45
@@ -0,0 +1,45 @@
|
|||||||
|
package art.arcane.iris.engine.platform;
|
||||||
|
|
||||||
|
import art.arcane.iris.util.common.plugin.Chunks;
|
||||||
|
import art.arcane.iris.util.common.scheduling.J;
|
||||||
|
import org.bukkit.Location;
|
||||||
|
import org.bukkit.World;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.mockito.MockedStatic;
|
||||||
|
|
||||||
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
|
import static org.mockito.ArgumentMatchers.anyInt;
|
||||||
|
import static org.mockito.ArgumentMatchers.eq;
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
import static org.mockito.Mockito.mockStatic;
|
||||||
|
import static org.mockito.Mockito.never;
|
||||||
|
|
||||||
|
public class BukkitEntitySpawnerRiseStartTest {
|
||||||
|
private static final double SPAWN_Y = 70;
|
||||||
|
private static final double RISE_START_Y = 65;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void watchedRiseStartRunsThroughTheRegionFutureAndNeverThroughTheNextTickFuture() {
|
||||||
|
World world = mock(World.class);
|
||||||
|
Location at = new Location(world, 33, SPAWN_Y, -17);
|
||||||
|
|
||||||
|
try (MockedStatic<J> scheduling = mockStatic(J.class);
|
||||||
|
MockedStatic<Chunks> chunks = mockStatic(Chunks.class)) {
|
||||||
|
scheduling.when(() -> J.runRegionFuture(eq(world), anyInt(), anyInt(), any(Runnable.class)))
|
||||||
|
.thenAnswer(invocation -> {
|
||||||
|
invocation.<Runnable>getArgument(3).run();
|
||||||
|
return CompletableFuture.completedFuture(null);
|
||||||
|
});
|
||||||
|
chunks.when(() -> Chunks.hasPlayersNearby(any(Location.class))).thenReturn(true);
|
||||||
|
|
||||||
|
Location start = BukkitEntitySpawner.startBelowGroundWhenWatched(at);
|
||||||
|
|
||||||
|
assertEquals(RISE_START_Y, start.getY(), 0.0);
|
||||||
|
scheduling.verify(() -> J.runRegionFuture(eq(world), eq(2), eq(-2), any(Runnable.class)));
|
||||||
|
scheduling.verify(() -> J.sfut(any(Runnable.class)), never());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user