2020-07-19 17:02:30 +02:00
|
|
|
/*
|
|
|
|
* _____ _ _ _____ _
|
|
|
|
* | __ \| | | | / ____| | |
|
|
|
|
* | |__) | | ___ | |_| (___ __ _ _ _ __ _ _ __ ___ __| |
|
|
|
|
* | ___/| |/ _ \| __|\___ \ / _` | | | |/ _` | '__/ _ \/ _` |
|
|
|
|
* | | | | (_) | |_ ____) | (_| | |_| | (_| | | | __/ (_| |
|
|
|
|
* |_| |_|\___/ \__|_____/ \__, |\__,_|\__,_|_| \___|\__,_|
|
|
|
|
* | |
|
|
|
|
* |_|
|
|
|
|
* PlotSquared plot management system for Minecraft
|
2021-01-01 00:19:45 +01:00
|
|
|
* Copyright (C) 2021 IntellectualSites
|
2020-07-19 17:02:30 +02:00
|
|
|
*
|
|
|
|
* 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
|
2020-08-15 14:59:29 +02:00
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2020-07-19 17:02:30 +02:00
|
|
|
*/
|
2020-07-18 13:07:56 +02:00
|
|
|
package com.plotsquared.core.queue;
|
|
|
|
|
|
|
|
import com.google.common.base.Preconditions;
|
|
|
|
import com.google.inject.Inject;
|
2020-09-11 13:59:40 +02:00
|
|
|
import com.plotsquared.core.configuration.Settings;
|
2020-07-18 13:07:56 +02:00
|
|
|
import com.plotsquared.core.inject.factory.ChunkCoordinatorFactory;
|
|
|
|
import com.plotsquared.core.location.Location;
|
2020-08-17 17:21:11 +02:00
|
|
|
import com.plotsquared.core.queue.subscriber.ProgressSubscriber;
|
2020-07-18 13:07:56 +02:00
|
|
|
import com.sk89q.worldedit.math.BlockVector2;
|
|
|
|
import com.sk89q.worldedit.world.World;
|
2021-01-09 22:28:41 +01:00
|
|
|
import org.checkerframework.checker.nullness.qual.NonNull;
|
|
|
|
import org.checkerframework.checker.nullness.qual.Nullable;
|
2020-07-18 13:07:56 +02:00
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.function.Consumer;
|
|
|
|
|
2020-07-24 18:00:08 +02:00
|
|
|
/**
|
|
|
|
* Builds a {@link ChunkCoordinator} instance
|
|
|
|
*/
|
2020-07-18 13:07:56 +02:00
|
|
|
public class ChunkCoordinatorBuilder {
|
|
|
|
|
|
|
|
private final List<BlockVector2> requestedChunks = new LinkedList<>();
|
2020-08-17 17:21:11 +02:00
|
|
|
private final List<ProgressSubscriber> progressSubscribers = new ArrayList<>();
|
2020-07-19 14:12:27 +02:00
|
|
|
private final ChunkCoordinatorFactory chunkCoordinatorFactory;
|
2020-07-18 13:07:56 +02:00
|
|
|
private Consumer<Throwable> throwableConsumer = Throwable::printStackTrace;
|
|
|
|
private World world;
|
|
|
|
private Consumer<BlockVector2> chunkConsumer;
|
|
|
|
private Runnable whenDone = () -> {
|
|
|
|
};
|
2020-09-11 13:59:40 +02:00
|
|
|
private long maxIterationTime = Settings.QUEUE.MAX_ITERATION_TIME; // A little over 1 tick;
|
|
|
|
private int initialBatchSize = Settings.QUEUE.INITIAL_BATCH_SIZE;
|
2020-07-23 18:30:23 +02:00
|
|
|
private boolean unloadAfter = true;
|
2020-07-18 13:07:56 +02:00
|
|
|
|
2021-01-09 22:28:41 +01:00
|
|
|
@Inject
|
|
|
|
public ChunkCoordinatorBuilder(@NonNull ChunkCoordinatorFactory chunkCoordinatorFactory) {
|
2020-07-18 13:07:56 +02:00
|
|
|
this.chunkCoordinatorFactory = chunkCoordinatorFactory;
|
|
|
|
}
|
|
|
|
|
2020-07-24 18:00:08 +02:00
|
|
|
/**
|
|
|
|
* Set the world
|
2020-08-16 14:22:49 +02:00
|
|
|
*
|
|
|
|
* @param world world
|
|
|
|
* @return this ChunkCoordinatorBuilder instance
|
2020-07-24 18:00:08 +02:00
|
|
|
*/
|
2021-01-09 22:28:41 +01:00
|
|
|
public @NonNull ChunkCoordinatorBuilder inWorld(final @NonNull World world) {
|
2020-07-18 13:07:56 +02:00
|
|
|
this.world = Preconditions.checkNotNull(world, "World may not be null");
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2020-07-24 18:00:08 +02:00
|
|
|
/**
|
|
|
|
* Add a chunk to be accessed
|
2020-08-16 14:22:49 +02:00
|
|
|
*
|
|
|
|
* @param chunkLocation BlockVector2 of chunk to add
|
|
|
|
* @return this ChunkCoordinatorBuilder instance
|
2020-07-24 18:00:08 +02:00
|
|
|
*/
|
2021-01-09 22:28:41 +01:00
|
|
|
public @NonNull ChunkCoordinatorBuilder withChunk(final @NonNull BlockVector2 chunkLocation) {
|
2020-07-24 17:24:53 +02:00
|
|
|
this.requestedChunks.add(Preconditions.checkNotNull(chunkLocation, "Chunk location may not be null"));
|
2020-07-18 13:07:56 +02:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2020-07-24 18:00:08 +02:00
|
|
|
/**
|
|
|
|
* Add a Collection of chunks to be accessed
|
2020-08-16 14:22:49 +02:00
|
|
|
*
|
|
|
|
* @param chunkLocations Collection of BlockVector2 to add
|
|
|
|
* @return this ChunkCoordinatorBuilder instance
|
2020-07-24 18:00:08 +02:00
|
|
|
*/
|
2021-01-09 22:28:41 +01:00
|
|
|
public @NonNull ChunkCoordinatorBuilder withChunks(final @NonNull Collection<BlockVector2> chunkLocations) {
|
2020-07-18 13:07:56 +02:00
|
|
|
chunkLocations.forEach(this::withChunk);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2020-07-24 18:00:08 +02:00
|
|
|
/**
|
|
|
|
* Add chunks within a region to be accessed
|
2020-08-16 14:22:49 +02:00
|
|
|
*
|
|
|
|
* @param pos1 minimum region location
|
|
|
|
* @param pos2 maximum region location
|
|
|
|
* @return this ChunkCoordinatorBuilder instance
|
2020-07-24 18:00:08 +02:00
|
|
|
*/
|
2021-01-09 22:28:41 +01:00
|
|
|
public @NonNull ChunkCoordinatorBuilder withRegion(@NonNull Location pos1, @NonNull Location pos2) {
|
2020-07-18 13:07:56 +02:00
|
|
|
final int p1x = pos1.getX();
|
|
|
|
final int p1z = pos1.getZ();
|
|
|
|
final int p2x = pos2.getX();
|
|
|
|
final int p2z = pos2.getZ();
|
|
|
|
final int bcx = p1x >> 4;
|
|
|
|
final int bcz = p1z >> 4;
|
|
|
|
final int tcx = p2x >> 4;
|
|
|
|
final int tcz = p2z >> 4;
|
|
|
|
final ArrayList<BlockVector2> chunks = new ArrayList<>();
|
|
|
|
|
|
|
|
for (int x = bcx; x <= tcx; x++) {
|
|
|
|
for (int z = bcz; z <= tcz; z++) {
|
|
|
|
chunks.add(BlockVector2.at(x, z));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
chunks.forEach(this::withChunk);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2020-07-24 18:00:08 +02:00
|
|
|
/**
|
|
|
|
* Set the consumer to be used when a chunk is loaded
|
2020-08-16 14:22:49 +02:00
|
|
|
*
|
|
|
|
* @param chunkConsumer Consumer to be used by the ChunkCoordinator
|
|
|
|
* @return this ChunkCoordinatorBuilder instance
|
2020-07-24 18:00:08 +02:00
|
|
|
*/
|
2021-01-09 22:28:41 +01:00
|
|
|
public @NonNull ChunkCoordinatorBuilder withConsumer(final @NonNull Consumer<BlockVector2> chunkConsumer) {
|
2020-07-24 17:24:53 +02:00
|
|
|
this.chunkConsumer = Preconditions.checkNotNull(chunkConsumer, "Chunk consumer may not be null");
|
2020-07-18 13:07:56 +02:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2020-07-24 18:00:08 +02:00
|
|
|
/**
|
|
|
|
* Set the Runnable to run when all chunks have been accessed
|
2020-08-16 14:22:49 +02:00
|
|
|
*
|
|
|
|
* @param whenDone task to run when all chunks are accessed
|
|
|
|
* @return this ChunkCoordinatorBuilder instance
|
2020-07-24 18:00:08 +02:00
|
|
|
*/
|
2021-01-09 22:28:41 +01:00
|
|
|
public @NonNull ChunkCoordinatorBuilder withFinalAction(final @Nullable Runnable whenDone) {
|
2020-07-24 15:36:50 +02:00
|
|
|
if (whenDone == null) {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
this.whenDone = whenDone;
|
2020-07-18 13:07:56 +02:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2020-07-24 18:00:08 +02:00
|
|
|
/**
|
|
|
|
* Set the max time taken while iterating over and accessing loaded chunks
|
2020-08-16 14:22:49 +02:00
|
|
|
*
|
|
|
|
* @param maxIterationTime max iteration time
|
|
|
|
* @return this ChunkCoordinatorBuilder instance
|
2020-07-24 18:00:08 +02:00
|
|
|
*/
|
2021-01-09 22:28:41 +01:00
|
|
|
public @NonNull ChunkCoordinatorBuilder withMaxIterationTime(final long maxIterationTime) {
|
2020-07-18 13:07:56 +02:00
|
|
|
Preconditions.checkArgument(maxIterationTime > 0, "Max iteration time must be positive");
|
|
|
|
this.maxIterationTime = maxIterationTime;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2020-07-24 18:00:08 +02:00
|
|
|
/**
|
|
|
|
* Set the initial batch size to be used for loading chunks
|
2020-08-16 14:22:49 +02:00
|
|
|
*
|
|
|
|
* @param initialBatchSize initial batch size
|
|
|
|
* @return this ChunkCoordinatorBuilder instance
|
2020-07-24 18:00:08 +02:00
|
|
|
*/
|
2021-01-09 22:28:41 +01:00
|
|
|
public @NonNull ChunkCoordinatorBuilder withInitialBatchSize(final int initialBatchSize) {
|
2020-07-18 13:07:56 +02:00
|
|
|
Preconditions.checkArgument(initialBatchSize > 0, "Initial batch size must be positive");
|
|
|
|
this.initialBatchSize = initialBatchSize;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2020-07-24 18:00:08 +02:00
|
|
|
/**
|
|
|
|
* Set the consumer to be used to handle {@link Throwable}s
|
2020-08-16 14:22:49 +02:00
|
|
|
*
|
|
|
|
* @param throwableConsumer consumer to hanble throwables
|
|
|
|
* @return this ChunkCoordinatorBuilder instance
|
2020-07-24 18:00:08 +02:00
|
|
|
*/
|
2021-01-09 22:28:41 +01:00
|
|
|
public @NonNull ChunkCoordinatorBuilder withThrowableConsumer(final @NonNull Consumer<Throwable> throwableConsumer) {
|
2020-07-24 17:24:53 +02:00
|
|
|
this.throwableConsumer = Preconditions.checkNotNull(throwableConsumer, "Throwable consumer may not be null");
|
2020-07-18 13:07:56 +02:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2020-07-24 18:00:08 +02:00
|
|
|
/**
|
|
|
|
* Set whether the chunks should be allow to unload after being accessed. This should only be used where the chunks are read from
|
2020-08-16 14:22:49 +02:00
|
|
|
* and then written to from a separate queue where they're consequently unloaded.
|
|
|
|
*
|
|
|
|
* @param unloadAfter if to unload chuns afterwards
|
|
|
|
* @return this ChunkCoordinatorBuilder instance
|
2020-07-24 18:00:08 +02:00
|
|
|
*/
|
2021-01-09 22:28:41 +01:00
|
|
|
public @NonNull ChunkCoordinatorBuilder unloadAfter(final boolean unloadAfter) {
|
2020-07-23 18:30:23 +02:00
|
|
|
this.unloadAfter = unloadAfter;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2021-01-09 22:28:41 +01:00
|
|
|
public @NonNull ChunkCoordinatorBuilder withProgressSubscriber(ProgressSubscriber progressSubscriber) {
|
2020-08-17 17:21:11 +02:00
|
|
|
this.progressSubscribers.add(progressSubscriber);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2021-01-09 22:28:41 +01:00
|
|
|
public @NonNull ChunkCoordinatorBuilder withProgressSubscribers(Collection<ProgressSubscriber> progressSubscribers) {
|
2020-08-17 17:21:11 +02:00
|
|
|
this.progressSubscribers.addAll(progressSubscribers);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2020-07-24 18:00:08 +02:00
|
|
|
/**
|
|
|
|
* Create a new {@link ChunkCoordinator} instance based on the values in the Builder instance.
|
2020-08-16 14:22:49 +02:00
|
|
|
*
|
|
|
|
* @return a new ChunkCoordinator
|
2020-07-24 18:00:08 +02:00
|
|
|
*/
|
2021-01-09 22:28:41 +01:00
|
|
|
public @NonNull ChunkCoordinator build() {
|
2020-07-18 13:07:56 +02:00
|
|
|
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");
|
2020-07-19 14:12:27 +02:00
|
|
|
return chunkCoordinatorFactory
|
2021-01-09 22:28:41 +01:00
|
|
|
.create(this.maxIterationTime,
|
|
|
|
this.initialBatchSize,
|
|
|
|
this.chunkConsumer,
|
|
|
|
this.world,
|
|
|
|
this.requestedChunks,
|
|
|
|
this.whenDone,
|
|
|
|
this.throwableConsumer,
|
|
|
|
this.unloadAfter,
|
|
|
|
this.progressSubscribers
|
|
|
|
);
|
2020-07-18 13:07:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|