SPIGOT-5880, SPIGOT-5567: New ChunkGenerator API

## **Current API**
The current world generation API is very old and limited when you want to make more complex world generation. Resulting in some hard to fix bugs such as that you cannot modify blocks outside the chunk in the BlockPopulator (which should and was per the docs possible), or strange behavior such as SPIGOT-5880.

## **New API**
With the new API, the generation is more separate in multiple methods and is more in line with Vanilla chunk generation. The new API is designed to as future proof as possible. If for example a new generation step is added it can easily also be added as a step in API by simply creating the method for it. On the other side if a generation step gets removed, the method can easily be called after another, which is the case with surface and bedrock. The new API and changes are also fully backwards compatible with old chunk generators.

### **Changes in the new api**
**Extra generation steps:**
Noise, surface, bedrock and caves are added as steps. With those generation steps three extra methods for Vanilla generation are also added. Those new methods provide the ChunkData instead of returning one. The reason for this is, that the ChunkData is now backed by a ChunkAccess. With this, each step has the information of the step before and the Vanilla information (if chosen by setting a 'should' method to true). The old method is deprecated.

**New class BiomeProvider**
The BiomeProvider acts as Biome source and wrapper for the NMS class WorldChunkManager. With this the underlying Vanilla ChunkGeneration knows which Biome to use for the structure and decoration generation. (Fixes: SPIGOT-5880). Although the List of Biomes which is required in BiomeProvider, is currently not much in use in Vanilla, I decided to add it to future proof the API when it may be required in later versions of Minecraft.
The BiomeProvider is also separated from the ChunkGenerator for plugins which only want to change the biome map, such as single Biome worlds or if some biomes should be more present than others.

**Deprecated isParallelCapable**
Mojang has and is pushing to a more multi threaded chunk generation. This should also be the case for custom chunk generators. This is why the new API only supports multi threaded generation. This does not affect the old API, which is still checking this.

**Base height method added**
This method was added to also bring the Minecraft generator and Bukkit generator more in line. With this it is possible to return the max height of a location (before decorations). This is useful to let most structures know were to place them. This fixes SPIGOT-5567. (This fixes not all structures placement, desert pyramids for example are still way up at y-level 64, This however is more a vanilla bug and should be fixed at Mojangs end).

**WorldInfo Class**
The World object was swapped for a WorldInfo object. This is because many methods of the World object won't work during world generation and would mostly likely result in a deadlock. It contains any information a plugin should need to identify the world.

**BlockPopulator Changes**
Instead of directly manipulating a chunk, changes are now made to a new class LimitedRegion, this class provides methods to populated the chunk and its surrounding area. The wrapping is done so that the population can be moved into the place where Minecraft generates decorations. Where there is no chunk to access yet. By moving it into this place the generation is now async and the surrounding area of the chunk can also be used.

For common methods between the World and LimitedRegion a RegionAccessor was added.

By: DerFrZocker <derrieple@gmail.com>
This commit is contained in:
Bukkit/Spigot
2021-08-15 08:08:11 +10:00
parent 511a9aba49
commit c255eb3333
11 changed files with 881 additions and 131 deletions

View File

@@ -0,0 +1,52 @@
package org.bukkit.generator;
import java.util.List;
import org.bukkit.block.Biome;
import org.jetbrains.annotations.NotNull;
/**
* Class for providing biomes.
*/
public abstract class BiomeProvider {
/**
* Return the Biome which should be present at the provided location.
* <p>
* Notes:
* <p>
* This method <b>must</b> be completely thread safe and able to handle
* multiple concurrent callers.
* <p>
* This method should only return biomes which are present in the list
* returned by {@link #getBiomes(WorldInfo)}
* <p>
* This method should <b>never</b> return {@link Biome#CUSTOM}.
*
* @param worldInfo The world info of the world the biome will be used for
* @param x The X-coordinate from world origin
* @param y The Y-coordinate from world origin
* @param z The Z-coordinate from world origin
* @return Biome for the given location
*/
@NotNull
public abstract Biome getBiome(@NotNull WorldInfo worldInfo, int x, int y, int z);
/**
* Returns a list with every biome the {@link BiomeProvider} will use for
* the given world.
* <p>
* Notes:
* <p>
* This method only gets called once, when the world is loaded. Returning
* another list or modifying the values from the initial returned list later
* one, are not respected.
* <p>
* This method should <b>never</b> return a list which contains
* {@link Biome#CUSTOM}.
*
* @param worldInfo The world info of the world the list will be used for
* @return A list with every biome the {@link BiomeProvider} uses
*/
@NotNull
public abstract List<Biome> getBiomes(@NotNull WorldInfo worldInfo);
}

