Advertisement
Guest User

Untitled

a guest
Jun 20th, 2015
327
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.78 KB | None | 0 0
  1. public class DATAFileFormatHelper {
  2.  
  3.     public static NBTTagCompound saveWorldRegionToNBT(World world, BlockPOS pos1,
  4.             BlockPOS pos2) {
  5.         NBTTagCompound compound = new NBTTagCompound();
  6.         if(pos1.x>pos2.x){
  7.             int temp1=pos1.x;
  8.             int temp2=pos2.x;
  9.             pos2.x=temp1;
  10.             pos1.x=temp2;
  11.         }
  12.         if(pos1.y>pos2.y){
  13.             int temp1=pos1.y;
  14.             int temp2=pos2.y;
  15.             pos2.y=temp1;
  16.             pos1.y=temp2;
  17.         }
  18.         if(pos1.z>pos2.z){
  19.             int temp1=pos1.z;
  20.             int temp2=pos2.z;
  21.             pos2.z=temp1;
  22.             pos1.z=temp2;
  23.         }
  24.         int height =Math.abs( pos1.y - pos2.y);
  25.         int width = Math.abs(pos1.x - pos2.x);
  26.         int depth = Math.abs(pos1.z - pos2.z);
  27.         System.out.println("Pos1:"+pos1);
  28.         System.out.println("Pos2:"+pos2);
  29.         System.out.println("height: "+height+"width: "+width+"depth: "+depth);
  30.         for (int y = pos1.y; y < pos2.y; y++) {
  31.             for (int x= pos1.x; x < pos2.x; x++) {
  32.                 for (int z = pos1.z; z < pos2.z; z++) {
  33.                     int relativeX=Math.abs(pos1.x-x);
  34.                     int relativeZ=Math.abs(pos1.z-z);
  35.                     int relativeY=Math.abs(pos1.y-y);
  36.                     Block currentBlock = world.getBlock(x, y, z);
  37.                     NBTTagCompound currentBlockCompound = new NBTTagCompound();
  38.                     NBTTagCompound tileEntityCompound=new NBTTagCompound();
  39.                     String currentBlockName = (String) Block.blockRegistry
  40.                             .getNameForObject(currentBlock);
  41.                    
  42.                     Integer metadata = world.getBlockMetadata(x, y, z);
  43.                     System.out.println("processing block at:" +new BlockPOS(x, y, z)+"relative condinates:"+new BlockPOS(relativeX, relativeY, relativeZ)+"Block type:"+currentBlockName+"matedata:"+metadata);
  44.                     currentBlockCompound.setBoolean("hasTileEntity", currentBlock.hasTileEntity(metadata));
  45.                     currentBlockCompound.setString("blockid", currentBlockName);
  46.                     currentBlockCompound.setInteger("metadata", metadata);
  47.                     String name = "Block|"+relativeX+"|"+relativeY+"|"+relativeZ;
  48.                     if(currentBlock.hasTileEntity(metadata)){
  49.                         world.getTileEntity(x, y, z).writeToNBT(tileEntityCompound);
  50.                     }
  51.                     currentBlockCompound.setTag("tileEntityTag", tileEntityCompound);
  52.                     compound.setTag(name, currentBlockCompound);
  53.                 }
  54.             }
  55.         }
  56.         compound.setInteger("width", width);
  57.         compound.setInteger("height", height);
  58.         compound.setInteger("depth", depth);
  59.         return compound;
  60.     }
  61.     public static NBTTagCompound loadWorldRegionFromNBT(World world, BlockPOS pos,NBTTagCompound compound) {
  62.        
  63.         System.out.println("Starting generating structure at: "+pos);
  64.         int height=compound.getInteger("height");
  65.         int width=compound.getInteger("width");
  66.         int depth=compound.getInteger("depth");
  67.         System.out.println("height: "+height+"width: "+width+"depth: "+depth);
  68.         for (int y = pos.y; y < pos.y+height; y++) {
  69.             for (int x= pos.x; x < pos.x+width; x++) {
  70.                 for (int z = pos.z; z < pos.z+depth; z++) {
  71.                     int relativeX=x-pos.x;
  72.                     int relativeY=x-pos.x;
  73.                     int relativeZ=x-pos.x;
  74.                    
  75.                     NBTTagCompound currentBlockCompound = (NBTTagCompound) compound.getTag("Block|"+relativeX+"|"+relativeY+"|"+relativeZ);
  76.                     if(currentBlockCompound==null){
  77.                         Minecraft.getMinecraft().crashed(new CrashReport("compound is null", new NullPointerException()));
  78.                     }
  79.                     NBTTagCompound tileEntityCompound=currentBlockCompound.getCompoundTag("tileEntityTag");
  80.                     int metadata=currentBlockCompound.getInteger("metadata");
  81.                    
  82.                     Block currentBlock=Block.getBlockFromName(currentBlockCompound.getString("blockid"));
  83.                     System.out.println("Setting block at: "+new BlockPOS(x, y, z));
  84.                     world.setBlock(x, y, z, currentBlock, metadata, 2);
  85.                     if(currentBlockCompound.getBoolean("hasTileEntity")){
  86.                         world.getTileEntity(x, y, z).readFromNBT(tileEntityCompound);
  87.                     }
  88.                    
  89.  
  90.                
  91.                 }
  92.             }
  93.         }
  94.        
  95.         return compound;
  96.     }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement