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:
@@ -43,7 +43,9 @@ public class CraftBarrel extends CraftLootable<TileEntityBarrel> implements Barr
|
||||
|
||||
if (!open) {
|
||||
getTileEntity().setOpenFlag(blockData, true);
|
||||
getTileEntity().playOpenSound(blockData, SoundEffects.BARREL_OPEN);
|
||||
if (getWorldHandle() instanceof net.minecraft.world.level.World) {
|
||||
getTileEntity().playOpenSound(blockData, SoundEffects.BARREL_OPEN);
|
||||
}
|
||||
}
|
||||
}
|
||||
getTileEntity().openersCounter.opened = true;
|
||||
@@ -55,7 +57,9 @@ public class CraftBarrel extends CraftLootable<TileEntityBarrel> implements Barr
|
||||
if (getTileEntity().openersCounter.opened) {
|
||||
IBlockData blockData = getTileEntity().getBlock();
|
||||
getTileEntity().setOpenFlag(blockData, false);
|
||||
getTileEntity().playOpenSound(blockData, SoundEffects.BARREL_CLOSE);
|
||||
if (getWorldHandle() instanceof net.minecraft.world.level.World) {
|
||||
getTileEntity().playOpenSound(blockData, SoundEffects.BARREL_CLOSE);
|
||||
}
|
||||
}
|
||||
getTileEntity().openersCounter.opened = false;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package org.bukkit.craftbukkit.block;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import net.minecraft.world.ChestLock;
|
||||
@@ -27,6 +28,8 @@ public class CraftBeacon extends CraftBlockEntityState<TileEntityBeacon> impleme
|
||||
|
||||
@Override
|
||||
public Collection<LivingEntity> getEntitiesInRange() {
|
||||
Preconditions.checkState(getWorldHandle() instanceof net.minecraft.world.level.World, "Can't get entities during world generation");
|
||||
|
||||
TileEntity tileEntity = this.getTileEntityFromWorld();
|
||||
if (tileEntity instanceof TileEntityBeacon) {
|
||||
TileEntityBeacon beacon = (TileEntityBeacon) tileEntity;
|
||||
|
||||
@@ -65,6 +65,8 @@ public class CraftBeehive extends CraftBlockEntityState<TileEntityBeehive> imple
|
||||
|
||||
@Override
|
||||
public List<Bee> releaseEntities() {
|
||||
Preconditions.checkState(getWorldHandle() instanceof net.minecraft.world.level.World, "Can't release entities during world generation");
|
||||
|
||||
List<Bee> bees = new ArrayList<>();
|
||||
|
||||
if (isPlaced()) {
|
||||
|
||||
@@ -81,6 +81,10 @@ public class CraftBlock implements Block {
|
||||
return position;
|
||||
}
|
||||
|
||||
public GeneratorAccess getHandle() {
|
||||
return world;
|
||||
}
|
||||
|
||||
@Override
|
||||
public World getWorld() {
|
||||
return world.getMinecraftWorld().getWorld();
|
||||
@@ -198,7 +202,7 @@ public class CraftBlock implements Block {
|
||||
return world.setTypeAndData(position, blockData, 3);
|
||||
} else {
|
||||
boolean success = world.setTypeAndData(position, blockData, 2 | 16 | 1024); // NOTIFY | NO_OBSERVER | NO_PLACE (custom)
|
||||
if (success) {
|
||||
if (success && world instanceof net.minecraft.world.level.World) {
|
||||
world.getMinecraftWorld().notify(
|
||||
position,
|
||||
old,
|
||||
|
||||
@@ -22,8 +22,7 @@ public class CraftBlockEntityState<T extends TileEntity> extends CraftBlockState
|
||||
this.tileEntityClass = tileEntityClass;
|
||||
|
||||
// get tile entity from block:
|
||||
CraftWorld world = (CraftWorld) this.getWorld();
|
||||
this.tileEntity = tileEntityClass.cast(world.getHandle().getTileEntity(this.getPosition()));
|
||||
this.tileEntity = tileEntityClass.cast(getWorldHandle().getTileEntity(this.getPosition()));
|
||||
Preconditions.checkState(this.tileEntity != null, "Tile is null, asynchronous access? %s", block);
|
||||
|
||||
// copy tile entity data:
|
||||
@@ -74,7 +73,7 @@ public class CraftBlockEntityState<T extends TileEntity> extends CraftBlockState
|
||||
protected TileEntity getTileEntityFromWorld() {
|
||||
requirePlaced();
|
||||
|
||||
return ((CraftWorld) this.getWorld()).getHandle().getTileEntity(this.getPosition());
|
||||
return getWorldHandle().getTileEntity(this.getPosition());
|
||||
}
|
||||
|
||||
// gets the NBT data of the TileEntity represented by this block state
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package org.bukkit.craftbukkit.block;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.util.List;
|
||||
import net.minecraft.core.BlockPosition;
|
||||
import net.minecraft.world.level.GeneratorAccess;
|
||||
@@ -25,12 +26,20 @@ public class CraftBlockState implements BlockState {
|
||||
private final BlockPosition position;
|
||||
protected IBlockData data;
|
||||
protected int flag;
|
||||
private WeakReference<GeneratorAccess> weakWorld;
|
||||
|
||||
public CraftBlockState(final Block block) {
|
||||
this.world = (CraftWorld) block.getWorld();
|
||||
this.position = ((CraftBlock) block).getPosition();
|
||||
this.data = ((CraftBlock) block).getNMS();
|
||||
this.flag = 3;
|
||||
|
||||
GeneratorAccess generatorAccess = ((CraftBlock) block).getHandle();
|
||||
if (generatorAccess instanceof net.minecraft.world.level.World) {
|
||||
this.weakWorld = null;
|
||||
} else {
|
||||
this.weakWorld = new WeakReference<>(generatorAccess);
|
||||
}
|
||||
}
|
||||
|
||||
public CraftBlockState(final Block block, int flag) {
|
||||
@@ -42,6 +51,7 @@ public class CraftBlockState implements BlockState {
|
||||
world = null;
|
||||
data = CraftMagicNumbers.getBlock(material).getBlockData();
|
||||
position = BlockPosition.ZERO;
|
||||
this.weakWorld = null;
|
||||
}
|
||||
|
||||
public static CraftBlockState getBlockState(GeneratorAccess world, net.minecraft.core.BlockPosition pos) {
|
||||
@@ -52,6 +62,20 @@ public class CraftBlockState implements BlockState {
|
||||
return new CraftBlockState(CraftBlock.at(world, pos), flag);
|
||||
}
|
||||
|
||||
public GeneratorAccess getWorldHandle() {
|
||||
if (weakWorld == null) {
|
||||
return world.getHandle();
|
||||
}
|
||||
|
||||
GeneratorAccess access = weakWorld.get();
|
||||
if (access == null) {
|
||||
weakWorld = null;
|
||||
return world.getHandle();
|
||||
}
|
||||
|
||||
return access;
|
||||
}
|
||||
|
||||
@Override
|
||||
public World getWorld() {
|
||||
requirePlaced();
|
||||
@@ -154,7 +178,7 @@ public class CraftBlockState implements BlockState {
|
||||
@Override
|
||||
public CraftBlock getBlock() {
|
||||
requirePlaced();
|
||||
return CraftBlock.at(world.getHandle(), position);
|
||||
return CraftBlock.at(getWorldHandle(), position);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -172,6 +196,7 @@ public class CraftBlockState implements BlockState {
|
||||
if (!isPlaced()) {
|
||||
return true;
|
||||
}
|
||||
GeneratorAccess access = getWorldHandle();
|
||||
CraftBlock block = getBlock();
|
||||
|
||||
if (block.getType() != getType()) {
|
||||
@@ -182,12 +207,14 @@ public class CraftBlockState implements BlockState {
|
||||
|
||||
IBlockData newBlock = this.data;
|
||||
block.setTypeAndData(newBlock, applyPhysics);
|
||||
world.getHandle().notify(
|
||||
position,
|
||||
block.getNMS(),
|
||||
newBlock,
|
||||
3
|
||||
);
|
||||
if (access instanceof net.minecraft.world.level.World) {
|
||||
world.getHandle().notify(
|
||||
position,
|
||||
block.getNMS(),
|
||||
newBlock,
|
||||
3
|
||||
);
|
||||
}
|
||||
|
||||
// Update levers etc
|
||||
if (false && applyPhysics && getData() instanceof Attachable) { // Call does not map to new API
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package org.bukkit.craftbukkit.block;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
import net.minecraft.sounds.SoundEffects;
|
||||
import net.minecraft.world.ITileInventory;
|
||||
import net.minecraft.world.level.block.BlockChest;
|
||||
@@ -40,6 +41,8 @@ public class CraftChest extends CraftLootable<TileEntityChest> implements Chest
|
||||
|
||||
@Override
|
||||
public Inventory getInventory() {
|
||||
Preconditions.checkState(getWorldHandle() instanceof net.minecraft.world.level.World, "Can't get inventory during world generation, use getBlockInventory() instead");
|
||||
|
||||
CraftInventory inventory = (CraftInventory) this.getBlockInventory();
|
||||
if (!isPlaced()) {
|
||||
return inventory;
|
||||
@@ -60,7 +63,7 @@ public class CraftChest extends CraftLootable<TileEntityChest> implements Chest
|
||||
@Override
|
||||
public void open() {
|
||||
requirePlaced();
|
||||
if (!getTileEntity().openersCounter.opened) {
|
||||
if (!getTileEntity().openersCounter.opened && getWorldHandle() instanceof net.minecraft.world.level.World) {
|
||||
IBlockData block = getTileEntity().getBlock();
|
||||
getTileEntity().getWorld().playBlockAction(getPosition(), block.getBlock(), 1, getTileEntity().openersCounter.getOpenerCount() + 1);
|
||||
TileEntityChest.playOpenSound(getTileEntity().getWorld(), getPosition(), block, SoundEffects.CHEST_OPEN);
|
||||
@@ -71,7 +74,7 @@ public class CraftChest extends CraftLootable<TileEntityChest> implements Chest
|
||||
@Override
|
||||
public void close() {
|
||||
requirePlaced();
|
||||
if (getTileEntity().openersCounter.opened) {
|
||||
if (getTileEntity().openersCounter.opened && getWorldHandle() instanceof net.minecraft.world.level.World) {
|
||||
IBlockData block = getTileEntity().getBlock();
|
||||
getTileEntity().getWorld().playBlockAction(getPosition(), block.getBlock(), 1, 0);
|
||||
TileEntityChest.playOpenSound(getTileEntity().getWorld(), getPosition(), block, SoundEffects.CHEST_CLOSE);
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package org.bukkit.craftbukkit.block;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
import net.minecraft.world.level.block.BlockDispenser;
|
||||
import net.minecraft.world.level.block.Blocks;
|
||||
import net.minecraft.world.level.block.entity.TileEntityDispenser;
|
||||
@@ -49,6 +50,8 @@ public class CraftDispenser extends CraftLootable<TileEntityDispenser> implement
|
||||
|
||||
@Override
|
||||
public boolean dispense() {
|
||||
Preconditions.checkState(getWorldHandle() instanceof net.minecraft.world.level.World, "Can't dispense during world generation");
|
||||
|
||||
Block block = getBlock();
|
||||
|
||||
if (block.getType() == Material.DISPENSER) {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package org.bukkit.craftbukkit.block;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
import net.minecraft.world.level.block.BlockDropper;
|
||||
import net.minecraft.world.level.block.Blocks;
|
||||
import net.minecraft.world.level.block.entity.TileEntityDropper;
|
||||
@@ -36,6 +37,8 @@ public class CraftDropper extends CraftLootable<TileEntityDropper> implements Dr
|
||||
|
||||
@Override
|
||||
public void drop() {
|
||||
Preconditions.checkState(getWorldHandle() instanceof net.minecraft.world.level.World, "Can't drop during world generation");
|
||||
|
||||
Block block = getBlock();
|
||||
|
||||
if (block.getType() == Material.DROPPER) {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package org.bukkit.craftbukkit.block;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
import net.minecraft.world.item.ItemStack;
|
||||
import net.minecraft.world.level.block.BlockJukeBox;
|
||||
import net.minecraft.world.level.block.Blocks;
|
||||
@@ -31,9 +32,9 @@ public class CraftJukebox extends CraftBlockEntityState<TileEntityJukeBox> imple
|
||||
CraftWorld world = (CraftWorld) this.getWorld();
|
||||
Material record = this.getPlaying();
|
||||
if (record == Material.AIR) {
|
||||
world.getHandle().setTypeAndData(this.getPosition(), Blocks.JUKEBOX.getBlockData().set(BlockJukeBox.HAS_RECORD, false), 3);
|
||||
getWorldHandle().setTypeAndData(this.getPosition(), Blocks.JUKEBOX.getBlockData().set(BlockJukeBox.HAS_RECORD, false), 3);
|
||||
} else {
|
||||
world.getHandle().setTypeAndData(this.getPosition(), Blocks.JUKEBOX.getBlockData().set(BlockJukeBox.HAS_RECORD, true), 3);
|
||||
getWorldHandle().setTypeAndData(this.getPosition(), Blocks.JUKEBOX.getBlockData().set(BlockJukeBox.HAS_RECORD, true), 3);
|
||||
}
|
||||
world.playEffect(this.getLocation(), Effect.RECORD_PLAY, record);
|
||||
}
|
||||
@@ -84,6 +85,8 @@ public class CraftJukebox extends CraftBlockEntityState<TileEntityJukeBox> imple
|
||||
|
||||
@Override
|
||||
public boolean eject() {
|
||||
Preconditions.checkState(getWorldHandle() instanceof net.minecraft.world.level.World, "Can't eject during world generation");
|
||||
|
||||
requirePlaced();
|
||||
TileEntity tileEntity = this.getTileEntityFromWorld();
|
||||
if (!(tileEntity instanceof TileEntityJukeBox)) return false;
|
||||
|
||||
@@ -46,7 +46,7 @@ public class CraftLectern extends CraftBlockEntityState<TileEntityLectern> imple
|
||||
public boolean update(boolean force, boolean applyPhysics) {
|
||||
boolean result = super.update(force, applyPhysics);
|
||||
|
||||
if (result && this.isPlaced() && this.getType() == Material.LECTERN) {
|
||||
if (result && this.isPlaced() && this.getType() == Material.LECTERN && getWorldHandle() instanceof net.minecraft.world.level.World) {
|
||||
BlockLectern.a(this.world.getHandle(), this.getPosition(), this.getHandle());
|
||||
}
|
||||
|
||||
|
||||
@@ -48,7 +48,7 @@ public class CraftShulkerBox extends CraftLootable<TileEntityShulkerBox> impleme
|
||||
@Override
|
||||
public void open() {
|
||||
requirePlaced();
|
||||
if (!getTileEntity().opened) {
|
||||
if (!getTileEntity().opened && getWorldHandle() instanceof net.minecraft.world.level.World) {
|
||||
World world = getTileEntity().getWorld();
|
||||
world.playBlockAction(getPosition(), getTileEntity().getBlock().getBlock(), 1, 1);
|
||||
world.playSound(null, getPosition(), SoundEffects.SHULKER_BOX_OPEN, SoundCategory.BLOCKS, 0.5F, world.random.nextFloat() * 0.1F + 0.9F);
|
||||
@@ -59,7 +59,7 @@ public class CraftShulkerBox extends CraftLootable<TileEntityShulkerBox> impleme
|
||||
@Override
|
||||
public void close() {
|
||||
requirePlaced();
|
||||
if (getTileEntity().opened) {
|
||||
if (getTileEntity().opened && getWorldHandle() instanceof net.minecraft.world.level.World) {
|
||||
World world = getTileEntity().getWorld();
|
||||
world.playBlockAction(getPosition(), getTileEntity().getBlock().getBlock(), 1, 0);
|
||||
world.playSound(null, getPosition(), SoundEffects.SHULKER_BOX_OPEN, SoundCategory.BLOCKS, 0.5F, world.random.nextFloat() * 0.1F + 0.9F);
|
||||
|
||||
@@ -180,9 +180,19 @@ public class CraftStructureBlock extends CraftBlockEntityState<TileEntityStructu
|
||||
@Override
|
||||
protected void applyTo(TileEntityStructure tileEntity) {
|
||||
super.applyTo(tileEntity);
|
||||
net.minecraft.world.level.GeneratorAccess access = getWorldHandle();
|
||||
|
||||
// Ensure block type is correct
|
||||
tileEntity.setUsageMode(tileEntity.getUsageMode());
|
||||
if (access instanceof net.minecraft.world.level.World) {
|
||||
tileEntity.setUsageMode(tileEntity.getUsageMode());
|
||||
} else {
|
||||
// Custom handle during world generation
|
||||
// From TileEntityStructure#setUsageMode(BlockPropertyStructureMode)
|
||||
net.minecraft.world.level.block.state.IBlockData data = access.getType(this.getPosition());
|
||||
if (data.a(net.minecraft.world.level.block.Blocks.STRUCTURE_BLOCK)) {
|
||||
access.setTypeAndData(this.getPosition(), data.set(net.minecraft.world.level.block.BlockStructure.MODE, tileEntity.getUsageMode()), 2);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean isBetween(int num, int min, int max) {
|
||||
|
||||
Reference in New Issue
Block a user