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.google.inject.assistedinject.Assisted; 023import com.intellectualsites.annotations.DoNotUse; 024import com.plotsquared.core.PlotSquared; 025import com.plotsquared.core.configuration.ConfigurationSection; 026import com.plotsquared.core.configuration.Settings; 027import com.plotsquared.core.configuration.file.YamlConfiguration; 028import com.plotsquared.core.inject.annotations.WorldConfig; 029import com.plotsquared.core.inject.factory.ProgressSubscriberFactory; 030import com.plotsquared.core.location.Location; 031import com.plotsquared.core.plot.Plot; 032import com.plotsquared.core.plot.PlotArea; 033import com.plotsquared.core.plot.PlotId; 034import com.plotsquared.core.plot.PlotManager; 035import com.plotsquared.core.plot.schematic.Schematic; 036import com.plotsquared.core.queue.GlobalBlockQueue; 037import com.plotsquared.core.util.FileUtils; 038import com.plotsquared.core.util.MathMan; 039import com.plotsquared.core.util.SchematicHandler; 040import com.sk89q.jnbt.CompoundTag; 041import com.sk89q.jnbt.CompoundTagBuilder; 042import com.sk89q.worldedit.entity.Entity; 043import com.sk89q.worldedit.extent.clipboard.Clipboard; 044import com.sk89q.worldedit.extent.transform.BlockTransformExtent; 045import com.sk89q.worldedit.internal.helper.MCDirections; 046import com.sk89q.worldedit.math.BlockVector2; 047import com.sk89q.worldedit.math.BlockVector3; 048import com.sk89q.worldedit.math.Vector3; 049import com.sk89q.worldedit.math.transform.AffineTransform; 050import com.sk89q.worldedit.util.Direction; 051import com.sk89q.worldedit.world.biome.BiomeType; 052import com.sk89q.worldedit.world.block.BaseBlock; 053import org.apache.logging.log4j.LogManager; 054import org.apache.logging.log4j.Logger; 055import org.checkerframework.checker.nullness.qual.NonNull; 056import org.checkerframework.checker.nullness.qual.Nullable; 057 058import java.io.File; 059import java.lang.reflect.Field; 060import java.util.ArrayList; 061import java.util.HashMap; 062import java.util.List; 063import java.util.Locale; 064 065public class HybridPlotWorld extends ClassicPlotWorld { 066 067 private static final Logger LOGGER = LogManager.getLogger("PlotSquared/" + HybridPlotWorld.class.getSimpleName()); 068 private static final AffineTransform transform = new AffineTransform().rotateY(90); 069 public boolean ROAD_SCHEMATIC_ENABLED; 070 public boolean PLOT_SCHEMATIC = false; 071 public short PATH_WIDTH_LOWER; 072 public short PATH_WIDTH_UPPER; 073 public HashMap<Integer, BaseBlock[]> G_SCH; 074 public HashMap<Integer, BiomeType> G_SCH_B; 075 /** 076 * The Y level at which schematic generation will start, lowest of either road or plot schematic generation. 077 */ 078 public int SCHEM_Y; 079 080 private int plotY; 081 private int roadY; 082 private Location SIGN_LOCATION; 083 private File root = null; 084 private int lastOverlayHeightError = Integer.MIN_VALUE; 085 private List<Entity> schem3Entities = null; 086 private BlockVector3 schem3MinPoint = null; 087 private boolean schem1PopulationNeeded = false; 088 private boolean schem2PopulationNeeded = false; 089 private boolean schem3PopulationNeeded = false; 090 091 @Inject 092 private SchematicHandler schematicHandler; 093 094 @Inject 095 public HybridPlotWorld( 096 @Assisted("world") final String worldName, 097 @javax.annotation.Nullable @Assisted("id") final String id, 098 @Assisted final @NonNull IndependentPlotGenerator generator, 099 @javax.annotation.Nullable @Assisted("min") final PlotId min, 100 @javax.annotation.Nullable @Assisted("max") final PlotId max, 101 @WorldConfig final @NonNull YamlConfiguration worldConfiguration, 102 final @NonNull GlobalBlockQueue blockQueue 103 ) { 104 super(worldName, id, generator, min, max, worldConfiguration, blockQueue); 105 PlotSquared.platform().injector().injectMembers(this); 106 } 107 108 public static BaseBlock rotate(BaseBlock id) { 109 110 CompoundTag tag = id.getNbtData(); 111 112 if (tag != null) { 113 // Handle blocks which store their rotation in NBT 114 if (tag.containsKey("Rot")) { 115 int rot = tag.asInt("Rot"); 116 117 Direction direction = MCDirections.fromRotation(rot); 118 119 if (direction != null) { 120 Vector3 vector = transform.apply(direction.toVector()).subtract(transform.apply(Vector3.ZERO)).normalize(); 121 Direction newDirection = 122 Direction.findClosest( 123 vector, 124 Direction.Flag.CARDINAL | Direction.Flag.ORDINAL | Direction.Flag.SECONDARY_ORDINAL 125 ); 126 127 if (newDirection != null) { 128 CompoundTagBuilder builder = tag.createBuilder(); 129 130 builder.putByte("Rot", (byte) MCDirections.toRotation(newDirection)); 131 132 id.setNbtData(builder.build()); 133 } 134 } 135 } 136 } 137 return BlockTransformExtent.transform(id, transform); 138 } 139 140 @NonNull 141 @Override 142 protected PlotManager createManager() { 143 return new HybridPlotManager(this, PlotSquared.platform().regionManager(), 144 PlotSquared.platform().injector().getInstance(ProgressSubscriberFactory.class) 145 ); 146 } 147 148 public Location getSignLocation(@NonNull Plot plot) { 149 plot = plot.getBasePlot(false); 150 final Location bot = plot.getBottomAbs(); 151 if (SIGN_LOCATION == null) { 152 return bot.withY(ROAD_HEIGHT + 1).add(-1, 0, -2); 153 } else { 154 return bot.withY(0).add(SIGN_LOCATION.getX(), SIGN_LOCATION.getY(), SIGN_LOCATION.getZ()); 155 } 156 } 157 158 /** 159 * <p>This method is called when a world loads. Make sure you set all your constants here. You are provided with the 160 * configuration section for that specific world.</p> 161 */ 162 @Override 163 public void loadConfiguration(ConfigurationSection config) { 164 super.loadConfiguration(config); 165 if ((this.ROAD_WIDTH & 1) == 0) { 166 this.PATH_WIDTH_LOWER = (short) (Math.floor(this.ROAD_WIDTH / 2f) - 1); 167 } else { 168 this.PATH_WIDTH_LOWER = (short) Math.floor(this.ROAD_WIDTH / 2f); 169 } 170 if (this.ROAD_WIDTH == 0) { 171 this.PATH_WIDTH_UPPER = (short) (this.SIZE + 1); 172 } else { 173 this.PATH_WIDTH_UPPER = (short) (this.PATH_WIDTH_LOWER + this.PLOT_WIDTH + 1); 174 } 175 try { 176 setupSchematics(); 177 } catch (Exception event) { 178 event.printStackTrace(); 179 } 180 181 // Dump world settings 182 if (Settings.DEBUG) { 183 LOGGER.info("- Dumping settings for ClassicPlotWorld with name {}", this.getWorldName()); 184 final Field[] fields = this.getClass().getFields(); 185 for (final Field field : fields) { 186 final String name = field.getName().toLowerCase(Locale.ENGLISH); 187 if (name.contains("g_sch")) { 188 continue; 189 } 190 Object value; 191 try { 192 final boolean accessible = field.isAccessible(); 193 field.setAccessible(true); 194 value = field.get(this); 195 field.setAccessible(accessible); 196 } catch (final IllegalAccessException e) { 197 value = String.format("Failed to parse: %s", e.getMessage()); 198 } 199 LOGGER.info("-- {} = {}", name, value); 200 } 201 } 202 } 203 204 @Override 205 public boolean isCompatible(final @NonNull PlotArea plotArea) { 206 if (!(plotArea instanceof SquarePlotWorld)) { 207 return false; 208 } 209 return ((SquarePlotWorld) plotArea).PLOT_WIDTH == this.PLOT_WIDTH; 210 } 211 212 public void setupSchematics() throws SchematicHandler.UnsupportedFormatException { 213 this.G_SCH = new HashMap<>(); 214 this.G_SCH_B = new HashMap<>(); 215 216 // Try to determine root. This means that plot areas can have separate schematic 217 // directories 218 if (!(root = 219 FileUtils.getFile( 220 PlotSquared.platform().getDirectory(), 221 "schematics/GEN_ROAD_SCHEMATIC/" + this.getWorldName() + "/" + this.getId() 222 )) 223 .exists()) { 224 root = FileUtils.getFile( 225 PlotSquared.platform().getDirectory(), 226 "schematics/GEN_ROAD_SCHEMATIC/" + this.getWorldName() 227 ); 228 } 229 230 File schematic1File = new File(root, "sideroad.schem"); 231 if (!schematic1File.exists()) { 232 schematic1File = new File(root, "sideroad.schematic"); 233 } 234 File schematic2File = new File(root, "intersection.schem"); 235 if (!schematic2File.exists()) { 236 schematic2File = new File(root, "intersection.schematic"); 237 } 238 File schematic3File = new File(root, "plot.schem"); 239 if (!schematic3File.exists()) { 240 schematic3File = new File(root, "plot.schematic"); 241 } 242 Schematic schematic1 = this.schematicHandler.getSchematic(schematic1File); 243 Schematic schematic2 = this.schematicHandler.getSchematic(schematic2File); 244 Schematic schematic3 = this.schematicHandler.getSchematic(schematic3File); 245 246 // If the plot schematic contains entities, then they need to be populated upon generation. 247 if (schematic3 != null && !schematic3.getClipboard().getEntities().isEmpty()) { 248 this.schem3Entities = new ArrayList<>(schematic3.getClipboard().getEntities()); 249 this.schem3MinPoint = schematic3.getClipboard().getMinimumPoint(); 250 this.schem3PopulationNeeded = true; 251 } 252 253 int shift = this.ROAD_WIDTH / 2; 254 int oddshift = (this.ROAD_WIDTH & 1); 255 256 SCHEM_Y = schematicStartHeight(); 257 258 // plotY and roadY are important to allow plot and/or road schematic "overflow" into each other 259 // without causing AIOOB exceptions when attempting either to set blocks to, or get block from G_SCH 260 // Default plot schematic start height, normalized to the minimum height schematics are pasted from. 261 plotY = PLOT_HEIGHT - SCHEM_Y; 262 int minRoadWall = Settings.Schematics.USE_WALL_IN_ROAD_SCHEM_HEIGHT ? Math.min(ROAD_HEIGHT, WALL_HEIGHT) : ROAD_HEIGHT; 263 // Default road schematic start height, normalized to the minimum height schematics are pasted from. 264 roadY = minRoadWall - SCHEM_Y; 265 266 int worldGenHeight = getMaxGenHeight() - getMinGenHeight() + 1; 267 268 int plotSchemHeight = 0; 269 270 // SCHEM_Y should be normalised to the plot "start" height 271 if (schematic3 != null) { 272 plotSchemHeight = schematic3.getClipboard().getDimensions().getY(); 273 if (plotSchemHeight == worldGenHeight) { 274 SCHEM_Y = getMinGenHeight(); 275 plotY = 0; 276 } else if (!Settings.Schematics.PASTE_ON_TOP) { 277 SCHEM_Y = getMinGenHeight(); 278 plotY = 0; 279 } 280 } 281 282 int roadSchemHeight = 0; 283 284 if (schematic1 != null) { 285 roadSchemHeight = Math.max( 286 schematic1.getClipboard().getDimensions().getY(), 287 schematic2.getClipboard().getDimensions().getY() 288 ); 289 if (roadSchemHeight == worldGenHeight) { 290 SCHEM_Y = getMinGenHeight(); 291 roadY = 0; // Road is the lowest schematic 292 if (schematic3 != null && schematic3.getClipboard().getDimensions().getY() != worldGenHeight) { 293 // Road is the lowest schematic. Normalize plotY to it. 294 if (Settings.Schematics.PASTE_ON_TOP) { 295 plotY = PLOT_HEIGHT - getMinGenHeight(); 296 } 297 } 298 } else if (!Settings.Schematics.PASTE_ROAD_ON_TOP) { 299 roadY = 0; 300 SCHEM_Y = getMinGenHeight(); 301 if (schematic3 != null) { 302 if (Settings.Schematics.PASTE_ON_TOP) { 303 // Road is the lowest schematic. Normalize plotY to it. 304 plotY = PLOT_HEIGHT - SCHEM_Y; 305 } 306 } 307 } else { 308 roadY = minRoadWall - SCHEM_Y; 309 } 310 } 311 int maxSchematicHeight = Math.max(plotY + plotSchemHeight, roadY + roadSchemHeight); 312 313 if (schematic3 != null) { 314 this.PLOT_SCHEMATIC = true; 315 Clipboard blockArrayClipboard3 = schematic3.getClipboard(); 316 317 BlockVector3 d3 = blockArrayClipboard3.getDimensions(); 318 short w3 = (short) d3.getX(); 319 short l3 = (short) d3.getZ(); 320 short h3 = (short) d3.getY(); 321 if (w3 > PLOT_WIDTH || l3 > PLOT_WIDTH) { 322 this.ROAD_SCHEMATIC_ENABLED = true; 323 } 324 int centerShiftZ; 325 if (l3 < this.PLOT_WIDTH) { 326 centerShiftZ = (this.PLOT_WIDTH - l3) / 2; 327 } else { 328 centerShiftZ = (PLOT_WIDTH - l3) / 2; 329 } 330 int centerShiftX; 331 if (w3 < this.PLOT_WIDTH) { 332 centerShiftX = (this.PLOT_WIDTH - w3) / 2; 333 } else { 334 centerShiftX = (PLOT_WIDTH - w3) / 2; 335 } 336 337 BlockVector3 min = blockArrayClipboard3.getMinimumPoint(); 338 for (short x = 0; x < w3; x++) { 339 for (short z = 0; z < l3; z++) { 340 for (short y = 0; y < h3; y++) { 341 BaseBlock id = blockArrayClipboard3.getFullBlock(BlockVector3.at( 342 x + min.getBlockX(), 343 y + min.getBlockY(), 344 z + min.getBlockZ() 345 )); 346 schem3PopulationNeeded |= id.hasNbtData(); 347 addOverlayBlock( 348 (short) (x + shift + oddshift + centerShiftX), 349 (short) (y + plotY), 350 (short) (z + shift + oddshift + centerShiftZ), 351 id, 352 false, 353 maxSchematicHeight 354 ); 355 } 356 if (blockArrayClipboard3.hasBiomes()) { 357 BiomeType biome = blockArrayClipboard3.getBiome(BlockVector2.at( 358 x + min.getBlockX(), 359 z + min.getBlockZ() 360 )); 361 addOverlayBiome( 362 (short) (x + shift + oddshift + centerShiftX), 363 (short) (z + shift + oddshift + centerShiftZ), 364 biome 365 ); 366 } 367 } 368 } 369 370 if (Settings.DEBUG) { 371 LOGGER.info("- plot schematic: {}", schematic3File.getPath()); 372 } 373 } 374 if ((schematic1 == null && schematic2 == null) || this.ROAD_WIDTH == 0) { 375 if (Settings.DEBUG) { 376 LOGGER.info("- road schematic: false"); 377 } 378 return; 379 } 380 this.ROAD_SCHEMATIC_ENABLED = true; 381 // Do not populate road if using schematic population 382 // TODO: What? this.ROAD_BLOCK = BlockBucket.empty(); // BlockState.getEmptyData(this.ROAD_BLOCK); // BlockUtil.get(this.ROAD_BLOCK.id, (byte) 0); 383 384 Clipboard blockArrayClipboard1 = schematic1.getClipboard(); 385 386 BlockVector3 d1 = blockArrayClipboard1.getDimensions(); 387 short w1 = (short) d1.getX(); 388 short l1 = (short) d1.getZ(); 389 short h1 = (short) d1.getY(); 390 // Workaround for schematic height issue if proper calculation of road schematic height is disabled 391 if (!Settings.Schematics.USE_WALL_IN_ROAD_SCHEM_HEIGHT) { 392 h1 += Math.max(ROAD_HEIGHT - WALL_HEIGHT, 0); 393 } 394 395 BlockVector3 min = blockArrayClipboard1.getMinimumPoint(); 396 for (short x = 0; x < w1; x++) { 397 for (short z = 0; z < l1; z++) { 398 for (short y = 0; y < h1; y++) { 399 BaseBlock id = blockArrayClipboard1.getFullBlock(BlockVector3.at( 400 x + min.getBlockX(), 401 y + min.getBlockY(), 402 z + min.getBlockZ() 403 )); 404 schem1PopulationNeeded |= id.hasNbtData(); 405 addOverlayBlock( 406 (short) (x - shift), 407 (short) (y + roadY), 408 (short) (z + shift + oddshift), 409 id, 410 false, 411 maxSchematicHeight 412 ); 413 addOverlayBlock( 414 (short) (z + shift + oddshift), 415 (short) (y + roadY), 416 (short) (shift - x + (oddshift - 1)), 417 id, 418 true, 419 maxSchematicHeight 420 ); 421 } 422 if (blockArrayClipboard1.hasBiomes()) { 423 BiomeType biome = blockArrayClipboard1.getBiome(BlockVector2.at(x + min.getBlockX(), z + min.getBlockZ())); 424 addOverlayBiome((short) (x - shift), (short) (z + shift + oddshift), biome); 425 addOverlayBiome((short) (z + shift + oddshift), (short) (shift - x + (oddshift - 1)), biome); 426 } 427 } 428 } 429 430 Clipboard blockArrayClipboard2 = schematic2.getClipboard(); 431 BlockVector3 d2 = blockArrayClipboard2.getDimensions(); 432 short w2 = (short) d2.getX(); 433 short l2 = (short) d2.getZ(); 434 short h2 = (short) d2.getY(); 435 // Workaround for schematic height issue if proper calculation of road schematic height is disabled 436 if (!Settings.Schematics.USE_WALL_IN_ROAD_SCHEM_HEIGHT) { 437 h2 += Math.max(ROAD_HEIGHT - WALL_HEIGHT, 0); 438 } 439 min = blockArrayClipboard2.getMinimumPoint(); 440 for (short x = 0; x < w2; x++) { 441 for (short z = 0; z < l2; z++) { 442 for (short y = 0; y < h2; y++) { 443 BaseBlock id = blockArrayClipboard2.getFullBlock(BlockVector3.at( 444 x + min.getBlockX(), 445 y + min.getBlockY(), 446 z + min.getBlockZ() 447 )); 448 schem2PopulationNeeded |= id.hasNbtData(); 449 addOverlayBlock( 450 (short) (x - shift), 451 (short) (y + roadY), 452 (short) (z - shift), 453 id, 454 false, 455 maxSchematicHeight 456 ); 457 } 458 if (blockArrayClipboard2.hasBiomes()) { 459 BiomeType biome = blockArrayClipboard2.getBiome(BlockVector2.at(x + min.getBlockX(), z + min.getBlockZ())); 460 addOverlayBiome((short) (x - shift), (short) (z - shift), biome); 461 } 462 } 463 } 464 } 465 466 private void addOverlayBlock(short x, short y, short z, BaseBlock id, boolean rotate, int height) { 467 if (z < 0) { 468 z += this.SIZE; 469 } else if (z >= this.SIZE) { 470 z -= this.SIZE; 471 } 472 if (x < 0) { 473 x += this.SIZE; 474 } else if (x >= this.SIZE) { 475 x -= this.SIZE; 476 } 477 if (rotate) { 478 id = rotate(id); 479 } 480 int pair = MathMan.pair(x, z); 481 BaseBlock[] existing = this.G_SCH.computeIfAbsent(pair, k -> new BaseBlock[height]); 482 if (y >= height) { 483 if (y > lastOverlayHeightError) { 484 lastOverlayHeightError = y; 485 LOGGER.error( 486 "Error adding overlay block in world {}. `y > height`. y={}, height={}", 487 getWorldName(), 488 y, 489 height 490 ); 491 } 492 return; 493 } 494 existing[y] = id; 495 } 496 497 private void addOverlayBiome(short x, short z, BiomeType id) { 498 if (z < 0) { 499 z += this.SIZE; 500 } else if (z >= this.SIZE) { 501 z -= this.SIZE; 502 } 503 if (x < 0) { 504 x += this.SIZE; 505 } else if (x >= this.SIZE) { 506 x -= this.SIZE; 507 } 508 int pair = MathMan.pair(x, z); 509 this.G_SCH_B.put(pair, id); 510 } 511 512 /** 513 * Get the entities contained within the plot schematic for generation. Intended for internal use only. 514 * 515 * @since 6.9.0 516 */ 517 @DoNotUse 518 public @Nullable List<Entity> getPlotSchematicEntities() { 519 return schem3Entities; 520 } 521 522 /** 523 * Get the minimum point of the plot schematic for generation. Intended for internal use only. 524 * 525 * @since 6.9.0 526 */ 527 @DoNotUse 528 public @Nullable BlockVector3 getPlotSchematicMinPoint() { 529 return schem3MinPoint; 530 } 531 532 /** 533 * Get if post-generation population of chunks with tiles/entities is needed for this world. Not for public API use. 534 * 535 * @since 6.9.0 536 */ 537 @DoNotUse 538 public boolean populationNeeded() { 539 return schem1PopulationNeeded || schem2PopulationNeeded || schem3PopulationNeeded; 540 } 541 542 /** 543 * Get the root folder for this world's generation schematics. May be null if schematics not initialised via 544 * {@link HybridPlotWorld#setupSchematics()} 545 * 546 * @since 6.9.0 547 */ 548 public @Nullable File getSchematicRoot() { 549 return this.root; 550 } 551 552 /** 553 * Get the y value where the plot schematic should be pasted from. 554 * 555 * @return plot schematic y start value 556 * @since 7.0.0 557 */ 558 public int getPlotYStart() { 559 return SCHEM_Y + plotY; 560 } 561 562 /** 563 * Get the y value where the road schematic should be pasted from. 564 * 565 * @return road schematic y start value 566 * @since 7.0.0 567 */ 568 public int getRoadYStart() { 569 return SCHEM_Y + roadY; 570 } 571 572}