mirror of
https://github.com/RonanPlugins/BetterRTP.git
synced 2026-11-28 08:44:07 +00:00
start up fix
This commit is contained in:
1 parent
36289b31af
commit
606510754e
8 files changed
+445
-40
No files matched your search
@@ -7,7 +7,7 @@
|
||||
<groupId>me.SuperRonanCraft</groupId>
|
||||
<artifactId>BetterRTP</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
<version>3.6.14</version>
|
||||
<version>3.6.15</version>
|
||||
|
||||
<!-- Upload patches to https://repo.ronanplugins.com/#/ -->
|
||||
|
||||
|
||||
+32
-32
@@ -11,7 +11,6 @@ import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import com.tcoded.folialib.wrapper.task.WrappedTask;
|
||||
|
||||
import io.papermc.lib.PaperLib;
|
||||
import me.SuperRonanCraft.BetterRTP.BetterRTP;
|
||||
import me.SuperRonanCraft.BetterRTP.player.commands.RTP_SETUP_TYPE;
|
||||
import me.SuperRonanCraft.BetterRTP.player.rtp.RTP;
|
||||
@@ -156,37 +155,38 @@ public class QueueGenerator {
|
||||
private void addQueue(RTPWorld rtpWorld, String id, ReQueueData reQueueData) {
|
||||
Location loc = RandomLocation.generateLocation(rtpWorld);
|
||||
if (loc != null) {
|
||||
AsyncHandler.sync(() -> {
|
||||
//BetterRTP.debug("Queued up a new position, attempts " + reQueueData.attempts);
|
||||
PaperLib.getChunkAtAsync(loc)
|
||||
.thenAccept(v -> {
|
||||
Location safeLoc = RandomLocation.getSafeLocation(
|
||||
HelperRTP.getWorldType(rtpWorld.getWorld()),
|
||||
loc.getWorld(),
|
||||
loc,
|
||||
rtpWorld.getMinY(),
|
||||
rtpWorld.getMaxY(),
|
||||
rtpWorld.getBiomes());
|
||||
//data.setLocation(safeLoc);
|
||||
if (safeLoc != null) {
|
||||
AsyncHandler.async(() -> {
|
||||
QueueData data = DatabaseHandler.getQueue().addQueue(safeLoc);
|
||||
if (data != null) {
|
||||
//queueList.add(data);
|
||||
String _x = String.valueOf(data.getLocation().getBlockX());
|
||||
String _y = String.valueOf(data.getLocation().getBlockY());
|
||||
String _z = String.valueOf(data.getLocation().getBlockZ());
|
||||
String _world = data.getLocation().getWorld().getName();
|
||||
BetterRTP.debug("Queue position generated"
|
||||
+ ": id= " + id + ", database_ID= " + data.database_id
|
||||
+ ", location= x: " + _x + ", y: " + _y + ", z: " + _z + ", world: " + _world);
|
||||
} else
|
||||
BetterRTP.debug("Database error occurred for a queue when trying to save: " + safeLoc);
|
||||
queueGenerator(reQueueData);
|
||||
});
|
||||
} else
|
||||
queueGenerator(reQueueData);
|
||||
});
|
||||
AsyncHandler.atLoadedChunk(loc, () -> {
|
||||
Location safeLoc = RandomLocation.getSafeLocation(
|
||||
HelperRTP.getWorldType(rtpWorld.getWorld()),
|
||||
loc.getWorld(),
|
||||
loc,
|
||||
rtpWorld.getMinY(),
|
||||
rtpWorld.getMaxY(),
|
||||
rtpWorld.getBiomes());
|
||||
//data.setLocation(safeLoc);
|
||||
if (safeLoc != null) {
|
||||
AsyncHandler.async(() -> {
|
||||
QueueData data = DatabaseHandler.getQueue().addQueue(safeLoc);
|
||||
if (data != null) {
|
||||
//queueList.add(data);
|
||||
String _x = String.valueOf(data.getLocation().getBlockX());
|
||||
String _y = String.valueOf(data.getLocation().getBlockY());
|
||||
String _z = String.valueOf(data.getLocation().getBlockZ());
|
||||
String _world = data.getLocation().getWorld().getName();
|
||||
BetterRTP.debug("Queue position generated"
|
||||
+ ": id= " + id + ", database_ID= " + data.database_id
|
||||
+ ", location= x: " + _x + ", y: " + _y + ", z: " + _z + ", world: " + _world);
|
||||
} else
|
||||
BetterRTP.debug("Database error occurred for a queue when trying to save: " + safeLoc);
|
||||
queueGenerator(reQueueData);
|
||||
});
|
||||
} else
|
||||
queueGenerator(reQueueData);
|
||||
}).exceptionally(error -> {
|
||||
BetterRTP.getInstance().getLogger().log(java.util.logging.Level.WARNING,
|
||||
"Failed to generate queue position for " + id + " at " + loc, error);
|
||||
queueGenerator(reQueueData);
|
||||
return null;
|
||||
});
|
||||
} else {
|
||||
BetterRTP.debug("Queue position wasn't able to generate a location!");
|
||||
|
||||
@@ -4,6 +4,12 @@ import com.tcoded.folialib.impl.ServerImplementation;
|
||||
import com.tcoded.folialib.wrapper.task.WrappedTask;
|
||||
import me.SuperRonanCraft.BetterRTP.BetterRTP;
|
||||
import org.bukkit.entity.Entity;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.Chunk;
|
||||
import io.papermc.lib.PaperLib;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public class AsyncHandler {
|
||||
|
||||
@@ -15,6 +21,47 @@ public class AsyncHandler {
|
||||
getFolia().runNextTick(task -> runnable.run());
|
||||
}
|
||||
|
||||
public static void syncAtLocation(Location location, Runnable runnable) {
|
||||
getFolia().runAtLocation(location, task -> runnable.run());
|
||||
}
|
||||
|
||||
/** Load and inspect a chunk on its owning region, including PaperLib's sync fallback. */
|
||||
public static CompletableFuture<Void> atLoadedChunk(Location location, Runnable action) {
|
||||
return atLoadedChunk(task -> syncAtLocation(location, task),
|
||||
() -> PaperLib.getChunkAtAsync(location), action);
|
||||
}
|
||||
|
||||
static CompletableFuture<Void> atLoadedChunk(Consumer<Runnable> scheduler,
|
||||
Supplier<CompletableFuture<Chunk>> loader, Runnable action) {
|
||||
CompletableFuture<Void> result = new CompletableFuture<>();
|
||||
schedule(scheduler, () -> loader.get().whenComplete((chunk, error) -> {
|
||||
if (error != null) {
|
||||
result.completeExceptionally(error);
|
||||
} else {
|
||||
// Future completion alone does not guarantee ownership of the destination region.
|
||||
schedule(scheduler, () -> {
|
||||
action.run();
|
||||
result.complete(null);
|
||||
}, result);
|
||||
}
|
||||
}), result);
|
||||
return result;
|
||||
}
|
||||
|
||||
private static void schedule(Consumer<Runnable> scheduler, Runnable action, CompletableFuture<Void> result) {
|
||||
try {
|
||||
scheduler.accept(() -> {
|
||||
try {
|
||||
action.run();
|
||||
} catch (Throwable error) {
|
||||
result.completeExceptionally(error);
|
||||
}
|
||||
});
|
||||
} catch (Throwable error) {
|
||||
result.completeExceptionally(error);
|
||||
}
|
||||
}
|
||||
|
||||
public static void syncAtEntity(Entity entity, Runnable runnable) {
|
||||
getFolia().runAtEntity(entity, task -> runnable.run());
|
||||
}
|
||||
|
||||
+81
@@ -0,0 +1,81 @@
|
||||
package me.SuperRonanCraft.BetterRTP.versions;
|
||||
|
||||
import org.bukkit.Chunk;
|
||||
import org.junit.Test;
|
||||
import java.util.ArrayDeque;
|
||||
import java.util.Queue;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.CompletionException;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class AsyncHandlerTest {
|
||||
@Test public void synchronousFallbackLoadsOnlyOnRegionAndReschedulesInspection() {
|
||||
Queue<Runnable> region = new ArrayDeque<>();
|
||||
AtomicBoolean loaded = new AtomicBoolean();
|
||||
AtomicBoolean checked = new AtomicBoolean();
|
||||
CompletableFuture<Void> result = AsyncHandler.atLoadedChunk(region::add, () -> {
|
||||
loaded.set(true);
|
||||
return CompletableFuture.completedFuture(null);
|
||||
}, () -> checked.set(true));
|
||||
assertFalse(loaded.get());
|
||||
region.remove().run();
|
||||
assertTrue(loaded.get());
|
||||
assertFalse(checked.get());
|
||||
assertFalse(result.isDone());
|
||||
region.remove().run();
|
||||
assertTrue(checked.get());
|
||||
result.join();
|
||||
}
|
||||
|
||||
@Test public void asynchronousCompletionDoesNotInspectBlocksOnCompletingThread() {
|
||||
Queue<Runnable> region = new ArrayDeque<>();
|
||||
CompletableFuture<Chunk> chunk = new CompletableFuture<>();
|
||||
AtomicBoolean checked = new AtomicBoolean();
|
||||
CompletableFuture<Void> result = AsyncHandler.atLoadedChunk(region::add, () -> chunk,
|
||||
() -> checked.set(true));
|
||||
region.remove().run();
|
||||
assertTrue(region.isEmpty());
|
||||
chunk.complete(null);
|
||||
assertFalse(checked.get());
|
||||
region.remove().run();
|
||||
assertTrue(checked.get());
|
||||
result.join();
|
||||
}
|
||||
|
||||
@Test public void synchronousLoadFailureIsReported() {
|
||||
RuntimeException failure = new RuntimeException("load failed");
|
||||
assertFailure(failure, AsyncHandler.atLoadedChunk(Runnable::run, () -> { throw failure; },
|
||||
() -> fail("Must not inspect failed chunk")));
|
||||
}
|
||||
|
||||
@Test public void asynchronousLoadFailureIsReported() {
|
||||
RuntimeException failure = new RuntimeException("load failed");
|
||||
CompletableFuture<Chunk> chunk = new CompletableFuture<>();
|
||||
CompletableFuture<Void> result = AsyncHandler.atLoadedChunk(Runnable::run, () -> chunk,
|
||||
() -> fail("Must not inspect failed chunk"));
|
||||
chunk.completeExceptionally(failure);
|
||||
assertFailure(failure, result);
|
||||
}
|
||||
|
||||
@Test public void inspectionFailureIsReported() {
|
||||
RuntimeException failure = new RuntimeException("inspection failed");
|
||||
assertFailure(failure, AsyncHandler.atLoadedChunk(Runnable::run,
|
||||
() -> CompletableFuture.completedFuture(null), () -> { throw failure; }));
|
||||
}
|
||||
|
||||
@Test public void schedulingFailureIsReported() {
|
||||
RuntimeException failure = new RuntimeException("scheduling failed");
|
||||
assertFailure(failure, AsyncHandler.atLoadedChunk(task -> { throw failure; },
|
||||
() -> { fail("Must not load without region ownership"); return null; }, () -> {}));
|
||||
}
|
||||
|
||||
private static void assertFailure(Throwable expected, CompletableFuture<Void> result) {
|
||||
try {
|
||||
result.join();
|
||||
fail("Expected failure");
|
||||
} catch (CompletionException failure) {
|
||||
assertSame(expected, failure.getCause());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user