Allow regeneration to accept Extents instead of EditSessions.

Allows for custom Extents to be parsed to regen, and for clipboard regeneration, etc whilst still allowing EditSessions
This commit is contained in:
dordsor21 2020-07-23 13:15:03 +01:00 committed by Matthew Miller
parent d4c06c73df
commit c4abc9a247
6 changed files with 65 additions and 23 deletions

View File

@ -27,6 +27,7 @@
import com.sk89q.worldedit.blocks.BaseItemStack;
import com.sk89q.worldedit.bukkit.adapter.BukkitImplAdapter;
import com.sk89q.worldedit.entity.BaseEntity;
import com.sk89q.worldedit.extent.Extent;
import com.sk89q.worldedit.internal.wna.WorldNativeAccess;
import com.sk89q.worldedit.math.BlockVector2;
import com.sk89q.worldedit.math.BlockVector3;
@ -198,11 +199,11 @@ public int getBlockLightLevel(BlockVector3 pt) {
}
@Override
public boolean regenerate(Region region, EditSession editSession, RegenOptions options) {
public boolean regenerate(Region region, Extent extent, RegenOptions options) {
BukkitImplAdapter adapter = WorldEditPlugin.getInstance().getBukkitImplAdapter();
try {
if (adapter != null) {
return adapter.regenerate(getWorld(), region, editSession, options);
return adapter.regenerate(getWorld(), region, extent, options);
} else {
throw new UnsupportedOperationException("Missing BukkitImplAdapater for this version.");
}

View File

@ -24,6 +24,7 @@
import com.sk89q.worldedit.blocks.BaseItem;
import com.sk89q.worldedit.blocks.BaseItemStack;
import com.sk89q.worldedit.entity.BaseEntity;
import com.sk89q.worldedit.extent.Extent;
import com.sk89q.worldedit.internal.wna.WorldNativeAccess;
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.regions.Region;
@ -227,6 +228,18 @@ default OptionalInt getInternalBlockStateId(BlockState state) {
* @return true on success, false on failure
*/
default boolean regenerate(World world, Region region, EditSession session, RegenOptions options) {
return regenerate(world, region, (Extent) session, options);
}
/**
* Regenerate a region in the given world, so it appears "as new".
* @param world the world to regen in
* @param region the region to regen
* @param extent the extent to use for setting blocks
* @param options the regeneration options
* @return true on success, false on failure
*/
default boolean regenerate(World world, Region region, Extent extent, RegenOptions options) {
throw new UnsupportedOperationException("This adapter does not support regeneration.");
}
}

View File

@ -204,6 +204,17 @@ default boolean notifyAndLightBlock(BlockVector3 position, BlockState previousTy
*/
void simulateBlockMine(BlockVector3 position);
/**
* Regenerate an area.
*
* @param region the region
* @param extent the {@link Extent}
* @return true if re-generation was successful
*/
default boolean regenerate(Region region, Extent extent) {
return regenerate(region, extent, RegenOptions.builder().build());
}
/**
* Regenerate an area.
*
@ -212,7 +223,23 @@ default boolean notifyAndLightBlock(BlockVector3 position, BlockState previousTy
* @return true if re-generation was successful
*/
default boolean regenerate(Region region, EditSession editSession) {
return regenerate(region, editSession, RegenOptions.builder().build());
return regenerate(region, (Extent) editSession, RegenOptions.builder().build());
}
/**
* Regenerate an area.
*
* @param region the region
* @param extent the {@link Extent}
* @param options the regeneration options
* @return true if regeneration was successful
*/
@NonAbstractForCompatibility(
delegateName = "regenerate",
delegateParams = { Region.class, Extent.class }
)
default boolean regenerate(Region region, Extent extent, RegenOptions options) {
return regenerate(region, extent);
}
/**
@ -228,7 +255,7 @@ default boolean regenerate(Region region, EditSession editSession) {
delegateParams = { Region.class, EditSession.class }
)
default boolean regenerate(Region region, EditSession editSession, RegenOptions options) {
return regenerate(region, editSession);
return regenerate(region, (Extent) editSession);
}
/**

View File

@ -28,12 +28,12 @@
import com.mojang.serialization.Dynamic;
import com.sk89q.jnbt.CompoundTag;
import com.sk89q.worldedit.EditSession;
import com.sk89q.worldedit.MaxChangedBlocksException;
import com.sk89q.worldedit.WorldEditException;
import com.sk89q.worldedit.blocks.BaseItem;
import com.sk89q.worldedit.blocks.BaseItemStack;
import com.sk89q.worldedit.entity.BaseEntity;
import com.sk89q.worldedit.entity.Entity;
import com.sk89q.worldedit.extent.Extent;
import com.sk89q.worldedit.fabric.internal.ExtendedMinecraftServer;
import com.sk89q.worldedit.fabric.internal.FabricWorldNativeAccess;
import com.sk89q.worldedit.fabric.internal.NBTConverter;
@ -284,7 +284,7 @@ public void simulateBlockMine(BlockVector3 position) {
}
@Override
public boolean regenerate(Region region, EditSession editSession, RegenOptions options) {
public boolean regenerate(Region region, Extent extent, RegenOptions options) {
// Don't even try to regen if it's going to fail.
ChunkManager provider = getWorld().getChunkManager();
if (!(provider instanceof ServerChunkManager)) {
@ -292,7 +292,7 @@ public boolean regenerate(Region region, EditSession editSession, RegenOptions o
}
try {
doRegen(region, editSession, options);
doRegen(region, extent, options);
} catch (Exception e) {
throw new IllegalStateException("Regen failed", e);
}
@ -300,7 +300,7 @@ public boolean regenerate(Region region, EditSession editSession, RegenOptions o
return true;
}
private void doRegen(Region region, EditSession editSession, RegenOptions options) throws Exception {
private void doRegen(Region region, Extent extent, RegenOptions options) throws Exception {
Path tempDir = Files.createTempDirectory("WorldEditWorldGen");
LevelStorage levelStorage = LevelStorage.create(tempDir);
try (LevelStorage.Session session = levelStorage.createSession("WorldEditTempGen")) {
@ -339,7 +339,7 @@ private void doRegen(Region region, EditSession editSession, RegenOptions option
// This controls ticking, we don't need it so set it to false.
false
)) {
regenForWorld(region, editSession, serverWorld, options);
regenForWorld(region, extent, serverWorld, options);
// drive the server executor until all tasks are popped off
while (originalWorld.getServer().runTask()) {
@ -369,8 +369,8 @@ private Dynamic<Tag> recursivelySetSeed(Dynamic<Tag> dynamic, long seed, Set<Dyn
});
}
private void regenForWorld(Region region, EditSession editSession, ServerWorld serverWorld,
RegenOptions options) throws MaxChangedBlocksException {
private void regenForWorld(Region region, Extent extent, ServerWorld serverWorld,
RegenOptions options) throws WorldEditException {
List<CompletableFuture<Chunk>> chunkLoadings = submitChunkLoadTasks(region, serverWorld);
// drive executor until loading finishes
@ -403,11 +403,11 @@ private void regenForWorld(Region region, EditSession editSession, ServerWorld s
blockEntity.toTag(tag);
state = state.toBaseBlock(NBTConverter.fromNative(tag));
}
editSession.setBlock(vec, state);
extent.setBlock(vec, state.toBaseBlock());
if (options.shouldRegenBiomes()) {
BiomeType biome = getBiomeInChunk(vec, chunk);
editSession.setBiome(vec, biome);
extent.setBiome(vec, biome);
}
}
}

View File

@ -29,12 +29,12 @@
import com.mojang.serialization.Dynamic;
import com.sk89q.jnbt.CompoundTag;
import com.sk89q.worldedit.EditSession;
import com.sk89q.worldedit.MaxChangedBlocksException;
import com.sk89q.worldedit.WorldEditException;
import com.sk89q.worldedit.blocks.BaseItem;
import com.sk89q.worldedit.blocks.BaseItemStack;
import com.sk89q.worldedit.entity.BaseEntity;
import com.sk89q.worldedit.entity.Entity;
import com.sk89q.worldedit.extent.Extent;
import com.sk89q.worldedit.forge.internal.ForgeWorldNativeAccess;
import com.sk89q.worldedit.forge.internal.NBTConverter;
import com.sk89q.worldedit.forge.internal.TileEntityUtils;
@ -294,7 +294,7 @@ public void simulateBlockMine(BlockVector3 position) {
// For unmapped regen names, see Fabric!
@Override
public boolean regenerate(Region region, EditSession editSession, RegenOptions options) {
public boolean regenerate(Region region, Extent extent, RegenOptions options) {
// Don't even try to regen if it's going to fail.
AbstractChunkProvider provider = getWorld().getChunkProvider();
if (!(provider instanceof ServerChunkProvider)) {
@ -302,7 +302,7 @@ public boolean regenerate(Region region, EditSession editSession, RegenOptions o
}
try {
doRegen(region, editSession, options);
doRegen(region, extent, options);
} catch (Exception e) {
throw new IllegalStateException("Regen failed", e);
}
@ -310,7 +310,7 @@ public boolean regenerate(Region region, EditSession editSession, RegenOptions o
return true;
}
private void doRegen(Region region, EditSession editSession, RegenOptions options) throws Exception {
private void doRegen(Region region, Extent extent, RegenOptions options) throws Exception {
Path tempDir = Files.createTempDirectory("WorldEditWorldGen");
SaveFormat levelStorage = SaveFormat.func_237269_a_(tempDir);
try (SaveFormat.LevelSave session = levelStorage.func_237274_c_("WorldEditTempGen")) {
@ -351,7 +351,7 @@ private void doRegen(Region region, EditSession editSession, RegenOptions option
// This controls ticking, we don't need it so set it to false.
false
)) {
regenForWorld(region, editSession, serverWorld, options);
regenForWorld(region, extent, serverWorld, options);
// drive the server executor until all tasks are popped off
while (originalWorld.getServer().driveOne()) {
@ -381,8 +381,8 @@ private Dynamic<INBT> recursivelySetSeed(Dynamic<INBT> dynamic, long seed, Set<D
});
}
private void regenForWorld(Region region, EditSession editSession, ServerWorld serverWorld,
RegenOptions options) throws MaxChangedBlocksException {
private void regenForWorld(Region region, Extent extent, ServerWorld serverWorld,
RegenOptions options) throws WorldEditException {
List<CompletableFuture<IChunk>> chunkLoadings = submitChunkLoadTasks(region, serverWorld);
// drive executor until loading finishes
@ -415,11 +415,11 @@ private void regenForWorld(Region region, EditSession editSession, ServerWorld s
blockEntity.write(tag);
state = state.toBaseBlock(NBTConverter.fromNative(tag));
}
editSession.setBlock(vec, state);
extent.setBlock(vec, state.toBaseBlock());
if (options.shouldRegenBiomes()) {
BiomeType biome = getBiomeInChunk(vec, chunk);
editSession.setBiome(vec, biome);
extent.setBiome(vec, biome);
}
}
}

View File

@ -26,6 +26,7 @@
import com.sk89q.worldedit.blocks.BaseItemStack;
import com.sk89q.worldedit.entity.BaseEntity;
import com.sk89q.worldedit.entity.Entity;
import com.sk89q.worldedit.extent.Extent;
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.math.Vector3;
import com.sk89q.worldedit.regions.Region;
@ -182,7 +183,7 @@ public boolean notifyAndLightBlock(BlockVector3 position, com.sk89q.worldedit.wo
}
@Override
public boolean regenerate(Region region, EditSession editSession, RegenOptions options) {
public boolean regenerate(Region region, Extent extent, RegenOptions options) {
return false;
}