Advertisement
Guest User

World save algorithm

a guest
Jun 19th, 2015
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.89 KB | None | 0 0
  1. public static NBTTagCompound saveWorldRegionToNBT(World world, BlockPOS pos1,
  2.             BlockPOS pos2) {
  3.         NBTTagCompound compound = new NBTTagCompound();
  4.         if(pos1.x>pos2.x){
  5.             int temp1=pos1.x;
  6.             int temp2=pos2.x;
  7.             pos2.x=temp1;
  8.             pos1.x=temp2;
  9.         }
  10.         if(pos1.y>pos2.y){
  11.             int temp1=pos1.y;
  12.             int temp2=pos2.y;
  13.             pos2.y=temp1;
  14.             pos1.y=temp2;
  15.         }
  16.         if(pos1.z>pos2.z){
  17.             int temp1=pos1.z;
  18.             int temp2=pos2.z;
  19.             pos2.z=temp1;
  20.             pos1.z=temp2;
  21.         }
  22.         int height = pos1.y - pos2.y;
  23.         int width = pos1.x - pos2.x;
  24.         int depth = pos1.z - pos2.z;
  25.         for (int y = pos1.y; y < height + 1; y++) {
  26.             for (int x = 0; x < width + 1; x++) {
  27.                 for (int z = 0; z < depth + 1; z++) {
  28.                     int relativeX=pos1.x-x;
  29.                     int relativeZ=pos1.z-z;
  30.                     int relativeY=pos1.y-y;
  31.                     Block currentBlock = world.getBlock(x, y, z);
  32.                     NBTTagCompound currentBlockCompound = new NBTTagCompound();
  33.                     NBTTagCompound tileEntityCompound=new NBTTagCompound();
  34.                     String currentBlockName = (String) Block.blockRegistry
  35.                             .getNameForObject(currentBlock);
  36.                    
  37.                     Integer metadata = world.getBlockMetadata(x, y, z);
  38.                     System.out.println("processing block at:" +new BlockPOS(x, y, z)+"relative condinates:"+new BlockPOS(relativeX, relativeY, relativeZ)+"Block type:"+currentBlockName+"matedata:"+metadata);
  39.                     currentBlockCompound.setBoolean("hasTileEntity", currentBlock.hasTileEntity(metadata));
  40.                     currentBlockCompound.setString("blockid", currentBlockName);
  41.                     currentBlockCompound.setInteger("metadata", metadata);
  42.                     String name = String.format("Block|%x|%y|%z", relativeX, relativeY, relativeZ);
  43.                     if(currentBlock.hasTileEntity(metadata)){
  44.                         world.getTileEntity(z, y, z).writeToNBT(tileEntityCompound);
  45.                     }
  46.                     currentBlockCompound.setTag("tileEntityTag", tileEntityCompound);
  47.                     compound.setTag(name, currentBlockCompound);
  48.                 }
  49.             }
  50.         }
  51.        
  52.         return compound;
  53.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement