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

@ -786,7 +786,9 @@ public class PlotSquared {
if (world.equals("CheckingPlotSquaredGenerator")) {
return;
}
this.getPlotAreaManager().addWorld(world);
if (!this.getPlotAreaManager().addWorld(world)) {
return;
}
Set<String> worlds;
if (this.worldConfiguration.contains("worlds")) {
worlds = this.worldConfiguration.getConfigurationSection("worlds").getKeys(false);

View File

@ -167,7 +167,7 @@ public class AugmentedUtils {
Location.at(world, blockX, area.getMinGenHeight(), blockZ),
Location.at(world, blockX + 15, area.getMaxGenHeight(), blockZ + 15)
);
generator.generateChunk(scoped, area);
generator.generateChunk(scoped, area, true);
generator.populateChunk(scoped, area);
}
if (enqueue) {

View File

@ -42,6 +42,8 @@ import com.sk89q.worldedit.world.block.BlockTypes;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import java.util.EnumSet;
public class HybridGen extends IndependentPlotGenerator {
private static final CuboidRegion CHUNK = new CuboidRegion(BlockVector3.ZERO, BlockVector3.at(15, 396, 15));
@ -64,11 +66,11 @@ public class HybridGen extends IndependentPlotGenerator {
short relativeZ,
int x,
int z,
boolean isRoad,
boolean isPopulating
EnumSet<SchematicFeature> features
) {
int minY; // Math.min(world.PLOT_HEIGHT, world.ROAD_HEIGHT);
if ((isRoad && Settings.Schematics.PASTE_ROAD_ON_TOP) || (!isRoad && Settings.Schematics.PASTE_ON_TOP)) {
if ((features.contains(SchematicFeature.ROAD) && Settings.Schematics.PASTE_ROAD_ON_TOP)
|| (!features.contains(SchematicFeature.ROAD) && Settings.Schematics.PASTE_ON_TOP)) {
minY = world.SCHEM_Y;
} else {
minY = world.getMinBuildHeight();
@ -77,12 +79,15 @@ public class HybridGen extends IndependentPlotGenerator {
if (blocks != null) {
for (int y = 0; y < blocks.length; y++) {
if (blocks[y] != null) {
if (!isPopulating || blocks[y].hasNbtData()) {
if (!features.contains(SchematicFeature.POPULATING) || blocks[y].hasNbtData()) {
result.setBlock(x, minY + y, z, blocks[y]);
}
}
}
}
if (!features.contains(SchematicFeature.BIOMES)) {
return;
}
BiomeType biome = world.G_SCH_B.get(MathMan.pair(relativeX, relativeZ));
if (biome != null) {
result.setBiome(x, z, biome);
@ -90,13 +95,15 @@ public class HybridGen extends IndependentPlotGenerator {
}
@Override
public void generateChunk(@NonNull ZeroedDelegateScopedQueueCoordinator result, @NonNull PlotArea settings) {
public void generateChunk(@NonNull ZeroedDelegateScopedQueueCoordinator result, @NonNull PlotArea settings, boolean biomes) {
Preconditions.checkNotNull(result, "result cannot be null");
Preconditions.checkNotNull(settings, "settings cannot be null");
HybridPlotWorld hybridPlotWorld = (HybridPlotWorld) settings;
// Biome
result.fillBiome(hybridPlotWorld.getPlotBiome());
if (biomes) {
result.fillBiome(hybridPlotWorld.getPlotBiome());
}
// Bedrock
if (hybridPlotWorld.PLOT_BEDROCK) {
for (short x = 0; x < 16; x++) {
@ -105,26 +112,25 @@ public class HybridGen extends IndependentPlotGenerator {
}
}
}
EnumSet<SchematicFeature> roadFeatures = EnumSet.of(SchematicFeature.ROAD);
EnumSet<SchematicFeature> plotFeatures = EnumSet.noneOf(SchematicFeature.class);
if (biomes) {
roadFeatures.add(SchematicFeature.BIOMES);
plotFeatures.add(SchematicFeature.BIOMES);
}
// Coords
Location min = result.getMin();
int bx = min.getX() - hybridPlotWorld.ROAD_OFFSET_X;
int bz = min.getZ() - hybridPlotWorld.ROAD_OFFSET_Z;
// The relative X-coordinate (within the plot) of the minimum X coordinate
// contained in the scoped queue
short relativeOffsetX;
if (bx < 0) {
relativeOffsetX = (short) (hybridPlotWorld.SIZE + (bx % hybridPlotWorld.SIZE));
} else {
relativeOffsetX = (short) (bx % hybridPlotWorld.SIZE);
}
short relativeOffsetX = (short) Math.floorMod(bx, hybridPlotWorld.SIZE);
// The relative Z-coordinate (within the plot) of the minimum Z coordinate
// contained in the scoped queue
short relativeOffsetZ;
if (bz < 0) {
relativeOffsetZ = (short) (hybridPlotWorld.SIZE + (bz % hybridPlotWorld.SIZE));
} else {
relativeOffsetZ = (short) (bz % hybridPlotWorld.SIZE);
}
short relativeOffsetZ = (short) Math.floorMod(bz, hybridPlotWorld.SIZE);
// The X-coordinate of a given X coordinate, relative to the
// plot (Counting from the corner with the least positive
// coordinates)
@ -171,7 +177,7 @@ public class HybridGen extends IndependentPlotGenerator {
result.setBlock(x, y, z, hybridPlotWorld.ROAD_BLOCK.toPattern());
}
if (hybridPlotWorld.ROAD_SCHEMATIC_ENABLED) {
placeSchem(hybridPlotWorld, result, relativeX[x], relativeZ[z], x, z, true, false);
placeSchem(hybridPlotWorld, result, relativeX[x], relativeZ[z], x, z, roadFeatures);
}
}
} else if (insideWallX[x]) {
@ -182,7 +188,7 @@ public class HybridGen extends IndependentPlotGenerator {
result.setBlock(x, y, z, hybridPlotWorld.ROAD_BLOCK.toPattern());
}
if (hybridPlotWorld.ROAD_SCHEMATIC_ENABLED) {
placeSchem(hybridPlotWorld, result, relativeX[x], relativeZ[z], x, z, true, false);
placeSchem(hybridPlotWorld, result, relativeX[x], relativeZ[z], x, z, roadFeatures);
}
} else {
// wall
@ -194,7 +200,7 @@ public class HybridGen extends IndependentPlotGenerator {
result.setBlock(x, hybridPlotWorld.WALL_HEIGHT + 1, z, hybridPlotWorld.WALL_BLOCK.toPattern());
}
} else {
placeSchem(hybridPlotWorld, result, relativeX[x], relativeZ[z], x, z, true, false);
placeSchem(hybridPlotWorld, result, relativeX[x], relativeZ[z], x, z, roadFeatures);
}
}
}
@ -206,7 +212,7 @@ public class HybridGen extends IndependentPlotGenerator {
result.setBlock(x, y, z, hybridPlotWorld.ROAD_BLOCK.toPattern());
}
if (hybridPlotWorld.ROAD_SCHEMATIC_ENABLED) {
placeSchem(hybridPlotWorld, result, relativeX[x], relativeZ[z], x, z, true, false);
placeSchem(hybridPlotWorld, result, relativeX[x], relativeZ[z], x, z, roadFeatures);
}
} else if (insideWallZ[z]) {
// wall
@ -218,7 +224,7 @@ public class HybridGen extends IndependentPlotGenerator {
result.setBlock(x, hybridPlotWorld.WALL_HEIGHT + 1, z, hybridPlotWorld.WALL_BLOCK.toPattern());
}
} else {
placeSchem(hybridPlotWorld, result, relativeX[x], relativeZ[z], x, z, true, false);
placeSchem(hybridPlotWorld, result, relativeX[x], relativeZ[z], x, z, roadFeatures);
}
} else {
// plot
@ -227,7 +233,7 @@ public class HybridGen extends IndependentPlotGenerator {
}
result.setBlock(x, hybridPlotWorld.PLOT_HEIGHT, z, hybridPlotWorld.TOP_BLOCK.toPattern());
if (hybridPlotWorld.PLOT_SCHEMATIC) {
placeSchem(hybridPlotWorld, result, relativeX[x], relativeZ[z], x, z, false, false);
placeSchem(hybridPlotWorld, result, relativeX[x], relativeZ[z], x, z, plotFeatures);
}
}
}
@ -236,31 +242,26 @@ public class HybridGen extends IndependentPlotGenerator {
}
@Override
public boolean populateChunk(final ZeroedDelegateScopedQueueCoordinator result, final PlotArea settings) {
public void populateChunk(final ZeroedDelegateScopedQueueCoordinator result, final PlotArea settings) {
HybridPlotWorld hybridPlotWorld = (HybridPlotWorld) settings;
if (!hybridPlotWorld.populationNeeded()) {
return false;
return;
}
EnumSet<SchematicFeature> roadFeatures = EnumSet.of(SchematicFeature.POPULATING, SchematicFeature.ROAD);
EnumSet<SchematicFeature> plotFeatures = EnumSet.of(SchematicFeature.POPULATING);
// Coords
Location min = result.getMin();
int bx = min.getX() - hybridPlotWorld.ROAD_OFFSET_X;
int bz = min.getZ() - hybridPlotWorld.ROAD_OFFSET_Z;
// The relative X-coordinate (within the plot) of the minimum X coordinate
// contained in the scoped queue
short relativeOffsetX;
if (bx < 0) {
relativeOffsetX = (short) (hybridPlotWorld.SIZE + (bx % hybridPlotWorld.SIZE));
} else {
relativeOffsetX = (short) (bx % hybridPlotWorld.SIZE);
}
short relativeOffsetX = (short) Math.floorMod(bx, hybridPlotWorld.SIZE);
// The relative Z-coordinate (within the plot) of the minimum Z coordinate
// contained in the scoped queue
short relativeOffsetZ;
if (bz < 0) {
relativeOffsetZ = (short) (hybridPlotWorld.SIZE + (bz % hybridPlotWorld.SIZE));
} else {
relativeOffsetZ = (short) (bz % hybridPlotWorld.SIZE);
}
short relativeOffsetZ = (short) Math.floorMod(bz, hybridPlotWorld.SIZE);
boolean allRoad = true;
boolean overlap = false;
@ -313,17 +314,17 @@ public class HybridGen extends IndependentPlotGenerator {
if (insideRoadX[x] || insideWallX[x]) {
if (hybridPlotWorld.ROAD_SCHEMATIC_ENABLED) {
for (short z = 0; z < 16; z++) {
placeSchem(hybridPlotWorld, result, relativeX[x], relativeZ[z], x, z, true, true);
placeSchem(hybridPlotWorld, result, relativeX[x], relativeZ[z], x, z, roadFeatures);
}
}
} else {
for (short z = 0; z < 16; z++) {
if (insideRoadZ[z] || insideWallZ[z]) {
if (hybridPlotWorld.ROAD_SCHEMATIC_ENABLED) {
placeSchem(hybridPlotWorld, result, relativeX[x], relativeZ[z], x, z, true, true);
placeSchem(hybridPlotWorld, result, relativeX[x], relativeZ[z], x, z, roadFeatures);
}
} else if (hybridPlotWorld.PLOT_SCHEMATIC) {
placeSchem(hybridPlotWorld, result, relativeX[x], relativeZ[z], x, z, false, true);
placeSchem(hybridPlotWorld, result, relativeX[x], relativeZ[z], x, z, plotFeatures);
}
}
}
@ -364,7 +365,7 @@ public class HybridGen extends IndependentPlotGenerator {
}
}
}
return true;
return;
}
@Override
@ -377,6 +378,27 @@ public class HybridGen extends IndependentPlotGenerator {
// All initialization is done in the PlotArea class
}
@Override
public BiomeType getBiome(final PlotArea settings, final int worldX, final int worldY, final int worldZ) {
HybridPlotWorld hybridPlotWorld = (HybridPlotWorld) settings;
if (!hybridPlotWorld.PLOT_SCHEMATIC && !hybridPlotWorld.ROAD_SCHEMATIC_ENABLED) {
return hybridPlotWorld.getPlotBiome();
}
int relativeX = worldX;
int relativeZ = worldZ;
if (hybridPlotWorld.ROAD_OFFSET_X != 0) {
relativeX -= hybridPlotWorld.ROAD_OFFSET_X;
}
if (hybridPlotWorld.ROAD_OFFSET_Z != 0) {
relativeZ -= hybridPlotWorld.ROAD_OFFSET_Z;
}
int size = hybridPlotWorld.PLOT_WIDTH + hybridPlotWorld.ROAD_WIDTH;
relativeX = Math.floorMod(relativeX, size);
relativeZ = Math.floorMod(relativeZ, size);
BiomeType biome = hybridPlotWorld.G_SCH_B.get(MathMan.pair((short) relativeX, (short) relativeZ));
return biome == null ? hybridPlotWorld.getPlotBiome() : biome;
}
/**
* Wrapper to allow a WorldEdit {@link Entity} to effectively have a mutable location as the location in its NBT should be changed
* when set to the world.
@ -431,4 +453,10 @@ public class HybridGen extends IndependentPlotGenerator {
}
private enum SchematicFeature {
BIOMES,
ROAD,
POPULATING
}
}

View File

@ -163,7 +163,7 @@ public class HybridUtils {
int relChunkZ = chunkPos.getZ() - cbz;
oldBlockQueue.setOffsetX(relChunkX << 4);
oldBlockQueue.setOffsetZ(relChunkZ << 4);
hpw.getGenerator().generateChunk(oldBlockQueue, hpw);
hpw.getGenerator().generateChunk(oldBlockQueue, hpw, false);
});
final BlockState[][][] oldBlocks = oldBlockQueue.getBlockStates();

View File

@ -23,6 +23,7 @@ import com.plotsquared.core.plot.PlotArea;
import com.plotsquared.core.plot.PlotId;
import com.plotsquared.core.queue.ZeroedDelegateScopedQueueCoordinator;
import com.plotsquared.core.setup.PlotAreaBuilder;
import com.sk89q.worldedit.world.biome.BiomeType;
import org.checkerframework.checker.nullness.qual.NonNull;
/**
@ -42,14 +43,21 @@ public abstract class IndependentPlotGenerator {
/**
* Generate chunk block data
*
* @param result queue
* @param result Queue to write to
* @param settings PlotArea (settings)
* @param biomes If biomes should be generated
* @since TODO
*/
public abstract void generateChunk(ZeroedDelegateScopedQueueCoordinator result, PlotArea settings);
public abstract void generateChunk(ZeroedDelegateScopedQueueCoordinator result, PlotArea settings, boolean biomes);
public boolean populateChunk(ZeroedDelegateScopedQueueCoordinator result, PlotArea setting) {
return false;
}
/**
* Populate a chunk-queue with tile entities, entities, etc.
*
* @param result Queue to write to
* @param setting PlotArea (settings)
* @since TODO
*/
public void populateChunk(ZeroedDelegateScopedQueueCoordinator result, PlotArea setting) {}
/**
* Return a new PlotArea object.
@ -91,6 +99,18 @@ public abstract class IndependentPlotGenerator {
return (GeneratorWrapper<T>) PlotSquared.platform().wrapPlotGenerator(world, this);
}
/**
* Get the biome to be generated at a specific point
*
* @param settings PlotArea settings to provide biome
* @param x World x position
* @param y World y position
* @param z World z position
* @return Biome type to be generated
* @since TODO
*/
public abstract BiomeType getBiome(PlotArea settings, int x, int y, int z);
@Override
public String toString() {
return getName();

View File

@ -26,7 +26,9 @@ import com.plotsquared.core.plot.world.PlotAreaManager;
import com.plotsquared.core.plot.world.SinglePlotArea;
import com.plotsquared.core.plot.world.SinglePlotAreaManager;
import com.plotsquared.core.queue.ZeroedDelegateScopedQueueCoordinator;
import com.sk89q.worldedit.world.biome.BiomeType;
import com.sk89q.worldedit.world.biome.BiomeTypes;
import com.sk89q.worldedit.world.block.BlockState;
import com.sk89q.worldedit.world.block.BlockTypes;
import org.checkerframework.checker.nullness.qual.NonNull;
@ -38,6 +40,9 @@ public class SingleWorldGenerator extends IndependentPlotGenerator {
private static final Location dirt2 = Location.at("", 15, 2, 15);
private static final Location grass1 = Location.at("", 0, 3, 0);
private static final Location grass2 = Location.at("", 15, 3, 15);
private static final BlockState BEDROCK = BlockTypes.BEDROCK.getDefaultState();
private static final BlockState DIRT = BlockTypes.DIRT.getDefaultState();
private static final BlockState GRASS_BLOCK = BlockTypes.GRASS_BLOCK.getDefaultState();
private final PlotAreaManager plotAreaManager;
@ -52,19 +57,21 @@ public class SingleWorldGenerator extends IndependentPlotGenerator {
}
@Override
public void generateChunk(ZeroedDelegateScopedQueueCoordinator result, PlotArea settings) {
public void generateChunk(ZeroedDelegateScopedQueueCoordinator result, PlotArea settings, boolean biomes) {
SinglePlotArea area = (SinglePlotArea) settings;
if (area.VOID) {
Location min = result.getMin();
if (min.getX() == 0 && min.getZ() == 0) {
result.setBlock(0, 0, 0, BlockTypes.BEDROCK.getDefaultState());
result.setBlock(0, 0, 0, BEDROCK);
}
} else {
result.setCuboid(bedrock1, bedrock2, BlockTypes.BEDROCK.getDefaultState());
result.setCuboid(dirt1, dirt2, BlockTypes.DIRT.getDefaultState());
result.setCuboid(grass1, grass2, BlockTypes.GRASS_BLOCK.getDefaultState());
result.setCuboid(bedrock1, bedrock2, BEDROCK);
result.setCuboid(dirt1, dirt2, DIRT);
result.setCuboid(grass1, grass2, GRASS_BLOCK);
}
if (biomes) {
result.fillBiome(BiomeTypes.PLAINS);
}
result.fillBiome(BiomeTypes.PLAINS);
}
@Override
@ -76,4 +83,9 @@ public class SingleWorldGenerator extends IndependentPlotGenerator {
public void initialize(PlotArea area) {
}
@Override
public BiomeType getBiome(final PlotArea settings, final int x, final int y, final int z) {
return BiomeTypes.PLAINS;
}
}

View File

@ -110,54 +110,62 @@ public final class BlockBucket implements ConfigurationSerializable {
if (isCompiled()) {
return;
}
this.compiled = true;
String string = this.input.toString();
if (string.isEmpty()) {
this.single = null;
this.pattern = null;
return;
}
// Convert legacy format
boolean legacy = false;
String[] blocksStr = string.split(",(?![^\\(\\[]*[\\]\\)])");
if (blocksStr.length == 1) {
try {
Matcher matcher = regex.matcher(string);
// Synchronized as BlockBuckets may require compilation asynchronously due to async chunk generation on Paper servers
synchronized (this) {
if (isCompiled()) {
return;
}
String string = this.input.toString();
if (string.isEmpty()) {
this.single = null;
this.pattern = null;
this.compiled = true;
return;
}
// Convert legacy format
boolean legacy = false;
String[] blocksStr = string.split(",(?![^\\(\\[]*[\\]\\)])");
if (blocksStr.length == 1) {
try {
Matcher matcher = regex.matcher(string);
if (matcher.find()) {
String chanceStr = matcher.group("chance");
String block = matcher.group("block");
//noinspection PointlessNullCheck
if (chanceStr != null && block != null && !MathMan.isInteger(block) && MathMan
.isInteger(chanceStr)) {
String namespace = matcher.group("namespace");
string = (namespace == null ? "" : namespace + ":") + block;
}
}
this.single = BlockUtil.get(string);
this.pattern = new BlockPattern(single);
this.compiled = true;
return;
} catch (Exception ignore) {
}
}
for (int i = 0; i < blocksStr.length; i++) {
String entry = blocksStr[i];
Matcher matcher = regex.matcher(entry);
if (matcher.find()) {
String chanceStr = matcher.group("chance");
String block = matcher.group("block");
//noinspection PointlessNullCheck
if (chanceStr != null && block != null && !MathMan.isInteger(block) && MathMan
.isInteger(chanceStr)) {
String namespace = matcher.group("namespace");
string = (namespace == null ? "" : namespace + ":") + block;
if (chanceStr != null && MathMan.isInteger(chanceStr)) {
String[] parts = entry.split(":");
parts = Arrays.copyOf(parts, parts.length - 1);
entry = chanceStr + "%" + StringMan.join(parts, ":");
blocksStr[i] = entry;
legacy = true;
}
}
this.single = BlockUtil.get(string);
this.pattern = new BlockPattern(single);
return;
} catch (Exception ignore) {
}
}
for (int i = 0; i < blocksStr.length; i++) {
String entry = blocksStr[i];
Matcher matcher = regex.matcher(entry);
if (matcher.find()) {
String chanceStr = matcher.group("chance");
//noinspection PointlessNullCheck
if (chanceStr != null && MathMan.isInteger(chanceStr)) {
String[] parts = entry.split(":");
parts = Arrays.copyOf(parts, parts.length - 1);
entry = chanceStr + "%" + StringMan.join(parts, ":");
blocksStr[i] = entry;
legacy = true;
}
if (legacy) {
string = StringMan.join(blocksStr, ",");
}
pattern = PatternUtil.parse(null, string);
this.compiled = true;
}
if (legacy) {
string = StringMan.join(blocksStr, ",");
}
pattern = PatternUtil.parse(null, string);
}
public boolean isCompiled() {

View File

@ -137,15 +137,16 @@ public class DefaultPlotAreaManager implements PlotAreaManager {
}
@Override
public void addWorld(final @NonNull String worldName) {
public boolean addWorld(final @NonNull String worldName) {
PlotWorld world = this.plotWorlds.get(worldName);
if (world != null) {
return;
return false;
}
// Create a new empty world. When a new area is added
// the world will be re-recreated with the correct type
world = new StandardPlotWorld(worldName, null);
this.plotWorlds.put(worldName, world);
return true;
}
@Override

View File

@ -110,8 +110,10 @@ public interface PlotAreaManager {
* Add a world
*
* @param worldName Name of the world to add
* @return {@code true} if successful, {@code false} if world already existed
* @since TODO
*/
void addWorld(@NonNull String worldName);
boolean addWorld(@NonNull String worldName);
/**
* Remove a world

View File

@ -172,8 +172,8 @@ public class SinglePlotAreaManager extends DefaultPlotAreaManager {
}
@Override
public void addWorld(final @NonNull String worldName) {
super.addWorld(worldName);
public boolean addWorld(final @NonNull String worldName) {
return super.addWorld(worldName);
}
@Override

View File

@ -38,7 +38,6 @@ public abstract class ChunkManager {
/**
* @since TODO
*/
@Deprecated(forRemoval = true, since = "6.9.0")
public static void setChunkInPlotArea(
RunnableVal<ZeroedDelegateScopedQueueCoordinator> force,
RunnableVal<ZeroedDelegateScopedQueueCoordinator> add,