From 32290b4095dce2d3706c5d810b0ad8f00aaa2378 Mon Sep 17 00:00:00 2001 From: sk89q Date: Wed, 20 Oct 2010 02:12:16 -0700 Subject: [PATCH] Added data framework (com.sk89q.worldedit.data.*); moved schematic loading/saving towards data framework; added work-in-progress EditSession.restoreBackup(). --- src/CuboidClipboard.java | 35 ++- src/EditSession.java | 70 +++++- src/WorldEdit.java | 12 +- src/com/sk89q/worldedit/BlockVector2D.java | 90 ++++++++ src/com/sk89q/worldedit/Vector2D.java | 185 +++++++++++++++ src/com/sk89q/worldedit/blocks/SignBlock.java | 12 +- .../worldedit/blocks/TileEntityBlock.java | 10 +- .../sk89q/worldedit/data/AlphaChunkStore.java | 59 +++++ src/com/sk89q/worldedit/data/Chunk.java | 217 ++++++++++++++++++ src/com/sk89q/worldedit/data/ChunkStore.java | 81 +++++++ .../ChunkStoreException.java} | 10 +- .../sk89q/worldedit/data/DataException.java | 31 +++ .../data/InvalidFormatException.java | 30 +++ .../worldedit/data/NestedFileChunkStore.java | 87 +++++++ .../worldedit/data/ZippedAlphaChunkStore.java | 107 +++++++++ 15 files changed, 991 insertions(+), 45 deletions(-) create mode 100644 src/com/sk89q/worldedit/BlockVector2D.java create mode 100644 src/com/sk89q/worldedit/Vector2D.java create mode 100644 src/com/sk89q/worldedit/data/AlphaChunkStore.java create mode 100644 src/com/sk89q/worldedit/data/Chunk.java create mode 100644 src/com/sk89q/worldedit/data/ChunkStore.java rename src/com/sk89q/worldedit/{SchematicException.java => data/ChunkStoreException.java} (80%) create mode 100644 src/com/sk89q/worldedit/data/DataException.java create mode 100644 src/com/sk89q/worldedit/data/InvalidFormatException.java create mode 100644 src/com/sk89q/worldedit/data/NestedFileChunkStore.java create mode 100644 src/com/sk89q/worldedit/data/ZippedAlphaChunkStore.java diff --git a/src/CuboidClipboard.java b/src/CuboidClipboard.java index 48c94251e..2ec613243 100644 --- a/src/CuboidClipboard.java +++ b/src/CuboidClipboard.java @@ -17,16 +17,15 @@ * along with this program. If not, see . */ -import com.sk89q.worldedit.blocks.TileEntityBlock; -import com.sk89q.worldedit.blocks.SignBlock; -import com.sk89q.worldedit.blocks.BaseBlock; +import com.sk89q.worldedit.*; +import com.sk89q.worldedit.blocks.*; +import com.sk89q.worldedit.data.*; import org.jnbt.*; import java.io.*; import java.util.Map; import java.util.HashMap; import java.util.ArrayList; import java.util.List; -import com.sk89q.worldedit.*; /** * The clipboard remembers the state of a cuboid region. @@ -203,21 +202,21 @@ public class CuboidClipboard { * * @param path * @throws IOException - * @throws SchematicException + * @throws DataException */ - public void saveSchematic(String path) throws IOException, SchematicException { + public void saveSchematic(String path) throws IOException, DataException { int width = getWidth(); int height = getHeight(); int length = getLength(); if (width > 65535) { - throw new SchematicException("Width of region too large for a .schematic"); + throw new DataException("Width of region too large for a .schematic"); } if (height > 65535) { - throw new SchematicException("Height of region too large for a .schematic"); + throw new DataException("Height of region too large for a .schematic"); } if (length > 65535) { - throw new SchematicException("Length of region too large for a .schematic"); + throw new DataException("Length of region too large for a .schematic"); } HashMap schematic = new HashMap(); @@ -278,24 +277,24 @@ public class CuboidClipboard { * @param path * @param origin * @return clipboard - * @throws SchematicException + * @throws DataException * @throws IOException */ public static CuboidClipboard loadSchematic(String path) - throws SchematicException, IOException { + throws DataException, IOException { FileInputStream stream = new FileInputStream(path); NBTInputStream nbtStream = new NBTInputStream(stream); // Schematic tag CompoundTag schematicTag = (CompoundTag)nbtStream.readTag(); if (!schematicTag.getName().equals("Schematic")) { - throw new SchematicException("Tag \"Schematic\" does not exist or is not first"); + throw new DataException("Tag \"Schematic\" does not exist or is not first"); } // Check Map schematic = schematicTag.getValue(); if (!schematic.containsKey("Blocks")) { - throw new SchematicException("Schematic file is missing a \"Blocks\" tag"); + throw new DataException("Schematic file is missing a \"Blocks\" tag"); } // Get information @@ -306,7 +305,7 @@ public class CuboidClipboard { // Check type of Schematic String materials = (String)getChildTag(schematic, "Materials", StringTag.class).getValue(); if (!materials.equals("Alpha")) { - throw new SchematicException("Schematic file is not an Alpha schematic"); + throw new DataException("Schematic file is not an Alpha schematic"); } // Get blocks @@ -386,16 +385,16 @@ public class CuboidClipboard { * @param key * @param expected * @return child tag - * @throws SchematicException + * @throws DataException */ private static Tag getChildTag(Map items, String key, Class expected) - throws SchematicException { + throws DataException { if (!items.containsKey(key)) { - throw new SchematicException("Schematic file is missing a \"" + key + "\" tag"); + throw new DataException("Schematic file is missing a \"" + key + "\" tag"); } Tag tag = items.get(key); if (!expected.isInstance(tag)) { - throw new SchematicException( + throw new DataException( key + " tag is not of tag type " + expected.getName()); } return tag; diff --git a/src/EditSession.java b/src/EditSession.java index c821d32c7..001fa2e2f 100644 --- a/src/EditSession.java +++ b/src/EditSession.java @@ -17,17 +17,18 @@ * along with this program. If not, see . */ -import com.sk89q.worldedit.regions.Region; -import com.sk89q.worldedit.regions.CuboidRegion; -import com.sk89q.worldedit.blocks.SignBlock; -import com.sk89q.worldedit.blocks.BaseBlock; +import com.sk89q.worldedit.*; +import com.sk89q.worldedit.regions.*; +import com.sk89q.worldedit.blocks.*; +import com.sk89q.worldedit.data.*; +import java.io.IOException; import java.util.Map; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.Set; import java.util.HashSet; import java.util.Stack; -import com.sk89q.worldedit.*; +import java.util.ArrayList; /** * This class can wrap all block editing operations into one "edit session" that @@ -1278,4 +1279,63 @@ public class EditSession { return affected; } + + /** + * Restores a region from a backup. + * + * @param region + * @param chunkStore + */ + public void restoreBackup(Region region, ChunkStore chunkStore) + throws MaxChangedBlocksException { + // TODO: Make this support non-cuboid regions + + Vector min = region.getMinimumPoint(); + Vector max = region.getMaximumPoint(); + + Map> neededChunks = + new LinkedHashMap>(); + + // First, we need to group points by chunk so that we only need + // to keep one chunk in memory at any given moment + for (int x = min.getBlockX(); x <= max.getBlockX(); x++) { + for (int y = min.getBlockY(); y <= max.getBlockY(); y++) { + for (int z = min.getBlockZ(); z <= max.getBlockZ(); z++) { + Vector pos = new Vector(x, y, z); + BlockVector2D chunkPos = ChunkStore.toChunk(pos); + + // Unidentified chunk + if (!neededChunks.containsKey(chunkPos)) { + neededChunks.put(chunkPos, new ArrayList()); + } + + neededChunks.get(chunkPos).add(pos); + } + } + } + + // Now let's start restoring! + for (Map.Entry> entry : + neededChunks.entrySet()) { + BlockVector2D chunkPos = entry.getKey(); + Chunk chunk; + + try { + chunk = chunkStore.getChunk(chunkPos); + // Good, the chunk could be at least loaded + + // Now just copy blocks! + for (Vector pos : entry.getValue()) { + BaseBlock block = chunk.getBlock(pos); + setBlock(pos, block); + } + } catch (DataException de) { + // TODO: Error handling + de.printStackTrace(); + } catch (IOException ioe) { + // TODO: Error handling + ioe.printStackTrace(); + } + } + } } diff --git a/src/WorldEdit.java b/src/WorldEdit.java index db651caf8..b2cc01f07 100644 --- a/src/WorldEdit.java +++ b/src/WorldEdit.java @@ -17,16 +17,16 @@ * along with this program. If not, see . */ +import com.sk89q.worldedit.*; +import com.sk89q.worldedit.blocks.*; +import com.sk89q.worldedit.data.*; import com.sk89q.worldedit.regions.Region; -import com.sk89q.worldedit.blocks.BaseBlock; import java.util.HashMap; import java.util.HashSet; import java.util.Arrays; import java.util.logging.Level; import java.util.logging.Logger; import java.io.*; -import com.sk89q.worldedit.*; -import com.sk89q.worldedit.blocks.*; /** * Plugin base. @@ -590,8 +590,8 @@ public class WorldEdit { logger.log(Level.INFO, player.getName() + " loaded " + filePath); player.print(filename + " loaded."); } - /*} catch (SchematicException e) { - player.printError("Load error: " + e.getMessage());*/ + } catch (DataException e) { + player.printError("Load error: " + e.getMessage()); } catch (IOException e) { player.printError("Schematic could not read or it does not exist."); } @@ -629,7 +629,7 @@ public class WorldEdit { logger.log(Level.INFO, player.getName() + " saved " + filePath); player.print(filename + " saved."); } - } catch (SchematicException se) { + } catch (DataException se) { player.printError("Save error: " + se.getMessage()); } catch (IOException e) { player.printError("Schematic could not written."); diff --git a/src/com/sk89q/worldedit/BlockVector2D.java b/src/com/sk89q/worldedit/BlockVector2D.java new file mode 100644 index 000000000..c05071c8d --- /dev/null +++ b/src/com/sk89q/worldedit/BlockVector2D.java @@ -0,0 +1,90 @@ +// $Id$ +/* + * WorldEdit + * Copyright (C) 2010 sk89q + * + * 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 . +*/ + +package com.sk89q.worldedit; + +/** + * Extension of Vector2D that supports being compared as ints (for accuracy). + * + * @author sk89q + */ +public class BlockVector2D extends Vector2D { + /** + * Construct the Vector object. + * + * @param pt + */ + public BlockVector2D(Vector2D pt) { + super(pt); + } + + /** + * Construct the Vector object. + * + * @param pt + */ + public BlockVector2D(int x, int z) { + super(x, z); + } + + /** + * Construct the Vector object. + * + * @param pt + */ + public BlockVector2D(float x, float z) { + super(x, z); + } + + /** + * Construct the Vector object. + * + * @param pt + */ + public BlockVector2D(double x, double z) { + super(x, z); + } + + /** + * Checks if another object is equivalent. + * + * @param obj + * @return whether the other object is equivalent + */ + @Override + public boolean equals(Object obj) { + if (!(obj instanceof Vector2D)) { + return false; + } + Vector2D other = (Vector2D)obj; + return (int)other.x == (int)this.x && (int)other.z == (int)this.z; + + } + + /** + * Gets the hash code. + * + * @return hash code + */ + @Override + public int hashCode() { + return (Integer.valueOf((int)x).hashCode() >> 13) ^ + Integer.valueOf((int)z).hashCode(); + } +} diff --git a/src/com/sk89q/worldedit/Vector2D.java b/src/com/sk89q/worldedit/Vector2D.java new file mode 100644 index 000000000..14ad7c5b1 --- /dev/null +++ b/src/com/sk89q/worldedit/Vector2D.java @@ -0,0 +1,185 @@ +// $Id$ +/* + * WorldEdit + * Copyright (C) 2010 sk89q + * + * 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 . +*/ + +package com.sk89q.worldedit; + +/** + * + * @author Albert + */ +public class Vector2D { + protected final double x, z; + + /** + * Construct the Vector2D object. + * + * @param x + * @param z + */ + public Vector2D(double x, double z) { + this.x = x; + this.z = z; + } + + /** + * Construct the Vector2D object. + * + * @param x + * @param z + */ + public Vector2D(int x, int z) { + this.x = (double)x; + this.z = (double)z; + } + + /** + * Construct the Vector2D object. + * + * @param x + * @param y + * @param z + */ + public Vector2D(float x, float z) { + this.x = (double)x; + this.z = (double)z; + } + + /** + * Construct the Vector2D object. + * + * @param pt + */ + public Vector2D(Vector2D pt) { + this.x = pt.x; + this.z = pt.z; + } + + /** + * Construct the Vector2D object. + */ + public Vector2D() { + this.x = 0; + this.z = 0; + } + + /** + * @return the x + */ + public double getX() { + return x; + } + + /** + * @return the x + */ + public int getBlockX() { + return (int)Math.round(x); + } + + /** + * Set X. + * + * @param x + * @return new vector + */ + public Vector2D setX(double x) { + return new Vector2D(x, z); + } + + /** + * Set X. + * + * @param x + * @return new vector + */ + public Vector2D setX(int x) { + return new Vector2D(x, z); + } + + /** + * @return the z + */ + public double getZ() { + return z; + } + + /** + * @return the z + */ + public int getBlockZ() { + return (int)Math.round(z); + } + + /** + * Set Z. + * + * @param z + * @return new vector + */ + public Vector2D setZ(double z) { + return new Vector2D(x, z); + } + + /** + * Set Z. + * + * @param z + * @return new vector + */ + public Vector2D setZ(int z) { + return new Vector2D(x, z); + } + + /** + * Checks if another object is equivalent. + * + * @param obj + * @return whether the other object is equivalent + */ + @Override + public boolean equals(Object obj) { + if (!(obj instanceof Vector2D)) { + return false; + } + Vector other = (Vector)obj; + return other.x == this.x && other.z == this.z; + + } + + /** + * Gets the hash code. + * + * @return hash code + */ + @Override + public int hashCode() { + return ((new Double(x)).hashCode() >> 13) ^ + (new Double(z)).hashCode(); + } + + /** + * Returns string representation "(x, y, z)". + * + * @return string + */ + @Override + public String toString() { + return "(" + x + ", " + z + ")"; + } +} diff --git a/src/com/sk89q/worldedit/blocks/SignBlock.java b/src/com/sk89q/worldedit/blocks/SignBlock.java index c58b9c36c..18a39a3f1 100644 --- a/src/com/sk89q/worldedit/blocks/SignBlock.java +++ b/src/com/sk89q/worldedit/blocks/SignBlock.java @@ -19,7 +19,7 @@ package com.sk89q.worldedit.blocks; -import com.sk89q.worldedit.SchematicException; +import com.sk89q.worldedit.data.*; import java.util.Map; import java.util.HashMap; import org.jnbt.*; @@ -81,10 +81,10 @@ public class SignBlock extends BaseBlock implements TileEntityBlock { * Store additional tile entity data. Returns true if the data is used. * * @return map of values - * @throws SchematicException + * @throws DataException */ public Map toTileEntityNBT() - throws SchematicException { + throws DataException { Map values = new HashMap(); values.put("Text1", new StringTag("Text1", text[0])); values.put("Text2", new StringTag("Text2", text[1])); @@ -97,10 +97,10 @@ public class SignBlock extends BaseBlock implements TileEntityBlock { * Get additional information from the title entity data. * * @param values - * @throws SchematicException + * @throws DataException */ public void fromTileEntityNBT(Map values) - throws SchematicException { + throws DataException { if (values == null) { return; } @@ -111,7 +111,7 @@ public class SignBlock extends BaseBlock implements TileEntityBlock { t = values.get("id"); if (!(t instanceof StringTag) || !((StringTag)t).getValue().equals("Sign")) { - throw new SchematicException("'Sign' tile entity expected"); + throw new DataException("'Sign' tile entity expected"); } t = values.get("Text1"); diff --git a/src/com/sk89q/worldedit/blocks/TileEntityBlock.java b/src/com/sk89q/worldedit/blocks/TileEntityBlock.java index e07fa9e91..4cf06ee84 100644 --- a/src/com/sk89q/worldedit/blocks/TileEntityBlock.java +++ b/src/com/sk89q/worldedit/blocks/TileEntityBlock.java @@ -19,7 +19,7 @@ package com.sk89q.worldedit.blocks; -import com.sk89q.worldedit.SchematicException; +import com.sk89q.worldedit.data.*; import java.util.Map; import org.jnbt.Tag; @@ -39,16 +39,16 @@ public interface TileEntityBlock { * Store additional tile entity data. * * @return map of values - * @throws SchematicException + * @throws DataException */ public Map toTileEntityNBT() - throws SchematicException ; + throws DataException; /** * Get additional information from the title entity data. * * @param values - * @throws SchematicException + * @throws DataException */ public void fromTileEntityNBT(Map values) - throws SchematicException ; + throws DataException; } diff --git a/src/com/sk89q/worldedit/data/AlphaChunkStore.java b/src/com/sk89q/worldedit/data/AlphaChunkStore.java new file mode 100644 index 000000000..cd7a61703 --- /dev/null +++ b/src/com/sk89q/worldedit/data/AlphaChunkStore.java @@ -0,0 +1,59 @@ +// $Id$ +/* + * WorldEdit + * Copyright (C) 2010 sk89q + * + * 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 . +*/ + +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)); + } +} diff --git a/src/com/sk89q/worldedit/data/Chunk.java b/src/com/sk89q/worldedit/data/Chunk.java new file mode 100644 index 000000000..9fa32f35e --- /dev/null +++ b/src/com/sk89q/worldedit/data/Chunk.java @@ -0,0 +1,217 @@ +// $Id$ +/* + * WorldEdit + * Copyright (C) 2010 sk89q + * + * 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 . +*/ + + +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> 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 tags = (List)((ListTag)getChildTag( + rootTag.getValue(), "TileEntities", ListTag.class)) + .getValue(); + + tileEntities = new HashMap>(); + + 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 values = new HashMap(); + + for (Map.Entry 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 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 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 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; + } +} diff --git a/src/com/sk89q/worldedit/data/ChunkStore.java b/src/com/sk89q/worldedit/data/ChunkStore.java new file mode 100644 index 000000000..cb51c92e8 --- /dev/null +++ b/src/com/sk89q/worldedit/data/ChunkStore.java @@ -0,0 +1,81 @@ +// $Id$ +/* + * WorldEdit + * Copyright (C) 2010 sk89q + * + * 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 . +*/ + +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 { + + } +} diff --git a/src/com/sk89q/worldedit/SchematicException.java b/src/com/sk89q/worldedit/data/ChunkStoreException.java similarity index 80% rename from src/com/sk89q/worldedit/SchematicException.java rename to src/com/sk89q/worldedit/data/ChunkStoreException.java index ea4cfbd55..b3d490645 100644 --- a/src/com/sk89q/worldedit/SchematicException.java +++ b/src/com/sk89q/worldedit/data/ChunkStoreException.java @@ -17,14 +17,14 @@ * along with this program. If not, see . */ -package com.sk89q.worldedit; +package com.sk89q.worldedit.data; /** * - * @author Albert + * @author sk89q */ -public class SchematicException extends WorldEditException { - public SchematicException(String error) { - super(error); +public class ChunkStoreException extends DataException { + public ChunkStoreException(String msg) { + super(msg); } } diff --git a/src/com/sk89q/worldedit/data/DataException.java b/src/com/sk89q/worldedit/data/DataException.java new file mode 100644 index 000000000..cd71d60c8 --- /dev/null +++ b/src/com/sk89q/worldedit/data/DataException.java @@ -0,0 +1,31 @@ +// $Id$ +/* + * WorldEdit + * Copyright (C) 2010 sk89q + * + * 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 . +*/ + +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); + } +} diff --git a/src/com/sk89q/worldedit/data/InvalidFormatException.java b/src/com/sk89q/worldedit/data/InvalidFormatException.java new file mode 100644 index 000000000..27a5ee170 --- /dev/null +++ b/src/com/sk89q/worldedit/data/InvalidFormatException.java @@ -0,0 +1,30 @@ +// $Id$ +/* + * WorldEdit + * Copyright (C) 2010 sk89q + * + * 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 . +*/ + +package com.sk89q.worldedit.data; + +/** + * + * @author sk89q + */ +public class InvalidFormatException extends DataException { + public InvalidFormatException(String msg) { + super(msg); + } +} diff --git a/src/com/sk89q/worldedit/data/NestedFileChunkStore.java b/src/com/sk89q/worldedit/data/NestedFileChunkStore.java new file mode 100644 index 000000000..e8b1db4ac --- /dev/null +++ b/src/com/sk89q/worldedit/data/NestedFileChunkStore.java @@ -0,0 +1,87 @@ +// $Id$ +/* + * WorldEdit + * Copyright (C) 2010 sk89q + * + * 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 . +*/ + +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; +} diff --git a/src/com/sk89q/worldedit/data/ZippedAlphaChunkStore.java b/src/com/sk89q/worldedit/data/ZippedAlphaChunkStore.java new file mode 100644 index 000000000..5054fa585 --- /dev/null +++ b/src/com/sk89q/worldedit/data/ZippedAlphaChunkStore.java @@ -0,0 +1,107 @@ +// $Id$ +/* + * WorldEdit + * Copyright (C) 2010 sk89q + * + * 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 . +*/ + +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(); + } +}