View File

@@ -25,6 +25,39 @@ public abstract class BlockPopulator {
* @param world The world to generate in
* @param random The random generator to use
* @param source The chunk to generate for
* @deprecated Use {@link #populate(WorldInfo, Random, int, int, LimitedRegion)}
*/
public abstract void populate(@NotNull World world, @NotNull Random random, @NotNull Chunk source);
@Deprecated
public void populate(@NotNull World world, @NotNull Random random, @NotNull Chunk source) {
}
/**
* Populates an area of blocks at or around the given chunk.
* <p>
* Notes:
* <p>
* This method should <b>never</b> attempt to get the Chunk at the passed
* coordinates, as doing so may cause an infinite loop
* <p>
* This method should <b>never</b> modify a {@link LimitedRegion} at a later
* point of time.
* <p>
* This method <b>must</b> be completely thread safe and able to handle
* multiple concurrent callers.
* <p>
* No physics are applied, whether or not it is set to true in
* {@link org.bukkit.block.BlockState#update(boolean, boolean)}
* <p>
* <b>Only</b> use the {@link org.bukkit.block.BlockState} returned by
* {@link LimitedRegion},
* <b>never</b> use methods from a {@link World} to modify the chunk.
*
* @param worldInfo The world info of the world to generate in
* @param random The random generator to use
* @param x The X-coordinate of the chunk
* @param z The Z-coordinate of the chunk
* @param limitedRegion The chunk region to populate
*/
public void populate(@NotNull WorldInfo worldInfo, @NotNull Random random, int x, int z, @NotNull LimitedRegion limitedRegion) {
}
}

View File

@@ -4,6 +4,7 @@ import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import org.bukkit.Bukkit;
import org.bukkit.HeightMap;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
@@ -19,28 +20,205 @@ import org.jetbrains.annotations.Nullable;
* chunk. For example, the nether chunk generator should shape netherrack and
* soulsand.
*
* By default only one thread will call
* {@link #generateChunkData(org.bukkit.World, java.util.Random, int, int, org.bukkit.generator.ChunkGenerator.BiomeGrid)}
* at a time, although this may not necessarily be the main server thread.
* A chunk is generated in multiple steps, those steps are always in the same
* order. Between those steps however an unlimited time may pass. This means, a
* chunk may generated until the surface step and continue with the bedrock step
* after one or multiple server restarts or even after multiple Minecraft
* versions.
*
* If your generator is capable of fully asynchronous generation, then
* {@link #isParallelCapable()} should be overridden accordingly to allow
* multiple concurrent callers.
* The order of generation is as follows
* <ol>
* <li>{@link #generateNoise(WorldInfo, Random, int, int, ChunkData)}</li>
* <li>{@link #generateSurface(WorldInfo, Random, int, int, ChunkData)}</li>
* <li>{@link #generateBedrock(WorldInfo, Random, int, int, ChunkData)}</li>
* <li>{@link #generateCaves(WorldInfo, Random, int, int, ChunkData)}</li>
* </ol>
*
* Every method listed above as well as
* {@link #getBaseHeight(WorldInfo, Random, int, int, HeightMap)}
* <b>must</b> be completely thread safe and able to handle multiple concurrent
* callers.
*
* Some aspects of world generation can be delegated to the Vanilla generator.
* The methods {@link ChunkGenerator#shouldGenerateCaves()}, {@link ChunkGenerator#shouldGenerateDecorations()},
* {@link ChunkGenerator#shouldGenerateMobs()} and {@link ChunkGenerator#shouldGenerateStructures()} can be
* overridden to enable this.
* The following methods can be overridden to enable this:
* <ul>
* <li>{@link ChunkGenerator#shouldGenerateNoise()}</li>
* <li>{@link ChunkGenerator#shouldGenerateSurface()}</li>
* <li>{@link ChunkGenerator#shouldGenerateBedrock()}</li>
* <li>{@link ChunkGenerator#shouldGenerateCaves()}</li>
* <li>{@link ChunkGenerator#shouldGenerateDecorations()}</li>
* <li>{@link ChunkGenerator#shouldGenerateMobs()}</li>
* <li>{@link ChunkGenerator#shouldGenerateStructures()}</li>
* </ul>
*/
public abstract class ChunkGenerator {
/**
* Shapes the Chunk noise for the given coordinates.
* <p>
* Notes:
* <p>
* This method should <b>never</b> attempt to get the Chunk at the passed
* coordinates, as doing so may cause an infinite loop.
* <p>
* This method should <b>never</b> modify the {@link ChunkData} at a later
* point of time.
* <p>
* The Y-coordinate range should <b>never</b> be hardcoded, to get the
* Y-coordinate range use the methods {@link ChunkData#getMinHeight()} and
* {@link ChunkData#getMaxHeight()}.
* <p>
* If {@link #shouldGenerateNoise()} is set to true, the given
* {@link ChunkData} contains already the Vanilla noise generation.
*
* @param worldInfo The world info of the world this chunk will be used for
* @param random The random generator to use
* @param x The X-coordinate of the chunk
* @param z The Z-coordinate of the chunk
* @param chunkData To modify
*/
public void generateNoise(@NotNull WorldInfo worldInfo, @NotNull Random random, int x, int z, @NotNull ChunkData chunkData) {
}
/**
* Shapes the Chunk surface for the given coordinates.
* <p>
* Notes:
* <p>
* This method should <b>never</b> attempt to get the Chunk at the passed
* coordinates, as doing so may cause an infinite loop.
* <p>
* This method should <b>never</b> modify the {@link ChunkData} at a later
* point of time.
* <p>
* The Y-coordinate range should <b>never</b> be hardcoded, to get the
* Y-coordinate range use the methods {@link ChunkData#getMinHeight()} and
* {@link ChunkData#getMaxHeight()}.
* <p>
* If {@link #shouldGenerateSurface()} is set to true, the given
* {@link ChunkData} contains already the Vanilla surface generation.
*
* @param worldInfo The world info of the world this chunk will be used for
* @param random The random generator to use
* @param x The X-coordinate of the chunk
* @param z The Z-coordinate of the chunk
* @param chunkData To modify
*/
public void generateSurface(@NotNull WorldInfo worldInfo, @NotNull Random random, int x, int z, @NotNull ChunkData chunkData) {
}
/**
* Shapes the Chunk bedrock layer for the given coordinates.
* <p>
* Notes:
* <p>
* This method should <b>never</b> attempt to get the Chunk at the passed
* coordinates, as doing so may cause an infinite loop.
* <p>
* This method should <b>never</b> modify the {@link ChunkData} at a later
* point of time.
* <p>
* The Y-coordinate range should <b>never</b> be hardcoded, to get the
* Y-coordinate range use the methods {@link ChunkData#getMinHeight()} and
* {@link ChunkData#getMaxHeight()}.
* <p>
* If {@link #shouldGenerateBedrock()} is set to true, the given
* {@link ChunkData} contains already the Vanilla bedrock generation.
*
* @param worldInfo The world info of the world this chunk will be used for
* @param random The random generator to use
* @param x The X-coordinate of the chunk
* @param z The Z-coordinate of the chunk
* @param chunkData To modify
*/
public void generateBedrock(@NotNull WorldInfo worldInfo, @NotNull Random random, int x, int z, @NotNull ChunkData chunkData) {
}
/**
* Shapes the Chunk caves for the given coordinates.
* <p>
* Notes:
* <p>
* This method should <b>never</b> attempt to get the Chunk at the passed
* coordinates, as doing so may cause an infinite loop.
* <p>
* This method should <b>never</b> modify the {@link ChunkData} at a later
* point of time.
* <p>
* The Y-coordinate range should <b>never</b> be hardcoded, to get the
* Y-coordinate range use the methods {@link ChunkData#getMinHeight()} and
* {@link ChunkData#getMaxHeight()}.
* <p>
* If {@link #shouldGenerateCaves()} is set to true, the given
* {@link ChunkData} contains already the Vanilla cave generation.
*
* @param worldInfo The world info of the world this chunk will be used for
* @param random The random generator to use
* @param x The X-coordinate of the chunk
* @param z The Z-coordinate of the chunk
* @param chunkData To modify
*/
public void generateCaves(@NotNull WorldInfo worldInfo, @NotNull Random random, int x, int z, @NotNull ChunkData chunkData) {
}
/**
* Gets called when no {@link BiomeProvider} is set in
* {@link org.bukkit.WorldCreator} or via the server configuration files. It
* is therefore possible that one plugin can provide the Biomes and another
* one the generation.
* <p>
* Notes:
* <p>
* If <code>null</code> is returned, than Vanilla biomes are used.
* <p>
* This method only gets called once when the world is loaded. Returning
* another {@link BiomeProvider} later one is not respected.
*
* @param worldInfo The world info of the world the biome provider will be
* used for
* @return BiomeProvider to use to fill the biomes of a chunk
*/
@Nullable
public BiomeProvider getDefaultBiomeProvider(@NotNull WorldInfo worldInfo) {
return null;
}
/**
* This method is similar to
* {@link World#getHighestBlockAt(int, int, HeightMap)}. With the difference
* being, that the highest y coordinate should be the block before any
* surface, bedrock, caves or decoration is applied. Or in other words the
* highest block when only the noise is present at the chunk.
* <p>
* Notes:
* <p>
* When this method is not overridden, the Vanilla base height is used.
* <p>
* This method should <b>never</b> attempt to get the Chunk at the passed
* coordinates, or use the method
* {@link World#getHighestBlockAt(int, int, HeightMap)}, as doing so may
* cause an infinite loop.
*
* @param worldInfo The world info of the world this chunk will be used for
* @param random The random generator to use
* @param x The X-coordinate from world origin
* @param z The Z-coordinate from world origin
* @param heightMap From the highest block should be get
* @return The y coordinate of the highest block at the given location
*/
public int getBaseHeight(@NotNull WorldInfo worldInfo, @NotNull Random random, int x, int z, @NotNull HeightMap heightMap) {
throw new UnsupportedOperationException("Not implemented");
}
/**
* Interface to biome section for chunk to be generated: initialized with
* default values for world type and seed.
* <p>
* Custom generator is free to access and tailor values during
* generateBlockSections() or generateExtBlockSections().
* @deprecated Biomes are now set with {@link BiomeProvider}
*/
@Deprecated
public interface BiomeGrid {
/**
@@ -111,8 +289,10 @@ public abstract class ChunkGenerator {
* generator
* @return ChunkData containing the types for each block created by this
* generator
* @deprecated The generation is now split up
*/
@NotNull
@Deprecated
public ChunkData generateChunkData(@NotNull World world, @NotNull Random random, int x, int z, @NotNull BiomeGrid biome) {
throw new UnsupportedOperationException("Custom generator " + getClass().getName() + " is missing required method generateChunkData");
}
@@ -121,8 +301,10 @@ public abstract class ChunkGenerator {
* Create a ChunkData for a world.
* @param world the world the ChunkData is for
* @return a new ChunkData for world
* @deprecated {@link ChunkData} are now directly provided
*/
@NotNull
@Deprecated
protected final ChunkData createChunkData(@NotNull World world) {
return Bukkit.getServer().createChunkData(world);
}
@@ -182,14 +364,56 @@ public abstract class ChunkGenerator {
* See {@link ChunkGenerator} for more information.
*
* @return parallel capable status
* @deprecated the chunk generation code should be thread safe
*/
@Deprecated
public boolean isParallelCapable() {
return false;
}
/**
* Gets if the server should generate Vanilla caves after this
* ChunkGenerator.
* Gets if the server should generate Vanilla noise.
* <p>
* The Vanilla noise is generated <b>before</b>
* {@link #generateNoise(WorldInfo, Random, int, int, ChunkData)} is called.
*
* @return true if the server should generate Vanilla noise
*/
public boolean shouldGenerateNoise() {
return false;
}
/**
* Gets if the server should generate Vanilla surface.
* <p>
* The Vanilla surface is generated <b>before</b>
* {@link #generateSurface(WorldInfo, Random, int, int, ChunkData)} is
* called.
*
* @return true if the server should generate Vanilla surface
*/
public boolean shouldGenerateSurface() {
return false;
}
/**
* Gets if the server should generate Vanilla bedrock.
* <p>
* The Vanilla bedrock is generated <b>before</b>
* {@link #generateBedrock(WorldInfo, Random, int, int, ChunkData)} is
* called.
*
* @return true if the server should generate Vanilla bedrock
*/
public boolean shouldGenerateBedrock() {
return false;
}
/**
* Gets if the server should generate Vanilla caves.
* <p>
* The Vanilla caves are generated <b>before</b>
* {@link #generateCaves(WorldInfo, Random, int, int, ChunkData)} is called.
*
* @return true if the server should generate Vanilla caves
*/
@@ -200,6 +424,9 @@ public abstract class ChunkGenerator {
/**
* Gets if the server should generate Vanilla decorations after this
* ChunkGenerator.
* <p>
* The Vanilla decoration are generated <b>before</b> any
* {@link BlockPopulator} are called.
*
* @return true if the server should generate Vanilla decorations
*/
@@ -232,8 +459,11 @@ public abstract class ChunkGenerator {
*/
public static interface ChunkData {
/**
* Get the minimum height for the chunk.
*
* Get the minimum height for this ChunkData.
* <p>
* It is not guaranteed that this method will return the same value as
* {@link World#getMinHeight()}.
* <p>
* Setting blocks below this height will do nothing.
*
* @return the minimum height
@@ -241,14 +471,29 @@ public abstract class ChunkGenerator {
public int getMinHeight();
/**
* Get the maximum height for the chunk.
*
* Get the maximum height for this ChunkData.
* <p>
* It is not guaranteed that this method will return the same value as
* {@link World#getMaxHeight()}.
* <p>
* Setting blocks at or above this height will do nothing.
*
* @return the maximum height
*/
public int getMaxHeight();
/**
* Get the biome at x, y, z within chunk being generated
*
* @param x the x location in the chunk from 0-15 inclusive
* @param y the y location in the chunk from minimum (inclusive) -
* maxHeight (exclusive)
* @param z the z location in the chunk from 0-15 inclusive
* @return Biome value
*/
@NotNull
public Biome getBiome(int x, int y, int z);
/**
* Set the block at x,y,z in the chunk data to material.
*

View File

@@ -0,0 +1,45 @@
package org.bukkit.generator;
import org.bukkit.Location;
import org.bukkit.RegionAccessor;
import org.jetbrains.annotations.NotNull;
/**
* A limited region is used in world generation for features which are
* going over a chunk. For example, trees or ores.
*
* Use {@link #getBuffer()} to know how much you can go beyond the central
* chunk. The buffer zone may or may not be already populated.
*
* The coordinates are <b>absolute</b> from the world origin.
*/
public interface LimitedRegion extends RegionAccessor {
/**
* Gets the buffer around the central chunk which is accessible.
* The returned value is in normal world coordinate scale.
* <p>
* For example: If the method returns 16 you have a working area of 48x48.
*
* @return The buffer in X and Z direction
*/
int getBuffer();
/**
* Checks if the given {@link Location} is in the region.
*
* @param location the location to check
* @return true if the location is in the region, otherwise false.
*/
boolean isInRegion(@NotNull Location location);
/**
* Checks if the given coordinates are in the region.
*
* @param x X-coordinate to check
* @param y Y-coordinate to check
* @param z Z-coordinate to check
* @return true if the coordinates are in the region, otherwise false.
*/
boolean isInRegion(int x, int y, int z);
}

View File

@@ -0,0 +1,60 @@
package org.bukkit.generator;
import java.util.UUID;
import org.bukkit.World;
import org.jetbrains.annotations.NotNull;
/**
* Holds various information of a World
*/
public interface WorldInfo {
/**
* Gets the unique name of this world
*
* @return Name of this world
*/
@NotNull
String getName();
/**
* Gets the Unique ID of this world
*
* @return Unique ID of this world.
*/
@NotNull
UUID getUID();
/**
* Gets the {@link World.Environment} type of this world
*
* @return This worlds Environment type
*/
@NotNull
World.Environment getEnvironment();
/**
* Gets the Seed for this world.
*
* @return This worlds Seed
*/
long getSeed();
/**
* Gets the minimum height of this world.
* <p>
* If the min height is 0, there are only blocks from y=0.
*
* @return Minimum height of the world
*/
int getMinHeight();
/**
* Gets the maximum height of this world.
* <p>
* If the max height is 100, there are only blocks from y=0 to y=99.
*
* @return Maximum height of the world
*/
int getMaxHeight();
}