2020-04-16 04:52:39 +02:00
|
|
|
/*
|
2022-06-15 20:17:17 +02:00
|
|
|
* PlotSquared, a land and world management plugin for Minecraft.
|
|
|
|
* Copyright (C) IntellectualSites <https://intellectualsites.com>
|
|
|
|
* Copyright (C) IntellectualSites team and contributors
|
2020-04-16 04:52:39 +02:00
|
|
|
*
|
2022-06-15 20:17:17 +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.
|
2020-04-16 04:52:39 +02:00
|
|
|
*
|
2022-06-15 20:17:17 +02:00
|
|
|
* 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.
|
2020-04-16 04:52:39 +02:00
|
|
|
*
|
2022-06-15 20:17:17 +02:00
|
|
|
* 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-16 04:52:39 +02:00
|
|
|
*/
|
2020-04-11 02:19:18 +02:00
|
|
|
package com.plotsquared.bukkit.generator;
|
2014-10-08 07:53:09 +02:00
|
|
|
|
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;
|
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;
|
2020-07-17 16:41:06 +02:00
|
|
|
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;
|
2021-01-09 22:28:41 +01:00
|
|
|
import org.checkerframework.checker.nullness.qual.NonNull;
|
2016-06-24 02:12:17 +02:00
|
|
|
|
2019-01-17 02:06:32 +01:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Random;
|
|
|
|
import java.util.Set;
|
2018-08-10 17:01:10 +02:00
|
|
|
|
2022-06-12 10:47:25 +02:00
|
|
|
public class BukkitPlotGenerator extends ChunkGenerator implements GeneratorWrapper<ChunkGenerator> {
|
2018-08-10 17:01:10 +02:00
|
|
|
|
2021-01-09 22:28:41 +01:00
|
|
|
@SuppressWarnings("unused")
|
|
|
|
public final boolean PAPER_ASYNC_SAFE = true;
|
2019-03-26 09:27:20 +01:00
|
|
|
|
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;
|
2021-01-09 22:28:41 +01:00
|
|
|
private final String levelName;
|
2018-08-10 17:01:10 +02:00
|
|
|
private List<BlockPopulator> populators;
|
2016-02-14 02:01:18 +01:00
|
|
|
private boolean loaded = false;
|
2016-02-10 19:59:51 +01:00
|
|
|
|
2021-01-09 22:28:41 +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;
|
2019-03-26 09:27:20 +01:00
|
|
|
this.populators = new ArrayList<>();
|
2022-06-12 10:47:25 +02:00
|
|
|
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;
|
2015-07-03 04:11:41 +02:00
|
|
|
}
|
2018-08-10 17:01:10 +02:00
|
|
|
|
2021-01-09 22:28:41 +01: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()
|
2021-01-09 22:28:41 +01:00
|
|
|
+ " 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;
|
2019-03-26 09:27:20 +01:00
|
|
|
this.plotGenerator = new DelegatePlotGenerator(cg, world);
|
2015-05-09 08:24:03 +02:00
|
|
|
}
|
2018-08-10 17:01:10 +02:00
|
|
|
|
2021-01-09 22:28:41 +01:00
|
|
|
@Override
|
|
|
|
public void augment(PlotArea area) {
|
2020-04-02 14:34:38 +02:00
|
|
|
BukkitAugmentedGenerator.get(BukkitUtil.getWorld(area.getWorldName()));
|
2016-02-10 19:59:51 +01:00
|
|
|
}
|
2018-08-10 17:01:10 +02:00
|
|
|
|
2021-01-09 22:28:41 +01: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
|
|
|
|
2021-01-09 22:28:41 +01: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
|
|
|
|
2021-01-09 22:28:41 +01:00
|
|
|
@Override
|
|
|
|
public ChunkGenerator getPlatformGenerator() {
|
2016-03-23 02:41:37 +01:00
|
|
|
return this.platformGenerator;
|
2016-02-10 19:59:51 +01:00
|
|
|
}
|
|
|
|
|
2021-01-09 22:28:41 +01:00
|
|
|
@Override
|
|
|
|
public @NonNull List<BlockPopulator> getDefaultPopulators(@NonNull World world) {
|
2015-09-13 06:04:31 +02:00
|
|
|
try {
|
2022-06-10 16:52:17 +02:00
|
|
|
checkLoaded(world);
|
2016-03-23 02:41:37 +01:00
|
|
|
} catch (Exception e) {
|
2015-05-10 05:17:10 +02:00
|
|
|
e.printStackTrace();
|
2015-03-11 09:47:22 +01:00
|
|
|
}
|
2016-04-30 00:14:12 +02:00
|
|
|
ArrayList<BlockPopulator> toAdd = new ArrayList<>();
|
2016-03-31 19:23:26 +02:00
|
|
|
List<BlockPopulator> existing = world.getPopulators();
|
2016-09-04 06:22:01 +02:00
|
|
|
if (populators == null && platformGenerator != null) {
|
2016-11-21 04:24:27 +01:00
|
|
|
populators = new ArrayList<>(platformGenerator.getDefaultPopulators(world));
|
2016-09-04 06:22:01 +02:00
|
|
|
}
|
2019-03-26 09:27:20 +01:00
|
|
|
if (populators != null) {
|
|
|
|
for (BlockPopulator populator : this.populators) {
|
|
|
|
if (!existing.contains(populator)) {
|
|
|
|
toAdd.add(populator);
|
|
|
|
}
|
2016-03-31 19:23:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return toAdd;
|
2015-07-18 19:03:59 +02:00
|
|
|
}
|
2016-03-31 19:23:26 +02:00
|
|
|
|
2022-06-10 16:52:17 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2023-01-23 13:18:20 +01:00
|
|
|
/**
|
2023-01-23 13:40:35 +01:00
|
|
|
* The entire method is deprecated, but kept for compatibility with versions lower than or equal to 1.16.2.
|
2023-01-23 13:18:20 +01:00
|
|
|
* The method will be removed in future versions, because WorldEdit and FastAsyncWorldEdit only support the latest point
|
|
|
|
* release.
|
|
|
|
*/
|
|
|
|
@Deprecated(forRemoval = true, since = "TODO")
|
2021-01-09 22:28:41 +01:00
|
|
|
@Override
|
|
|
|
public @NonNull ChunkData generateChunkData(
|
|
|
|
@NonNull World world, @NonNull Random random, int x, int z,
|
|
|
|
@NonNull BiomeGrid biome
|
|
|
|
) {
|
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) {
|
2019-05-17 20:38:57 +02:00
|
|
|
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++) {
|
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
|
|
|
}
|
|
|
|
}
|
2019-05-17 20:38:57 +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
|
2019-02-15 18:50:43 +01:00
|
|
|
result.setChunk(new ChunkWrapper(world.getName(), x, z));
|
2016-02-10 19:59:51 +01:00
|
|
|
// Set the result data
|
2019-05-17 20:38:57 +02:00
|
|
|
result.setChunkData(createChunkData(world));
|
2019-02-15 18:50:43 +01:00
|
|
|
result.biomeGrid = biome;
|
2019-01-17 02:06:32 +01:00
|
|
|
result.result = null;
|
2018-12-20 13:23:49 +01:00
|
|
|
|
2016-03-29 09:10:24 +02:00
|
|
|
// 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) {
|
2019-02-15 18:50:43 +01:00
|
|
|
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
|
2019-05-17 20:38:57 +02:00
|
|
|
return result.getChunkData();
|
2015-03-27 08:15:41 +01:00
|
|
|
}
|
2016-04-05 18:37:11 +02:00
|
|
|
|
2020-07-17 16:41:06 +02:00
|
|
|
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) {
|
2022-06-10 16:52:17 +02:00
|
|
|
checkLoaded(world);
|
2016-02-10 19:59:51 +01:00
|
|
|
}
|
|
|
|
// Process the chunk
|
2019-03-26 09:27:20 +01:00
|
|
|
if (ChunkManager.preProcessChunk(loc, result)) {
|
2016-02-10 19:59:51 +01:00
|
|
|
return;
|
2015-03-27 08:15:41 +01:00
|
|
|
}
|
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(
|
2021-01-09 22:28:41 +01:00
|
|
|
"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 {
|
2019-03-26 09:27:20 +01:00
|
|
|
this.plotGenerator.generateChunk(result, area);
|
2016-04-26 01:32:16 +02:00
|
|
|
} catch (Throwable e) {
|
|
|
|
// Recover from generator error
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2019-03-26 09:27:20 +01:00
|
|
|
ChunkManager.postProcessChunk(loc, result);
|
2015-03-27 08:15:41 +01:00
|
|
|
}
|
2018-08-10 17:01:10 +02:00
|
|
|
|
2021-01-09 22:28:41 +01: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
|
|
|
}
|
|
|
|
|
2021-01-09 22:28:41 +01:00
|
|
|
@Override
|
|
|
|
public String toString() {
|
2016-03-23 02:41:37 +01:00
|
|
|
if (this.platformGenerator == this) {
|
2016-04-05 18:37:11 +02:00
|
|
|
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
|
|
|
|
2021-01-09 22:28:41 +01: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
|
|
|
}
|
2019-03-26 09:27:20 +01:00
|
|
|
|
2020-07-17 17:24:45 +02:00
|
|
|
public String getLevelName() {
|
|
|
|
return this.levelName;
|
|
|
|
}
|
2021-01-09 22:28:41 +01:00
|
|
|
|
2014-10-08 07:53:09 +02:00
|
|
|
}
|