use of com.plotsquared.core.queue.QueueCoordinator in project PlotSquared by IntellectualSites.
the class PlotModificationManager method autoMerge.
/**
* Auto merge a plot in a specific direction.
*
* @param dir the direction to merge
* @param max the max number of merges to do
* @param uuid the UUID it is allowed to merge with
* @param actor The actor executing the task
* @param removeRoads whether to remove roads
* @return {@code true} if a merge takes place, else {@code false}
*/
public boolean autoMerge(@NonNull final Direction dir, int max, @NonNull final UUID uuid, @Nullable PlotPlayer<?> actor, final boolean removeRoads) {
// Ignore merging if there is no owner for the plot
if (!this.plot.hasOwner()) {
return false;
}
Set<Plot> connected = this.plot.getConnectedPlots();
HashSet<PlotId> merged = connected.stream().map(Plot::getId).collect(Collectors.toCollection(HashSet::new));
ArrayDeque<Plot> frontier = new ArrayDeque<>(connected);
Plot current;
boolean toReturn = false;
HashSet<Plot> visited = new HashSet<>();
QueueCoordinator queue = this.plot.getArea().getQueue();
while ((current = frontier.poll()) != null && max >= 0) {
if (visited.contains(current)) {
continue;
}
visited.add(current);
Set<Plot> plots;
if ((dir == Direction.ALL || dir == Direction.NORTH) && !current.isMerged(Direction.NORTH)) {
Plot other = current.getRelative(Direction.NORTH);
if (other != null && other.isOwner(uuid) && (other.getBasePlot(false).equals(current.getBasePlot(false)) || (plots = other.getConnectedPlots()).size() <= max && frontier.addAll(plots) && (max -= plots.size()) != -1)) {
current.mergePlot(other, removeRoads, queue);
merged.add(current.getId());
merged.add(other.getId());
toReturn = true;
if (removeRoads) {
ArrayList<PlotId> ids = new ArrayList<>();
ids.add(current.getId());
ids.add(other.getId());
this.plot.getManager().finishPlotMerge(ids, queue);
}
}
}
if (max >= 0 && (dir == Direction.ALL || dir == Direction.EAST) && !current.isMerged(Direction.EAST)) {
Plot other = current.getRelative(Direction.EAST);
if (other != null && other.isOwner(uuid) && (other.getBasePlot(false).equals(current.getBasePlot(false)) || (plots = other.getConnectedPlots()).size() <= max && frontier.addAll(plots) && (max -= plots.size()) != -1)) {
current.mergePlot(other, removeRoads, queue);
merged.add(current.getId());
merged.add(other.getId());
toReturn = true;
if (removeRoads) {
ArrayList<PlotId> ids = new ArrayList<>();
ids.add(current.getId());
ids.add(other.getId());
this.plot.getManager().finishPlotMerge(ids, queue);
}
}
}
if (max >= 0 && (dir == Direction.ALL || dir == Direction.SOUTH) && !current.isMerged(Direction.SOUTH)) {
Plot other = current.getRelative(Direction.SOUTH);
if (other != null && other.isOwner(uuid) && (other.getBasePlot(false).equals(current.getBasePlot(false)) || (plots = other.getConnectedPlots()).size() <= max && frontier.addAll(plots) && (max -= plots.size()) != -1)) {
current.mergePlot(other, removeRoads, queue);
merged.add(current.getId());
merged.add(other.getId());
toReturn = true;
if (removeRoads) {
ArrayList<PlotId> ids = new ArrayList<>();
ids.add(current.getId());
ids.add(other.getId());
this.plot.getManager().finishPlotMerge(ids, queue);
}
}
}
if (max >= 0 && (dir == Direction.ALL || dir == Direction.WEST) && !current.isMerged(Direction.WEST)) {
Plot other = current.getRelative(Direction.WEST);
if (other != null && other.isOwner(uuid) && (other.getBasePlot(false).equals(current.getBasePlot(false)) || (plots = other.getConnectedPlots()).size() <= max && frontier.addAll(plots) && (max -= plots.size()) != -1)) {
current.mergePlot(other, removeRoads, queue);
merged.add(current.getId());
merged.add(other.getId());
toReturn = true;
if (removeRoads) {
ArrayList<PlotId> ids = new ArrayList<>();
ids.add(current.getId());
ids.add(other.getId());
this.plot.getManager().finishPlotMerge(ids, queue);
}
}
}
}
if (actor != null && Settings.QUEUE.NOTIFY_PROGRESS) {
queue.addProgressSubscriber(subscriberFactory.createWithActor(actor));
}
if (queue.size() > 0) {
queue.enqueue();
}
return toReturn;
}
use of com.plotsquared.core.queue.QueueCoordinator in project PlotSquared by IntellectualSites.
the class PlotModificationManager method removeSign.
/**
* Remove the plot sign if it is set.
*/
public void removeSign() {
PlotManager manager = this.plot.getArea().getPlotManager();
if (!this.plot.getArea().allowSigns()) {
return;
}
Location location = manager.getSignLoc(this.plot);
QueueCoordinator queue = PlotSquared.platform().globalBlockQueue().getNewQueue(PlotSquared.platform().worldUtil().getWeWorld(this.plot.getWorldName()));
queue.setBlock(location.getX(), location.getY(), location.getZ(), BlockTypes.AIR.getDefaultState());
queue.enqueue();
}
use of com.plotsquared.core.queue.QueueCoordinator in project PlotSquared by IntellectualSites.
the class AugmentedUtils method generate.
public static boolean generate(@Nullable Object chunkObject, @NonNull final String world, final int chunkX, final int chunkZ, QueueCoordinator queue) {
if (!enabled) {
return false;
}
// The coordinates of the block on the
// least positive corner of the chunk
final int blockX = chunkX << 4;
final int blockZ = chunkZ << 4;
// Create a region that contains the
// entire chunk
CuboidRegion region = RegionUtil.createRegion(blockX, blockX + 15, 0, 0, blockZ, blockZ + 15);
// Query for plot areas in the chunk
final Set<PlotArea> areas = PlotSquared.get().getPlotAreaManager().getPlotAreasSet(world, region);
if (areas.isEmpty()) {
return false;
}
boolean enqueue = false;
boolean generationResult = false;
for (final PlotArea area : areas) {
// and so there's no reason to continue searching
if (area.getType() == PlotAreaType.NORMAL) {
return false;
}
// so we do not interfere
if (area.getTerrain() == PlotAreaTerrainType.ALL || !area.contains(blockX, blockZ)) {
continue;
}
IndependentPlotGenerator generator = area.getGenerator();
// Mask
if (queue == null) {
enqueue = true;
queue = PlotSquared.platform().globalBlockQueue().getNewQueue(PlotSquared.platform().worldUtil().getWeWorld(world));
if (chunkObject != null) {
queue.setChunkObject(chunkObject);
}
}
QueueCoordinator primaryMask;
// coordinates
int relativeBottomX;
int relativeBottomZ;
int relativeTopX;
int relativeTopZ;
// Generation
if (area.getType() == PlotAreaType.PARTIAL) {
relativeBottomX = Math.max(0, area.getRegion().getMinimumPoint().getX() - blockX);
relativeBottomZ = Math.max(0, area.getRegion().getMinimumPoint().getZ() - blockZ);
relativeTopX = Math.min(15, area.getRegion().getMaximumPoint().getX() - blockX);
relativeTopZ = Math.min(15, area.getRegion().getMaximumPoint().getZ() - blockZ);
primaryMask = new AreaBoundDelegateQueueCoordinator(area, queue);
} else {
relativeBottomX = relativeBottomZ = 0;
relativeTopX = relativeTopZ = 15;
primaryMask = queue;
}
QueueCoordinator secondaryMask;
BlockState air = BlockTypes.AIR.getDefaultState();
int startYOffset = !(area instanceof ClassicPlotWorld) || ((ClassicPlotWorld) area).PLOT_BEDROCK ? 1 : 0;
if (area.getTerrain() == PlotAreaTerrainType.ROAD) {
PlotManager manager = area.getPlotManager();
final boolean[][] canPlace = new boolean[16][16];
boolean has = false;
for (int x = relativeBottomX; x <= relativeTopX; x++) {
for (int z = relativeBottomZ; z <= relativeTopZ; z++) {
int worldX = x + blockX;
int worldZ = z + blockZ;
boolean can = manager.getPlotId(worldX, 0, worldZ) == null;
if (can) {
for (int y = area.getMinGenHeight() + startYOffset; y <= area.getMaxGenHeight(); y++) {
queue.setBlock(worldX, y, worldZ, air);
}
canPlace[x][z] = true;
has = true;
}
}
}
if (!has) {
continue;
}
generationResult = true;
secondaryMask = new LocationOffsetDelegateQueueCoordinator(canPlace, blockX, blockZ, primaryMask);
} else {
secondaryMask = primaryMask;
for (int x = relativeBottomX; x <= relativeTopX; x++) {
for (int z = relativeBottomZ; z <= relativeTopZ; z++) {
for (int y = area.getMinGenHeight() + startYOffset; y <= area.getMaxGenHeight(); y++) {
queue.setBlock(blockX + x, y, blockZ + z, air);
}
}
}
generationResult = true;
}
if (chunkObject != null) {
primaryMask.setChunkObject(chunkObject);
}
if (chunkObject != null) {
secondaryMask.setChunkObject(chunkObject);
}
ScopedQueueCoordinator scoped = new ScopedQueueCoordinator(secondaryMask, Location.at(world, blockX, area.getMinGenHeight(), blockZ), Location.at(world, blockX + 15, area.getMaxGenHeight(), blockZ + 15));
generator.generateChunk(scoped, area);
generator.populateChunk(scoped, area);
scoped.setForceSync(true);
scoped.enqueue();
}
if (enqueue) {
queue.enqueue();
}
return generationResult;
}
use of com.plotsquared.core.queue.QueueCoordinator in project PlotSquared by IntellectualSites.
the class DebugRoadRegen method regenPlot.
public boolean regenPlot(PlotPlayer<?> player) {
Location location = player.getLocation();
PlotArea area = location.getPlotArea();
if (area == null) {
player.sendMessage(TranslatableCaption.of("errors.not_in_plot_world"));
}
Plot plot = player.getCurrentPlot();
if (plot == null) {
player.sendMessage(TranslatableCaption.of("errors.not_in_plot"));
} else if (plot.isMerged()) {
player.sendMessage(TranslatableCaption.of("debug.requires_unmerged"));
} else {
PlotManager manager = area.getPlotManager();
QueueCoordinator queue = area.getQueue();
queue.setCompleteTask(() -> {
player.sendMessage(TranslatableCaption.of("debugroadregen.regen_done"), Template.of("value", plot.getId().toString()));
player.sendMessage(TranslatableCaption.of("debugroadregen.regen_all"), Template.of("value", "/plot regenallroads"));
});
manager.createRoadEast(plot, queue);
manager.createRoadSouth(plot, queue);
manager.createRoadSouthEast(plot, queue);
queue.enqueue();
}
return true;
}
use of com.plotsquared.core.queue.QueueCoordinator in project PlotSquared by IntellectualSites.
the class SchematicHandler method paste.
/**
* Paste a schematic.
*
* @param schematic the schematic object to paste
* @param plot plot to paste in
* @param xOffset offset x to paste it from plot origin
* @param yOffset offset y to paste it from plot origin
* @param zOffset offset z to paste it from plot origin
* @param autoHeight if to automatically choose height to paste from
* @param actor the actor pasting the schematic
* @param whenDone task to run when schematic is pasted
*/
public void paste(final Schematic schematic, final Plot plot, final int xOffset, final int yOffset, final int zOffset, final boolean autoHeight, final PlotPlayer<?> actor, final RunnableVal<Boolean> whenDone) {
if (whenDone != null) {
whenDone.value = false;
}
if (schematic == null) {
TaskManager.runTask(whenDone);
return;
}
try {
BlockVector3 dimension = schematic.getClipboard().getDimensions();
final int WIDTH = dimension.getX();
final int LENGTH = dimension.getZ();
final int HEIGHT = dimension.getY();
final int worldHeight = plot.getArea().getMaxGenHeight() - plot.getArea().getMinGenHeight() + 1;
// Validate dimensions
CuboidRegion region = plot.getLargestRegion();
boolean sizeMismatch = ((region.getMaximumPoint().getX() - region.getMinimumPoint().getX() + xOffset + 1) < WIDTH) || ((region.getMaximumPoint().getZ() - region.getMinimumPoint().getZ() + zOffset + 1) < LENGTH) || (HEIGHT > worldHeight);
if (!Settings.Schematics.PASTE_MISMATCHES && sizeMismatch) {
actor.sendMessage(TranslatableCaption.of("schematics.schematic_size_mismatch"));
TaskManager.runTask(whenDone);
return;
}
// block type and data arrays
final Clipboard blockArrayClipboard = schematic.getClipboard();
// Calculate the optimal height to paste the schematic at
final int y_offset_actual;
if (autoHeight) {
if (HEIGHT >= worldHeight) {
y_offset_actual = yOffset;
} else {
PlotArea pw = plot.getArea();
if (pw instanceof ClassicPlotWorld) {
y_offset_actual = yOffset + pw.getMinBuildHeight() + ((ClassicPlotWorld) pw).PLOT_HEIGHT;
} else {
y_offset_actual = yOffset + pw.getMinBuildHeight() + this.worldUtil.getHighestBlockSynchronous(plot.getWorldName(), region.getMinimumPoint().getX() + 1, region.getMinimumPoint().getZ() + 1);
}
}
} else {
y_offset_actual = yOffset;
}
final int p1x;
final int p1z;
final int p2x;
final int p2z;
final Region allRegion;
if (!sizeMismatch || plot.getRegions().size() == 1) {
p1x = region.getMinimumPoint().getX() + xOffset;
p1z = region.getMinimumPoint().getZ() + zOffset;
p2x = region.getMaximumPoint().getX() + xOffset;
p2z = region.getMaximumPoint().getZ() + zOffset;
allRegion = region;
} else {
Location[] corners = plot.getCorners();
p1x = corners[0].getX() + xOffset;
p1z = corners[0].getZ() + zOffset;
p2x = corners[1].getX() + xOffset;
p2z = corners[1].getZ() + zOffset;
allRegion = new RegionIntersection(null, plot.getRegions().toArray(new CuboidRegion[] {}));
}
// Paste schematic here
final QueueCoordinator queue = plot.getArea().getQueue();
for (int ry = 0; ry < Math.min(worldHeight, HEIGHT); ry++) {
int yy = y_offset_actual + ry;
if (yy > plot.getArea().getMaxGenHeight() || yy < plot.getArea().getMinGenHeight()) {
continue;
}
for (int rz = 0; rz < blockArrayClipboard.getDimensions().getZ(); rz++) {
for (int rx = 0; rx < blockArrayClipboard.getDimensions().getX(); rx++) {
int xx = p1x + rx;
int zz = p1z + rz;
if (sizeMismatch && (xx < p1x || xx > p2x || zz < p1z || zz > p2z || !allRegion.contains(BlockVector3.at(xx, ry, zz)))) {
continue;
}
BlockVector3 loc = BlockVector3.at(rx, ry, rz);
BaseBlock id = blockArrayClipboard.getFullBlock(loc);
queue.setBlock(xx, yy, zz, id);
BiomeType biome = blockArrayClipboard.getBiome(loc);
queue.setBiome(xx, yy, zz, biome);
}
}
}
if (actor != null && Settings.QUEUE.NOTIFY_PROGRESS) {
queue.addProgressSubscriber(subscriberFactory.createWithActor(actor));
}
if (whenDone != null) {
whenDone.value = true;
queue.setCompleteTask(whenDone);
}
queue.enqueue();
} catch (Exception e) {
e.printStackTrace();
TaskManager.runTask(whenDone);
}
}
Aggregations