Added data framework (com.sk89q.worldedit.data.*); moved schematic loading/saving towards data framework; added work-in-progress EditSession.restoreBackup().

This commit is contained in:
sk89q
2010-10-20 02:12:16 -07:00
parent 6299734cdd
commit 32290b4095
15 changed files with 991 additions and 45 deletions

View File

@ -0,0 +1,59 @@
// $Id$
/*
* WorldEdit
* Copyright (C) 2010 sk89q <http://www.sk89q.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.data;
import java.io.*;
/**
* Represents the chunk store used by Minecraft alpha.
*
* @author sk89q
*/
public class AlphaChunkStore extends NestedFileChunkStore {
/**
* Folder to read from.
*/
private File path;
/**
* Create an instance. The passed path is the folder to read the
* chunk files from.
*
* @param path
*/
public AlphaChunkStore(File path) {
this.path = path;
}
/**
* Get the input stream for a chunk file.
*
* @param f1
* @param f2
* @param name
* @return
* @throws IOException
*/
protected InputStream getInputStream(String f1, String f2, String name)
throws IOException {
String file = f1 + File.separator + f2 + File.separator + name;
return new FileInputStream(new File(path, file));
}
}

View File

@ -0,0 +1,217 @@
// $Id$
/*
* WorldEdit
* Copyright (C) 2010 sk89q <http://www.sk89q.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.data;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import com.sk89q.worldedit.*;
import com.sk89q.worldedit.blocks.*;
import org.jnbt.*;
/**
* Represents a chunk.
*
* @author sk89q
*/
public class Chunk {
private CompoundTag rootTag;
private byte[] blocks;
private byte[] data;
private int rootX;
private int rootZ;
Map<BlockVector,Map<String,Tag>> tileEntities;
/**
* Construct the chunk with a compound tag.
*
* @param rootTag
*/
public Chunk(CompoundTag tag) throws DataException {
rootTag = tag;
blocks = ((ByteArrayTag)getChildTag(
rootTag.getValue(), "Blocks", ByteArrayTag.class)).getValue();
data = ((ByteArrayTag)getChildTag(
rootTag.getValue(), "Data", ByteArrayTag.class)).getValue();
rootX = ((IntTag)getChildTag(
rootTag.getValue(), "xPos", IntTag.class)).getValue();
rootZ = ((IntTag)getChildTag(
rootTag.getValue(), "zPos", IntTag.class)).getValue();
if (blocks.length != 16384) {
throw new InvalidFormatException("Chunk blocks byte array expected to contain 16,384 blocks");
}
if (data.length != 16384) {
throw new InvalidFormatException("Chunk block data byte array expected to contain 16,384 blocks");
}
}
/**
* Get the block ID of a block.
*
* @param pos
* @return
* @throws DataException
*/
public int getBlockID(Vector pos) throws DataException {
int index = pos.getBlockY() * 16 * 16
+ (pos.getBlockZ() - rootZ) * 16 + (pos.getBlockX() - rootX);
try {
return blocks[index];
} catch (IndexOutOfBoundsException e) {
throw new DataException("Chunk does not contain position " + pos);
}
}
/**
* Get the block data of a block.
*
* @param pos
* @return
* @throws DataException
*/
public int getBlockData(Vector pos) throws DataException {
int index = pos.getBlockY() * 16 * 16
+ (pos.getBlockZ() - rootZ) * 16 + (pos.getBlockX() - rootX);
try {
return data[index];
} catch (IndexOutOfBoundsException e) {
throw new DataException("Chunk does not contain position " + pos);
}
}
/**
* Used to load the tile entities.
*
* @throws DataException
*/
private void populateTileEntities() throws DataException {
List<Tag> tags = (List<Tag>)((ListTag)getChildTag(
rootTag.getValue(), "TileEntities", ListTag.class))
.getValue();
tileEntities = new HashMap<BlockVector,Map<String,Tag>>();
for (Tag tag : tags) {
if (!(tag instanceof CompoundTag)) {
throw new InvalidFormatException("CompoundTag expected in TileEntities");
}
CompoundTag t = (CompoundTag)tag;
int x = 0;
int y = 0;
int z = 0;
Map<String,Tag> values = new HashMap<String,Tag>();
for (Map.Entry<String,Tag> entry : t.getValue().entrySet()) {
if (entry.getKey().equals("x")) {
if (entry.getValue() instanceof IntTag) {
x = ((IntTag)entry.getValue()).getValue();
}
} else if (entry.getKey().equals("y")) {
if (entry.getValue() instanceof IntTag) {
y = ((IntTag)entry.getValue()).getValue();
}
} else if (entry.getKey().equals("z")) {
if (entry.getValue() instanceof IntTag) {
z = ((IntTag)entry.getValue()).getValue();
}
}
values.put(entry.getKey(), entry.getValue());
}
BlockVector vec = new BlockVector(x, y, z);
tileEntities.put(vec, values);
}
}
/**
* Get the map of tags keyed to strings for a block's tile entity data. May
* return null if there is no tile entity data. Not public yet because
* what this function returns isn't ideal for usage.
*
* @param pos
* @return
* @throws DataException
*/
private Map<String,Tag> getBlockTileEntity(Vector pos) throws DataException {
if (tileEntities == null)
populateTileEntities();
return tileEntities.get(new BlockVector(pos));
}
/**
* Get a block;
*
* @param pos
* @return block
* @throws DataException
*/
public BaseBlock getBlock(Vector pos) throws DataException {
int id = getBlockID(pos);
int data = getBlockData(pos);
// Signs
if (id == 63 || id == 68) {
SignBlock block = new SignBlock(id, data);
Map<String,Tag> tileEntity = getBlockTileEntity(pos);
if (tileEntity != null) {
((TileEntityBlock)block).fromTileEntityNBT(tileEntity);
}
return block;
} else {
return new BaseBlock(id, data);
}
}
/**
* Get child tag of a NBT structure.
*
* @param items
* @param key
* @param expected
* @return child tag
* @throws InvalidFormatException
*/
private static Tag getChildTag(Map<String,Tag> items, String key, Class expected)
throws InvalidFormatException {
if (!items.containsKey(key)) {
throw new InvalidFormatException("Missing a \"" + key + "\" tag");
}
Tag tag = items.get(key);
if (!expected.isInstance(tag)) {
throw new InvalidFormatException(
key + " tag is not of tag type " + expected.getName());
}
return tag;
}
}

