Implemented Importing and Exporting Tile Entities; Additional Improvements #41

Merged
SenseiKiwi merged 15 commits from master into master 2013-07-15 22:08:53 +00:00
3 changed files with 170 additions and 76 deletions
Showing only changes of commit 1ac934b5e4 - Show all commits

View File

@@ -30,16 +30,16 @@ public class Point3D implements Serializable {
return z; return z;
} }
public int setY(int y)
{
return this.y=y;
}
public int setX(int x) public int setX(int x)
{ {
return this.x = x; return this.x = x;
} }
public int setY(int y)
{
return this.y = y;
}
public int setZ(int z) public int setZ(int z)
{ {
return this.z = z; return this.z = z;
@@ -50,14 +50,57 @@ public class Point3D implements Serializable {
return new Point3D(x, y, z); return new Point3D(x, y, z);
} }
public boolean equals(Point3D other)
{
if (other == null)
return false;
if (this == other)
return true;
return (this.x == other.x && this.y == other.y && this.z == other.z);
}
public boolean equals(Object other) public boolean equals(Object other)
{ {
boolean result = false; return equals((Point3D) other);
if (other instanceof Point3D) }
@Override
public int hashCode()
{ {
Point3D that = (Point3D) other; //Time for some witchcraft.
result = (this.getX() == that.getX() && this.getY() == that.getY()&& this.getY() == that.getZ()); //The code here is inspired by a discussion on Stack Overflow regarding hash codes for 3D.
//Source: http://stackoverflow.com/questions/9858376/hashcode-for-3d-integer-coordinates-with-high-spatial-coherence
//I believe that most of the time, any points we might be hashing will be in close proximity to each other.
//For instance, points that are within the same chunk or within a few neighboring chunks. Only the low-order
//bits of each component would differ. I'll use 8 bits from Y and the 12 bits from X and Z. ~SenseiKiwi
//The result of this could be cached but that would break serialization for this class! >_<
int bit;
int hash;
int index;
hash = 0;
index = 0;
for (bit = 0; bit < 8; bit++)
{
hash |= ((y >> bit) & 1) << index;
index++;
hash |= ((x >> bit) & 1) << index;
index++;
hash |= ((z >> bit) & 1) << index;
index++;
} }
return result; for (; bit < 12; bit++)
{
hash |= ((x >> bit) & 1) << index;
index++;
hash |= ((z >> bit) & 1) << index;
index++;
}
return hash;
} }
} }

View File

