PlotSquared/Bukkit/src/main/java/com/plotsquared/bukkit/generator/BukkitPlotGenerator.java

287 lines
9.9 KiB
Java
Raw Normal View History

/*
* PlotSquared, a land and world management plugin for Minecraft.
* Copyright (C) IntellectualSites <https://intellectualsites.com>
* Copyright (C) IntellectualSites team and contributors
*
* 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 <https://www.gnu.org/licenses/>.
*/
2020-04-11 02:19:18 +02:00
package com.plotsquared.bukkit.generator;
2020-04-15 21:26:54 +02:00
import com.plotsquared.bukkit.queue.GenChunk;
2020-04-30 12:01:52 +02:00
import com.plotsquared.bukkit.util.BukkitUtil;
Implement extended world heights from Y-64 to Y319 #3473 (#3473) * Begin to implement extended world heights: - Implemented in Bukkit module (and where required in Core module) * Implement extended world heights into core module * Add min gen height to setup, * Default gen/build heights based on minecraft version * Few fixes * Fix up queues * Address comments * Make road schematic stuff slightly more efficient by sharing queues * Minor fixes, don't overlay error many times for the same y * Fix incorrect schematic paste height, undo changes to HybridUtils * Overhall regenallroads method to make it work, make sure BukkitChunkCoordinator can/will finish * Process chunks in order when regenerating all roads * Address comments * Address comments * Ground level//bedrock is at min gen height - Add comment on == rather than <= being used - It's because it's only checking for the bedrock layer being broken if that's disabled * Fix offset for min build height in SchematicHandler * Better javadoc Co-authored-by: Hannes Greule <SirYwell@users.noreply.github.com> * Address inclusivity issues for max world height * Javadocs/comments/deprecation * Use world min/max heights if present in QueueCoordinator * Address some deprecations for regions and biome setting * Add a count for chunks we're currently trying to load to not skip chunks at the end of a queue's edit * Use minGenHeight + 1 rather than build height in AugmentedUtils * Create utility method for layer index in GenChunk * Correct height in HybridUtils, also use minGenHeight + 1 * Don't magically split to 128 height in regeneration * Add utility methods for world height in QueueCoordinator * Clean up ClassicPlotManager road creation/removal * Start generation at min gen height if bedrock is disabled * min gen height is set in PlotArea * Add note on schem y normalisation * Improve plot getVolume method readability * Don't overly extend height when regenerating road region * y index utility method in ChunknQueueCoordinator * Layer index utility method in LocalChunk * Use version min/max heights if world not present in QueueCoordinator * Fix min -> max * Don't allow players to modify outside build height when using plot set / schematics. - Also fixes schematic height issues * Remove debug * Address comments * Switch loadingChunks to AtomicInteger to be safe (in case of multi-threaded) * Fix "security" issue that was already present * Ensure sign isn't physicsed Co-authored-by: Hannes Greule <SirYwell@users.noreply.github.com>
2022-03-05 19:03:39 +01:00
import com.plotsquared.bukkit.util.BukkitWorld;
2020-04-15 21:26:54 +02:00
import com.plotsquared.core.PlotSquared;
import com.plotsquared.core.generator.GeneratorWrapper;
import com.plotsquared.core.generator.IndependentPlotGenerator;
2020-04-30 12:01:52 +02:00
import com.plotsquared.core.generator.SingleWorldGenerator;
2020-04-15 21:26:54 +02:00
import com.plotsquared.core.location.ChunkWrapper;
import com.plotsquared.core.plot.PlotArea;
2020-07-10 17:32:07 +02:00
import com.plotsquared.core.plot.world.PlotAreaManager;
import com.plotsquared.core.queue.ScopedQueueCoordinator;
2020-04-15 21:26:54 +02:00
import com.plotsquared.core.util.ChunkManager;
2019-11-04 18:44:23 +01:00
import com.sk89q.worldedit.math.BlockVector2;
2017-03-23 01:10:29 +01:00
import org.bukkit.World;
import org.bukkit.block.Biome;
import org.bukkit.generator.BlockPopulator;
import org.bukkit.generator.ChunkGenerator;
import org.checkerframework.checker.nullness.qual.NonNull;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Set;
2018-08-10 17:01:10 +02:00
public class BukkitPlotGenerator extends ChunkGenerator implements GeneratorWrapper<ChunkGenerator> {
2018-08-10 17:01:10 +02:00
@SuppressWarnings("unused")
public final boolean PAPER_ASYNC_SAFE = true;
2020-07-10 17:32:07 +02:00
private final PlotAreaManager plotAreaManager;
2016-02-10 19:59:51 +01:00
private final IndependentPlotGenerator plotGenerator;
2016-03-14 07:18:04 +01:00
private final ChunkGenerator platformGenerator;
private final boolean full;
private final String levelName;
2018-08-10 17:01:10 +02:00
private List<BlockPopulator> populators;
private boolean loaded = false;
2016-02-10 19:59:51 +01:00
public BukkitPlotGenerator(
final @NonNull String name,
final @NonNull IndependentPlotGenerator generator,
final @NonNull PlotAreaManager plotAreaManager
) {
2020-07-10 17:32:07 +02:00
this.plotAreaManager = plotAreaManager;
2020-02-24 14:00:45 +01:00
this.levelName = name;
2016-02-10 19:59:51 +01:00
this.plotGenerator = generator;
this.platformGenerator = this;
this.populators = new ArrayList<>();
int minecraftMinorVersion = PlotSquared.platform().serverVersion()[1];
if (minecraftMinorVersion >= 17) {
this.populators.add(new BlockStatePopulator(this.plotGenerator));
} else {
this.populators.add(new LegacyBlockStatePopulator(this.plotGenerator));
}
2016-02-10 19:59:51 +01:00
this.full = true;
}
2018-08-10 17:01:10 +02:00
public BukkitPlotGenerator(final String world, final ChunkGenerator cg, final @NonNull PlotAreaManager plotAreaManager) {
2016-02-10 19:59:51 +01:00
if (cg instanceof BukkitPlotGenerator) {
2018-08-10 17:01:10 +02:00
throw new IllegalArgumentException("ChunkGenerator: " + cg.getClass().getName()
+ " is already a BukkitPlotGenerator!");
2016-02-10 19:59:51 +01:00
}
2020-07-10 17:32:07 +02:00
this.plotAreaManager = plotAreaManager;
2020-02-24 14:00:45 +01:00
this.levelName = world;
2016-02-10 19:59:51 +01:00
this.full = false;
2016-03-23 02:41:37 +01:00
this.platformGenerator = cg;
this.plotGenerator = new DelegatePlotGenerator(cg, world);
}
2018-08-10 17:01:10 +02:00
@Override
public void augment(PlotArea area) {
BukkitAugmentedGenerator.get(BukkitUtil.getWorld(area.getWorldName()));
2016-02-10 19:59:51 +01:00
}
2018-08-10 17:01:10 +02:00
@Override
public boolean isFull() {
2016-03-23 02:41:37 +01:00
return this.full;
2016-02-10 19:59:51 +01:00
}
2018-08-10 17:01:10 +02:00
@Override
public IndependentPlotGenerator getPlotGenerator() {
2016-03-23 02:41:37 +01:00
return this.plotGenerator;
2016-02-10 19:59:51 +01:00
}
2018-08-10 17:01:10 +02:00
@Override
public ChunkGenerator getPlatformGenerator() {
2016-03-23 02:41:37 +01:00
return this.platformGenerator;
2016-02-10 19:59:51 +01:00
}
@Override
public @NonNull List<BlockPopulator> getDefaultPopulators(@NonNull World world) {
2015-09-13 06:04:31 +02:00
try {
checkLoaded(world);
2016-03-23 02:41:37 +01:00
} catch (Exception e) {
2015-05-10 05:17:10 +02:00
e.printStackTrace();
}
2016-04-30 00:14:12 +02:00
ArrayList<BlockPopulator> toAdd = new ArrayList<>();
List<BlockPopulator> existing = world.getPopulators();
if (populators == null && platformGenerator != null) {
2016-11-21 04:24:27 +01:00
populators = new ArrayList<>(platformGenerator.getDefaultPopulators(world));
}
if (populators != null) {
for (BlockPopulator populator : this.populators) {
if (!existing.contains(populator)) {
toAdd.add(populator);
}
}
}
return toAdd;
}
private synchronized void checkLoaded(@NonNull World world) {
// Do not attempt to load configurations until WorldEdit has a platform ready.
if (!PlotSquared.get().isWeInitialised()) {
return;
}
if (!this.loaded) {
String name = world.getName();
PlotSquared.get().loadWorld(name, this);
final Set<PlotArea> areas = this.plotAreaManager.getPlotAreasSet(name);
if (!areas.isEmpty()) {
PlotArea area = areas.iterator().next();
if (!area.isMobSpawning()) {
if (!area.isSpawnEggs()) {
world.setSpawnFlags(false, false);
}
setSpawnLimits(world, 0);
} else {
world.setSpawnFlags(true, true);
setSpawnLimits(world, -1);
}
}
this.loaded = true;
}
}
@SuppressWarnings("deprecation")
private void setSpawnLimits(@NonNull World world, int limit) {
world.setAmbientSpawnLimit(limit);
world.setAnimalSpawnLimit(limit);
world.setMonsterSpawnLimit(limit);
world.setWaterAnimalSpawnLimit(limit);
}
/**
* The entire method is deprecated, but kept for compatibility with versions lower than or equal to 1.16.2.
* The method will be removed in future versions, because WorldEdit and FastAsyncWorldEdit only support the latest point
* release.
*/
@Deprecated(forRemoval = true, since = "TODO")
@Override
public @NonNull ChunkData generateChunkData(
@NonNull World world, @NonNull Random random, int x, int z,
@NonNull BiomeGrid biome
) {
Implement extended world heights from Y-64 to Y319 #3473 (#3473) * Begin to implement extended world heights: - Implemented in Bukkit module (and where required in Core module) * Implement extended world heights into core module * Add min gen height to setup, * Default gen/build heights based on minecraft version * Few fixes * Fix up queues * Address comments * Make road schematic stuff slightly more efficient by sharing queues * Minor fixes, don't overlay error many times for the same y * Fix incorrect schematic paste height, undo changes to HybridUtils * Overhall regenallroads method to make it work, make sure BukkitChunkCoordinator can/will finish * Process chunks in order when regenerating all roads * Address comments * Address comments * Ground level//bedrock is at min gen height - Add comment on == rather than <= being used - It's because it's only checking for the bedrock layer being broken if that's disabled * Fix offset for min build height in SchematicHandler * Better javadoc Co-authored-by: Hannes Greule <SirYwell@users.noreply.github.com> * Address inclusivity issues for max world height * Javadocs/comments/deprecation * Use world min/max heights if present in QueueCoordinator * Address some deprecations for regions and biome setting * Add a count for chunks we're currently trying to load to not skip chunks at the end of a queue's edit * Use minGenHeight + 1 rather than build height in AugmentedUtils * Create utility method for layer index in GenChunk * Correct height in HybridUtils, also use minGenHeight + 1 * Don't magically split to 128 height in regeneration * Add utility methods for world height in QueueCoordinator * Clean up ClassicPlotManager road creation/removal * Start generation at min gen height if bedrock is disabled * min gen height is set in PlotArea * Add note on schem y normalisation * Improve plot getVolume method readability * Don't overly extend height when regenerating road region * y index utility method in ChunknQueueCoordinator * Layer index utility method in LocalChunk * Use version min/max heights if world not present in QueueCoordinator * Fix min -> max * Don't allow players to modify outside build height when using plot set / schematics. - Also fixes schematic height issues * Remove debug * Address comments * Switch loadingChunks to AtomicInteger to be safe (in case of multi-threaded) * Fix "security" issue that was already present * Ensure sign isn't physicsed Co-authored-by: Hannes Greule <SirYwell@users.noreply.github.com>
2022-03-05 19:03:39 +01:00
int minY = BukkitWorld.getMinWorldHeight(world);
int maxY = BukkitWorld.getMaxWorldHeight(world);
GenChunk result = new GenChunk(minY, maxY);
2017-09-25 08:57:42 +02:00
if (this.getPlotGenerator() instanceof SingleWorldGenerator) {
if (result.getChunkData() != null) {
2019-05-17 21:32:05 +02:00
for (int chunkX = 0; chunkX < 16; chunkX++) {
for (int chunkZ = 0; chunkZ < 16; chunkZ++) {
Implement extended world heights from Y-64 to Y319 #3473 (#3473) * Begin to implement extended world heights: - Implemented in Bukkit module (and where required in Core module) * Implement extended world heights into core module * Add min gen height to setup, * Default gen/build heights based on minecraft version * Few fixes * Fix up queues * Address comments * Make road schematic stuff slightly more efficient by sharing queues * Minor fixes, don't overlay error many times for the same y * Fix incorrect schematic paste height, undo changes to HybridUtils * Overhall regenallroads method to make it work, make sure BukkitChunkCoordinator can/will finish * Process chunks in order when regenerating all roads * Address comments * Address comments * Ground level//bedrock is at min gen height - Add comment on == rather than <= being used - It's because it's only checking for the bedrock layer being broken if that's disabled * Fix offset for min build height in SchematicHandler * Better javadoc Co-authored-by: Hannes Greule <SirYwell@users.noreply.github.com> * Address inclusivity issues for max world height * Javadocs/comments/deprecation * Use world min/max heights if present in QueueCoordinator * Address some deprecations for regions and biome setting * Add a count for chunks we're currently trying to load to not skip chunks at the end of a queue's edit * Use minGenHeight + 1 rather than build height in AugmentedUtils * Create utility method for layer index in GenChunk * Correct height in HybridUtils, also use minGenHeight + 1 * Don't magically split to 128 height in regeneration * Add utility methods for world height in QueueCoordinator * Clean up ClassicPlotManager road creation/removal * Start generation at min gen height if bedrock is disabled * min gen height is set in PlotArea * Add note on schem y normalisation * Improve plot getVolume method readability * Don't overly extend height when regenerating road region * y index utility method in ChunknQueueCoordinator * Layer index utility method in LocalChunk * Use version min/max heights if world not present in QueueCoordinator * Fix min -> max * Don't allow players to modify outside build height when using plot set / schematics. - Also fixes schematic height issues * Remove debug * Address comments * Switch loadingChunks to AtomicInteger to be safe (in case of multi-threaded) * Fix "security" issue that was already present * Ensure sign isn't physicsed Co-authored-by: Hannes Greule <SirYwell@users.noreply.github.com>
2022-03-05 19:03:39 +01:00
for (int y = minY; y < maxY; y++) {
2020-02-11 22:13:03 +01:00
biome.setBiome(chunkX, y, chunkZ, Biome.PLAINS);
}
2018-04-15 02:47:00 +02:00
}
}
return result.getChunkData();
2018-04-15 02:47:00 +02:00
}
2017-09-25 08:57:42 +02:00
}
2016-02-10 19:59:51 +01:00
// Set the chunk location
result.setChunk(new ChunkWrapper(world.getName(), x, z));
2016-02-10 19:59:51 +01:00
// Set the result data
result.setChunkData(createChunkData(world));
result.biomeGrid = biome;
result.result = null;
// Catch any exceptions (as exceptions usually thrown)
2015-09-13 06:04:31 +02:00
try {
2016-02-10 19:59:51 +01:00
// Fill the result data if necessary
2016-03-23 02:41:37 +01:00
if (this.platformGenerator != this) {
return this.platformGenerator.generateChunkData(world, random, x, z, biome);
2016-02-10 19:59:51 +01:00
} else {
2019-11-04 18:44:23 +01:00
generate(BlockVector2.at(x, z), world, result);
2015-05-10 05:17:10 +02:00
}
2016-02-10 19:59:51 +01:00
} catch (Throwable e) {
2015-05-10 05:17:10 +02:00
e.printStackTrace();
}
2016-02-10 19:59:51 +01:00
// Return the result data
return result.getChunkData();
}
private void generate(BlockVector2 loc, World world, ScopedQueueCoordinator result) {
2016-02-10 19:59:51 +01:00
// Load if improperly loaded
2016-03-23 02:41:37 +01:00
if (!this.loaded) {
checkLoaded(world);
2016-02-10 19:59:51 +01:00
}
// Process the chunk
if (ChunkManager.preProcessChunk(loc, result)) {
2016-02-10 19:59:51 +01:00
return;
}
2020-07-10 17:32:07 +02:00
PlotArea area = this.plotAreaManager.getPlotArea(world.getName(), null);
if (area == null && (area = this.plotAreaManager.getPlotArea(this.levelName, null)) == null) {
2020-04-30 12:01:52 +02:00
throw new IllegalStateException(
"Cannot regenerate chunk that does not belong to a plot area." + " Location: " + loc
+ ", world: " + world);
2020-02-24 14:00:45 +01:00
}
2016-04-26 01:32:16 +02:00
try {
this.plotGenerator.generateChunk(result, area);
2016-04-26 01:32:16 +02:00
} catch (Throwable e) {
// Recover from generator error
e.printStackTrace();
}
ChunkManager.postProcessChunk(loc, result);
}
2018-08-10 17:01:10 +02:00
@Override
public boolean canSpawn(final @NonNull World world, final int x, final int z) {
2020-02-11 22:13:03 +01:00
return true;
}
public boolean shouldGenerateCaves() {
return false;
}
public boolean shouldGenerateDecorations() {
2020-02-13 00:57:42 +01:00
return false;
2020-02-11 22:13:03 +01:00
}
public boolean isParallelCapable() {
2015-03-27 09:20:19 +01:00
return true;
}
2018-08-10 17:01:10 +02:00
2020-02-11 22:13:03 +01:00
public boolean shouldGenerateMobs() {
return false;
}
public boolean shouldGenerateStructures() {
2020-02-12 21:42:31 +01:00
return true;
2020-02-11 22:13:03 +01:00
}
@Override
public String toString() {
2016-03-23 02:41:37 +01:00
if (this.platformGenerator == this) {
return this.plotGenerator.getName();
2016-03-23 02:41:37 +01:00
}
if (this.platformGenerator == null) {
return "null";
} else {
return this.platformGenerator.getClass().getName();
2016-02-10 19:59:51 +01:00
}
}
2018-08-10 17:01:10 +02:00
@Override
public boolean equals(final Object obj) {
2016-02-10 19:59:51 +01:00
if (obj == null) {
return false;
}
2016-03-21 03:52:16 +01:00
return toString().equals(obj.toString()) || toString().equals(obj.getClass().getName());
2016-02-10 19:59:51 +01:00
}
2020-07-17 17:24:45 +02:00
public String getLevelName() {
return this.levelName;
}
}