Move to new generation API

- Currently not working due to lack of biome-setting capability via BiomeProvider for flat worlds
This commit is contained in:
dordsor21
2022-06-08 21:12:16 +01:00
parent 43150abb86
commit 9188c8c40d
14 changed files with 434 additions and 145 deletions

View File

@ -29,28 +29,35 @@ import com.plotsquared.bukkit.queue.GenChunk;
import com.plotsquared.bukkit.util.BukkitUtil;
import com.plotsquared.bukkit.util.BukkitWorld;
import com.plotsquared.core.PlotSquared;
import com.plotsquared.core.generator.ClassicPlotWorld;
import com.plotsquared.core.generator.GeneratorWrapper;
import com.plotsquared.core.generator.IndependentPlotGenerator;
import com.plotsquared.core.generator.SingleWorldGenerator;
import com.plotsquared.core.location.ChunkWrapper;
import com.plotsquared.core.location.UncheckedWorldLocation;
import com.plotsquared.core.plot.PlotArea;
import com.plotsquared.core.plot.world.PlotAreaManager;
import com.plotsquared.core.queue.ZeroedDelegateScopedQueueCoordinator;
import com.plotsquared.core.util.ChunkManager;
import com.sk89q.worldedit.bukkit.BukkitAdapter;
import com.sk89q.worldedit.math.BlockVector2;
import org.bukkit.HeightMap;
import org.bukkit.World;
import org.bukkit.block.Biome;
import org.bukkit.generator.BiomeProvider;
import org.bukkit.generator.BlockPopulator;
import org.bukkit.generator.ChunkGenerator;
import org.bukkit.generator.WorldInfo;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Set;
public class BukkitPlotGenerator extends ChunkGenerator
implements GeneratorWrapper<ChunkGenerator> {
public class BukkitPlotGenerator extends ChunkGenerator implements GeneratorWrapper<ChunkGenerator> {
@SuppressWarnings("unused")
public final boolean PAPER_ASYNC_SAFE = true;
@ -60,9 +67,15 @@ public class BukkitPlotGenerator extends ChunkGenerator
private final ChunkGenerator platformGenerator;
private final boolean full;
private final String levelName;
private final boolean useNewGenerationMethods;
private final BiomeProvider biomeProvider;
private List<BlockPopulator> populators;
private boolean loaded = false;
private PlotArea lastPlotArea;
private int lastChunkX = Integer.MIN_VALUE;
private int lastChunkZ = Integer.MIN_VALUE;
public BukkitPlotGenerator(
final @NonNull String name,
final @NonNull IndependentPlotGenerator generator,
@ -75,18 +88,23 @@ public class BukkitPlotGenerator extends ChunkGenerator
this.populators = new ArrayList<>();
this.populators.add(new BlockStatePopulator(this.plotGenerator, this.plotAreaManager));
this.full = true;
this.useNewGenerationMethods = PlotSquared.platform().serverVersion()[1] >= 17;
this.biomeProvider = new BukkitPlotBiomeProvider();
}
public BukkitPlotGenerator(final String world, final ChunkGenerator cg, final @NonNull PlotAreaManager plotAreaManager) {
if (cg instanceof BukkitPlotGenerator) {
throw new IllegalArgumentException("ChunkGenerator: " + cg.getClass().getName()
+ " is already a BukkitPlotGenerator!");
throw new IllegalArgumentException("ChunkGenerator: " + cg
.getClass()
.getName() + " is already a BukkitPlotGenerator!");
}
this.plotAreaManager = plotAreaManager;
this.levelName = world;
this.full = false;
this.platformGenerator = cg;
this.plotGenerator = new DelegatePlotGenerator(cg, world);
this.useNewGenerationMethods = PlotSquared.platform().serverVersion()[1] >= 17;
this.biomeProvider = null;
}
@Override
@ -112,30 +130,7 @@ public class BukkitPlotGenerator extends ChunkGenerator
@Override
public @NonNull List<BlockPopulator> getDefaultPopulators(@NonNull World world) {
try {
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);
}
world.setAmbientSpawnLimit(0);
world.setAnimalSpawnLimit(0);
world.setMonsterSpawnLimit(0);
world.setWaterAnimalSpawnLimit(0);
} else {
world.setSpawnFlags(true, true);
world.setAmbientSpawnLimit(-1);
world.setAnimalSpawnLimit(-1);
world.setMonsterSpawnLimit(-1);
world.setWaterAnimalSpawnLimit(-1);
}
}
this.loaded = true;
}
checkLoaded(world);
} catch (Exception e) {
e.printStackTrace();
}
@ -154,12 +149,138 @@ public class BukkitPlotGenerator extends ChunkGenerator
return toAdd;
}
@Override
public @NonNull ChunkData generateChunkData(
@NonNull World world, @NonNull Random random, int x, int z,
@NonNull BiomeGrid biome
) {
// Extracted to synchronized method for thread-safety, preventing multiple internal world load calls
private synchronized void checkLoaded(World world) {
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") // Kept for compatibility with <=1.17.1
private void setSpawnLimits(World world, int spawnLimit) {
world.setAmbientSpawnLimit(spawnLimit);
world.setAnimalSpawnLimit(spawnLimit);
world.setMonsterSpawnLimit(spawnLimit);
world.setWaterAnimalSpawnLimit(spawnLimit);
}
@Override
public void generateNoise(
@NotNull final WorldInfo worldInfo,
@NotNull final Random random,
final int chunkX,
final int chunkZ,
@NotNull final ChunkData chunkData
) {
if (this.platformGenerator != this) {
this.platformGenerator.generateNoise(worldInfo, random, chunkX, chunkZ, chunkData);
return;
}
int minY = chunkData.getMinHeight();
int maxY = chunkData.getMaxHeight();
GenChunk result = new GenChunk(minY, maxY);
// Set the chunk location
result.setChunk(new ChunkWrapper(worldInfo.getName(), chunkX, chunkZ));
// Set the result data
result.setChunkData(chunkData);
result.result = null;
// Catch any exceptions (as exceptions usually thrown)
try {
generate(BlockVector2.at(chunkX, chunkZ), worldInfo.getName(), result, false);
} catch (Throwable e) {
e.printStackTrace();
}
}
@Override
public void generateSurface(
@NotNull final WorldInfo worldInfo,
@NotNull final Random random,
final int chunkX,
final int chunkZ,
@NotNull final ChunkData chunkData
) {
if (platformGenerator != this) {
platformGenerator.generateSurface(worldInfo, random, chunkX, chunkZ, chunkData);
}
}
@Override
public void generateBedrock(
@NotNull final WorldInfo worldInfo,
@NotNull final Random random,
final int chunkX,
final int chunkZ,
@NotNull final ChunkData chunkData
) {
if (platformGenerator != this) {
platformGenerator.generateBedrock(worldInfo, random, chunkX, chunkZ, chunkData);
}
}
@Override
public void generateCaves(
@NotNull final WorldInfo worldInfo,
@NotNull final Random random,
final int chunkX,
final int chunkZ,
@NotNull final ChunkData chunkData
) {
if (platformGenerator != this) {
platformGenerator.generateCaves(worldInfo, random, chunkX, chunkZ, chunkData);
}
}
@Override
public @Nullable BiomeProvider getDefaultBiomeProvider(@NotNull final WorldInfo worldInfo) {
if (platformGenerator != this) {
return platformGenerator.getDefaultBiomeProvider(worldInfo);
}
return biomeProvider;
}
@Override
public int getBaseHeight(
@NotNull final WorldInfo worldInfo,
@NotNull final Random random,
final int x,
final int z,
@NotNull final HeightMap heightMap
) {
PlotArea area = getPlotArea(worldInfo.getName(), x, z);
if (area instanceof ClassicPlotWorld cpw) {
// Default to plot height being the heighest point before decoration (i.e. roads, walls etc.)
return cpw.PLOT_HEIGHT;
}
return super.getBaseHeight(worldInfo, random, x, z, heightMap);
}
@SuppressWarnings("deprecation") // The entire method is deprecated, but kept for compatibility with <=1.16.2
@Override
@Deprecated(since = "TODO")
public @NonNull ChunkData generateChunkData(
@NonNull World world, @NonNull Random random, int x, int z, @NonNull BiomeGrid biome
) {
if (useNewGenerationMethods) {
// Return super as it will throw an exception caught by the server that will mean this method is no longer used.
return super.generateChunkData(world, random, x, z, biome);
}
int minY = BukkitWorld.getMinWorldHeight(world);
int maxY = BukkitWorld.getMaxWorldHeight(world);
GenChunk result = new GenChunk(minY, maxY);
@ -169,7 +290,6 @@ public class BukkitPlotGenerator extends ChunkGenerator
for (int chunkZ = 0; chunkZ < 16; chunkZ++) {
for (int y = minY; y < maxY; y++) {
biome.setBiome(chunkX, y, chunkZ, Biome.PLAINS);
}
}
}
@ -189,7 +309,7 @@ public class BukkitPlotGenerator extends ChunkGenerator
if (this.platformGenerator != this) {
return this.platformGenerator.generateChunkData(world, random, x, z, biome);
} else {
generate(BlockVector2.at(x, z), world, result);
generate(BlockVector2.at(x, z), world.getName(), result, true);
}
} catch (Throwable e) {
e.printStackTrace();
@ -198,25 +318,14 @@ public class BukkitPlotGenerator extends ChunkGenerator
return result.getChunkData();
}
private void generate(BlockVector2 loc, World world, ZeroedDelegateScopedQueueCoordinator result) {
// Load if improperly loaded
if (!this.loaded) {
String name = world.getName();
PlotSquared.get().loadWorld(name, this);
this.loaded = true;
}
private void generate(BlockVector2 loc, String world, ZeroedDelegateScopedQueueCoordinator result, boolean biomes) {
// Process the chunk
if (ChunkManager.preProcessChunk(loc, result)) {
return;
}
PlotArea area = this.plotAreaManager.getPlotArea(world.getName(), null);
if (area == null && (area = this.plotAreaManager.getPlotArea(this.levelName, null)) == null) {
throw new IllegalStateException(
"Cannot regenerate chunk that does not belong to a plot area." + " Location: " + loc
+ ", world: " + world);
}
PlotArea area = getPlotArea(world, loc.getX(), loc.getZ());
try {
this.plotGenerator.generateChunk(result, area);
this.plotGenerator.generateChunk(result, area, biomes);
} catch (Throwable e) {
// Recover from generator error
e.printStackTrace();
@ -273,4 +382,59 @@ public class BukkitPlotGenerator extends ChunkGenerator
return this.levelName;
}
private synchronized PlotArea getPlotArea(String name, int chunkX, int chunkZ) {
// Load if improperly loaded
if (!this.loaded) {
PlotSquared.get().loadWorld(name, this);
// Do not set loaded to true as we want to ensure spawn limits are set when "loading" is actually able to be
// completed properly.
}
if (lastPlotArea != null && name.equals(this.levelName) && chunkX == lastChunkX && chunkZ == lastChunkZ) {
return lastPlotArea;
}
PlotArea area = UncheckedWorldLocation.at(name, chunkX << 4, 0, chunkZ << 4).getPlotArea();
if (area == null) {
throw new IllegalStateException(String.format("Cannot generate chunk that does not belong to a plot area. World: %s",
name
));
}
this.lastChunkX = chunkX;
this.lastChunkZ = chunkZ;
return this.lastPlotArea = area;
}
private PlotArea getPlotArea(String name) {
final Set<PlotArea> areas = this.plotAreaManager.getPlotAreasSet(name);
if (!areas.isEmpty()) {
return areas.iterator().next();
}
return null;
}
/**
* Biome provider should never need to be accessed outside of this class.
*/
private final class BukkitPlotBiomeProvider extends BiomeProvider {
private static final List<Biome> BIOMES;
static {
ArrayList<Biome> biomes = new ArrayList<>(List.of(Biome.values()));
biomes.remove(Biome.CUSTOM);
BIOMES = List.copyOf(biomes);
}
@Override
public @NotNull Biome getBiome(@NotNull final WorldInfo worldInfo, final int x, final int y, final int z) {
PlotArea area = getPlotArea(worldInfo.getName(), x >> 4, z >> 4);
return BukkitAdapter.adapt(plotGenerator.getBiome(area, x, y, z));
}
@Override
public @NotNull List<Biome> getBiomes(@NotNull final WorldInfo worldInfo) {
return BIOMES; // Allow all biomes
}
}
}

View File

@ -34,6 +34,7 @@ import com.plotsquared.core.plot.PlotId;
import com.plotsquared.core.queue.ZeroedDelegateScopedQueueCoordinator;
import com.plotsquared.core.util.MathMan;
import com.sk89q.worldedit.bukkit.BukkitAdapter;
import com.sk89q.worldedit.world.biome.BiomeType;
import org.bukkit.World;
import org.bukkit.block.Biome;
import org.bukkit.generator.BlockPopulator;
@ -56,6 +57,11 @@ final class DelegatePlotGenerator extends IndependentPlotGenerator {
public void initialize(PlotArea area) {
}
@Override
public BiomeType getBiome(final PlotArea settings, final int x, final int y, final int z) {
return null;
}
@Override
public String getName() {
return this.chunkGenerator.getClass().getName();
@ -67,7 +73,7 @@ final class DelegatePlotGenerator extends IndependentPlotGenerator {
}
@Override
public void generateChunk(final ZeroedDelegateScopedQueueCoordinator result, PlotArea settings) {
public void generateChunk(final ZeroedDelegateScopedQueueCoordinator result, PlotArea settings, boolean biomes) {
World world = BukkitUtil.getWorld(this.world);
Location min = result.getMin();
int chunkX = min.getX() >> 4;