@@ -3,6 +3,7 @@ import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.InputStream; import java.io.InputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.Random; import java.util.Random;
import net.minecraft.block.Block; import net.minecraft.block.Block;
@@ -12,11 +13,13 @@ import net.minecraft.block.BlockDoor;
import net.minecraft.block.BlockRedstoneRepeater; import net.minecraft.block.BlockRedstoneRepeater;
import net.minecraft.block.BlockStairs; import net.minecraft.block.BlockStairs;
import net.minecraft.entity.Entity; import net.minecraft.entity.Entity;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.Item; import net.minecraft.item.Item;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompressedStreamTools; import net.minecraft.nbt.CompressedStreamTools;
import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList; import net.minecraft.nbt.NBTTagList;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.tileentity.TileEntityChest; import net.minecraft.tileentity.TileEntityChest;
import net.minecraft.tileentity.TileEntityDispenser; import net.minecraft.tileentity.TileEntityDispenser;
import net.minecraft.util.MathHelper; import net.minecraft.util.MathHelper;
@@ -34,6 +37,7 @@ public class SchematicLoader
{ {
private final static int EAST_DOOR_METADATA = 0; private final static int EAST_DOOR_METADATA = 0;
private final static int NORTH_DOOR_METADATA = 3; private final static int NORTH_DOOR_METADATA = 3;
private final static int MAX_VANILLA_BLOCK_ID = 158;
private DDProperties properties = DDProperties.instance(); private DDProperties properties = DDProperties.instance();
@@ -688,15 +692,16 @@ public class SchematicLoader
//block metaData //block metaData
byte[] blockData = new byte[0]; byte[] blockData = new byte[0];
//first 4 bytes of the block ID //first 8 bits of the block ID
byte[] blockId = new byte[0]; byte[] blockId = new byte[0];
//second 4 bytes of the block ID //additional 4 bits of the block ID
byte[] addId = new byte[0]; byte[] addId = new byte[0];
//Variables for loading tile entities
boolean blockChanged = false;
NBTTagList tileEntities = null; NBTTagList tileEntities = null;
NBTTagCompound[] tileEntityList; HashMap<Point3D, NBTTagCompound> pointToTileEntityMap = new HashMap<Point3D, NBTTagCompound>();
NBTTagList entities;
//the wooden door leading into the pocket //the wooden door leading into the pocket
Point3D schematicEntrance = new Point3D(0,0,0); Point3D schematicEntrance = new Point3D(0,0,0);
@@ -727,37 +732,24 @@ public class SchematicLoader
System.out.println(new File(fname).exists()); System.out.println(new File(fname).exists());
input = new FileInputStream(fname); input = new FileInputStream(fname);
} }
NBTTagCompound nbtdata = CompressedStreamTools.readCompressed(input);
NBTTagCompound schematicTag = CompressedStreamTools.readCompressed(input);
input.close(); //readCompressed() probably closes the stream anyway, but close again to be sure.
//load size of schematic to generate //load size of schematic to generate
width = nbtdata.getShort("Width"); width = schematicTag.getShort("Width");
height = nbtdata.getShort("Height"); height = schematicTag.getShort("Height");
length = nbtdata.getShort("Length"); length = schematicTag.getShort("Length");
//load block info //load block info
blockId = nbtdata.getByteArray("Blocks"); blockId = schematicTag.getByteArray("Blocks");
blockData = nbtdata.getByteArray("Data"); blockData = schematicTag.getByteArray("Data");
addId = nbtdata.getByteArray("AddBlocks"); addId = schematicTag.getByteArray("AddBlocks");
//create combined block list //create combined block list
blocks = new short[blockId.length]; blocks = new short[blockId.length];
//load ticking things //Combine the split block IDs into a single short[]
tileEntities = nbtdata.getTagList("TileEntities");
//tileEntityList = new NBTTagCompound[width*height*length];
/**
for(int count = 0; count<tileEntities.tagCount(); count++)
{
NBTTagCompound tag = (NBTTagCompound)tileEntities.tagAt(count);
tileEntityList[tag.getInteger("y")*width*length+tag.getInteger("z")*width+tag.getInteger("x")]=tag;
}
entities = nbtdata.getTagList("Entities");
tileentities = nbtdata.getTagList("TileEntities");
**/
input.close();
//combine the split block IDs into a single short[]
for (int index = 0; index < blockId.length; index++) for (int index = 0; index < blockId.length; index++)
{ {
if ((index >> 1) >= addId.length) if ((index >> 1) >= addId.length)
@@ -776,6 +768,21 @@ public class SchematicLoader
} }
} }
} }
//Get the list of tile entities
tileEntities = schematicTag.getTagList("TileEntities");
//Map tile entity positions to the tile entity itself using a hash map
int count = tileEntities.tagCount();
for (int index = 0; index < count; index++)
{
NBTTagCompound tileEntityData = (NBTTagCompound) tileEntities.tagAt(index);
Point3D location = new Point3D(
tileEntityData.getInteger("x"),
tileEntityData.getInteger("y"),
tileEntityData.getInteger("z"));
pointToTileEntityMap.put(location, tileEntityData);
}
} }
catch (Exception e) catch (Exception e)
{ {
@@ -801,6 +808,7 @@ public class SchematicLoader
//Coordinates relative to the schematic, start at 0 and increase up to max width/height/length //Coordinates relative to the schematic, start at 0 and increase up to max width/height/length
int x, y, z; int x, y, z;
Point3D schematicPoint = new Point3D(0, 0, 0);
//The real point where a block will be placed //The real point where a block will be placed
int realX, realY, realZ; int realX, realY, realZ;
@@ -816,11 +824,11 @@ public class SchematicLoader
//First loop through the schematic to load in all rift locations and Monolith spawn locations. //First loop through the schematic to load in all rift locations and Monolith spawn locations.
//Also find the entry door, which determines the final position and orientation of the dungeon. //Also find the entry door, which determines the final position and orientation of the dungeon.
for ( x = 0; x < width; ++x) for (y = 0; y < height; y++)
{ {
for ( y = 0; y < height; ++y) for (z = 0; z < length; z++)
{ {
for ( z = 0; z < length; ++z) for (x = 0; x < width; x++)
{ {
int index = y * width * length + z * width + x; int index = y * width * length + z * width + x;
int indexBelow = (y - 1) * width * length + z * width + x; int indexBelow = (y - 1) * width * length + z * width + x;
@@ -857,12 +865,16 @@ public class SchematicLoader
} }
//Loop to actually place the blocks //Loop to actually place the blocks
for (x = 0; x < width; x++)
{
for (z = 0; z < length; z++)
{
for (y = 0; y < height; y++) for (y = 0; y < height; y++)
{ {
schematicPoint.setY(y);
for (z = 0; z < length; z++)
{
schematicPoint.setZ(z);
for (x = 0; x < width; x++)
{
schematicPoint.setX(x);
//Reinitialize pocketPoint with the current schematic coordinate system //Reinitialize pocketPoint with the current schematic coordinate system
pocketPoint.setX(x); pocketPoint.setX(x);
pocketPoint.setY(y); pocketPoint.setY(y);
@@ -877,17 +889,28 @@ public class SchematicLoader
int index = y * width * length + z * width + x; int index = y * width * length + z * width + x;
int currentBlock = blocks[index]; int currentBlock = blocks[index];
int blockMetadata = blockData[index]; int blockMetadata = blockData[index];
NBTTagList tileEntity = tileEntities; blockChanged = false;
//replace tagging blocks with air, and mod blocks with FoR //replace tagging blocks with air, and mod blocks with FoR
if (currentBlock == Block.endPortalFrame.blockID) if (currentBlock == Block.endPortalFrame.blockID)
{ {
currentBlock = 0; currentBlock = 0;
blockChanged = true;
} }
else if ((Block.blocksList[currentBlock] == null && currentBlock != 0) || else if (currentBlock == DungeonHelper.FABRIC_OF_REALITY_EXPORT_ID)
(currentBlock > 158 && currentBlock != mod_pocketDim.blockDimWallPerm.blockID)) //TODO- replace 158 with max vanilla block ID
{ {
currentBlock = mod_pocketDim.blockDimWall.blockID; currentBlock = mod_pocketDim.blockDimWall.blockID;
blockChanged = true;
}
else if (currentBlock == DungeonHelper.PERMAFABRIC_EXPORT_ID)
{
currentBlock = mod_pocketDim.blockDimWallPerm.blockID;
blockChanged = true;
}
else if ((Block.blocksList[currentBlock] == null && currentBlock != 0) || currentBlock > MAX_VANILLA_BLOCK_ID)
{
currentBlock = mod_pocketDim.blockDimWall.blockID;
blockChanged = true;
} }
//Place blocks and set metadata //Place blocks and set metadata
@@ -901,40 +924,56 @@ public class SchematicLoader
if (currentBlock == Block.doorIron.blockID) if (currentBlock == Block.doorIron.blockID)
{ {
setBlockDirectly(world, realX, realY, realZ, properties.DimensionalDoorID, fixedMetadata); setBlockDirectly(world, realX, realY, realZ, properties.DimensionalDoorID, fixedMetadata);
blockChanged = true;
} }
else if (currentBlock == Block.doorWood.blockID) else if (currentBlock == Block.doorWood.blockID)
{ {
setBlockDirectly(world, realX, realY, realZ, properties.WarpDoorID, fixedMetadata); setBlockDirectly(world, realX, realY, realZ, properties.WarpDoorID, fixedMetadata);
blockChanged = true;
} }
else else
{ {
setBlockDirectly(world, realX, realY, realZ, currentBlock, fixedMetadata); setBlockDirectly(world, realX, realY, realZ, currentBlock, fixedMetadata);
} }
//Fill containers (e.g. chests, dispensers) //Load the tile entity at this location if any exists, but only if the block wasn't changed
if (!blockChanged)
{
NBTTagCompound tileEntityData = pointToTileEntityMap.get(schematicPoint);
if (tileEntityData != null)
{
//Change the tile entity's position
tileEntityData.setInteger("x", realX);
tileEntityData.setInteger("y", realY);
tileEntityData.setInteger("z", realZ);
//Load the tile entity into the world
world.setBlockTileEntity(realX, realY, realZ, TileEntity.createAndLoadEntity(tileEntityData));
}
}
//Fill empty chests and dispensers
if (Block.blocksList[currentBlock] instanceof BlockContainer) if (Block.blocksList[currentBlock] instanceof BlockContainer)
{ {
/** TileEntity tileEntity = world.getBlockTileEntity(realX, realY, realZ);
TileEntity tile = world.getBlockTileEntity(i+xCooe, j+yCooe, k+zCooe);
NBTTagCompound tag = this.tileEntityList[index];
if(tag!=null)
{
tile.readFromNBT(tag);
}
**/
//Fill chests //Fill chests
if (world.getBlockTileEntity(realX, realY, realZ) instanceof TileEntityChest) if (tileEntity instanceof TileEntityChest)
{
TileEntityChest chest = (TileEntityChest) tileEntity;
if (isInventoryEmpty(chest))
{ {
TileEntityChest chest = (TileEntityChest) world.getBlockTileEntity(realX, realY, realZ);
ChestGenHooks info = DDLoot.DungeonChestInfo; ChestGenHooks info = DDLoot.DungeonChestInfo;
WeightedRandomChestContent.generateChestContents(rand, info.getItems(rand), chest, info.getCount(rand)); WeightedRandomChestContent.generateChestContents(rand, info.getItems(rand), chest, info.getCount(rand));
} }
}
//Fill dispensers //Fill dispensers
if (world.getBlockTileEntity(realX, realY, realZ) instanceof TileEntityDispenser) if (tileEntity instanceof TileEntityDispenser)
{
TileEntityDispenser dispenser = (TileEntityDispenser) tileEntity;
if (isInventoryEmpty(dispenser))
{ {
TileEntityDispenser dispenser = (TileEntityDispenser) world.getBlockTileEntity(realX, realY, realZ);
dispenser.addItem(new ItemStack(Item.arrow, 64)); dispenser.addItem(new ItemStack(Item.arrow, 64));
} }
} }
@@ -942,6 +981,7 @@ public class SchematicLoader
} }
} }
} }
}
//We'll use an extra block here to restrict the scope of these variables to just inside the block //We'll use an extra block here to restrict the scope of these variables to just inside the block
//This is to avoid declaration conflicts with the loops below //This is to avoid declaration conflicts with the loops below
@@ -958,8 +998,6 @@ public class SchematicLoader
entranceRiftLocation.getX(), entranceRiftLocation.getX(),
entranceRiftLocation.getY() - 1, entranceRiftLocation.getY() - 1,
entranceRiftLocation.getZ()); entranceRiftLocation.getZ());
//TODO: Remove this debug print
System.out.println("Metadata Orientation: " + sideLink.linkOrientation);
} }
//Generate the LinkData defined by the door placement, Iron Dim doors first //Generate the LinkData defined by the door placement, Iron Dim doors first
@@ -1077,7 +1115,20 @@ public class SchematicLoader
} }
} }
private void transformPoint(Point3D position, Point3D srcOrigin, int angle, Point3D destOrigin) private static boolean isInventoryEmpty(IInventory inventory)
{
int size = inventory.getSizeInventory();
for (int index = 0; index < size; index++)
{
if (inventory.getStackInSlot(index) != null)
{
return false;
}
}
return true;
}
private static void transformPoint(Point3D position, Point3D srcOrigin, int angle, Point3D destOrigin)
{ {
//This function receives a position (e.g. point in schematic space), translates it relative //This function receives a position (e.g. point in schematic space), translates it relative
//to a source coordinate system (e.g. the point that will be the center of a schematic), //to a source coordinate system (e.g. the point that will be the center of a schematic),

View File

@@ -397,11 +397,11 @@ public class DungeonHelper
//This could be done more efficiently, but honestly, this is the simplest approach and it //This could be done more efficiently, but honestly, this is the simplest approach and it
//makes it easy for us to verify that the code is correct. //makes it easy for us to verify that the code is correct.
for (int x = xStart; x <= xEnd; x++) for (int y = yStart; y <= yEnd; y++)
{ {
for (int z = zStart; z <= zEnd; z++) for (int z = zStart; z <= zEnd; z++)
{ {
for (int y = yStart; y <= yEnd; y++) for (int x = xStart; x <= xEnd; x++)
{ {
if (!world.isAirBlock(x, y, z)) if (!world.isAirBlock(x, y, z))
{ {
@@ -427,11 +427,11 @@ public class DungeonHelper
byte[] blockData = new byte[width * height * length]; byte[] blockData = new byte[width * height * length];
NBTTagList tileEntities = new NBTTagList(); NBTTagList tileEntities = new NBTTagList();
for (int x = 0; x < width; x++) for (int y = 0; y < height; y++)
{ {
for (int z = 0; z < length; z++) for (int z = 0; z < length; z++)
{ {
for (int y = 0; y < height; y++) for (int x = 0; x < width; x++)
{ {
int index = y * width * length + z * width + x; int index = y * width * length + z * width + x;
int blockID = world.getBlockId(x + xMin, y + yMin, z + zMin); int blockID = world.getBlockId(x + xMin, y + yMin, z + zMin);