WorldCanvas srcCanvas = srcManager.getCanvas();
WorldCanvas destCanvas = destManager.getCanvas();
// export schematic from source map
Schematic schema = srcCanvas.exportSchematic(xSrc, ySrc, zSrc,
xSrc+width-1, ySrc+height-1, zSrc+length-1);
int dx = xSrc - xDest;
int dy = ySrc - yDest;
int dz = zSrc - zDest;
// regular expression for tp commands
String space = "\\s+?";
String target = "(@[prafPRAF](?:\\[.*\\])*?)";
String number = "(~?-?[\\p{Digit}]+?)";
String tpstr = "/tp" + space + target + space + number
+ space + number + space + number + "\\s*?";
// regular expression for general commands
String argstr = "/([^\\s]*)" + space + "(.*)";
String argsplit = "(@[prafPRAF])(\\[.*\\])*?)";
Pattern apattern = Pattern.compile(argstr);
Pattern tpattern = Pattern.compile(tpstr);
// edit command block teleport coordinates
List<TileEntity> tileEntities = schema.getTileEntities();
for(TileEntity e : new ArrayList<TileEntity>(tileEntities))
{
if(e instanceof CommandBlock)
{
CommandBlock cb = (CommandBlock)e;
int xc = cb.getX();
int yc = cb.getY();
int zc = cb.getZ();
String cmd = cb.getCommand();
if(cmd == null)
continue;
Matcher tmatcher = tpattern.matcher(cmd);
if(tmatcher.matches())
{
String tar = tmatcher.group(1);
String xstr = tmatcher.group(2);
String ystr = tmatcher.group(3);
String zstr = tmatcher.group(4);
int xt = Integer.parseInt(xstr.replace("~", "")) - dx;
int yt = Integer.parseInt(ystr.replace("~", "")) - dy;
int zt = Integer.parseInt(zstr.replace("~", "")) - dz;
xstr = xstr.startsWith("~") ? xstr : Integer.toString(xt);
ystr = ystr.startsWith("~") ? ystr : Integer.toString(yt);
zstr = zstr.startsWith("~") ? zstr : Integer.toString(zt);
cmd = "/tp " + tar + " " + xt + " " + yt + " " + zt;
}
Matcher amatcher = apattern.matcher(cmd);
if(amatcher.matches())
{
String arg = tmatcher.group(1);
String val = tmatcher.group(2);
// TODO: split val into tokens, fix the xyz ones
}
else
continue;
cb = EntityFactory.getDefaultFactory().createCommandBlock(xc, yc, zc, cmd);
schema.removeTileEntity(e);
schema.addTileEntity(cb);
}
}
// remove existing entities from target region
List<Entity> removeEntities = destCanvas.getEntities(xDest, yDest, zDest,