View File

@ -0,0 +1,81 @@
// $Id$
/*
* WorldEdit
* Copyright (C) 2010 sk89q <http://www.sk89q.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.data;
import java.io.*;
import com.sk89q.worldedit.*;
import org.jnbt.*;
/**
* Represents chunk storage mechanisms.
*
* @author sk89q
*/
public abstract class ChunkStore {
/**
* Convert a position to a chunk.
*
* @param pos
* @return
* @throws ChunkStoreException
* @throws IOException
*/
public static BlockVector2D toChunk(Vector pos) {
int chunkX = (int)Math.floor(pos.getBlockX() / 16.0);
int chunkZ = (int)Math.floor(pos.getBlockZ() / 16.0);
return new BlockVector2D(chunkX, chunkZ);
}
/**
* Get the tag for a chunk.
*
* @param pos
* @return tag
* @throws ChunkStoreException
* @throws IOException
*/
public abstract CompoundTag getChunkTag(Vector2D pos)
throws ChunkStoreException, IOException;
/**
* Get a chunk at a location.
*
* @param x
* @param z
* @return
* @throws ChunkStoreException
* @throws IOException
* @throws DataException
*/
public Chunk getChunk(Vector2D pos)
throws DataException, IOException {
return new Chunk(getChunkTag(pos));
}
/**
* Close resources.
*
* @throws IOException
*/
public void close() throws IOException {
}
}

View File

@ -0,0 +1,30 @@
// $Id$
/*
* WorldEdit
* Copyright (C) 2010 sk89q <http://www.sk89q.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.data;
/**
*
* @author sk89q
*/
public class ChunkStoreException extends DataException {
public ChunkStoreException(String msg) {
super(msg);
}
}

View File

@ -0,0 +1,31 @@
// $Id$
/*
* WorldEdit
* Copyright (C) 2010 sk89q <http://www.sk89q.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.data;
/**
* Thrown when there is an exception related to data handling.
*
* @author sk89q
*/
public class DataException extends Exception {
public DataException(String msg) {
super(msg);
}
}

