mirror of
https://github.com/IntellectualSites/PlotSquared.git
synced 2025-06-25 02:04:44 +02:00
begin new block setting/chunk pipeline
This will ultimately replace both the GlobalBlockQueue and the ChunkTask stuff
This commit is contained in:
@ -26,7 +26,7 @@
|
||||
package com.plotsquared.bukkit.queue;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
import com.plotsquared.bukkit.BukkitMain;
|
||||
import com.plotsquared.bukkit.BukkitPlatform;
|
||||
import com.sk89q.worldedit.math.BlockVector2;
|
||||
import io.papermc.lib.PaperLib;
|
||||
import org.bukkit.Chunk;
|
||||
@ -44,34 +44,7 @@ import java.util.concurrent.LinkedBlockingQueue;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
/**
|
||||
* Utility that allows for the loading and coordination of chunk actions
|
||||
* <p>
|
||||
* The coordinator takes in collection of chunk coordinates, loads them
|
||||
* and allows the caller to specify a sink for the loaded chunks. The
|
||||
* coordinator will prevent the chunks from being unloaded until the sink
|
||||
* has fully consumed the chunk
|
||||
* <p>
|
||||
* Usage:
|
||||
* <pre>{@code
|
||||
* final ChunkCoordinator chunkCoordinator = ChunkCoordinator.builder()
|
||||
* .inWorld(Objects.requireNonNull(Bukkit.getWorld("world"))).withChunk(BlockVector2.at(0, 0))
|
||||
* .withConsumer(chunk -> System.out.printf("Got chunk %d;%d", chunk.getX(), chunk.getZ()))
|
||||
* .withFinalAction(() -> System.out.println("All chunks have been loaded"))
|
||||
* .withThrowableConsumer(throwable -> System.err.println("Something went wrong... =("))
|
||||
* .withMaxIterationTime(25L)
|
||||
* .build();
|
||||
* chunkCoordinator.subscribeToProgress((coordinator, progress) ->
|
||||
* System.out.printf("Progress: %.1f", progress * 100.0f));
|
||||
* chunkCoordinator.start();
|
||||
* }</pre>
|
||||
*
|
||||
* @author Alexander Söderberg
|
||||
* @see #builder() To create a new coordinator instance
|
||||
*/
|
||||
public final class ChunkCoordinator extends BukkitRunnable {
|
||||
|
||||
private final List<ProgressSubscriber> progressSubscribers = new LinkedList<>();
|
||||
public final class BukkitChunkCoordinator extends BukkitRunnable {
|
||||
|
||||
private final Queue<BlockVector2> requestedChunks;
|
||||
private final Queue<Chunk> availableChunks;
|
||||
@ -80,48 +53,32 @@ public final class ChunkCoordinator extends BukkitRunnable {
|
||||
private final Consumer<Chunk> chunkConsumer;
|
||||
private final World world;
|
||||
private final Runnable whenDone;
|
||||
private final Consumer<Throwable> throwableConsumer;
|
||||
private final int totalSize;
|
||||
|
||||
private AtomicInteger expectedSize;
|
||||
private int batchSize;
|
||||
|
||||
private ChunkCoordinator(final long maxIterationTime, final int initialBatchSize,
|
||||
private BukkitChunkCoordinator(final long maxIterationTime, final int initialBatchSize,
|
||||
@NotNull final Consumer<Chunk> chunkConsumer, @NotNull final World world,
|
||||
@NotNull final Collection<BlockVector2> requestedChunks, @NotNull final Runnable whenDone,
|
||||
@NotNull final Consumer<Throwable> throwableConsumer) {
|
||||
@NotNull final Collection<BlockVector2> requestedChunks, @NotNull final Runnable whenDone) {
|
||||
this.requestedChunks = new LinkedBlockingQueue<>(requestedChunks);
|
||||
this.availableChunks = new LinkedBlockingQueue<>();
|
||||
this.totalSize = requestedChunks.size();
|
||||
this.expectedSize = new AtomicInteger(this.totalSize);
|
||||
this.expectedSize = new AtomicInteger(requestedChunks.size());
|
||||
this.world = world;
|
||||
this.batchSize = initialBatchSize;
|
||||
this.chunkConsumer = chunkConsumer;
|
||||
this.maxIterationTime = maxIterationTime;
|
||||
this.whenDone = whenDone;
|
||||
this.throwableConsumer = throwableConsumer;
|
||||
this.plugin = JavaPlugin.getPlugin(BukkitMain.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new {@link ChunkCoordinator} instance
|
||||
*
|
||||
* @return Coordinator builder instance
|
||||
*/
|
||||
@NotNull public static ChunkCoordinatorBuilder builder() {
|
||||
return new ChunkCoordinatorBuilder();
|
||||
}
|
||||
|
||||
/**
|
||||
* Start the coordinator instance
|
||||
*/
|
||||
public void start() {
|
||||
this.plugin = JavaPlugin.getPlugin(BukkitPlatform.class);
|
||||
// Request initial batch
|
||||
this.requestBatch();
|
||||
// Wait until next tick to give the chunks a chance to be loaded
|
||||
this.runTaskTimer(this.plugin, 1L, 1L);
|
||||
}
|
||||
|
||||
@NotNull public static ChunkCoordinatorBuilder builder() {
|
||||
return new ChunkCoordinatorBuilder();
|
||||
}
|
||||
|
||||
@Override public void run() {
|
||||
Chunk chunk = this.availableChunks.poll();
|
||||
if (chunk == null) {
|
||||
@ -133,8 +90,7 @@ public final class ChunkCoordinator extends BukkitRunnable {
|
||||
final long start = System.currentTimeMillis();
|
||||
try {
|
||||
this.chunkConsumer.accept(chunk);
|
||||
} catch (final Throwable throwable) {
|
||||
this.throwableConsumer.accept(throwable);
|
||||
} catch (final Exception ignored) {
|
||||
}
|
||||
this.freeChunk(chunk);
|
||||
processedChunks++;
|
||||
@ -147,19 +103,10 @@ public final class ChunkCoordinator extends BukkitRunnable {
|
||||
// Adjust batch size based on the amount of processed chunks per tick
|
||||
this.batchSize = processedChunks;
|
||||
}
|
||||
|
||||
final int expected = this.expectedSize.addAndGet(-processedChunks);
|
||||
|
||||
final float progress = ((float) totalSize - (float) expected) / (float) totalSize;
|
||||
for (final ProgressSubscriber subscriber : this.progressSubscribers) {
|
||||
subscriber.notifyProgress(this, progress);
|
||||
}
|
||||
|
||||
if (expected <= 0) {
|
||||
if (this.expectedSize.addAndGet(-processedChunks) <= 0) {
|
||||
try {
|
||||
this.whenDone.run();
|
||||
} catch (final Throwable throwable) {
|
||||
this.throwableConsumer.accept(throwable);
|
||||
} catch (final Exception ignored) {
|
||||
}
|
||||
this.cancel();
|
||||
} else {
|
||||
@ -203,52 +150,10 @@ public final class ChunkCoordinator extends BukkitRunnable {
|
||||
chunk.removePluginChunkTicket(this.plugin);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the amount of remaining chunks (at the time of the method call)
|
||||
*
|
||||
* @return Snapshot view of remaining chunk count
|
||||
*/
|
||||
public int getRemainingChunks() {
|
||||
return this.expectedSize.get();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the amount of requested chunks
|
||||
*
|
||||
* @return Requested chunk count
|
||||
*/
|
||||
public int getTotalChunks() {
|
||||
return this.totalSize;
|
||||
}
|
||||
|
||||
/**
|
||||
* Subscribe to coordinator progress updates
|
||||
*
|
||||
* @param subscriber Subscriber
|
||||
*/
|
||||
public void subscribeToProgress(@NotNull final ChunkCoordinator.ProgressSubscriber subscriber) {
|
||||
this.progressSubscribers.add(subscriber);
|
||||
}
|
||||
|
||||
|
||||
@FunctionalInterface
|
||||
public interface ProgressSubscriber {
|
||||
|
||||
/**
|
||||
* Notify about a progress update in the coordinator
|
||||
*
|
||||
* @param coordinator Coordinator instance that triggered the notification
|
||||
* @param progress Progress in the range [0, 1]
|
||||
*/
|
||||
void notifyProgress(@NotNull final ChunkCoordinator coordinator, final float progress);
|
||||
|
||||
}
|
||||
|
||||
|
||||
public static final class ChunkCoordinatorBuilder {
|
||||
|
||||
private final List<BlockVector2> requestedChunks = new LinkedList<>();
|
||||
private Consumer<Throwable> throwableConsumer = Throwable::printStackTrace;
|
||||
private World world;
|
||||
private Consumer<Chunk> chunkConsumer;
|
||||
private Runnable whenDone = () -> {
|
||||
@ -303,22 +208,12 @@ public final class ChunkCoordinator extends BukkitRunnable {
|
||||
return this;
|
||||
}
|
||||
|
||||
@NotNull public ChunkCoordinatorBuilder withThrowableConsumer(
|
||||
@NotNull final Consumer<Throwable> throwableConsumer) {
|
||||
this.throwableConsumer =
|
||||
Preconditions.checkNotNull(throwableConsumer, "Throwable consumer may not be null");
|
||||
return this;
|
||||
}
|
||||
|
||||
@NotNull public ChunkCoordinator build() {
|
||||
@NotNull public BukkitChunkCoordinator build() {
|
||||
Preconditions.checkNotNull(this.world, "No world was supplied");
|
||||
Preconditions.checkNotNull(this.chunkConsumer, "No chunk consumer was supplied");
|
||||
Preconditions.checkNotNull(this.whenDone, "No final action was supplied");
|
||||
Preconditions
|
||||
.checkNotNull(this.throwableConsumer, "No throwable consumer was supplied");
|
||||
return new ChunkCoordinator(this.maxIterationTime, this.initialBatchSize,
|
||||
this.chunkConsumer, this.world, this.requestedChunks, this.whenDone,
|
||||
this.throwableConsumer);
|
||||
return new BukkitChunkCoordinator(this.maxIterationTime, this.initialBatchSize,
|
||||
this.chunkConsumer, this.world, this.requestedChunks, this.whenDone);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,52 @@
|
||||
/*
|
||||
* _____ _ _ _____ _
|
||||
* | __ \| | | | / ____| | |
|
||||
* | |__) | | ___ | |_| (___ __ _ _ _ __ _ _ __ ___ __| |
|
||||
* | ___/| |/ _ \| __|\___ \ / _` | | | |/ _` | '__/ _ \/ _` |
|
||||
* | | | | (_) | |_ ____) | (_| | |_| | (_| | | | __/ (_| |
|
||||
* |_| |_|\___/ \__|_____/ \__, |\__,_|\__,_|_| \___|\__,_|
|
||||
* | |
|
||||
* |_|
|
||||
* PlotSquared plot management system for Minecraft
|
||||
* Copyright (C) 2020 IntellectualSites
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.plotsquared.bukkit.queue;
|
||||
|
||||
import com.plotsquared.bukkit.util.BukkitBlockUtil;
|
||||
import com.plotsquared.core.queue.BasicQueueCoordinator;
|
||||
import com.plotsquared.core.util.BlockUtil;
|
||||
import com.sk89q.worldedit.world.block.BlockState;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.block.Block;
|
||||
|
||||
public class BukkitQueueCoordinator extends BasicQueueCoordinator {
|
||||
|
||||
public BukkitQueueCoordinator(String world) {
|
||||
super(world);
|
||||
}
|
||||
|
||||
@Override public BlockState getBlock(int x, int y, int z) {
|
||||
World worldObj = Bukkit.getWorld(getWorld());
|
||||
if (worldObj != null) {
|
||||
Block block = worldObj.getBlockAt(x, y, z);
|
||||
return BukkitBlockUtil.get(block);
|
||||
} else {
|
||||
return BlockUtil.get(0, 0);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user