001/* 002 * PlotSquared, a land and world management plugin for Minecraft. 003 * Copyright (C) IntellectualSites <https://intellectualsites.com> 004 * Copyright (C) IntellectualSites team and contributors 005 * 006 * This program is free software: you can redistribute it and/or modify 007 * it under the terms of the GNU General Public License as published by 008 * the Free Software Foundation, either version 3 of the License, or 009 * (at your option) any later version. 010 * 011 * This program is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 014 * GNU General Public License for more details. 015 * 016 * You should have received a copy of the GNU General Public License 017 * along with this program. If not, see <https://www.gnu.org/licenses/>. 018 */ 019package com.plotsquared.core.generator; 020 021import com.google.inject.Inject; 022import com.plotsquared.core.configuration.Settings; 023import com.plotsquared.core.events.PlotFlagAddEvent; 024import com.plotsquared.core.events.Result; 025import com.plotsquared.core.listener.WEExtent; 026import com.plotsquared.core.location.Location; 027import com.plotsquared.core.plot.Plot; 028import com.plotsquared.core.plot.PlotArea; 029import com.plotsquared.core.plot.PlotAreaType; 030import com.plotsquared.core.plot.PlotId; 031import com.plotsquared.core.plot.PlotManager; 032import com.plotsquared.core.plot.expiration.PlotAnalysis; 033import com.plotsquared.core.plot.flag.GlobalFlagContainer; 034import com.plotsquared.core.plot.flag.PlotFlag; 035import com.plotsquared.core.plot.flag.implementations.AnalysisFlag; 036import com.plotsquared.core.plot.world.PlotAreaManager; 037import com.plotsquared.core.queue.BlockArrayCacheScopedQueueCoordinator; 038import com.plotsquared.core.queue.GlobalBlockQueue; 039import com.plotsquared.core.queue.QueueCoordinator; 040import com.plotsquared.core.util.ChunkManager; 041import com.plotsquared.core.util.EventDispatcher; 042import com.plotsquared.core.util.MathMan; 043import com.plotsquared.core.util.RegionManager; 044import com.plotsquared.core.util.RegionUtil; 045import com.plotsquared.core.util.SchematicHandler; 046import com.plotsquared.core.util.WorldUtil; 047import com.plotsquared.core.util.task.RunnableVal; 048import com.plotsquared.core.util.task.TaskManager; 049import com.plotsquared.core.util.task.TaskTime; 050import com.sk89q.worldedit.math.BlockVector2; 051import com.sk89q.worldedit.math.BlockVector3; 052import com.sk89q.worldedit.regions.CuboidRegion; 053import com.sk89q.worldedit.world.biome.BiomeType; 054import com.sk89q.worldedit.world.block.BaseBlock; 055import com.sk89q.worldedit.world.block.BlockState; 056import com.sk89q.worldedit.world.block.BlockType; 057import com.sk89q.worldedit.world.block.BlockTypes; 058import org.apache.logging.log4j.LogManager; 059import org.apache.logging.log4j.Logger; 060import org.checkerframework.checker.nullness.qual.NonNull; 061import org.checkerframework.checker.nullness.qual.Nullable; 062 063import java.io.File; 064import java.util.ArrayDeque; 065import java.util.ArrayList; 066import java.util.Arrays; 067import java.util.Collections; 068import java.util.HashSet; 069import java.util.Iterator; 070import java.util.LinkedHashSet; 071import java.util.List; 072import java.util.Set; 073import java.util.concurrent.atomic.AtomicBoolean; 074import java.util.concurrent.atomic.AtomicInteger; 075 076public class HybridUtils { 077 078 private static final Logger LOGGER = LogManager.getLogger("PlotSquared/" + HybridUtils.class.getSimpleName()); 079 private static final BlockState AIR = BlockTypes.AIR.getDefaultState(); 080 081 public static HybridUtils manager; 082 public static Set<BlockVector2> regions; 083 public static int height; 084 // Use ordered for reasonable chunk loading order to reduce paper unloading neighbour chunks and then us attempting to load 085 // them again, causing errors 086 public static Set<BlockVector2> chunks = new LinkedHashSet<>(); 087 public static PlotArea area; 088 public static boolean UPDATE = false; 089 090 private final PlotAreaManager plotAreaManager; 091 private final ChunkManager chunkManager; 092 private final GlobalBlockQueue blockQueue; 093 private final WorldUtil worldUtil; 094 private final SchematicHandler schematicHandler; 095 private final EventDispatcher eventDispatcher; 096 097 @Inject 098 public HybridUtils( 099 final @NonNull PlotAreaManager plotAreaManager, 100 final @NonNull ChunkManager chunkManager, 101 final @NonNull GlobalBlockQueue blockQueue, 102 final @NonNull WorldUtil worldUtil, 103 final @NonNull SchematicHandler schematicHandler, 104 final @NonNull EventDispatcher eventDispatcher 105 ) { 106 this.plotAreaManager = plotAreaManager; 107 this.chunkManager = chunkManager; 108 this.blockQueue = blockQueue; 109 this.worldUtil = worldUtil; 110 this.schematicHandler = schematicHandler; 111 this.eventDispatcher = eventDispatcher; 112 } 113 114 public void regeneratePlotWalls(final PlotArea area) { 115 PlotManager plotManager = area.getPlotManager(); 116 plotManager.regenerateAllPlotWalls(null); 117 } 118 119 public void analyzeRegion(final String world, final CuboidRegion region, final RunnableVal<PlotAnalysis> whenDone) { 120 // int diff, int variety, int vertices, int rotation, int height_sd 121 /* 122 * diff: compare to base by looping through all blocks 123 * variety: add to HashSet for each BlockState 124 * height_sd: loop over all blocks and get top block 125 * 126 * vertices: store air map and compare with neighbours 127 * for each block check the adjacent 128 * - Store all blocks then go through in second loop 129 * - recheck each block 130 * 131 */ 132 TaskManager.runTaskAsync(() -> { 133 final PlotArea area = this.plotAreaManager.getPlotArea(world, null); 134 if (!(area instanceof HybridPlotWorld hpw)) { 135 return; 136 } 137 138 final BlockVector3 bot = region.getMinimumPoint(); 139 final BlockVector3 top = region.getMaximumPoint(); 140 141 final int bx = bot.getX(); 142 final int bz = bot.getZ(); 143 final int tx = top.getX(); 144 final int tz = top.getZ(); 145 final int cbx = bx >> 4; 146 final int cbz = bz >> 4; 147 final int ctx = tx >> 4; 148 final int ctz = tz >> 4; 149 final int width = tx - bx + 1; 150 final int length = tz - bz + 1; 151 final int height = area.getMaxGenHeight() - area.getMinGenHeight() + 1; 152 final int minHeight = area.getMinGenHeight(); 153 154 final BlockState[][][] newBlocks = new BlockState[height][width][length]; 155 156 BlockArrayCacheScopedQueueCoordinator oldBlockQueue = new BlockArrayCacheScopedQueueCoordinator( 157 Location.at("", region.getMinimumPoint().withY(hpw.getMinGenHeight())), 158 Location.at("", region.getMaximumPoint().withY(hpw.getMaxGenHeight())) 159 ); 160 161 region.getChunks().forEach(chunkPos -> { 162 int relChunkX = chunkPos.getX() - cbx; 163 int relChunkZ = chunkPos.getZ() - cbz; 164 oldBlockQueue.setOffsetX(relChunkX << 4); 165 oldBlockQueue.setOffsetZ(relChunkZ << 4); 166 hpw.getGenerator().generateChunk(oldBlockQueue, hpw); 167 }); 168 169 final BlockState[][][] oldBlocks = oldBlockQueue.getBlockStates(); 170 171 QueueCoordinator queue = area.getQueue(); 172 queue.addReadChunks(region.getChunks()); 173 queue.setChunkConsumer(chunkPos -> { 174 int X = chunkPos.getX(); 175 int Z = chunkPos.getZ(); 176 int minX; 177 if (X == cbx) { 178 minX = bx & 15; 179 } else { 180 minX = 0; 181 } 182 int minZ; 183 if (Z == cbz) { 184 minZ = bz & 15; 185 } else { 186 minZ = 0; 187 } 188 int maxX; 189 if (X == ctx) { 190 maxX = tx & 15; 191 } else { 192 maxX = 15; 193 } 194 int maxZ; 195 if (Z == ctz) { 196 maxZ = tz & 15; 197 } else { 198 maxZ = 15; 199 } 200 201 int chunkBlockX = X << 4; 202 int chunkBlockZ = Z << 4; 203 204 int xb = chunkBlockX - bx; 205 int zb = chunkBlockZ - bz; 206 for (int x = minX; x <= maxX; x++) { 207 int xx = chunkBlockX + x; 208 for (int z = minZ; z <= maxZ; z++) { 209 int zz = chunkBlockZ + z; 210 for (int yIndex = 0; yIndex < height; yIndex++) { 211 int y = yIndex + minHeight; 212 BlockState block = queue.getBlock(xx, y, zz); 213 if (block == null) { 214 block = AIR; 215 } 216 int xr = xb + x; 217 int zr = zb + z; 218 newBlocks[yIndex][xr][zr] = block; 219 } 220 } 221 } 222 }); 223 224 final Runnable run = () -> { 225 int size = width * length; 226 int[] changes = new int[size]; 227 int[] faces = new int[size]; 228 int[] data = new int[size]; 229 int[] air = new int[size]; 230 int[] variety = new int[size]; 231 int i = 0; 232 for (int x = 0; x < width; x++) { 233 for (int z = 0; z < length; z++) { 234 Set<BlockType> types = new HashSet<>(); 235 for (int yIndex = 0; yIndex < height; yIndex++) { 236 BlockState old = oldBlocks[yIndex][x][z]; // Nullable 237 BlockState now = newBlocks[yIndex][x][z]; // Not null 238 if (now == null) { 239 throw new NullPointerException(String.format( 240 "\"now\" block null attempting to perform plot analysis. Indexes: x=%d of %d, yIndex=%d" + 241 " of %d, z=%d of %d", 242 x, 243 width, 244 yIndex, 245 height, 246 z, 247 length 248 )); 249 } 250 if (!now.equals(old) && !(old == null && now.getBlockType().equals(BlockTypes.AIR))) { 251 changes[i]++; 252 } 253 if (now.getBlockType().getMaterial().isAir()) { 254 air[i]++; 255 } else { 256 // check vertices 257 // modifications_adjacent 258 if (x > 0 && z > 0 && yIndex > 0 && x < width - 1 && z < length - 1 && yIndex < (height - 1)) { 259 if (newBlocks[yIndex - 1][x][z].getBlockType().getMaterial().isAir()) { 260 faces[i]++; 261 } 262 if (newBlocks[yIndex][x - 1][z].getBlockType().getMaterial().isAir()) { 263 faces[i]++; 264 } 265 if (newBlocks[yIndex][x][z - 1].getBlockType().getMaterial().isAir()) { 266 faces[i]++; 267 } 268 if (newBlocks[yIndex + 1][x][z].getBlockType().getMaterial().isAir()) { 269 faces[i]++; 270 } 271 if (newBlocks[yIndex][x + 1][z].getBlockType().getMaterial().isAir()) { 272 faces[i]++; 273 } 274 if (newBlocks[yIndex][x][z + 1].getBlockType().getMaterial().isAir()) { 275 faces[i]++; 276 } 277 } 278 279 if (!now.equals(now.getBlockType().getDefaultState())) { 280 data[i]++; 281 } 282 types.add(now.getBlockType()); 283 } 284 } 285 variety[i] = types.size(); 286 i++; 287 } 288 } 289 // analyze plot 290 // put in analysis obj 291 292 // run whenDone 293 PlotAnalysis analysis = new PlotAnalysis(); 294 analysis.changes = (int) (MathMan.getMean(changes) * 100); 295 analysis.faces = (int) (MathMan.getMean(faces) * 100); 296 analysis.data = (int) (MathMan.getMean(data) * 100); 297 analysis.air = (int) (MathMan.getMean(air) * 100); 298 analysis.variety = (int) (MathMan.getMean(variety) * 100); 299 300 analysis.changes_sd = (int) (MathMan.getSD(changes, analysis.changes) * 100); 301 analysis.faces_sd = (int) (MathMan.getSD(faces, analysis.faces) * 100); 302 analysis.data_sd = (int) (MathMan.getSD(data, analysis.data) * 100); 303 analysis.air_sd = (int) (MathMan.getSD(air, analysis.air) * 100); 304 analysis.variety_sd = (int) (MathMan.getSD(variety, analysis.variety) * 100); 305 whenDone.value = analysis; 306 whenDone.run(); 307 }; 308 queue.setCompleteTask(run); 309 queue.enqueue(); 310 }); 311 } 312 313 public void analyzePlot(final Plot origin, final RunnableVal<PlotAnalysis> whenDone) { 314 final ArrayDeque<CuboidRegion> zones = new ArrayDeque<>(origin.getRegions()); 315 final ArrayList<PlotAnalysis> analysis = new ArrayList<>(); 316 Runnable run = new Runnable() { 317 @Override 318 public void run() { 319 if (zones.isEmpty()) { 320 if (!analysis.isEmpty()) { 321 whenDone.value = new PlotAnalysis(); 322 for (PlotAnalysis data : analysis) { 323 whenDone.value.air += data.air; 324 whenDone.value.air_sd += data.air_sd; 325 whenDone.value.changes += data.changes; 326 whenDone.value.changes_sd += data.changes_sd; 327 whenDone.value.data += data.data; 328 whenDone.value.data_sd += data.data_sd; 329 whenDone.value.faces += data.faces; 330 whenDone.value.faces_sd += data.faces_sd; 331 whenDone.value.variety += data.variety; 332 whenDone.value.variety_sd += data.variety_sd; 333 } 334 whenDone.value.air /= analysis.size(); 335 whenDone.value.air_sd /= analysis.size(); 336 whenDone.value.changes /= analysis.size(); 337 whenDone.value.changes_sd /= analysis.size(); 338 whenDone.value.data /= analysis.size(); 339 whenDone.value.data_sd /= analysis.size(); 340 whenDone.value.faces /= analysis.size(); 341 whenDone.value.faces_sd /= analysis.size(); 342 whenDone.value.variety /= analysis.size(); 343 whenDone.value.variety_sd /= analysis.size(); 344 } else { 345 whenDone.value = analysis.get(0); 346 } 347 List<Integer> result = new ArrayList<>(); 348 result.add(whenDone.value.changes); 349 result.add(whenDone.value.faces); 350 result.add(whenDone.value.data); 351 result.add(whenDone.value.air); 352 result.add(whenDone.value.variety); 353 354 result.add(whenDone.value.changes_sd); 355 result.add(whenDone.value.faces_sd); 356 result.add(whenDone.value.data_sd); 357 result.add(whenDone.value.air_sd); 358 result.add(whenDone.value.variety_sd); 359 PlotFlag<?, ?> plotFlag = GlobalFlagContainer.getInstance().getFlag(AnalysisFlag.class).createFlagInstance( 360 result); 361 PlotFlagAddEvent event = eventDispatcher.callFlagAdd(plotFlag, origin); 362 if (event.getEventResult() == Result.DENY) { 363 return; 364 } 365 origin.setFlag(event.getFlag()); 366 TaskManager.runTask(whenDone); 367 return; 368 } 369 CuboidRegion region = zones.poll(); 370 final Runnable task = this; 371 analyzeRegion(origin.getWorldName(), region, new RunnableVal<>() { 372 @Override 373 public void run(PlotAnalysis value) { 374 analysis.add(value); 375 TaskManager.runTaskLater(task, TaskTime.ticks(1L)); 376 } 377 }); 378 } 379 }; 380 run.run(); 381 } 382 383 public int checkModified(QueueCoordinator queue, int x1, int x2, int y1, int y2, int z1, int z2, BlockState[] blocks) { 384 int count = 0; 385 for (int y = y1; y <= y2; y++) { 386 for (int x = x1; x <= x2; x++) { 387 for (int z = z1; z <= z2; z++) { 388 BlockState block = queue.getBlock(x, y, z); 389 boolean same = Arrays.stream(blocks).anyMatch(p -> this.worldUtil.isBlockSame(block, p)); 390 if (!same) { 391 count++; 392 } 393 } 394 } 395 } 396 return count; 397 } 398 399 public final ArrayList<BlockVector2> getChunks(BlockVector2 region) { 400 ArrayList<BlockVector2> chunks = new ArrayList<>(); 401 int sx = region.getX() << 5; 402 int sz = region.getZ() << 5; 403 for (int x = sx; x < sx + 32; x++) { 404 for (int z = sz; z < sz + 32; z++) { 405 chunks.add(BlockVector2.at(x, z)); 406 } 407 } 408 return chunks; 409 } 410 411 public boolean scheduleRoadUpdate(PlotArea area, int extend) { 412 if (HybridUtils.UPDATE) { 413 return false; 414 } 415 HybridUtils.UPDATE = true; 416 Set<BlockVector2> regions = this.worldUtil.getChunkChunks(area.getWorldName()); 417 return scheduleRoadUpdate(area, regions, extend, new LinkedHashSet<>()); 418 } 419 420 public boolean scheduleSingleRegionRoadUpdate(Plot plot, int extend) { 421 if (HybridUtils.UPDATE) { 422 return false; 423 } 424 HybridUtils.UPDATE = true; 425 Set<BlockVector2> regions = new HashSet<>(); 426 regions.add(RegionManager.getRegion(plot.getCenterSynchronous())); 427 return scheduleRoadUpdate(plot.getArea(), regions, extend, new LinkedHashSet<>()); 428 } 429 430 public boolean scheduleRoadUpdate( 431 final PlotArea area, 432 Set<BlockVector2> regions, 433 final int extend, 434 Set<BlockVector2> chunks 435 ) { 436 HybridUtils.regions = regions; 437 HybridUtils.area = area; 438 HybridUtils.height = extend; 439 HybridUtils.chunks = chunks; 440 final int initial = 1024 * regions.size() + chunks.size(); 441 final AtomicInteger count = new AtomicInteger(0); 442 TaskManager.runTask(new Runnable() { 443 @Override 444 public void run() { 445 if (!UPDATE) { 446 Iterator<BlockVector2> iter = chunks.iterator(); 447 QueueCoordinator queue = blockQueue.getNewQueue(worldUtil.getWeWorld(area.getWorldName())); 448 while (iter.hasNext()) { 449 BlockVector2 chunk = iter.next(); 450 iter.remove(); 451 boolean regenedRoad = regenerateRoad(area, chunk, extend, queue); 452 if (!regenedRoad) { 453 LOGGER.info("Failed to regenerate roads in chunk {}", chunk); 454 } 455 } 456 queue.enqueue(); 457 LOGGER.info("Cancelled road task"); 458 return; 459 } 460 count.incrementAndGet(); 461 if (count.intValue() % 10 == 0) { 462 LOGGER.info("Progress: {}%", 100 * (initial - (chunks.size() + 1024 * regions.size())) / initial); 463 } 464 if (HybridUtils.regions.isEmpty() && chunks.isEmpty()) { 465 regeneratePlotWalls(area); 466 467 HybridUtils.UPDATE = false; 468 LOGGER.info("Finished road conversion"); 469 // CANCEL TASK 470 } else { 471 final Runnable task = this; 472 TaskManager.runTaskAsync(() -> { 473 try { 474 if (chunks.size() < 64) { 475 if (!HybridUtils.regions.isEmpty()) { 476 Iterator<BlockVector2> iterator = HybridUtils.regions.iterator(); 477 BlockVector2 loc = iterator.next(); 478 iterator.remove(); 479 LOGGER.info("Updating .mcr: {}, {} (approx 1024 chunks)", loc.getX(), loc.getZ()); 480 LOGGER.info("- Remaining: {}", HybridUtils.regions.size()); 481 chunks.addAll(getChunks(loc)); 482 System.gc(); 483 } 484 } 485 if (!chunks.isEmpty()) { 486 TaskManager.getPlatformImplementation().sync(() -> { 487 Iterator<BlockVector2> iterator = chunks.iterator(); 488 if (chunks.size() >= 32) { 489 QueueCoordinator queue = blockQueue.getNewQueue(worldUtil.getWeWorld(area.getWorldName())); 490 for (int i = 0; i < 32; i++) { 491 final BlockVector2 chunk = iterator.next(); 492 iterator.remove(); 493 boolean regenedRoads = regenerateRoad(area, chunk, extend, queue); 494 if (!regenedRoads) { 495 LOGGER.info("Failed to regenerate the road in chunk {}", chunk); 496 } 497 } 498 queue.setCompleteTask(task); 499 queue.enqueue(); 500 return null; 501 } 502 QueueCoordinator queue = blockQueue.getNewQueue(worldUtil.getWeWorld(area.getWorldName())); 503 while (!chunks.isEmpty()) { 504 final BlockVector2 chunk = iterator.next(); 505 iterator.remove(); 506 boolean regenedRoads = regenerateRoad(area, chunk, extend, queue); 507 if (!regenedRoads) { 508 LOGGER.info("Failed to regenerate road in chunk {}", chunk); 509 } 510 } 511 queue.setCompleteTask(task); 512 queue.enqueue(); 513 return null; 514 }); 515 return; 516 } 517 } catch (Exception e) { 518 e.printStackTrace(); 519 Iterator<BlockVector2> iterator = HybridUtils.regions.iterator(); 520 BlockVector2 loc = iterator.next(); 521 iterator.remove(); 522 LOGGER.error( 523 "Error! Could not update '{}/region/r.{}.{}.mca' (Corrupt chunk?)", 524 area.getWorldHash(), 525 loc.getX(), 526 loc.getZ() 527 ); 528 } 529 TaskManager.runTaskLater(task, TaskTime.seconds(1L)); 530 }); 531 } 532 } 533 }); 534 return true; 535 } 536 537 public boolean setupRoadSchematic(Plot plot) { 538 final String world = plot.getWorldName(); 539 final QueueCoordinator queue = blockQueue.getNewQueue(worldUtil.getWeWorld(world)); 540 Location bot = plot.getBottomAbs().subtract(1, 0, 1); 541 Location top = plot.getTopAbs(); 542 final HybridPlotWorld plotworld = (HybridPlotWorld) plot.getArea(); 543 // Do not use plotworld#schematicStartHeight() here as we want to restore the pre 6.1.4 way of doing it if 544 // USE_WALL_IN_ROAD_SCHEM_HEIGHT is false 545 int schemY = Settings.Schematics.USE_WALL_IN_ROAD_SCHEM_HEIGHT ? 546 Math.min(plotworld.PLOT_HEIGHT, Math.min(plotworld.WALL_HEIGHT, plotworld.ROAD_HEIGHT)) : plotworld.ROAD_HEIGHT; 547 int sx = bot.getX() - plotworld.ROAD_WIDTH + 1; 548 int sz = bot.getZ() + 1; 549 int sy = Settings.Schematics.PASTE_ROAD_ON_TOP ? schemY : plot.getArea().getMinBuildHeight(); 550 int ex = bot.getX(); 551 int ez = top.getZ(); 552 int ey = get_ey(plotworld, queue, sx, ex, sz, ez, sy); 553 int bz = sz - plotworld.ROAD_WIDTH; 554 int tz = sz - 1; 555 int ty = get_ey(plotworld, queue, sx, ex, bz, tz, sy); 556 557 final Set<CuboidRegion> sideRoad = Collections.singleton(RegionUtil.createRegion(sx, ex, sy, ey, sz, ez)); 558 final Set<CuboidRegion> intersection = Collections.singleton(RegionUtil.createRegion(sx, ex, sy, ty, bz, tz)); 559 560 final String dir = Settings.Paths.SCHEMATICS + File.separator + "GEN_ROAD_SCHEMATIC" + File.separator + plot 561 .getArea() 562 .toString() + File.separator; 563 564 this.schematicHandler.getCompoundTag(world, sideRoad) 565 .whenComplete((compoundTag, throwable) -> { 566 schematicHandler.save(compoundTag, dir + "sideroad.schem"); 567 schematicHandler.getCompoundTag(world, intersection) 568 .whenComplete((c, t) -> { 569 schematicHandler.save(c, dir + "intersection.schem"); 570 plotworld.ROAD_SCHEMATIC_ENABLED = true; 571 try { 572 plotworld.setupSchematics(); 573 } catch (SchematicHandler.UnsupportedFormatException e) { 574 e.printStackTrace(); 575 } 576 }); 577 }); 578 return true; 579 } 580 581 private int get_ey(final HybridPlotWorld hpw, QueueCoordinator queue, int sx, int ex, int sz, int ez, int sy) { 582 int ey = sy; 583 for (int x = sx; x <= ex; x++) { 584 for (int z = sz; z <= ez; z++) { 585 for (int y = sy; y <= hpw.getMaxGenHeight(); y++) { 586 if (y > ey) { 587 BlockState block = queue.getBlock(x, y, z); 588 if (!block.getBlockType().getMaterial().isAir()) { 589 ey = y; 590 } 591 } 592 } 593 } 594 } 595 return ey; 596 } 597 598 /** 599 * Regenerate the road in a chunk in a plot area. 600 * 601 * @param area Plot area to regenerate road for 602 * @param chunk Chunk location to regenerate 603 * @param extend How far to extend setting air above the road 604 * @return if successful 605 * @deprecated use {@link HybridUtils#regenerateRoad(PlotArea, BlockVector2, int, QueueCoordinator)} 606 */ 607 @Deprecated(forRemoval = true, since = "6.6.0") 608 public boolean regenerateRoad(final PlotArea area, final BlockVector2 chunk, int extend) { 609 return regenerateRoad(area, chunk, extend, null); 610 } 611 612 /** 613 * Regenerate the road in a chunk in a plot area. 614 * 615 * @param area Plot area to regenerate road for 616 * @param chunk Chunk location to regenerate 617 * @param extend How far to extend setting air above the road 618 * @param queueCoordinator {@link QueueCoordinator} to use to set the blocks. Null if one should be created and enqueued 619 * @return if successful 620 * @since 6.6.0 621 */ 622 public boolean regenerateRoad( 623 final PlotArea area, 624 final BlockVector2 chunk, 625 int extend, 626 @Nullable QueueCoordinator queueCoordinator 627 ) { 628 int x = chunk.getX() << 4; 629 int z = chunk.getZ() << 4; 630 int ex = x + 15; 631 int ez = z + 15; 632 HybridPlotWorld plotWorld = (HybridPlotWorld) area; 633 if (!plotWorld.ROAD_SCHEMATIC_ENABLED) { 634 return false; 635 } 636 AtomicBoolean toCheck = new AtomicBoolean(false); 637 if (plotWorld.getType() == PlotAreaType.PARTIAL) { 638 boolean chunk1 = area.contains(x, z); 639 boolean chunk2 = area.contains(ex, ez); 640 if (!chunk1 && !chunk2) { 641 return false; 642 } else { 643 toCheck.set(chunk1 ^ chunk2); 644 } 645 } 646 PlotManager manager = area.getPlotManager(); 647 PlotId id1 = manager.getPlotId(x, 0, z); 648 PlotId id2 = manager.getPlotId(ex, 0, ez); 649 x = x - plotWorld.ROAD_OFFSET_X; 650 z -= plotWorld.ROAD_OFFSET_Z; 651 final int finalX = x; 652 final int finalZ = z; 653 final boolean enqueue; 654 final QueueCoordinator queue; 655 if (queueCoordinator == null) { 656 queue = this.blockQueue.getNewQueue(worldUtil.getWeWorld(plotWorld.getWorldName())); 657 enqueue = true; 658 } else { 659 queue = queueCoordinator; 660 enqueue = false; 661 } 662 if (id1 == null || id2 == null || id1 != id2) { 663 if (id1 != null) { 664 Plot p1 = area.getPlotAbs(id1); 665 if (p1 != null && p1.hasOwner() && p1.isMerged()) { 666 toCheck.set(true); 667 } 668 } 669 if (id2 != null && !toCheck.get()) { 670 Plot p2 = area.getPlotAbs(id2); 671 if (p2 != null && p2.hasOwner() && p2.isMerged()) { 672 toCheck.set(true); 673 } 674 } 675 short size = plotWorld.SIZE; 676 for (int X = 0; X < 16; X++) { 677 short absX = (short) ((finalX + X) % size); 678 for (int Z = 0; Z < 16; Z++) { 679 short absZ = (short) ((finalZ + Z) % size); 680 if (absX < 0) { 681 absX += size; 682 } 683 if (absZ < 0) { 684 absZ += size; 685 } 686 boolean condition; 687 if (toCheck.get()) { 688 condition = manager.getPlotId( 689 finalX + X + plotWorld.ROAD_OFFSET_X, 690 1, 691 finalZ + Z + plotWorld.ROAD_OFFSET_Z 692 ) == null; 693 } else { 694 boolean gx = absX > plotWorld.PATH_WIDTH_LOWER; 695 boolean gz = absZ > plotWorld.PATH_WIDTH_LOWER; 696 boolean lx = absX < plotWorld.PATH_WIDTH_UPPER; 697 boolean lz = absZ < plotWorld.PATH_WIDTH_UPPER; 698 condition = !gx || !gz || !lx || !lz; 699 } 700 if (condition) { 701 BaseBlock[] blocks = plotWorld.G_SCH.get(MathMan.pair(absX, absZ)); 702 int minY = Settings.Schematics.PASTE_ROAD_ON_TOP ? plotWorld.SCHEM_Y : area.getMinGenHeight() + 1; 703 int maxDy = Math.max(extend, blocks.length); 704 for (int dy = 0; dy < maxDy; dy++) { 705 if (dy > blocks.length - 1) { 706 queue.setBlock( 707 finalX + X + plotWorld.ROAD_OFFSET_X, 708 minY + dy, 709 finalZ + Z + plotWorld.ROAD_OFFSET_Z, 710 WEExtent.AIRBASE 711 ); 712 } else { 713 BaseBlock block = blocks[dy]; 714 if (block != null) { 715 queue.setBlock( 716 finalX + X + plotWorld.ROAD_OFFSET_X, 717 minY + dy, 718 finalZ + Z + plotWorld.ROAD_OFFSET_Z, 719 block 720 ); 721 } else { 722 queue.setBlock( 723 finalX + X + plotWorld.ROAD_OFFSET_X, 724 minY + dy, 725 finalZ + Z + plotWorld.ROAD_OFFSET_Z, 726 WEExtent.AIRBASE 727 ); 728 } 729 } 730 } 731 BiomeType biome = plotWorld.G_SCH_B.get(MathMan.pair(absX, absZ)); 732 if (biome != null) { 733 queue.setBiome(finalX + X + plotWorld.ROAD_OFFSET_X, finalZ + Z + plotWorld.ROAD_OFFSET_Z, biome); 734 } else { 735 queue.setBiome( 736 finalX + X + plotWorld.ROAD_OFFSET_X, 737 finalZ + Z + plotWorld.ROAD_OFFSET_Z, 738 plotWorld.getPlotBiome() 739 ); 740 } 741 } 742 } 743 } 744 if (enqueue) { 745 queue.enqueue(); 746 } 747 return true; 748 } 749 return false; 750 } 751 752}