mirror of
https://github.com/plexusorg/Plex-FAWE.git
synced 2024-12-22 17:27:38 +00:00
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:
parent
6299734cdd
commit
32290b4095
@ -17,16 +17,15 @@
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
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<String,Tag> schematic = new HashMap<String,Tag>();
|
||||
@ -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<String,Tag> 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<String,Tag> 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;
|
||||
|
@ -17,17 +17,18 @@
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
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<BlockVector2D,ArrayList<Vector>> neededChunks =
|
||||
new LinkedHashMap<BlockVector2D,ArrayList<Vector>>();
|
||||
|
||||
// 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<Vector>());
|
||||
}
|
||||
|
||||
neededChunks.get(chunkPos).add(pos);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Now let's start restoring!
|
||||
for (Map.Entry<BlockVector2D,ArrayList<Vector>> 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -17,16 +17,16 @@
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
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.");
|
||||
|
90
src/com/sk89q/worldedit/BlockVector2D.java
Normal file
90
src/com/sk89q/worldedit/BlockVector2D.java
Normal file
@ -0,0 +1,90 @@
|
||||
// $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;
|
||||
|
||||
/**
|
||||
* 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();
|
||||
}
|
||||
}
|
185
src/com/sk89q/worldedit/Vector2D.java
Normal file
185
src/com/sk89q/worldedit/Vector2D.java
Normal file
@ -0,0 +1,185 @@
|
||||
// $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;
|
||||
|
||||
/**
|
||||
*
|
||||
* @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 + ")";
|
||||
}
|
||||
}
|
@ -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<String,Tag> toTileEntityNBT()
|
||||
throws SchematicException {
|
||||
throws DataException {
|
||||
Map<String,Tag> values = new HashMap<String,Tag>();
|
||||
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<String,Tag> 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");
|
||||
|
@ -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<String,Tag> toTileEntityNBT()
|
||||
throws SchematicException ;
|
||||
throws DataException;
|
||||
/**
|
||||
* Get additional information from the title entity data.
|
||||
*
|
||||
* @param values
|
||||
* @throws SchematicException
|
||||
* @throws DataException
|
||||
*/
|
||||
public void fromTileEntityNBT(Map<String,Tag> values)
|
||||
throws SchematicException ;
|
||||
throws DataException;
|
||||
}
|
||||
|
59
src/com/sk89q/worldedit/data/AlphaChunkStore.java
Normal file
59
src/com/sk89q/worldedit/data/AlphaChunkStore.java
Normal 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));
|
||||
}
|
||||
}
|
217
src/com/sk89q/worldedit/data/Chunk.java
Normal file
217
src/com/sk89q/worldedit/data/Chunk.java
Normal 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;
|
||||
}
|
||||
}
|
81
src/com/sk89q/worldedit/data/ChunkStore.java
Normal file
81
src/com/sk89q/worldedit/data/ChunkStore.java
Normal 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 {
|
||||
|
||||
}
|
||||
}
|
@ -17,14 +17,14 @@
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
31
src/com/sk89q/worldedit/data/DataException.java
Normal file
31
src/com/sk89q/worldedit/data/DataException.java
Normal 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);
|
||||
}
|
||||
}
|
30
src/com/sk89q/worldedit/data/InvalidFormatException.java
Normal file
30
src/com/sk89q/worldedit/data/InvalidFormatException.java
Normal 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);
|
||||
}
|
||||
}
|
87
src/com/sk89q/worldedit/data/NestedFileChunkStore.java
Normal file
87
src/com/sk89q/worldedit/data/NestedFileChunkStore.java
Normal 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;
|
||||
}
|
107
src/com/sk89q/worldedit/data/ZippedAlphaChunkStore.java
Normal file
107
src/com/sk89q/worldedit/data/ZippedAlphaChunkStore.java
Normal 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();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user