Skip to content

Instantly share code, notes, and snippets.

@aikar
Created July 27, 2020 23:45
Show Gist options
  • Save aikar/c7e12918d6be46f62c1e93c373acfb23 to your computer and use it in GitHub Desktop.
Save aikar/c7e12918d6be46f62c1e93c373acfb23 to your computer and use it in GitHub Desktop.
diff --git a/src/main/java/net/minecraft/server/LightEngineThreaded.java b/src/main/java/net/minecraft/server/LightEngineThreaded.java
index 721f86a5405..e8223670017 100644
--- a/src/main/java/net/minecraft/server/LightEngineThreaded.java
+++ b/src/main/java/net/minecraft/server/LightEngineThreaded.java
@@ -17,9 +17,10 @@ public class LightEngineThreaded extends LightEngine implements AutoCloseable {
private final ThreadedMailbox<Runnable> b; ThreadedMailbox<Runnable> mailbox; // Paper
// Paper start
private static final int MAX_PRIORITIES = PlayerChunkMap.GOLDEN_TICKET + 2;
+ private final java.util.concurrent.ConcurrentLinkedQueue<Runnable> priorityChanges = new java.util.concurrent.ConcurrentLinkedQueue<>();
public void changePriority(long pair, int currentPriority, int priority) {
- this.mailbox.queue(() -> {
+ this.priorityChanges.add(() -> {
ChunkLightQueue remove = this.queue.buckets[currentPriority].remove(pair);
if (remove != null) {
ChunkLightQueue existing = this.queue.buckets[priority].put(pair, remove);
@@ -31,6 +32,15 @@ public class LightEngineThreaded extends LightEngine implements AutoCloseable {
});
}
+ private boolean isChunkLightStatus(long pair) {
+ PlayerChunk playerChunk = playerChunkMap.getUpdatingChunk(pair);
+ if (playerChunk == null) {
+ return false;
+ }
+ ChunkStatus status = PlayerChunk.getChunkStatus(playerChunk.getTicketLevel());
+ return status != null && status.isAtLeastStatus(ChunkStatus.LIGHT);
+ }
+
static class ChunkLightQueue {
public boolean shouldFastUpdate;
java.util.ArrayDeque<Runnable> pre = new java.util.ArrayDeque<Runnable>();
@@ -41,7 +51,7 @@ public class LightEngineThreaded extends LightEngine implements AutoCloseable {
// Retain the chunks priority level for queued light tasks
- private static class LightQueue {
+ private class LightQueue {
private int size = 0;
private int lowestPriority = MAX_PRIORITIES;
private final it.unimi.dsi.fastutil.longs.Long2ObjectLinkedOpenHashMap<ChunkLightQueue>[] buckets = new it.unimi.dsi.fastutil.longs.Long2ObjectLinkedOpenHashMap[MAX_PRIORITIES];
@@ -81,6 +91,10 @@ public class LightEngineThreaded extends LightEngine implements AutoCloseable {
}
public boolean poll(java.util.List<Runnable> pre, java.util.List<Runnable> post) {
+ Runnable run;
+ while ((run = priorityChanges.poll()) != null) {
+ run.run();
+ }
boolean hasWork = false;
it.unimi.dsi.fastutil.longs.Long2ObjectLinkedOpenHashMap<ChunkLightQueue>[] buckets = this.buckets;
while (lowestPriority < MAX_PRIORITIES && !isEmpty()) {
@@ -106,14 +120,14 @@ public class LightEngineThreaded extends LightEngine implements AutoCloseable {
private final LightQueue queue = new LightQueue();
// Paper end
- private final PlayerChunkMap d;
+ private final PlayerChunkMap d; private final PlayerChunkMap playerChunkMap; // Paper
private final Mailbox<ChunkTaskQueueSorter.a<Runnable>> e;
private volatile int f = 5;
private final AtomicBoolean g = new AtomicBoolean();
public LightEngineThreaded(ILightAccess ilightaccess, PlayerChunkMap playerchunkmap, boolean flag, ThreadedMailbox<Runnable> threadedmailbox, Mailbox<ChunkTaskQueueSorter.a<Runnable>> mailbox) {
super(ilightaccess, true, flag);
- this.d = playerchunkmap;
+ this.d = playerchunkmap; this.playerChunkMap = d; // Paper
this.e = mailbox;
this.mailbox = this.b = threadedmailbox; // Paper
}
@@ -203,11 +217,8 @@ public class LightEngineThreaded extends LightEngine implements AutoCloseable {
this.e.a(ChunkTaskQueueSorter.a(() -> {
// Paper start
int priority = intsupplier.getAsInt();
- this.queue.add(ChunkCoordIntPair.pair(i, j), priority, lightenginethreaded_update, runnable); // Paper
- if (priority <= 25) { // don't auto kick off unless priority
- // Paper end
- this.b();
- }
+ this.queue.add(ChunkCoordIntPair.pair(i, j), priority, lightenginethreaded_update, runnable);
+ // Paper end
}, ChunkCoordIntPair.pair(i, j), intsupplier));
}
@@ -226,13 +237,23 @@ public class LightEngineThreaded extends LightEngine implements AutoCloseable {
public CompletableFuture<IChunkAccess> a(IChunkAccess ichunkaccess, boolean flag) {
ChunkCoordIntPair chunkcoordintpair = ichunkaccess.getPos();
- ichunkaccess.b(false);
// Paper start
+ //ichunkaccess.b(false); // Don't need to disable this
long pair = chunkcoordintpair.pair();
CompletableFuture<IChunkAccess> future = new CompletableFuture<>();
- IntSupplier prioritySupplier = d.getPrioritySupplier(pair);
+ IntSupplier prioritySupplier = playerChunkMap.getPrioritySupplier(pair);
this.e.a(ChunkTaskQueueSorter.a(() -> {
+ // Chunk's no longer needed
+ if (!isChunkLightStatus(pair)) {
+ System.out.println("drop chunk 1 " + ichunkaccess.getPos() + ichunkaccess.getChunkStatus());
+ future.complete(ichunkaccess);
+ return;
+ }
this.queue.add(pair, prioritySupplier.getAsInt(), LightEngineThreaded.Update.PRE_UPDATE, SystemUtils.a(() -> {
+ if (!isChunkLightStatus(pair)) {
+ System.out.println("drop chunk 2 " + ichunkaccess.getPos() + ichunkaccess.getChunkStatus());
+ return;
+ }
// Paper end
ChunkSection[] achunksection = ichunkaccess.getSections();
@@ -258,7 +279,11 @@ public class LightEngineThreaded extends LightEngine implements AutoCloseable {
}));
this.queue.add(pair, prioritySupplier.getAsInt(), LightEngineThreaded.Update.POST_UPDATE, () -> {
- ichunkaccess.b(true);
+ // Paper start - only mark as lit if still at lit status
+ if (flag || isChunkLightStatus(pair)) {
+ ichunkaccess.b(true);
+ }
+ // Paper end
super.b(chunkcoordintpair, false);
// Paper start
future.complete(ichunkaccess);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment