diff --git a/build.gradle.kts b/build.gradle.kts index c79bb8083..63e05aad2 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -34,7 +34,7 @@ logger.lifecycle(""" ******************************************* """) -var rootVersion by extra("2.8.2") +var rootVersion by extra("2.8.3") var snapshot by extra("SNAPSHOT") var revision: String by extra("") var buildNumber by extra("") diff --git a/buildSrc/build.gradle.kts b/buildSrc/build.gradle.kts index de4d0df68..a65407ce5 100644 --- a/buildSrc/build.gradle.kts +++ b/buildSrc/build.gradle.kts @@ -22,7 +22,7 @@ val properties = Properties().also { props -> dependencies { implementation(gradleApi()) - implementation("org.ajoberstar.grgit:grgit-gradle:5.2.0") + implementation("org.ajoberstar.grgit:grgit-gradle:5.2.1") implementation("com.github.johnrengelman:shadow:8.1.1") implementation("io.papermc.paperweight.userdev:io.papermc.paperweight.userdev.gradle.plugin:1.5.5") } diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 1cd38a659..397834962 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -14,8 +14,8 @@ mapmanager = "1.8.0-SNAPSHOT" griefprevention = "16.18.1" griefdefender = "2.1.0-SNAPSHOT" residence = "4.5._13.1" -towny = "0.99.6.0" -plotsquared = "7.0.0" +towny = "0.100.0.1" +plotsquared = "7.1.0" # Third party bstats = "3.0.2" @@ -23,7 +23,7 @@ sparsebitset = "1.3" parallelgzip = "1.0.5" adventure = "4.14.0" adventure-bukkit = "4.3.1" -checkerqual = "3.39.0" +checkerqual = "3.40.0" truezip = "6.8.4" auto-value = "1.10.4" findbugs = "3.0.2" @@ -35,7 +35,7 @@ jlibnoise = "1.0.0" jchronic = "0.2.4a" lz4-java = "1.8.0" lz4-stream = "1.0.0" -commons-cli = "1.5.0" +commons-cli = "1.6.0" paperlib = "1.0.8" paster = "1.1.5" vault = "1.7.1" diff --git a/worldedit-bukkit/adapters/adapter-1_17_1/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_17_R1_2/PaperweightStarlightRelighter.java b/worldedit-bukkit/adapters/adapter-1_17_1/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_17_R1_2/PaperweightStarlightRelighter.java index 8298be6f1..ea2afede3 100644 --- a/worldedit-bukkit/adapters/adapter-1_17_1/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_17_R1_2/PaperweightStarlightRelighter.java +++ b/worldedit-bukkit/adapters/adapter-1_17_1/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_17_R1_2/PaperweightStarlightRelighter.java @@ -1,17 +1,8 @@ package com.sk89q.worldedit.bukkit.adapter.impl.fawe.v1_17_R1_2; +import com.fastasyncworldedit.bukkit.adapter.StarlightRelighter; import com.fastasyncworldedit.core.configuration.Settings; -import com.fastasyncworldedit.core.extent.processor.lighting.NMSRelighter; -import com.fastasyncworldedit.core.extent.processor.lighting.Relighter; -import com.fastasyncworldedit.core.queue.IQueueChunk; import com.fastasyncworldedit.core.queue.IQueueExtent; -import com.fastasyncworldedit.core.util.MathMan; -import com.fastasyncworldedit.core.util.TaskManager; -import com.sk89q.worldedit.internal.util.LogManagerCompat; -import it.unimi.dsi.fastutil.longs.Long2ObjectLinkedOpenHashMap; -import it.unimi.dsi.fastutil.longs.LongArraySet; -import it.unimi.dsi.fastutil.longs.LongIterator; -import it.unimi.dsi.fastutil.longs.LongSet; import net.minecraft.server.MCUtil; import net.minecraft.server.level.ServerLevel; import net.minecraft.server.level.ThreadedLevelLightEngine; @@ -19,27 +10,18 @@ import net.minecraft.server.level.TicketType; import net.minecraft.util.Unit; import net.minecraft.world.level.ChunkPos; import net.minecraft.world.level.chunk.ChunkStatus; -import org.apache.logging.log4j.Logger; import java.lang.invoke.MethodHandle; import java.lang.invoke.MethodHandles; import java.lang.invoke.MethodType; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.List; import java.util.Set; import java.util.concurrent.CompletableFuture; -import java.util.concurrent.locks.ReentrantLock; import java.util.function.Consumer; import java.util.function.IntConsumer; -public class PaperweightStarlightRelighter implements Relighter { - - public static final MethodHandle RELIGHT; - private static final Logger LOGGER = LogManagerCompat.getLogger(); - private static final int CHUNKS_PER_BATCH = 1024; // 32 * 32 - private static final int CHUNKS_PER_BATCH_SQRT_LOG2 = 5; // for shifting +public class PaperweightStarlightRelighter extends StarlightRelighter { + private static final MethodHandle RELIGHT; private static final TicketType FAWE_TICKET = TicketType.create("fawe_ticket", (a, b) -> 0); private static final int LIGHT_LEVEL = MCUtil.getTicketLevelFor(ChunkStatus.LIGHT); @@ -58,22 +40,36 @@ public class PaperweightStarlightRelighter implements Relighter { IntConsumer.class ) ); + tmp = MethodHandles.dropReturn(tmp); } catch (NoSuchMethodException | IllegalAccessException e) { LOGGER.error("Failed to locate 'relight' method in ThreadedLevelLightEngine. Is everything up to date?", e); } RELIGHT = tmp; } - private final ServerLevel serverLevel; - private final ReentrantLock lock = new ReentrantLock(); - private final Long2ObjectLinkedOpenHashMap regions = new Long2ObjectLinkedOpenHashMap<>(); - private final ReentrantLock areaLock = new ReentrantLock(); - private final NMSRelighter delegate; + public PaperweightStarlightRelighter(ServerLevel serverLevel, IQueueExtent queue) { + super(serverLevel, queue); + } - @SuppressWarnings("rawtypes") - public PaperweightStarlightRelighter(ServerLevel serverLevel, IQueueExtent queue) { - this.serverLevel = serverLevel; - this.delegate = new NMSRelighter(queue); + @Override + protected ChunkPos createChunkPos(final long chunkKey) { + return new ChunkPos(chunkKey); + } + + @Override + protected long asLong(final int chunkX, final int chunkZ) { + return ChunkPos.asLong(chunkX, chunkZ); + } + + @Override + protected CompletableFuture chunkLoadFuture(final ChunkPos chunkPos) { + return serverLevel.getWorld().getChunkAtAsync(chunkPos.x, chunkPos.z) + .thenAccept(c -> serverLevel.getChunkSource().addTicketAtLevel( + FAWE_TICKET, + chunkPos, + LIGHT_LEVEL, + Unit.INSTANCE + )); } public static boolean isUsable() { @@ -81,95 +77,13 @@ public class PaperweightStarlightRelighter implements Relighter { } @Override - public boolean addChunk(int cx, int cz, byte[] skipReason, int bitmask) { - areaLock.lock(); - try { - long key = MathMan.pairInt(cx >> CHUNKS_PER_BATCH_SQRT_LOG2, cz >> CHUNKS_PER_BATCH_SQRT_LOG2); - // TODO probably submit here already if chunks.size == CHUNKS_PER_BATCH? - LongSet chunks = this.regions.computeIfAbsent(key, k -> new LongArraySet(CHUNKS_PER_BATCH >> 2)); - chunks.add(ChunkPos.asLong(cx, cz)); - } finally { - areaLock.unlock(); - } - return true; - } - - @Override - public void addLightUpdate(int x, int y, int z) { - delegate.addLightUpdate(x, y, z); - } - - /* - * This method is called "recursively", iterating and removing elements - * from the regions linked map. This way, chunks are loaded in batches to avoid - * OOMEs. - */ - @Override - public void fixLightingSafe(boolean sky) { - this.areaLock.lock(); - try { - if (regions.isEmpty()) { - return; - } - LongSet first = regions.removeFirst(); - fixLighting(first, () -> fixLightingSafe(true)); - } finally { - this.areaLock.unlock(); - } - } - - /* - * Processes a set of chunks and runs an action afterwards. - * The action is run async, the chunks are partly processed on the main thread - * (as required by the server). - */ - private void fixLighting(LongSet chunks, Runnable andThen) { - // convert from long keys to ChunkPos - Set coords = new HashSet<>(); - LongIterator iterator = chunks.iterator(); - while (iterator.hasNext()) { - coords.add(new ChunkPos(iterator.nextLong())); - } - TaskManager.taskManager().task(() -> { - // trigger chunk load and apply ticket on main thread - List> futures = new ArrayList<>(); - for (ChunkPos pos : coords) { - futures.add(serverLevel.getWorld().getChunkAtAsync(pos.x, pos.z) - .thenAccept(c -> serverLevel.getChunkSource().addTicketAtLevel( - FAWE_TICKET, - pos, - LIGHT_LEVEL, - Unit.INSTANCE - )) - ); - } - // collect futures and trigger relight once all chunks are loaded - CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).thenAccept(v -> - invokeRelight( - coords, - c -> { - }, // no callback for single chunks required - i -> { - if (i != coords.size()) { - LOGGER.warn("Processed {} chunks instead of {}", i, coords.size()); - } - // post process chunks on main thread - TaskManager.taskManager().task(() -> postProcessChunks(coords)); - // call callback on our own threads - TaskManager.taskManager().async(andThen); - } - ) - ); - }); - } - - private void invokeRelight( + protected void invokeRelight( Set coords, Consumer chunkCallback, IntConsumer processCallback ) { try { - int unused = (int) RELIGHT.invokeExact( + RELIGHT.invokeExact( serverLevel.getChunkSource().getLightEngine(), coords, chunkCallback, // callback per chunk @@ -184,7 +98,7 @@ public class PaperweightStarlightRelighter implements Relighter { * Allow the server to unload the chunks again. * Also, if chunk packets are sent delayed, we need to do that here */ - private void postProcessChunks(Set coords) { + protected void postProcessChunks(Set coords) { boolean delay = Settings.settings().LIGHTING.DELAY_PACKET_SENDING; for (ChunkPos pos : coords) { int x = pos.x; @@ -196,44 +110,4 @@ public class PaperweightStarlightRelighter implements Relighter { } } - @Override - public void clear() { - - } - - @Override - public void removeLighting() { - this.delegate.removeLighting(); - } - - @Override - public void fixBlockLighting() { - fixLightingSafe(true); - } - - @Override - public void fixSkyLighting() { - fixLightingSafe(true); - } - - @Override - public boolean isEmpty() { - return true; - } - - @Override - public ReentrantLock getLock() { - return this.lock; - } - - @Override - public boolean isFinished() { - return false; - } - - @Override - public void close() throws Exception { - fixLightingSafe(true); - } - } diff --git a/worldedit-bukkit/adapters/adapter-1_17_1/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_17_R1_2/PaperweightStarlightRelighterFactory.java b/worldedit-bukkit/adapters/adapter-1_17_1/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_17_R1_2/PaperweightStarlightRelighterFactory.java index 2672c5b62..9cd0a1ef8 100644 --- a/worldedit-bukkit/adapters/adapter-1_17_1/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_17_R1_2/PaperweightStarlightRelighterFactory.java +++ b/worldedit-bukkit/adapters/adapter-1_17_1/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_17_R1_2/PaperweightStarlightRelighterFactory.java @@ -4,7 +4,6 @@ import com.fastasyncworldedit.core.extent.processor.lighting.NullRelighter; import com.fastasyncworldedit.core.extent.processor.lighting.RelightMode; import com.fastasyncworldedit.core.extent.processor.lighting.Relighter; import com.fastasyncworldedit.core.extent.processor.lighting.RelighterFactory; -import com.fastasyncworldedit.core.queue.IQueueChunk; import com.fastasyncworldedit.core.queue.IQueueExtent; import com.sk89q.worldedit.world.World; import org.bukkit.Bukkit; @@ -15,9 +14,7 @@ import javax.annotation.Nonnull; public class PaperweightStarlightRelighterFactory implements RelighterFactory { @Override - public @Nonnull - @SuppressWarnings("rawtypes") - Relighter createRelighter(RelightMode relightMode, World world, IQueueExtent queue) { + public @Nonnull Relighter createRelighter(RelightMode relightMode, World world, IQueueExtent queue) { org.bukkit.World w = Bukkit.getWorld(world.getName()); if (w == null) { return NullRelighter.INSTANCE; diff --git a/worldedit-bukkit/adapters/adapter-1_18_2/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_18_R2/PaperweightStarlightRelighter.java b/worldedit-bukkit/adapters/adapter-1_18_2/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_18_R2/PaperweightStarlightRelighter.java index 397931bc5..c832fc98a 100644 --- a/worldedit-bukkit/adapters/adapter-1_18_2/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_18_R2/PaperweightStarlightRelighter.java +++ b/worldedit-bukkit/adapters/adapter-1_18_2/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_18_R2/PaperweightStarlightRelighter.java @@ -1,140 +1,51 @@ package com.sk89q.worldedit.bukkit.adapter.impl.fawe.v1_18_R2; +import com.fastasyncworldedit.bukkit.adapter.StarlightRelighter; import com.fastasyncworldedit.core.configuration.Settings; -import com.fastasyncworldedit.core.extent.processor.lighting.NMSRelighter; -import com.fastasyncworldedit.core.extent.processor.lighting.Relighter; -import com.fastasyncworldedit.core.queue.IQueueChunk; import com.fastasyncworldedit.core.queue.IQueueExtent; -import com.fastasyncworldedit.core.util.MathMan; -import com.fastasyncworldedit.core.util.TaskManager; -import com.sk89q.worldedit.internal.util.LogManagerCompat; -import it.unimi.dsi.fastutil.longs.Long2ObjectLinkedOpenHashMap; -import it.unimi.dsi.fastutil.longs.LongArraySet; -import it.unimi.dsi.fastutil.longs.LongIterator; -import it.unimi.dsi.fastutil.longs.LongSet; import net.minecraft.server.MCUtil; import net.minecraft.server.level.ServerLevel; import net.minecraft.server.level.TicketType; import net.minecraft.util.Unit; import net.minecraft.world.level.ChunkPos; import net.minecraft.world.level.chunk.ChunkStatus; -import org.apache.logging.log4j.Logger; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.List; import java.util.Set; import java.util.concurrent.CompletableFuture; -import java.util.concurrent.locks.ReentrantLock; import java.util.function.Consumer; import java.util.function.IntConsumer; -public class PaperweightStarlightRelighter implements Relighter { - - private static final Logger LOGGER = LogManagerCompat.getLogger(); - private static final int CHUNKS_PER_BATCH = 1024; // 32 * 32 - private static final int CHUNKS_PER_BATCH_SQRT_LOG2 = 5; // for shifting +public class PaperweightStarlightRelighter extends StarlightRelighter { private static final TicketType FAWE_TICKET = TicketType.create("fawe_ticket", (a, b) -> 0); private static final int LIGHT_LEVEL = MCUtil.getTicketLevelFor(ChunkStatus.LIGHT); - - private final ServerLevel serverLevel; - private final ReentrantLock lock = new ReentrantLock(); - private final Long2ObjectLinkedOpenHashMap regions = new Long2ObjectLinkedOpenHashMap<>(); - private final ReentrantLock areaLock = new ReentrantLock(); - private final NMSRelighter delegate; - - @SuppressWarnings("rawtypes") - public PaperweightStarlightRelighter(ServerLevel serverLevel, IQueueExtent queue) { - this.serverLevel = serverLevel; - this.delegate = new NMSRelighter(queue); + public PaperweightStarlightRelighter(ServerLevel serverLevel, IQueueExtent queue) { + super(serverLevel, queue); } @Override - public boolean addChunk(int cx, int cz, byte[] skipReason, int bitmask) { - areaLock.lock(); - try { - long key = MathMan.pairInt(cx >> CHUNKS_PER_BATCH_SQRT_LOG2, cz >> CHUNKS_PER_BATCH_SQRT_LOG2); - // TODO probably submit here already if chunks.size == CHUNKS_PER_BATCH? - LongSet chunks = this.regions.computeIfAbsent(key, k -> new LongArraySet(CHUNKS_PER_BATCH >> 2)); - chunks.add(ChunkPos.asLong(cx, cz)); - } finally { - areaLock.unlock(); - } - return true; + protected ChunkPos createChunkPos(final long chunkKey) { + return new ChunkPos(chunkKey); } @Override - public void addLightUpdate(int x, int y, int z) { - delegate.addLightUpdate(x, y, z); + protected long asLong(final int chunkX, final int chunkZ) { + return ChunkPos.asLong(chunkX, chunkZ); } - /* - * This method is called "recursively", iterating and removing elements - * from the regions linked map. This way, chunks are loaded in batches to avoid - * OOMEs. - */ @Override - public void fixLightingSafe(boolean sky) { - this.areaLock.lock(); - try { - if (regions.isEmpty()) { - return; - } - LongSet first = regions.removeFirst(); - fixLighting(first, () -> fixLightingSafe(true)); - } finally { - this.areaLock.unlock(); - } + protected CompletableFuture chunkLoadFuture(final ChunkPos chunkPos) { + return serverLevel.getWorld().getChunkAtAsync(chunkPos.x, chunkPos.z) + .thenAccept(c -> serverLevel.getChunkSource().addTicketAtLevel( + FAWE_TICKET, + chunkPos, + LIGHT_LEVEL, + Unit.INSTANCE + )); } - /* - * Processes a set of chunks and runs an action afterwards. - * The action is run async, the chunks are partly processed on the main thread - * (as required by the server). - */ - private void fixLighting(LongSet chunks, Runnable andThen) { - // convert from long keys to ChunkPos - Set coords = new HashSet<>(); - LongIterator iterator = chunks.iterator(); - while (iterator.hasNext()) { - coords.add(new ChunkPos(iterator.nextLong())); - } - TaskManager.taskManager().task(() -> { - // trigger chunk load and apply ticket on main thread - List> futures = new ArrayList<>(); - for (ChunkPos pos : coords) { - futures.add(serverLevel.getWorld().getChunkAtAsync(pos.x, pos.z) - .thenAccept(c -> serverLevel.getChunkSource().addTicketAtLevel( - FAWE_TICKET, - pos, - LIGHT_LEVEL, - Unit.INSTANCE - )) - ); - } - // collect futures and trigger relight once all chunks are loaded - CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).thenAccept(v -> - invokeRelight( - coords, - c -> { - }, // no callback for single chunks required - i -> { - if (i != coords.size()) { - LOGGER.warn("Processed {} chunks instead of {}", i, coords.size()); - } - // post process chunks on main thread - TaskManager.taskManager().task(() -> postProcessChunks(coords)); - // call callback on our own threads - TaskManager.taskManager().async(andThen); - } - ) - ); - }); - } - - private void invokeRelight( + protected void invokeRelight( Set coords, Consumer chunkCallback, IntConsumer processCallback @@ -150,7 +61,7 @@ public class PaperweightStarlightRelighter implements Relighter { * Allow the server to unload the chunks again. * Also, if chunk packets are sent delayed, we need to do that here */ - private void postProcessChunks(Set coords) { + protected void postProcessChunks(Set coords) { boolean delay = Settings.settings().LIGHTING.DELAY_PACKET_SENDING; for (ChunkPos pos : coords) { int x = pos.x; @@ -162,44 +73,4 @@ public class PaperweightStarlightRelighter implements Relighter { } } - @Override - public void clear() { - - } - - @Override - public void removeLighting() { - this.delegate.removeLighting(); - } - - @Override - public void fixBlockLighting() { - fixLightingSafe(true); - } - - @Override - public void fixSkyLighting() { - fixLightingSafe(true); - } - - @Override - public boolean isEmpty() { - return true; - } - - @Override - public ReentrantLock getLock() { - return this.lock; - } - - @Override - public boolean isFinished() { - return false; - } - - @Override - public void close() throws Exception { - fixLightingSafe(true); - } - } diff --git a/worldedit-bukkit/adapters/adapter-1_18_2/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_18_R2/PaperweightStarlightRelighterFactory.java b/worldedit-bukkit/adapters/adapter-1_18_2/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_18_R2/PaperweightStarlightRelighterFactory.java index 960174342..735bcc677 100644 --- a/worldedit-bukkit/adapters/adapter-1_18_2/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_18_R2/PaperweightStarlightRelighterFactory.java +++ b/worldedit-bukkit/adapters/adapter-1_18_2/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_18_R2/PaperweightStarlightRelighterFactory.java @@ -4,7 +4,6 @@ import com.fastasyncworldedit.core.extent.processor.lighting.NullRelighter; import com.fastasyncworldedit.core.extent.processor.lighting.RelightMode; import com.fastasyncworldedit.core.extent.processor.lighting.Relighter; import com.fastasyncworldedit.core.extent.processor.lighting.RelighterFactory; -import com.fastasyncworldedit.core.queue.IQueueChunk; import com.fastasyncworldedit.core.queue.IQueueExtent; import com.sk89q.worldedit.world.World; import org.bukkit.Bukkit; @@ -15,9 +14,7 @@ import javax.annotation.Nonnull; public class PaperweightStarlightRelighterFactory implements RelighterFactory { @Override - public @Nonnull - @SuppressWarnings("rawtypes") - Relighter createRelighter(RelightMode relightMode, World world, IQueueExtent queue) { + public @Nonnull Relighter createRelighter(RelightMode relightMode, World world, IQueueExtent queue) { org.bukkit.World w = Bukkit.getWorld(world.getName()); if (w == null) { return NullRelighter.INSTANCE; diff --git a/worldedit-bukkit/adapters/adapter-1_19_4/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_19_R3/PaperweightStarlightRelighter.java b/worldedit-bukkit/adapters/adapter-1_19_4/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_19_R3/PaperweightStarlightRelighter.java index 62d781212..580bbf5a6 100644 --- a/worldedit-bukkit/adapters/adapter-1_19_4/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_19_R3/PaperweightStarlightRelighter.java +++ b/worldedit-bukkit/adapters/adapter-1_19_4/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_19_R3/PaperweightStarlightRelighter.java @@ -1,140 +1,51 @@ package com.sk89q.worldedit.bukkit.adapter.impl.fawe.v1_19_R3; +import com.fastasyncworldedit.bukkit.adapter.StarlightRelighter; import com.fastasyncworldedit.core.configuration.Settings; -import com.fastasyncworldedit.core.extent.processor.lighting.NMSRelighter; -import com.fastasyncworldedit.core.extent.processor.lighting.Relighter; -import com.fastasyncworldedit.core.queue.IQueueChunk; import com.fastasyncworldedit.core.queue.IQueueExtent; -import com.fastasyncworldedit.core.util.MathMan; -import com.fastasyncworldedit.core.util.TaskManager; -import com.sk89q.worldedit.internal.util.LogManagerCompat; -import it.unimi.dsi.fastutil.longs.Long2ObjectLinkedOpenHashMap; -import it.unimi.dsi.fastutil.longs.LongArraySet; -import it.unimi.dsi.fastutil.longs.LongIterator; -import it.unimi.dsi.fastutil.longs.LongSet; import net.minecraft.server.level.ChunkMap; import net.minecraft.server.level.ServerLevel; import net.minecraft.server.level.TicketType; import net.minecraft.util.Unit; import net.minecraft.world.level.ChunkPos; import net.minecraft.world.level.chunk.ChunkStatus; -import org.apache.logging.log4j.Logger; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.List; import java.util.Set; import java.util.concurrent.CompletableFuture; -import java.util.concurrent.locks.ReentrantLock; import java.util.function.Consumer; import java.util.function.IntConsumer; -public class PaperweightStarlightRelighter implements Relighter { - - private static final Logger LOGGER = LogManagerCompat.getLogger(); - private static final int CHUNKS_PER_BATCH = 1024; // 32 * 32 - private static final int CHUNKS_PER_BATCH_SQRT_LOG2 = 5; // for shifting +public class PaperweightStarlightRelighter extends StarlightRelighter { private static final TicketType FAWE_TICKET = TicketType.create("fawe_ticket", (a, b) -> 0); private static final int LIGHT_LEVEL = ChunkMap.MAX_VIEW_DISTANCE + ChunkStatus.getDistance(ChunkStatus.LIGHT); - - private final ServerLevel serverLevel; - private final ReentrantLock lock = new ReentrantLock(); - private final Long2ObjectLinkedOpenHashMap regions = new Long2ObjectLinkedOpenHashMap<>(); - private final ReentrantLock areaLock = new ReentrantLock(); - private final NMSRelighter delegate; - - @SuppressWarnings("rawtypes") - public PaperweightStarlightRelighter(ServerLevel serverLevel, IQueueExtent queue) { - this.serverLevel = serverLevel; - this.delegate = new NMSRelighter(queue); + public PaperweightStarlightRelighter(ServerLevel serverLevel, IQueueExtent queue) { + super(serverLevel, queue); } @Override - public boolean addChunk(int cx, int cz, byte[] skipReason, int bitmask) { - areaLock.lock(); - try { - long key = MathMan.pairInt(cx >> CHUNKS_PER_BATCH_SQRT_LOG2, cz >> CHUNKS_PER_BATCH_SQRT_LOG2); - // TODO probably submit here already if chunks.size == CHUNKS_PER_BATCH? - LongSet chunks = this.regions.computeIfAbsent(key, k -> new LongArraySet(CHUNKS_PER_BATCH >> 2)); - chunks.add(ChunkPos.asLong(cx, cz)); - } finally { - areaLock.unlock(); - } - return true; + protected ChunkPos createChunkPos(final long chunkKey) { + return new ChunkPos(chunkKey); } @Override - public void addLightUpdate(int x, int y, int z) { - delegate.addLightUpdate(x, y, z); + protected long asLong(final int chunkX, final int chunkZ) { + return ChunkPos.asLong(chunkX, chunkZ); } - /* - * This method is called "recursively", iterating and removing elements - * from the regions linked map. This way, chunks are loaded in batches to avoid - * OOMEs. - */ @Override - public void fixLightingSafe(boolean sky) { - this.areaLock.lock(); - try { - if (regions.isEmpty()) { - return; - } - LongSet first = regions.removeFirst(); - fixLighting(first, () -> fixLightingSafe(true)); - } finally { - this.areaLock.unlock(); - } + protected CompletableFuture chunkLoadFuture(final ChunkPos chunkPos) { + return serverLevel.getWorld().getChunkAtAsync(chunkPos.x, chunkPos.z) + .thenAccept(c -> serverLevel.getChunkSource().addTicketAtLevel( + FAWE_TICKET, + chunkPos, + LIGHT_LEVEL, + Unit.INSTANCE + )); } - /* - * Processes a set of chunks and runs an action afterwards. - * The action is run async, the chunks are partly processed on the main thread - * (as required by the server). - */ - private void fixLighting(LongSet chunks, Runnable andThen) { - // convert from long keys to ChunkPos - Set coords = new HashSet<>(); - LongIterator iterator = chunks.iterator(); - while (iterator.hasNext()) { - coords.add(new ChunkPos(iterator.nextLong())); - } - TaskManager.taskManager().task(() -> { - // trigger chunk load and apply ticket on main thread - List> futures = new ArrayList<>(); - for (ChunkPos pos : coords) { - futures.add(serverLevel.getWorld().getChunkAtAsync(pos.x, pos.z) - .thenAccept(c -> serverLevel.getChunkSource().addTicketAtLevel( - FAWE_TICKET, - pos, - LIGHT_LEVEL, - Unit.INSTANCE - )) - ); - } - // collect futures and trigger relight once all chunks are loaded - CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).thenAccept(v -> - invokeRelight( - coords, - c -> { - }, // no callback for single chunks required - i -> { - if (i != coords.size()) { - LOGGER.warn("Processed {} chunks instead of {}", i, coords.size()); - } - // post process chunks on main thread - TaskManager.taskManager().task(() -> postProcessChunks(coords)); - // call callback on our own threads - TaskManager.taskManager().async(andThen); - } - ) - ); - }); - } - - private void invokeRelight( + protected void invokeRelight( Set coords, Consumer chunkCallback, IntConsumer processCallback @@ -150,7 +61,7 @@ public class PaperweightStarlightRelighter implements Relighter { * Allow the server to unload the chunks again. * Also, if chunk packets are sent delayed, we need to do that here */ - private void postProcessChunks(Set coords) { + protected void postProcessChunks(Set coords) { boolean delay = Settings.settings().LIGHTING.DELAY_PACKET_SENDING; for (ChunkPos pos : coords) { int x = pos.x; @@ -162,44 +73,4 @@ public class PaperweightStarlightRelighter implements Relighter { } } - @Override - public void clear() { - - } - - @Override - public void removeLighting() { - this.delegate.removeLighting(); - } - - @Override - public void fixBlockLighting() { - fixLightingSafe(true); - } - - @Override - public void fixSkyLighting() { - fixLightingSafe(true); - } - - @Override - public boolean isEmpty() { - return true; - } - - @Override - public ReentrantLock getLock() { - return this.lock; - } - - @Override - public boolean isFinished() { - return false; - } - - @Override - public void close() throws Exception { - fixLightingSafe(true); - } - } diff --git a/worldedit-bukkit/adapters/adapter-1_19_4/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_19_R3/PaperweightStarlightRelighterFactory.java b/worldedit-bukkit/adapters/adapter-1_19_4/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_19_R3/PaperweightStarlightRelighterFactory.java index ad525f242..78c623c7a 100644 --- a/worldedit-bukkit/adapters/adapter-1_19_4/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_19_R3/PaperweightStarlightRelighterFactory.java +++ b/worldedit-bukkit/adapters/adapter-1_19_4/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_19_R3/PaperweightStarlightRelighterFactory.java @@ -4,7 +4,6 @@ import com.fastasyncworldedit.core.extent.processor.lighting.NullRelighter; import com.fastasyncworldedit.core.extent.processor.lighting.RelightMode; import com.fastasyncworldedit.core.extent.processor.lighting.Relighter; import com.fastasyncworldedit.core.extent.processor.lighting.RelighterFactory; -import com.fastasyncworldedit.core.queue.IQueueChunk; import com.fastasyncworldedit.core.queue.IQueueExtent; import com.sk89q.worldedit.world.World; import org.bukkit.Bukkit; @@ -15,9 +14,7 @@ import javax.annotation.Nonnull; public class PaperweightStarlightRelighterFactory implements RelighterFactory { @Override - public @Nonnull - @SuppressWarnings("rawtypes") - Relighter createRelighter(RelightMode relightMode, World world, IQueueExtent queue) { + public @Nonnull Relighter createRelighter(RelightMode relightMode, World world, IQueueExtent queue) { org.bukkit.World w = Bukkit.getWorld(world.getName()); if (w == null) { return NullRelighter.INSTANCE; diff --git a/worldedit-bukkit/adapters/adapter-1_20/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_20_R1/PaperweightStarlightRelighter.java b/worldedit-bukkit/adapters/adapter-1_20/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_20_R1/PaperweightStarlightRelighter.java index 2cc67360b..30df91459 100644 --- a/worldedit-bukkit/adapters/adapter-1_20/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_20_R1/PaperweightStarlightRelighter.java +++ b/worldedit-bukkit/adapters/adapter-1_20/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_20_R1/PaperweightStarlightRelighter.java @@ -1,140 +1,51 @@ package com.sk89q.worldedit.bukkit.adapter.impl.fawe.v1_20_R1; +import com.fastasyncworldedit.bukkit.adapter.StarlightRelighter; import com.fastasyncworldedit.core.configuration.Settings; -import com.fastasyncworldedit.core.extent.processor.lighting.NMSRelighter; -import com.fastasyncworldedit.core.extent.processor.lighting.Relighter; -import com.fastasyncworldedit.core.queue.IQueueChunk; import com.fastasyncworldedit.core.queue.IQueueExtent; -import com.fastasyncworldedit.core.util.MathMan; -import com.fastasyncworldedit.core.util.TaskManager; -import com.sk89q.worldedit.internal.util.LogManagerCompat; -import it.unimi.dsi.fastutil.longs.Long2ObjectLinkedOpenHashMap; -import it.unimi.dsi.fastutil.longs.LongArraySet; -import it.unimi.dsi.fastutil.longs.LongIterator; -import it.unimi.dsi.fastutil.longs.LongSet; import net.minecraft.server.level.ChunkMap; import net.minecraft.server.level.ServerLevel; import net.minecraft.server.level.TicketType; import net.minecraft.util.Unit; import net.minecraft.world.level.ChunkPos; import net.minecraft.world.level.chunk.ChunkStatus; -import org.apache.logging.log4j.Logger; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.List; import java.util.Set; import java.util.concurrent.CompletableFuture; -import java.util.concurrent.locks.ReentrantLock; import java.util.function.Consumer; import java.util.function.IntConsumer; -public class PaperweightStarlightRelighter implements Relighter { - - private static final Logger LOGGER = LogManagerCompat.getLogger(); - private static final int CHUNKS_PER_BATCH = 1024; // 32 * 32 - private static final int CHUNKS_PER_BATCH_SQRT_LOG2 = 5; // for shifting +public class PaperweightStarlightRelighter extends StarlightRelighter { private static final TicketType FAWE_TICKET = TicketType.create("fawe_ticket", (a, b) -> 0); private static final int LIGHT_LEVEL = ChunkMap.MAX_VIEW_DISTANCE + ChunkStatus.getDistance(ChunkStatus.LIGHT); - - private final ServerLevel serverLevel; - private final ReentrantLock lock = new ReentrantLock(); - private final Long2ObjectLinkedOpenHashMap regions = new Long2ObjectLinkedOpenHashMap<>(); - private final ReentrantLock areaLock = new ReentrantLock(); - private final NMSRelighter delegate; - - @SuppressWarnings("rawtypes") - public PaperweightStarlightRelighter(ServerLevel serverLevel, IQueueExtent queue) { - this.serverLevel = serverLevel; - this.delegate = new NMSRelighter(queue); + public PaperweightStarlightRelighter(ServerLevel serverLevel, IQueueExtent queue) { + super(serverLevel, queue); } @Override - public boolean addChunk(int cx, int cz, byte[] skipReason, int bitmask) { - areaLock.lock(); - try { - long key = MathMan.pairInt(cx >> CHUNKS_PER_BATCH_SQRT_LOG2, cz >> CHUNKS_PER_BATCH_SQRT_LOG2); - // TODO probably submit here already if chunks.size == CHUNKS_PER_BATCH? - LongSet chunks = this.regions.computeIfAbsent(key, k -> new LongArraySet(CHUNKS_PER_BATCH >> 2)); - chunks.add(ChunkPos.asLong(cx, cz)); - } finally { - areaLock.unlock(); - } - return true; + protected ChunkPos createChunkPos(final long chunkKey) { + return new ChunkPos(chunkKey); } @Override - public void addLightUpdate(int x, int y, int z) { - delegate.addLightUpdate(x, y, z); + protected long asLong(final int chunkX, final int chunkZ) { + return ChunkPos.asLong(chunkX, chunkZ); } - /* - * This method is called "recursively", iterating and removing elements - * from the regions linked map. This way, chunks are loaded in batches to avoid - * OOMEs. - */ @Override - public void fixLightingSafe(boolean sky) { - this.areaLock.lock(); - try { - if (regions.isEmpty()) { - return; - } - LongSet first = regions.removeFirst(); - fixLighting(first, () -> fixLightingSafe(true)); - } finally { - this.areaLock.unlock(); - } + protected CompletableFuture chunkLoadFuture(final ChunkPos chunkPos) { + return serverLevel.getWorld().getChunkAtAsync(chunkPos.x, chunkPos.z) + .thenAccept(c -> serverLevel.getChunkSource().addTicketAtLevel( + FAWE_TICKET, + chunkPos, + LIGHT_LEVEL, + Unit.INSTANCE + )); } - /* - * Processes a set of chunks and runs an action afterwards. - * The action is run async, the chunks are partly processed on the main thread - * (as required by the server). - */ - private void fixLighting(LongSet chunks, Runnable andThen) { - // convert from long keys to ChunkPos - Set coords = new HashSet<>(); - LongIterator iterator = chunks.iterator(); - while (iterator.hasNext()) { - coords.add(new ChunkPos(iterator.nextLong())); - } - TaskManager.taskManager().task(() -> { - // trigger chunk load and apply ticket on main thread - List> futures = new ArrayList<>(); - for (ChunkPos pos : coords) { - futures.add(serverLevel.getWorld().getChunkAtAsync(pos.x, pos.z) - .thenAccept(c -> serverLevel.getChunkSource().addTicketAtLevel( - FAWE_TICKET, - pos, - LIGHT_LEVEL, - Unit.INSTANCE - )) - ); - } - // collect futures and trigger relight once all chunks are loaded - CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).thenAccept(v -> - invokeRelight( - coords, - c -> { - }, // no callback for single chunks required - i -> { - if (i != coords.size()) { - LOGGER.warn("Processed {} chunks instead of {}", i, coords.size()); - } - // post process chunks on main thread - TaskManager.taskManager().task(() -> postProcessChunks(coords)); - // call callback on our own threads - TaskManager.taskManager().async(andThen); - } - ) - ); - }); - } - - private void invokeRelight( + protected void invokeRelight( Set coords, Consumer chunkCallback, IntConsumer processCallback @@ -150,7 +61,7 @@ public class PaperweightStarlightRelighter implements Relighter { * Allow the server to unload the chunks again. * Also, if chunk packets are sent delayed, we need to do that here */ - private void postProcessChunks(Set coords) { + protected void postProcessChunks(Set coords) { boolean delay = Settings.settings().LIGHTING.DELAY_PACKET_SENDING; for (ChunkPos pos : coords) { int x = pos.x; @@ -162,44 +73,4 @@ public class PaperweightStarlightRelighter implements Relighter { } } - @Override - public void clear() { - - } - - @Override - public void removeLighting() { - this.delegate.removeLighting(); - } - - @Override - public void fixBlockLighting() { - fixLightingSafe(true); - } - - @Override - public void fixSkyLighting() { - fixLightingSafe(true); - } - - @Override - public boolean isEmpty() { - return true; - } - - @Override - public ReentrantLock getLock() { - return this.lock; - } - - @Override - public boolean isFinished() { - return false; - } - - @Override - public void close() throws Exception { - fixLightingSafe(true); - } - } diff --git a/worldedit-bukkit/adapters/adapter-1_20/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_20_R1/PaperweightStarlightRelighterFactory.java b/worldedit-bukkit/adapters/adapter-1_20/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_20_R1/PaperweightStarlightRelighterFactory.java index e2658ee0e..83509a2bb 100644 --- a/worldedit-bukkit/adapters/adapter-1_20/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_20_R1/PaperweightStarlightRelighterFactory.java +++ b/worldedit-bukkit/adapters/adapter-1_20/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_20_R1/PaperweightStarlightRelighterFactory.java @@ -4,7 +4,6 @@ import com.fastasyncworldedit.core.extent.processor.lighting.NullRelighter; import com.fastasyncworldedit.core.extent.processor.lighting.RelightMode; import com.fastasyncworldedit.core.extent.processor.lighting.Relighter; import com.fastasyncworldedit.core.extent.processor.lighting.RelighterFactory; -import com.fastasyncworldedit.core.queue.IQueueChunk; import com.fastasyncworldedit.core.queue.IQueueExtent; import com.sk89q.worldedit.world.World; import org.bukkit.Bukkit; @@ -15,9 +14,7 @@ import javax.annotation.Nonnull; public class PaperweightStarlightRelighterFactory implements RelighterFactory { @Override - public @Nonnull - @SuppressWarnings("rawtypes") - Relighter createRelighter(RelightMode relightMode, World world, IQueueExtent queue) { + public @Nonnull Relighter createRelighter(RelightMode relightMode, World world, IQueueExtent queue) { org.bukkit.World w = Bukkit.getWorld(world.getName()); if (w == null) { return NullRelighter.INSTANCE; diff --git a/worldedit-bukkit/adapters/adapter-1_20_2/build.gradle.kts b/worldedit-bukkit/adapters/adapter-1_20_2/build.gradle.kts index 041ed296f..2f525f245 100644 --- a/worldedit-bukkit/adapters/adapter-1_20_2/build.gradle.kts +++ b/worldedit-bukkit/adapters/adapter-1_20_2/build.gradle.kts @@ -12,6 +12,6 @@ repositories { dependencies { // https://repo.papermc.io/service/rest/repository/browse/maven-public/io/papermc/paper/dev-bundle/ - the().paperDevBundle("1.20.2-R0.1-20231008.101509-26") + the().paperDevBundle("1.20.2-R0.1-20231029.153906-63") compileOnly(libs.paperlib) } diff --git a/worldedit-bukkit/adapters/adapter-1_20_2/src/main/java/com/sk89q/worldedit/bukkit/adapter/ext/fawe/v1_20_R2/PaperweightAdapter.java b/worldedit-bukkit/adapters/adapter-1_20_2/src/main/java/com/sk89q/worldedit/bukkit/adapter/ext/fawe/v1_20_R2/PaperweightAdapter.java index 38b0701d4..b73e6d06e 100644 --- a/worldedit-bukkit/adapters/adapter-1_20_2/src/main/java/com/sk89q/worldedit/bukkit/adapter/ext/fawe/v1_20_R2/PaperweightAdapter.java +++ b/worldedit-bukkit/adapters/adapter-1_20_2/src/main/java/com/sk89q/worldedit/bukkit/adapter/ext/fawe/v1_20_R2/PaperweightAdapter.java @@ -17,7 +17,7 @@ * along with this program. If not, see . */ -package com.sk89q.worldedit.bukkit.adapter.impl.v1_20_R2; +package com.sk89q.worldedit.bukkit.adapter.ext.fawe.v1_20_R2; import com.google.common.cache.CacheBuilder; import com.google.common.cache.CacheLoader; @@ -28,8 +28,6 @@ import com.google.common.util.concurrent.Futures; import com.mojang.datafixers.util.Either; import com.mojang.serialization.Lifecycle; import com.sk89q.jnbt.NBTConstants; -import com.sk89q.worldedit.bukkit.adapter.ext.fawe.v1_20_R2.PaperweightDataConverters; -import com.sk89q.worldedit.bukkit.adapter.impl.v1_20_R2.PaperweightFakePlayer; import com.sk89q.worldedit.WorldEditException; import com.sk89q.worldedit.blocks.BaseItem; import com.sk89q.worldedit.blocks.BaseItemStack; @@ -358,8 +356,7 @@ public final class PaperweightAdapter implements BukkitImplAdapter createWorldNativeAccess(org.bukkit.World world) { - return new com.sk89q.worldedit.bukkit.adapter.impl.v1_20_R2.PaperweightWorldNativeAccess(this, - new WeakReference<>(((CraftWorld) world).getHandle())); + return new PaperweightWorldNativeAccess(this, new WeakReference<>(((CraftWorld) world).getHandle())); } private static net.minecraft.core.Direction adapt(Direction face) { diff --git a/worldedit-bukkit/adapters/adapter-1_20_2/src/main/java/com/sk89q/worldedit/bukkit/adapter/ext/fawe/v1_20_R2/PaperweightDataConverters.java b/worldedit-bukkit/adapters/adapter-1_20_2/src/main/java/com/sk89q/worldedit/bukkit/adapter/ext/fawe/v1_20_R2/PaperweightDataConverters.java index af49e2809..f7be01738 100644 --- a/worldedit-bukkit/adapters/adapter-1_20_2/src/main/java/com/sk89q/worldedit/bukkit/adapter/ext/fawe/v1_20_R2/PaperweightDataConverters.java +++ b/worldedit-bukkit/adapters/adapter-1_20_2/src/main/java/com/sk89q/worldedit/bukkit/adapter/ext/fawe/v1_20_R2/PaperweightDataConverters.java @@ -168,7 +168,7 @@ public class PaperweightDataConverters extends DataFixerBuilder implements com.s .getValue().getAsString(); } - private final com.sk89q.worldedit.bukkit.adapter.impl.v1_20_R2.PaperweightAdapter adapter; + private final PaperweightAdapter adapter; private static final NbtOps OPS_NBT = NbtOps.INSTANCE; private static final int LEGACY_VERSION = 1343; @@ -204,8 +204,7 @@ public class PaperweightDataConverters extends DataFixerBuilder implements com.s } } - public PaperweightDataConverters(int dataVersion, - com.sk89q.worldedit.bukkit.adapter.impl.v1_20_R2.PaperweightAdapter adapter) { + public PaperweightDataConverters(int dataVersion, PaperweightAdapter adapter) { super(dataVersion); DATA_VERSION = dataVersion; INSTANCE = this; diff --git a/worldedit-bukkit/adapters/adapter-1_20_2/src/main/java/com/sk89q/worldedit/bukkit/adapter/ext/fawe/v1_20_R2/PaperweightFakePlayer.java b/worldedit-bukkit/adapters/adapter-1_20_2/src/main/java/com/sk89q/worldedit/bukkit/adapter/ext/fawe/v1_20_R2/PaperweightFakePlayer.java index f05eebe0c..c6d0b936c 100644 --- a/worldedit-bukkit/adapters/adapter-1_20_2/src/main/java/com/sk89q/worldedit/bukkit/adapter/ext/fawe/v1_20_R2/PaperweightFakePlayer.java +++ b/worldedit-bukkit/adapters/adapter-1_20_2/src/main/java/com/sk89q/worldedit/bukkit/adapter/ext/fawe/v1_20_R2/PaperweightFakePlayer.java @@ -17,7 +17,7 @@ * along with this program. If not, see . */ -package com.sk89q.worldedit.bukkit.adapter.impl.v1_20_R2; +package com.sk89q.worldedit.bukkit.adapter.ext.fawe.v1_20_R2; import com.mojang.authlib.GameProfile; import net.minecraft.network.chat.Component; diff --git a/worldedit-bukkit/adapters/adapter-1_20_2/src/main/java/com/sk89q/worldedit/bukkit/adapter/ext/fawe/v1_20_R2/PaperweightWorldNativeAccess.java b/worldedit-bukkit/adapters/adapter-1_20_2/src/main/java/com/sk89q/worldedit/bukkit/adapter/ext/fawe/v1_20_R2/PaperweightWorldNativeAccess.java index ac5baa35f..9e69e4a31 100644 --- a/worldedit-bukkit/adapters/adapter-1_20_2/src/main/java/com/sk89q/worldedit/bukkit/adapter/ext/fawe/v1_20_R2/PaperweightWorldNativeAccess.java +++ b/worldedit-bukkit/adapters/adapter-1_20_2/src/main/java/com/sk89q/worldedit/bukkit/adapter/ext/fawe/v1_20_R2/PaperweightWorldNativeAccess.java @@ -17,7 +17,7 @@ * along with this program. If not, see . */ -package com.sk89q.worldedit.bukkit.adapter.impl.v1_20_R2; +package com.sk89q.worldedit.bukkit.adapter.ext.fawe.v1_20_R2; import com.sk89q.worldedit.bukkit.BukkitAdapter; import com.sk89q.worldedit.internal.block.BlockStateIdAccess; @@ -43,11 +43,11 @@ public class PaperweightWorldNativeAccess implements WorldNativeAccess world; private SideEffectSet sideEffectSet; - public PaperweightWorldNativeAccess(com.sk89q.worldedit.bukkit.adapter.impl.v1_20_R2.PaperweightAdapter adapter, WeakReference world) { + public PaperweightWorldNativeAccess(PaperweightAdapter adapter, WeakReference world) { this.adapter = adapter; this.world = world; } diff --git a/worldedit-bukkit/adapters/adapter-1_20_2/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_20_R2/PaperweightFaweAdapter.java b/worldedit-bukkit/adapters/adapter-1_20_2/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_20_R2/PaperweightFaweAdapter.java index a9e657b2e..48bc935cb 100644 --- a/worldedit-bukkit/adapters/adapter-1_20_2/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_20_R2/PaperweightFaweAdapter.java +++ b/worldedit-bukkit/adapters/adapter-1_20_2/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_20_R2/PaperweightFaweAdapter.java @@ -20,7 +20,6 @@ import com.sk89q.worldedit.blocks.BaseItemStack; import com.sk89q.worldedit.bukkit.BukkitAdapter; import com.sk89q.worldedit.bukkit.BukkitWorld; import com.sk89q.worldedit.bukkit.adapter.BukkitImplAdapter; -import com.sk89q.worldedit.bukkit.adapter.impl.v1_20_R2.PaperweightAdapter; import com.sk89q.worldedit.bukkit.adapter.impl.fawe.v1_20_R2.nbt.PaperweightLazyCompoundTag; import com.sk89q.worldedit.bukkit.adapter.impl.fawe.v1_20_R2.regen.PaperweightRegen; import com.sk89q.worldedit.entity.BaseEntity; @@ -124,7 +123,7 @@ public final class PaperweightFaweAdapter extends CachedBukkitAdapter implements } } - private final PaperweightAdapter parent; + private final com.sk89q.worldedit.bukkit.adapter.ext.fawe.v1_20_R2.PaperweightAdapter parent; // ------------------------------------------------------------------------ // Code that may break between versions of Minecraft // ------------------------------------------------------------------------ @@ -135,7 +134,7 @@ public final class PaperweightFaweAdapter extends CachedBukkitAdapter implements private Map>> allBlockProperties = null; public PaperweightFaweAdapter() throws NoSuchFieldException, NoSuchMethodException { - this.parent = new PaperweightAdapter(); + this.parent = new com.sk89q.worldedit.bukkit.adapter.ext.fawe.v1_20_R2.PaperweightAdapter(); } @Nullable diff --git a/worldedit-bukkit/adapters/adapter-1_20_2/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_20_R2/PaperweightStarlightRelighter.java b/worldedit-bukkit/adapters/adapter-1_20_2/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_20_R2/PaperweightStarlightRelighter.java index 6c6527c02..e869046da 100644 --- a/worldedit-bukkit/adapters/adapter-1_20_2/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_20_R2/PaperweightStarlightRelighter.java +++ b/worldedit-bukkit/adapters/adapter-1_20_2/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_20_R2/PaperweightStarlightRelighter.java @@ -1,140 +1,51 @@ package com.sk89q.worldedit.bukkit.adapter.impl.fawe.v1_20_R2; +import com.fastasyncworldedit.bukkit.adapter.StarlightRelighter; import com.fastasyncworldedit.core.configuration.Settings; -import com.fastasyncworldedit.core.extent.processor.lighting.NMSRelighter; -import com.fastasyncworldedit.core.extent.processor.lighting.Relighter; -import com.fastasyncworldedit.core.queue.IQueueChunk; import com.fastasyncworldedit.core.queue.IQueueExtent; -import com.fastasyncworldedit.core.util.MathMan; -import com.fastasyncworldedit.core.util.TaskManager; -import com.sk89q.worldedit.internal.util.LogManagerCompat; -import it.unimi.dsi.fastutil.longs.Long2ObjectLinkedOpenHashMap; -import it.unimi.dsi.fastutil.longs.LongArraySet; -import it.unimi.dsi.fastutil.longs.LongIterator; -import it.unimi.dsi.fastutil.longs.LongSet; import net.minecraft.server.level.ChunkMap; import net.minecraft.server.level.ServerLevel; import net.minecraft.server.level.TicketType; import net.minecraft.util.Unit; import net.minecraft.world.level.ChunkPos; import net.minecraft.world.level.chunk.ChunkStatus; -import org.apache.logging.log4j.Logger; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.List; import java.util.Set; import java.util.concurrent.CompletableFuture; -import java.util.concurrent.locks.ReentrantLock; import java.util.function.Consumer; import java.util.function.IntConsumer; -public class PaperweightStarlightRelighter implements Relighter { - - private static final Logger LOGGER = LogManagerCompat.getLogger(); - private static final int CHUNKS_PER_BATCH = 1024; // 32 * 32 - private static final int CHUNKS_PER_BATCH_SQRT_LOG2 = 5; // for shifting +public class PaperweightStarlightRelighter extends StarlightRelighter { private static final TicketType FAWE_TICKET = TicketType.create("fawe_ticket", (a, b) -> 0); private static final int LIGHT_LEVEL = ChunkMap.MAX_VIEW_DISTANCE + ChunkStatus.getDistance(ChunkStatus.LIGHT); - - private final ServerLevel serverLevel; - private final ReentrantLock lock = new ReentrantLock(); - private final Long2ObjectLinkedOpenHashMap regions = new Long2ObjectLinkedOpenHashMap<>(); - private final ReentrantLock areaLock = new ReentrantLock(); - private final NMSRelighter delegate; - - @SuppressWarnings("rawtypes") - public PaperweightStarlightRelighter(ServerLevel serverLevel, IQueueExtent queue) { - this.serverLevel = serverLevel; - this.delegate = new NMSRelighter(queue); + public PaperweightStarlightRelighter(ServerLevel serverLevel, IQueueExtent queue) { + super(serverLevel, queue); } @Override - public boolean addChunk(int cx, int cz, byte[] skipReason, int bitmask) { - areaLock.lock(); - try { - long key = MathMan.pairInt(cx >> CHUNKS_PER_BATCH_SQRT_LOG2, cz >> CHUNKS_PER_BATCH_SQRT_LOG2); - // TODO probably submit here already if chunks.size == CHUNKS_PER_BATCH? - LongSet chunks = this.regions.computeIfAbsent(key, k -> new LongArraySet(CHUNKS_PER_BATCH >> 2)); - chunks.add(ChunkPos.asLong(cx, cz)); - } finally { - areaLock.unlock(); - } - return true; + protected ChunkPos createChunkPos(final long chunkKey) { + return new ChunkPos(chunkKey); } @Override - public void addLightUpdate(int x, int y, int z) { - delegate.addLightUpdate(x, y, z); + protected long asLong(final int chunkX, final int chunkZ) { + return ChunkPos.asLong(chunkX, chunkZ); } - /* - * This method is called "recursively", iterating and removing elements - * from the regions linked map. This way, chunks are loaded in batches to avoid - * OOMEs. - */ @Override - public void fixLightingSafe(boolean sky) { - this.areaLock.lock(); - try { - if (regions.isEmpty()) { - return; - } - LongSet first = regions.removeFirst(); - fixLighting(first, () -> fixLightingSafe(true)); - } finally { - this.areaLock.unlock(); - } + protected CompletableFuture chunkLoadFuture(final ChunkPos chunkPos) { + return serverLevel.getWorld().getChunkAtAsync(chunkPos.x, chunkPos.z) + .thenAccept(c -> serverLevel.getChunkSource().addTicketAtLevel( + FAWE_TICKET, + chunkPos, + LIGHT_LEVEL, + Unit.INSTANCE + )); } - /* - * Processes a set of chunks and runs an action afterwards. - * The action is run async, the chunks are partly processed on the main thread - * (as required by the server). - */ - private void fixLighting(LongSet chunks, Runnable andThen) { - // convert from long keys to ChunkPos - Set coords = new HashSet<>(); - LongIterator iterator = chunks.iterator(); - while (iterator.hasNext()) { - coords.add(new ChunkPos(iterator.nextLong())); - } - TaskManager.taskManager().task(() -> { - // trigger chunk load and apply ticket on main thread - List> futures = new ArrayList<>(); - for (ChunkPos pos : coords) { - futures.add(serverLevel.getWorld().getChunkAtAsync(pos.x, pos.z) - .thenAccept(c -> serverLevel.getChunkSource().addTicketAtLevel( - FAWE_TICKET, - pos, - LIGHT_LEVEL, - Unit.INSTANCE - )) - ); - } - // collect futures and trigger relight once all chunks are loaded - CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).thenAccept(v -> - invokeRelight( - coords, - c -> { - }, // no callback for single chunks required - i -> { - if (i != coords.size()) { - LOGGER.warn("Processed {} chunks instead of {}", i, coords.size()); - } - // post process chunks on main thread - TaskManager.taskManager().task(() -> postProcessChunks(coords)); - // call callback on our own threads - TaskManager.taskManager().async(andThen); - } - ) - ); - }); - } - - private void invokeRelight( + protected void invokeRelight( Set coords, Consumer chunkCallback, IntConsumer processCallback @@ -150,7 +61,7 @@ public class PaperweightStarlightRelighter implements Relighter { * Allow the server to unload the chunks again. * Also, if chunk packets are sent delayed, we need to do that here */ - private void postProcessChunks(Set coords) { + protected void postProcessChunks(Set coords) { boolean delay = Settings.settings().LIGHTING.DELAY_PACKET_SENDING; for (ChunkPos pos : coords) { int x = pos.x; @@ -162,44 +73,4 @@ public class PaperweightStarlightRelighter implements Relighter { } } - @Override - public void clear() { - - } - - @Override - public void removeLighting() { - this.delegate.removeLighting(); - } - - @Override - public void fixBlockLighting() { - fixLightingSafe(true); - } - - @Override - public void fixSkyLighting() { - fixLightingSafe(true); - } - - @Override - public boolean isEmpty() { - return true; - } - - @Override - public ReentrantLock getLock() { - return this.lock; - } - - @Override - public boolean isFinished() { - return false; - } - - @Override - public void close() throws Exception { - fixLightingSafe(true); - } - } diff --git a/worldedit-bukkit/adapters/adapter-1_20_2/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_20_R2/PaperweightStarlightRelighterFactory.java b/worldedit-bukkit/adapters/adapter-1_20_2/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_20_R2/PaperweightStarlightRelighterFactory.java index 4e5b9b7ff..4afc48689 100644 --- a/worldedit-bukkit/adapters/adapter-1_20_2/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_20_R2/PaperweightStarlightRelighterFactory.java +++ b/worldedit-bukkit/adapters/adapter-1_20_2/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_20_R2/PaperweightStarlightRelighterFactory.java @@ -4,7 +4,6 @@ import com.fastasyncworldedit.core.extent.processor.lighting.NullRelighter; import com.fastasyncworldedit.core.extent.processor.lighting.RelightMode; import com.fastasyncworldedit.core.extent.processor.lighting.Relighter; import com.fastasyncworldedit.core.extent.processor.lighting.RelighterFactory; -import com.fastasyncworldedit.core.queue.IQueueChunk; import com.fastasyncworldedit.core.queue.IQueueExtent; import com.sk89q.worldedit.world.World; import org.bukkit.Bukkit; @@ -15,9 +14,7 @@ import javax.annotation.Nonnull; public class PaperweightStarlightRelighterFactory implements RelighterFactory { @Override - public @Nonnull - @SuppressWarnings("rawtypes") - Relighter createRelighter(RelightMode relightMode, World world, IQueueExtent queue) { + public @Nonnull Relighter createRelighter(RelightMode relightMode, World world, IQueueExtent queue) { org.bukkit.World w = Bukkit.getWorld(world.getName()); if (w == null) { return NullRelighter.INSTANCE; diff --git a/worldedit-bukkit/src/main/java/com/fastasyncworldedit/bukkit/adapter/NMSRelighterFactory.java b/worldedit-bukkit/src/main/java/com/fastasyncworldedit/bukkit/adapter/NMSRelighterFactory.java index 4fd78df85..39c327991 100644 --- a/worldedit-bukkit/src/main/java/com/fastasyncworldedit/bukkit/adapter/NMSRelighterFactory.java +++ b/worldedit-bukkit/src/main/java/com/fastasyncworldedit/bukkit/adapter/NMSRelighterFactory.java @@ -5,7 +5,6 @@ import com.fastasyncworldedit.core.extent.processor.lighting.NMSRelighter; import com.fastasyncworldedit.core.extent.processor.lighting.RelightMode; import com.fastasyncworldedit.core.extent.processor.lighting.Relighter; import com.fastasyncworldedit.core.extent.processor.lighting.RelighterFactory; -import com.fastasyncworldedit.core.queue.IQueueChunk; import com.fastasyncworldedit.core.queue.IQueueExtent; import com.sk89q.worldedit.world.World; @@ -14,8 +13,7 @@ import javax.annotation.Nonnull; public class NMSRelighterFactory implements RelighterFactory { @Override - public @Nonnull - Relighter createRelighter(RelightMode relightMode, World world, IQueueExtent queue) { + public @Nonnull Relighter createRelighter(RelightMode relightMode, World world, IQueueExtent queue) { return new NMSRelighter( queue, relightMode != null ? relightMode : RelightMode.valueOf(Settings.settings().LIGHTING.MODE) diff --git a/worldedit-bukkit/src/main/java/com/fastasyncworldedit/bukkit/adapter/StarlightRelighter.java b/worldedit-bukkit/src/main/java/com/fastasyncworldedit/bukkit/adapter/StarlightRelighter.java new file mode 100644 index 000000000..02729cfa9 --- /dev/null +++ b/worldedit-bukkit/src/main/java/com/fastasyncworldedit/bukkit/adapter/StarlightRelighter.java @@ -0,0 +1,196 @@ +package com.fastasyncworldedit.bukkit.adapter; + +import com.fastasyncworldedit.core.extent.processor.lighting.NMSRelighter; +import com.fastasyncworldedit.core.extent.processor.lighting.Relighter; +import com.fastasyncworldedit.core.queue.IQueueExtent; +import com.fastasyncworldedit.core.util.MathMan; +import com.fastasyncworldedit.core.util.TaskManager; +import com.sk89q.worldedit.internal.util.LogManagerCompat; +import it.unimi.dsi.fastutil.longs.Long2ObjectLinkedOpenHashMap; +import it.unimi.dsi.fastutil.longs.LongArraySet; +import it.unimi.dsi.fastutil.longs.LongIterator; +import it.unimi.dsi.fastutil.longs.LongSet; +import org.apache.logging.log4j.Logger; +import org.jetbrains.annotations.NotNull; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.locks.ReentrantLock; +import java.util.function.Consumer; +import java.util.function.IntConsumer; + +/** + * A base class for version-specific implementations of the starlight relighting mechanism + * + * @param the version-specific ServerLevel type + * @param the version-specific ChunkPos type + * @since 2.8.2 + */ +public abstract class StarlightRelighter implements Relighter { + + protected static final Logger LOGGER = LogManagerCompat.getLogger(); + + private static final int CHUNKS_PER_BATCH = 1024; // 32 * 32 + private static final int CHUNKS_PER_BATCH_SQRT_LOG2 = 5; // for shifting + + private final ReentrantLock lock = new ReentrantLock(); + private final Long2ObjectLinkedOpenHashMap regions = new Long2ObjectLinkedOpenHashMap<>(); + private final ReentrantLock areaLock = new ReentrantLock(); + private final NMSRelighter delegate; + protected final SERVER_LEVEL serverLevel; + + protected StarlightRelighter(SERVER_LEVEL serverLevel, IQueueExtent queue) { + this.serverLevel = serverLevel; + this.delegate = new NMSRelighter(queue); + } + + protected Set convertChunkKeysToChunkPos(LongSet chunks) { + // convert from long keys to ChunkPos + Set coords = new HashSet<>(); + LongIterator iterator = chunks.iterator(); + while (iterator.hasNext()) { + coords.add(createChunkPos(iterator.nextLong())); + } + return coords; + } + + protected abstract CHUNK_POS createChunkPos(long chunkKey); + + protected abstract long asLong(int chunkX, int chunkZ); + + protected abstract CompletableFuture chunkLoadFuture(CHUNK_POS pos); + + protected List> chunkLoadFutures(Set coords) { + List> futures = new ArrayList<>(); + for (final CHUNK_POS coord : coords) { + futures.add(chunkLoadFuture(coord)); + } + return futures; + } + + @NotNull + protected IntConsumer postProcessCallback(Runnable andThen, Set coords) { + return i -> { + if (i != coords.size()) { + LOGGER.warn("Processed {} chunks instead of {}", i, coords.size()); + } + // post process chunks on main thread + TaskManager.taskManager().task(() -> postProcessChunks(coords)); + // call callback on our own threads + TaskManager.taskManager().async(andThen); + }; + } + + protected abstract void invokeRelight( + Set coords, + Consumer chunkCallback, + IntConsumer processCallback + ); + + protected abstract void postProcessChunks(Set coords); + + /* + * Processes a set of chunks and runs an action afterwards. + * The action is run async, the chunks are partly processed on the main thread + * (as required by the server). + */ + protected void fixLighting(LongSet chunks, Runnable andThen) { + Set coords = convertChunkKeysToChunkPos(chunks); + TaskManager.taskManager().task(() -> { + // trigger chunk load and apply ticket on main thread + List> futures = chunkLoadFutures(coords); + // collect futures and trigger relight once all chunks are loaded + CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).thenAccept(v -> + invokeRelight( + coords, + c -> { + }, // no callback for single chunks required + postProcessCallback(andThen, coords) + ) + ); + }); + } + + @Override + public boolean addChunk(int cx, int cz, byte[] skipReason, int bitmask) { + areaLock.lock(); + try { + long key = MathMan.pairInt(cx >> CHUNKS_PER_BATCH_SQRT_LOG2, cz >> CHUNKS_PER_BATCH_SQRT_LOG2); + // TODO probably submit here already if chunks.size == CHUNKS_PER_BATCH? + LongSet chunks = this.regions.computeIfAbsent(key, k -> new LongArraySet(CHUNKS_PER_BATCH >> 2)); + chunks.add(asLong(cx, cz)); + } finally { + areaLock.unlock(); + } + return true; + } + + /* + * This method is called "recursively", iterating and removing elements + * from the regions linked map. This way, chunks are loaded in batches to avoid + * OOMEs. + */ + + @Override + public void fixLightingSafe(boolean sky) { + this.areaLock.lock(); + try { + if (regions.isEmpty()) { + return; + } + LongSet first = regions.removeFirst(); + fixLighting(first, () -> fixLightingSafe(true)); + } finally { + this.areaLock.unlock(); + } + } + + @Override + public void addLightUpdate(int x, int y, int z) { + this.delegate.addLightUpdate(x, y, z); + } + + @Override + public void clear() { + + } + + @Override + public void removeLighting() { + this.delegate.removeLighting(); + } + + @Override + public void fixBlockLighting() { + fixLightingSafe(true); + } + + @Override + public void fixSkyLighting() { + fixLightingSafe(true); + } + + @Override + public boolean isEmpty() { + return true; + } + + @Override + public ReentrantLock getLock() { + return this.lock; + } + + @Override + public boolean isFinished() { + return false; + } + + @Override + public void close() throws Exception { + fixLightingSafe(true); + } + +} diff --git a/worldedit-core/src/main/java/com/fastasyncworldedit/core/FaweCache.java b/worldedit-core/src/main/java/com/fastasyncworldedit/core/FaweCache.java index d8e1474e6..0655e4214 100644 --- a/worldedit-core/src/main/java/com/fastasyncworldedit/core/FaweCache.java +++ b/worldedit-core/src/main/java/com/fastasyncworldedit/core/FaweCache.java @@ -60,16 +60,17 @@ import java.util.function.Supplier; import static com.google.common.base.Preconditions.checkNotNull; public enum FaweCache implements Trimable { - /** - * @deprecated Use {@link #INSTANCE} to get an instance. - */ - @Deprecated(forRemoval = true, since = "2.0.0") - IMP, /** * @since 2.0.0 */ INSTANCE; + /** + * @deprecated Use {@link #INSTANCE} to get an instance. + */ + @Deprecated(forRemoval = true, since = "2.0.0") + public static final FaweCache IMP = INSTANCE; + private static final Logger LOGGER = LogManagerCompat.getLogger(); public final int BLOCKS_PER_LAYER = 4096; diff --git a/worldedit-core/src/main/java/com/fastasyncworldedit/core/configuration/Settings.java b/worldedit-core/src/main/java/com/fastasyncworldedit/core/configuration/Settings.java index ce322f75c..a2bd17eba 100644 --- a/worldedit-core/src/main/java/com/fastasyncworldedit/core/configuration/Settings.java +++ b/worldedit-core/src/main/java/com/fastasyncworldedit/core/configuration/Settings.java @@ -19,14 +19,14 @@ import java.util.stream.Stream; public class Settings extends Config { + @Ignore + static Settings INSTANCE = new Settings(); /** * @deprecated Use {@link #settings()} instead to get an instance. */ @Ignore @Deprecated(forRemoval = true, since = "2.0.0") - public static final Settings IMP = new Settings(); - @Ignore - static Settings INSTANCE = new Settings(); + public static final Settings IMP = INSTANCE; @Ignore public boolean PROTOCOL_SUPPORT_FIX = false; @Comment("These first 6 aren't configurable") // This is a comment diff --git a/worldedit-core/src/main/java/com/fastasyncworldedit/core/database/DBHandler.java b/worldedit-core/src/main/java/com/fastasyncworldedit/core/database/DBHandler.java index fd1263b11..755e94b2f 100644 --- a/worldedit-core/src/main/java/com/fastasyncworldedit/core/database/DBHandler.java +++ b/worldedit-core/src/main/java/com/fastasyncworldedit/core/database/DBHandler.java @@ -13,7 +13,7 @@ public class DBHandler { * @deprecated Use {@link #dbHandler()} instead. */ @Deprecated(forRemoval = true, since = "2.0.0") - public static final DBHandler IMP = new DBHandler(); + public static final DBHandler IMP = dbHandler(); private static final Logger LOGGER = LogManagerCompat.getLogger(); private static DBHandler INSTANCE; private final Map databases = new ConcurrentHashMap<>(8, 0.9f, 1); diff --git a/worldedit-core/src/main/java/com/fastasyncworldedit/core/extent/processor/lighting/NMSRelighter.java b/worldedit-core/src/main/java/com/fastasyncworldedit/core/extent/processor/lighting/NMSRelighter.java index 97a97f9a7..e38df12f5 100644 --- a/worldedit-core/src/main/java/com/fastasyncworldedit/core/extent/processor/lighting/NMSRelighter.java +++ b/worldedit-core/src/main/java/com/fastasyncworldedit/core/extent/processor/lighting/NMSRelighter.java @@ -4,7 +4,6 @@ import com.fastasyncworldedit.core.Fawe; import com.fastasyncworldedit.core.configuration.Settings; import com.fastasyncworldedit.core.math.BlockVectorSet; import com.fastasyncworldedit.core.math.MutableBlockVector3; -import com.fastasyncworldedit.core.queue.IQueueChunk; import com.fastasyncworldedit.core.queue.IQueueExtent; import com.fastasyncworldedit.core.queue.implementation.chunk.ChunkHolder; import com.fastasyncworldedit.core.util.MathMan; @@ -34,7 +33,6 @@ import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.locks.ReentrantLock; -@SuppressWarnings("rawtypes") public class NMSRelighter implements Relighter { private static final int DISPATCH_SIZE = 64; @@ -51,7 +49,7 @@ public class NMSRelighter implements Relighter { } public final MutableBlockVector3 mutableBlockPos = new MutableBlockVector3(0, 0, 0); - private final IQueueExtent queue; + private final IQueueExtent queue; private final Map skyToRelight; private final Object present = new Object(); private final Map chunksToSend; @@ -66,11 +64,11 @@ public class NMSRelighter implements Relighter { private final AtomicBoolean finished = new AtomicBoolean(false); private boolean removeFirst; - public NMSRelighter(IQueueExtent queue) { + public NMSRelighter(IQueueExtent queue) { this(queue, null); } - public NMSRelighter(IQueueExtent queue, RelightMode relightMode) { + public NMSRelighter(IQueueExtent queue, RelightMode relightMode) { this.queue = queue; this.skyToRelight = new Long2ObjectOpenHashMap<>(12); this.lightQueue = new Long2ObjectOpenHashMap<>(12); diff --git a/worldedit-core/src/main/java/com/fastasyncworldedit/core/extent/processor/lighting/RelighterFactory.java b/worldedit-core/src/main/java/com/fastasyncworldedit/core/extent/processor/lighting/RelighterFactory.java index 34275f77d..f0bca36ed 100644 --- a/worldedit-core/src/main/java/com/fastasyncworldedit/core/extent/processor/lighting/RelighterFactory.java +++ b/worldedit-core/src/main/java/com/fastasyncworldedit/core/extent/processor/lighting/RelighterFactory.java @@ -1,6 +1,5 @@ package com.fastasyncworldedit.core.extent.processor.lighting; -import com.fastasyncworldedit.core.queue.IQueueChunk; import com.fastasyncworldedit.core.queue.IQueueExtent; import com.sk89q.worldedit.world.World; @@ -25,6 +24,6 @@ public interface RelighterFactory { * @return a new Relighter instance with the specified settings. */ @Nonnull - Relighter createRelighter(RelightMode relightMode, World world, IQueueExtent queue); + Relighter createRelighter(RelightMode relightMode, World world, IQueueExtent queue); } diff --git a/worldedit-core/src/main/java/com/fastasyncworldedit/core/queue/IChunkGet.java b/worldedit-core/src/main/java/com/fastasyncworldedit/core/queue/IChunkGet.java index a3c554c49..ff964e7a3 100644 --- a/worldedit-core/src/main/java/com/fastasyncworldedit/core/queue/IChunkGet.java +++ b/worldedit-core/src/main/java/com/fastasyncworldedit/core/queue/IChunkGet.java @@ -73,14 +73,14 @@ public interface IChunkGet extends IBlocks, Trimable, InputExtent, ITileInput { * Lock the {@link IChunkGet#call(IChunkSet, Runnable)} method to the current thread using a reentrant lock. Also locks * related methods e.g. {@link IChunkGet#setCreateCopy(boolean)} * - * @since TODO + * @since 2.8.2 */ default void lockCall() {} /** * Unlock {@link IChunkGet#call(IChunkSet, Runnable)} (and other related methods) to executions from other threads * - * @since TODO + * @since 2.8.2 */ default void unlockCall() {} diff --git a/worldedit-core/src/main/java/com/fastasyncworldedit/core/queue/IQueueExtent.java b/worldedit-core/src/main/java/com/fastasyncworldedit/core/queue/IQueueExtent.java index cb09aef7e..e2d86ef57 100644 --- a/worldedit-core/src/main/java/com/fastasyncworldedit/core/queue/IQueueExtent.java +++ b/worldedit-core/src/main/java/com/fastasyncworldedit/core/queue/IQueueExtent.java @@ -135,9 +135,6 @@ public interface IQueueExtent extends Flushable, Trimable, ICh return block; } T chunk = this.getOrCreateChunk(chunkX, chunkZ); - // Initialize - chunk.init(this, chunkX, chunkZ); - chunk.setFastMode(isFastMode()); T newChunk = filter.applyChunk(chunk, region); if (newChunk != null) { diff --git a/worldedit-core/src/main/java/com/fastasyncworldedit/core/util/WEManager.java b/worldedit-core/src/main/java/com/fastasyncworldedit/core/util/WEManager.java index 17b7882f2..b31853dd0 100644 --- a/worldedit-core/src/main/java/com/fastasyncworldedit/core/util/WEManager.java +++ b/worldedit-core/src/main/java/com/fastasyncworldedit/core/util/WEManager.java @@ -32,7 +32,7 @@ public class WEManager { * @deprecated Use {@link #weManager()} instead. */ @Deprecated(forRemoval = true, since = "2.0.0") - public static WEManager IMP = new WEManager(); + public static WEManager IMP = weManager(); private final ArrayDeque managers = new ArrayDeque<>(); /** diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/LocalSession.java b/worldedit-core/src/main/java/com/sk89q/worldedit/LocalSession.java index 3391bad80..67bbff7af 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/LocalSession.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/LocalSession.java @@ -33,6 +33,7 @@ import com.fastasyncworldedit.core.limit.FaweLimit; import com.fastasyncworldedit.core.util.BrushCache; import com.fastasyncworldedit.core.util.MainUtil; import com.fastasyncworldedit.core.util.StringMan; +import com.fastasyncworldedit.core.util.TaskManager; import com.fastasyncworldedit.core.util.TextureHolder; import com.fastasyncworldedit.core.util.TextureUtil; import com.fastasyncworldedit.core.wrappers.WorldWrapper; @@ -99,7 +100,6 @@ import java.util.TimeZone; import java.util.UUID; import java.util.concurrent.ExecutionException; import java.util.concurrent.atomic.AtomicBoolean; -import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; import java.util.stream.Collectors; @@ -143,7 +143,7 @@ public class LocalSession implements TextureHolder { } }); private transient volatile Integer historyNegativeIndex; - private transient final Lock historyWriteLock = new ReentrantLock(true); + private transient final ReentrantLock historyWriteLock = new ReentrantLock(true); private final transient Int2ObjectOpenHashMap tools = new Int2ObjectOpenHashMap<>(0); private transient Mask sourceMask; private transient TextureUtil texture; @@ -405,6 +405,23 @@ public class LocalSession implements TextureHolder { */ public void clearHistory() { //FAWE start + boolean mainThread = Fawe.isMainThread(); + if (mainThread && !historyWriteLock.tryLock()) { + // Do not make main thread wait if we cannot immediately clear history (on player logout usually) + TaskManager.taskManager().async(this::clearHistoryTask); + return; + } + try { + clearHistoryTask(); + } finally { + // only if we are on the main thread, we ever called tryLock -> need to unlock again + if (mainThread) { + historyWriteLock.unlock(); + } + } + } + + private void clearHistoryTask() { historyWriteLock.lock(); try { // Ensure that changesets are properly removed @@ -420,8 +437,8 @@ public class LocalSession implements TextureHolder { save(); historySize = 0; currentWorld = null; - //FAWE end } + //FAWE end /** * Remember an edit session for the undo history. If the history maximum diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/ClipboardCommands.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/ClipboardCommands.java index e39bbc641..8ccd4f44e 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/command/ClipboardCommands.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/command/ClipboardCommands.java @@ -29,7 +29,6 @@ import com.fastasyncworldedit.core.extent.clipboard.DiskOptimizedClipboard; import com.fastasyncworldedit.core.extent.clipboard.MultiClipboardHolder; import com.fastasyncworldedit.core.extent.clipboard.ReadOnlyClipboard; import com.fastasyncworldedit.core.extent.clipboard.URIClipboardHolder; -import com.fastasyncworldedit.core.internal.exception.FaweException; import com.fastasyncworldedit.core.internal.io.FastByteArrayOutputStream; import com.fastasyncworldedit.core.limit.FaweLimit; import com.fastasyncworldedit.core.util.ImgurUtility; @@ -449,6 +448,8 @@ public class ClipboardCommands { boolean atOrigin, @Switch(name = 's', desc = "Select the region after pasting") boolean selectPasted, + @Switch(name = 'n', desc = "No paste, select only. (Implies -s)") + boolean onlySelect, @Switch(name = 'e', desc = "Paste entities if available") boolean pasteEntities, @Switch(name = 'b', desc = "Paste biomes if available") @@ -460,10 +461,12 @@ public class ClipboardCommands { final BlockVector3 to = atOrigin ? origin : session.getPlacementPosition(actor); checkPaste(actor, editSession, to, holder, clipboard); - clipboard.paste(editSession, to, !ignoreAirBlocks, pasteEntities, pasteBiomes); + if (!onlySelect) { + clipboard.paste(editSession, to, !ignoreAirBlocks, pasteEntities, pasteBiomes); + } Region region = clipboard.getRegion().clone(); - if (selectPasted) { + if (selectPasted || onlySelect) { BlockVector3 clipboardOffset = clipboard.getRegion().getMinimumPoint().subtract(clipboard.getOrigin()); BlockVector3 realTo = to.add(holder.getTransform().apply(clipboardOffset.toVector3()).toBlockPoint()); BlockVector3 max = realTo.add(holder @@ -475,7 +478,11 @@ public class ClipboardCommands { selector.learnChanges(); selector.explainRegionAdjust(actor, session); } - actor.print(Caption.of("fawe.worldedit.paste.command.paste", to)); + if (onlySelect) { + actor.print(Caption.of("worldedit.paste.selected")); + } else { + actor.print(Caption.of("fawe.worldedit.paste.command.paste", to)); + } if (!actor.hasPermission("fawe.tips")) { actor.print(Caption.of("fawe.tips.tip.copypaste")); @@ -512,7 +519,7 @@ public class ClipboardCommands { ClipboardHolder holder = session.getClipboard(); //FAWE start - use place if (holder.getTransform().isIdentity() && sourceMask == null) { - place(actor, world, session, editSession, ignoreAirBlocks, atOrigin, selectPasted, + place(actor, world, session, editSession, ignoreAirBlocks, atOrigin, selectPasted, onlySelect, pasteEntities, pasteBiomes ); return; diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/HistorySubCommands.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/HistorySubCommands.java index 452d5084c..6b30e9e7e 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/command/HistorySubCommands.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/command/HistorySubCommands.java @@ -201,7 +201,7 @@ public class HistorySubCommands { .at(summary.maxX, world.getMaxY(), summary.maxZ) ); rollback.setTime(historyFile.lastModified()); - RollbackDatabase db = DBHandler.IMP + RollbackDatabase db = DBHandler.dbHandler() .getDatabase(world); db.logEdit(rollback); actor.print(TextComponent.of("Logging: " + historyFile)); diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/SchematicCommands.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/SchematicCommands.java index c677734ef..1158a7847 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/command/SchematicCommands.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/command/SchematicCommands.java @@ -313,9 +313,9 @@ public class SchematicCommands { Actor actor, LocalSession session, @Arg(desc = "File name.") String filename, - @Arg(desc = "Format name.", def = "fast") - String formatName, //FAWE start - random rotation + @Arg(desc = "Format name.", def = "") + String formatName, @Switch(name = 'r', desc = "Apply random rotation to the clipboard") boolean randomRotate //FAWE end @@ -325,6 +325,11 @@ public class SchematicCommands { //FAWE start ClipboardFormat format; InputStream in = null; + // if format is set explicitly, do not look up by extension! + boolean noExplicitFormat = formatName == null; + if (noExplicitFormat) { + formatName = "fast"; + } try { URI uri; if (formatName.startsWith("url:")) { @@ -369,7 +374,7 @@ public class SchematicCommands { actor.print(Caption.of("fawe.error.no-perm", "worldedit.schematic.load.other")); return; } - if (filename.matches(".*\\.[\\w].*")) { + if (noExplicitFormat && filename.matches(".*\\.[\\w].*")) { format = ClipboardFormats .findByExtension(filename.substring(filename.lastIndexOf('.') + 1)); } else { diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/extent/clipboard/io/ClipboardFormats.java b/worldedit-core/src/main/java/com/sk89q/worldedit/extent/clipboard/io/ClipboardFormats.java index b00c9ea5b..03cce5d80 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/extent/clipboard/io/ClipboardFormats.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/extent/clipboard/io/ClipboardFormats.java @@ -26,7 +26,6 @@ import com.fastasyncworldedit.core.extent.clipboard.MultiClipboardHolder; import com.fastasyncworldedit.core.extent.clipboard.URIClipboardHolder; import com.fastasyncworldedit.core.internal.io.FastByteArrayOutputStream; import com.fastasyncworldedit.core.util.MainUtil; -import com.google.common.collect.HashMultimap; import com.google.common.collect.Multimap; import com.google.common.collect.Multimaps; import com.google.common.io.ByteSource; @@ -51,6 +50,7 @@ import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; +import java.util.LinkedHashSet; import java.util.List; import java.util.Locale; import java.util.Map; @@ -64,7 +64,9 @@ import static com.google.common.base.Preconditions.checkNotNull; public class ClipboardFormats { private static final Map aliasMap = new HashMap<>(); - private static final Multimap fileExtensionMap = HashMultimap.create(); + // FAWE start - keep order of ClipboardFormat entries -> prefer FAST over SPONGE_SCHEMATIC + private static final Multimap fileExtensionMap = Multimaps.newMultimap(new HashMap<>(), LinkedHashSet::new); + // FAWE end private static final List registeredFormats = new ArrayList<>(); public static void registerClipboardFormat(ClipboardFormat format) { diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/regions/RegionIntersection.java b/worldedit-core/src/main/java/com/sk89q/worldedit/regions/RegionIntersection.java index 8298a8b00..7a3e91343 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/regions/RegionIntersection.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/regions/RegionIntersection.java @@ -164,14 +164,23 @@ public class RegionIntersection extends AbstractRegion { int bz = chunk.getZ() << 4; int tx = bx + 15; int tz = bz + 15; + List intersecting = new ArrayList<>(2); for (Region region : regions) { BlockVector3 regMin = region.getMinimumPoint(); BlockVector3 regMax = region.getMaximumPoint(); if (tx >= regMin.getX() && bx <= regMax.getX() && tz >= regMin.getZ() && bz <= regMax.getZ()) { - return region.processSet(chunk, get, set); + intersecting.add(region); } } - return null; + if (intersecting.isEmpty()) { + return null; + } + if (intersecting.size() == 1) { + return intersecting.get(0).processSet(chunk, get, set); + } + // if multiple regions intersect with this chunk, we must be more careful, otherwise one region might trim content of + // another region + return super.processSet(chunk, get, set); } @Override