Update to new Spigot generation API (#3659)

* Address deprecations in queue/generation code

* Move to new generation API
 - Currently not working due to lack of biome-setting capability via BiomeProvider for flat worlds

* Any fixes to flat world biome setting will target 1.19

* Ensure compiled is actually set to true in BlockBucket

* Delegate to platformGenerator in deprecated generation method if applicable when using new generation methods (1.19)

* Re-add wrongly removed method

* Handle exceptions using logger

* We can simplify getting relative offset using floormod

* Replace many booleans with EnumSet

* Address comments, remove needless boolean return for populateChunk
This commit is contained in:
Jordan
2022-06-22 13:57:39 +01:00
committed by GitHub
parent 6b680fb2c0
commit 75fd9b2631
13 changed files with 370 additions and 123 deletions

View File

@ -22,20 +22,30 @@ 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.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
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;
@ -44,6 +54,8 @@ import java.util.Set;
public class BukkitPlotGenerator extends ChunkGenerator implements GeneratorWrapper<ChunkGenerator> {
private static final Logger LOGGER = LogManager.getLogger("PlotSquared/" + BukkitPlotGenerator.class.getSimpleName());
@SuppressWarnings("unused")
public final boolean PAPER_ASYNC_SAFE = true;
@ -52,9 +64,15 @@ public class BukkitPlotGenerator extends ChunkGenerator implements GeneratorWrap
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,
@ -72,18 +90,23 @@ public class BukkitPlotGenerator extends ChunkGenerator implements GeneratorWrap
this.populators.add(new LegacyBlockStatePopulator(this.plotGenerator));
}
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] >= 19;
this.biomeProvider = null;
}
@Override
@ -111,7 +134,7 @@ public class BukkitPlotGenerator extends ChunkGenerator implements GeneratorWrap
try {
checkLoaded(world);
} catch (Exception e) {
e.printStackTrace();
LOGGER.error("Error attempting to load world into PlotSquared.", e);
}
ArrayList<BlockPopulator> toAdd = new ArrayList<>();
List<BlockPopulator> existing = world.getPopulators();
@ -128,6 +151,7 @@ public class BukkitPlotGenerator extends ChunkGenerator implements GeneratorWrap
return toAdd;
}
// Extracted to synchronized method for thread-safety, preventing multiple internal world load calls
private synchronized void checkLoaded(@NonNull World world) {
// Do not attempt to load configurations until WorldEdit has a platform ready.
if (!PlotSquared.get().isWeInitialised()) {
@ -153,7 +177,7 @@ public class BukkitPlotGenerator extends ChunkGenerator implements GeneratorWrap
}
}
@SuppressWarnings("deprecation")
@SuppressWarnings("deprecation") // Kept for compatibility with <=1.17.1
private void setSpawnLimits(@NonNull World world, int limit) {
world.setAmbientSpawnLimit(limit);
world.setAnimalSpawnLimit(limit);
@ -162,10 +186,112 @@ public class BukkitPlotGenerator extends ChunkGenerator implements GeneratorWrap
}
@Override
public @NonNull ChunkData generateChunkData(
@NonNull World world, @NonNull Random random, int x, int z,
@NonNull BiomeGrid biome
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) {
LOGGER.error("Error attempting to generate chunk.", e);
}
}
@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) {
if (this.platformGenerator != this) {
return this.platformGenerator.generateChunkData(world, random, x, z, biome);
} else {
// 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);
@ -175,7 +301,6 @@ public class BukkitPlotGenerator extends ChunkGenerator implements GeneratorWrap
for (int chunkZ = 0; chunkZ < 16; chunkZ++) {
for (int y = minY; y < maxY; y++) {
biome.setBiome(chunkX, y, chunkZ, Biome.PLAINS);
}
}
}
@ -195,35 +320,32 @@ public class BukkitPlotGenerator extends ChunkGenerator implements GeneratorWrap
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();
LOGGER.error("Error attempting to load world into PlotSquared.", e);
}
// Return the result data
return result.getChunkData();
}
private void generate(BlockVector2 loc, World world, ZeroedDelegateScopedQueueCoordinator result) {
private void generate(BlockVector2 loc, String world, ZeroedDelegateScopedQueueCoordinator result, boolean biomes) {
// Load if improperly loaded
if (!this.loaded) {
checkLoaded(world);
synchronized (this) {
PlotSquared.get().loadWorld(world, this);
}
}
// 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();
LOGGER.error("Error attempting to generate chunk.", e);
}
ChunkManager.postProcessChunk(loc, result);
}
@ -277,4 +399,51 @@ public class BukkitPlotGenerator extends ChunkGenerator implements GeneratorWrap
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;
}
/**
* 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

@ -27,6 +27,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;
@ -49,6 +50,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();
@ -60,7 +66,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;