Added //regen to regenerate an area.

This commit is contained in:
sk89q
2011-03-12 16:37:07 -08:00
parent 0fa675cecc
commit 1b670a1c98
6 changed files with 167 additions and 66 deletions

View File

@ -249,6 +249,20 @@ public class EditSession {
return smartSetBlock(pt, block);
}
/**
* Insert a contrived block change into the history.
*
* @param pt
* @param existing
* @param block
*/
public void rememberChange(Vector pt, BaseBlock existing, BaseBlock block) {
BlockVector blockPt = pt.toBlockVector();
original.put(blockPt, existing);
current.put(pt.toBlockVector(), block);
}
/**
* Set a block with a pattern.
*
@ -286,7 +300,7 @@ public class EditSession {
* @param block
* @return
*/
private boolean smartSetBlock(Vector pt, BaseBlock block) {
public boolean smartSetBlock(Vector pt, BaseBlock block) {
if (queued) {
// Place torches, etc. last
if (BlockType.shouldPlaceLast(block.getType())) {

View File

@ -22,6 +22,7 @@ package com.sk89q.worldedit;
import java.util.Random;
import com.sk89q.worldedit.blocks.BaseBlock;
import com.sk89q.worldedit.blocks.BaseItemStack;
import com.sk89q.worldedit.regions.Region;
/**
* Represents a world.
@ -78,6 +79,15 @@ public abstract class LocalWorld {
* @return
*/
public abstract int getBlockData(Vector pt);
/**
* Regenerate an area.
*
* @param region
* @param editSession
* @return
*/
public abstract boolean regenerate(Region region, EditSession editSession);
/**
* Attempts to accurately copy a BaseBlock's extra data to the world.

View File

@ -43,7 +43,9 @@ import org.bukkit.World;
import com.sk89q.worldedit.EditSession;
import com.sk89q.worldedit.LocalWorld;
import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.Vector2D;
import com.sk89q.worldedit.blocks.*;
import com.sk89q.worldedit.regions.Region;
public class BukkitWorld extends LocalWorld {
private World world;
@ -111,6 +113,60 @@ public class BukkitWorld extends LocalWorld {
return world.getBlockAt(pt.getBlockX(), pt.getBlockY(), pt.getBlockZ()).getData();
}
/**
* Regenerate an area.
*
* @param region
* @param editSession
* @return
*/
@Override
public boolean regenerate(Region region, EditSession editSession) {
BaseBlock[] history = new BaseBlock[16 * 16 * 128];
for (Vector2D chunk : region.getChunks()) {
Vector min = new Vector(chunk.getBlockX() * 16, 0, chunk.getBlockZ() * 16);
Vector max = min.add(15, 127, 15);
// First save all the blocks inside
for (int x = 0; x < 16; x++) {
for (int y = 0; y < 128; y++) {
for (int z = 0; z < 16; z++) {
Vector pt = min.add(x, y, z);
int index = y * 16 * 16 + z * 16 + x;
history[index] = editSession.getBlock(pt);
}
}
}
try {
world.regenerateChunk(chunk.getBlockX(), chunk.getBlockZ());
} catch (Throwable t) {
t.printStackTrace();
}
// Then restore
for (int x = 0; x < 16; x++) {
for (int y = 0; y < 128; y++) {
for (int z = 0; z < 16; z++) {
Vector pt = min.add(x, y, z);
int index = y * 16 * 16 + z * 16 + x;
// We have to restore the block if it was outside
if (!region.contains(pt)) {
editSession.smartSetBlock(pt, history[index]);
} else { // Otherwise fool with history
editSession.rememberChange(pt, history[index],
editSession.rawGetBlock(pt));
}
}
}
}
}
return true;
}
/**
* Attempts to accurately copy a BaseBlock's extra data to the world.
*

View File

@ -26,6 +26,7 @@ import com.sk89q.minecraft.util.commands.CommandContext;
import com.sk89q.minecraft.util.commands.CommandPermissions;
import com.sk89q.worldedit.*;
import com.sk89q.worldedit.data.LegacyChunkStore;
import com.sk89q.worldedit.regions.Region;
/**
* Chunk tools.

View File

@ -230,4 +230,21 @@ public class RegionCommands {
dir, count, !args.hasFlag('a'));
player.print(affected + " blocks changed. Undo with //undo");
}
@Command(
aliases = {"/regen"},
usage = "",
desc = "Regenerates the contents of the selection",
min = 0,
max = 0
)
@CommandPermissions({"worldedit.regen"})
public static void regenerateChunk(CommandContext args, WorldEdit we,
LocalSession session, LocalPlayer player, EditSession editSession)
throws WorldEditException {
Region region = session.getSelection(player.getWorld());
player.getWorld().regenerate(region, editSession);
player.print("Region regenerated.");
}
}