View File

@ -0,0 +1,30 @@
// $Id$
/*
* WorldEdit
* Copyright (C) 2010 sk89q <http://www.sk89q.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.data;
/**
*
* @author sk89q
*/
public class InvalidFormatException extends DataException {
public InvalidFormatException(String msg) {
super(msg);
}
}

View File

@ -0,0 +1,87 @@
// $Id$
/*
* WorldEdit
* Copyright (C) 2010 sk89q <http://www.sk89q.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.data;
import com.sk89q.worldedit.Vector2D;
import java.io.*;
import org.jnbt.*;
/**
* Represents chunk stores that use Alpha's file format for storing chunks.
* The code to resolve the filename is already implemented in this class
* and an inheriting class merely needs to implement getInputStream().
*
* @author sk89q
*/
public abstract class NestedFileChunkStore extends ChunkStore {
/**
* Get the tag for a chunk.
*
* @param pos
* @return tag
*/
public CompoundTag getChunkTag(Vector2D pos)
throws ChunkStoreException, IOException {
int x = pos.getBlockX();
int z = pos.getBlockZ();
String folder1 = Integer.toString(divisorMod(x, 64), 36);
String folder2 = Integer.toString(divisorMod(z, 64), 36);
String filename = "c." + Integer.toString(x, 36)
+ "." + Integer.toString(z, 36) + ".dat";
InputStream stream = getInputStream(folder1, folder2, filename);
NBTInputStream nbt = new NBTInputStream(stream);
Tag tag;
tag = nbt.readTag();
if (!(tag instanceof CompoundTag)) {
throw new ChunkStoreException("CompoundTag expected for chunk; got "
+ tag.getClass());
}
stream.close();
return (CompoundTag)tag;
}
/**
* Modulus, divisor-style.
*
* @param a
* @param n
* @return
*/
private static int divisorMod(int a, int n) {
return (int)(a - n * Math.floor(Math.floor(a) / (double)n));
}
/**
* Get the input stream for a chunk file.
*
* @param f1
* @param f2
* @param name
* @return
* @throws IOException
*/
protected abstract InputStream getInputStream(String f1, String f2, String name)
throws IOException;
}

View File

@ -0,0 +1,107 @@
// $Id$
/*
* WorldEdit
* Copyright (C) 2010 sk89q <http://www.sk89q.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.data;
import java.io.*;
import java.util.zip.*;
/**
* Represents the chunk store used by Minecraft alpha but zipped.
*
* @author sk89q
*/
public class ZippedAlphaChunkStore extends NestedFileChunkStore {
/**
* ZIP file.
*/
private File zipFile;
/**
* Actual ZIP.
*/
private ZipFile zip;
/**
* Folder inside the ZIP file to read from, if any.
*/
private String folder;
/**
* Create an instance. The folder argument lets you choose a folder or
* path to look into in the ZIP for the files.
*
* @param zipFile
* @param folder
* @throws IOException
* @throws ZIPException
*/
public ZippedAlphaChunkStore(File zipFile, String folder)
throws IOException, ZipException {
this.zipFile = zipFile;
this.folder = folder;
zip = new ZipFile(zipFile);
}
/**
* Create an instance.
*
* @param zipFile
* @param folder
* @throws IOException
* @throws ZIPException
*/
public ZippedAlphaChunkStore(File zipFile)
throws IOException, ZipException {
this.zipFile = zipFile;
zip = new ZipFile(zipFile);
}
/**
* Get the input stream for a chunk file.
*
* @param f1
* @param f2
* @param name
* @return
* @throws IOException
*/
protected InputStream getInputStream(String f1, String f2, String name)
throws IOException {
String file = f1 + "/" + f2 + "/" + name;
ZipEntry entry = zip.getEntry(file);
if (entry == null) {
throw new IOException("ZIP doesn't contain chunk");
}
try {
return zip.getInputStream(entry);
} catch (ZipException e) {
throw new IOException("Failed to read " + file + " in ZIP");
}
}
/**
* Close resources.
*
* @throws IOException
*/
public void close() throws IOException {
zip.close();
}
}