Advertisement
Corosus

Untitled

Dec 3rd, 2012
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.77 KB | None | 0 0
  1. package build.world;
  2.  
  3. import java.io.FileInputStream;
  4. /*import net.minecraft.src.mod_ZombieCraft;*/
  5.  
  6. public class Build {
  7.  
  8.     //public int id = 0;
  9.     public String file = "";
  10.  
  11.     //Basic placement data
  12.     /*public int startX = 0;
  13.     public int startY = 0;
  14.     public int startZ = 0;
  15.    
  16.     public int sizeX = 0;
  17.     public int sizeY = 0;
  18.     public int sizeZ = 0;*/
  19.    
  20.     //visual data
  21.     public int curTick = 0;
  22.     public int maxTicks = 0;
  23.    
  24.     //world data
  25.     public NBTTagCompound levelData = null;
  26.     public NBTTagList tileEntities;
  27.     public NBTTagList entities;
  28.     public int build_blockIDArr[][][];
  29.     public int build_blockMetaArr[][][];
  30.     public boolean build_blockPlaced[][][];
  31.    
  32.     //REFACTOR THESE NAMES TO THE ABOVE COMMENTED OUT ONES ONCE COMPILING
  33.     public int map_sizeX = 0;
  34.     public int map_sizeY = 0;
  35.     public int map_sizeZ = 0;
  36.     public int map_coord_minX = 0;
  37.     public int map_coord_minY = 0;
  38.     public int map_coord_minZ = 0;
  39.     public int map_surfaceOffset = 0;
  40.    
  41.     public int dim = 0;
  42.    
  43.     public Build(int x, int y, int z, String parFile) {
  44.         //id = parID;
  45.         file = parFile;
  46.        
  47.         map_coord_minX = x;
  48.         map_coord_minY = y;
  49.         map_coord_minZ = z;
  50.        
  51.         readNBT(file);
  52.     }
  53.    
  54.     public void load() {
  55.        
  56.     }
  57.    
  58.     public void start() {
  59.        
  60.     }
  61.    
  62.     public void updateTick() {
  63.        
  64.     }
  65.    
  66.     public void setCornerPosition(int x, int y, int z) {
  67.         map_coord_minX = x;
  68.         map_coord_minY = y;
  69.         map_coord_minZ = z;
  70.     }
  71.    
  72.     public void recalculateLevelSize(int x1, int y1, int z1, int x2, int y2, int z2) {
  73.         recalculateLevelSize(x1, y1, z1, x2, y2, z2, false);
  74.     }
  75.    
  76.     public void recalculateLevelSize(int x1, int y1, int z1, int x2, int y2, int z2, boolean sizeUp) {
  77.        
  78.         map_sizeX = 0;
  79.         map_sizeY = 0;
  80.         map_sizeZ = 0;
  81.         map_coord_minX = x1;
  82.         map_coord_minY = y1;
  83.         map_coord_minZ = z1;
  84.        
  85.         if (x1 > x2) map_coord_minX = x2;
  86.         if (y1 > y2) map_coord_minY = y2;
  87.         if (z1 > z2) map_coord_minZ = z2;
  88.        
  89.         if (x1 - x2 >= 0) {
  90.             map_sizeX = x1 - x2;
  91.         } else {
  92.             map_sizeX = x2 - x1;
  93.         }
  94.        
  95.         if (y1 - y2 >= 0) {
  96.             map_sizeY = y1 - y2;
  97.         } else {
  98.             map_sizeY = y2 - y1;
  99.         }
  100.        
  101.         if (z1 - z2 >= 0) {
  102.             map_sizeZ = z1 - z2;
  103.         } else {
  104.             map_sizeZ = z2 - z1;
  105.         }
  106.        
  107.         //map_coord_minY--;
  108.         //map_sizeY+=2;
  109.         if (sizeUp) {
  110.             map_sizeX++;
  111.             map_sizeY++;
  112.             map_sizeZ++;
  113.         }
  114.        
  115.         //System.out.println("Size: " + map_sizeX + "," + map_sizeY + "," + map_sizeZ);
  116.         //System.out.println("map_coord_min: " + map_coord_minX + "," + map_coord_minY + "," + map_coord_minZ);
  117.        
  118.     }
  119.    
  120.     public void readNBT(String level) {
  121.        
  122.        
  123.        
  124.         levelData = null;
  125.        
  126.         try {
  127.            
  128.             NBTTagCompound nbttagcompound = CompressedStreamTools.readCompressed(new FileInputStream(level + ".schematic"));
  129.            
  130.             levelData = nbttagcompound;
  131.            
  132.             byte metadata[] = nbttagcompound.getByteArray("Data");
  133.             byte blockids[] = nbttagcompound.getByteArray("Blocks");
  134.             tileEntities = nbttagcompound.getTagList("TileEntities");
  135.             entities = nbttagcompound.getTagList("Entities");
  136.            
  137.            
  138.            
  139.             int sizeX = map_sizeX = nbttagcompound.getShort("Width");
  140.             int sizeZ = map_sizeZ = nbttagcompound.getShort("Length");
  141.             int sizeY = map_sizeY = nbttagcompound.getShort("Height");
  142.            
  143.             map_surfaceOffset = nbttagcompound.getShort("surfaceOffset");
  144.            
  145.             int bPosX = 0;
  146.             int bPosY = 0;
  147.             int bPosZ = 0;
  148.            
  149.             build_blockIDArr = new int
  150.             [sizeX]
  151.             [sizeY]
  152.             [sizeZ];
  153.            
  154.             build_blockMetaArr = new int
  155.             [sizeX]
  156.             [sizeY]
  157.             [sizeZ];
  158.            
  159.             build_blockPlaced = new boolean
  160.             [sizeX]
  161.             [sizeY]
  162.             [sizeZ];
  163.            
  164.             for (int xx = 0; xx < sizeX; xx++) {
  165.                 for (int yy = 0; yy < sizeY; yy++) {
  166.                     for (int zz = 0; zz < sizeZ; zz++) {
  167.                         int index = yy * sizeX * sizeZ + zz * sizeX + xx;
  168.                         build_blockIDArr[xx][yy][zz] = blockids[index];
  169.                         build_blockMetaArr[xx][yy][zz] = metadata[index];
  170.                         build_blockPlaced[xx][yy][zz] = false;
  171.                        
  172.                     }
  173.                 }
  174.             }
  175.            
  176.             file = level;
  177.             //ZCGame.instance.mapMan.curLevel = level;
  178.            
  179.         } catch (Exception ex) {
  180.             //notification off until generic build copy paste interface is supported for server
  181.             //ex.printStackTrace();
  182.         }
  183.        
  184.     }
  185.    
  186.     public void resetData() {
  187.         build_blockIDArr = new int
  188.         [map_sizeX]
  189.         [map_sizeY]
  190.         [map_sizeZ];
  191.        
  192.         build_blockMetaArr = new int
  193.         [map_sizeX]
  194.         [map_sizeY]
  195.         [map_sizeZ];
  196.        
  197.         build_blockPlaced = new boolean
  198.         [map_sizeX]
  199.         [map_sizeY]
  200.         [map_sizeZ];
  201.     }
  202.    
  203.     public void scanLevelToData() {
  204.        
  205.         resetData();
  206.        
  207.         if (false) map_surfaceOffset = map_coord_minY - ZCGame.ZCWorldHeight;
  208.        
  209.         for (int xx = 0; xx < map_sizeX; xx++) {
  210.             for (int yy = 0; yy < map_sizeY; yy++) {
  211.                 for (int zz = 0; zz < map_sizeZ; zz++) {
  212.                     int index = yy * map_sizeX * map_sizeZ + zz * map_sizeX + xx;
  213.                    
  214.                     World worldRef = FMLCommonHandler.instance().getMinecraftServerInstance().worldServerForDimension(dim);
  215.                    
  216.                     build_blockIDArr[xx][yy][zz] = worldRef.getBlockId(map_coord_minX+xx, map_coord_minY+yy, map_coord_minZ+zz);
  217.                     build_blockMetaArr[xx][yy][zz] = worldRef.getBlockMetadata(map_coord_minX+xx, map_coord_minY+yy, map_coord_minZ+zz);
  218.                     build_blockPlaced[xx][yy][zz] = false;
  219.                    
  220.                     //check for tile entity to write out!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  221.                    
  222.                 }
  223.             }
  224.         }
  225.     }
  226.    
  227.     public void scanWriteNBT() {
  228.         scanLevelToData();
  229.         writeNBT();
  230.     }
  231.    
  232.     public void writeNBT() {
  233.         //scans over level area and updates all data needed
  234.        
  235.         try {
  236.             if (levelData == null) {
  237.                 System.out.println("New NBT Data Object");
  238.                 levelData = new NBTTagCompound();
  239.             }
  240.                
  241.             byte metadata[] = new byte[map_sizeX * map_sizeY * map_sizeZ];// = levelData.getByteArray("Data");
  242.             byte blockids[] = new byte[map_sizeX * map_sizeY * map_sizeZ];// = levelData.getByteArray("Blocks");
  243.             //tileEntities = nbttagcompound.getTagList("TileEntities");
  244.             //entities = nbttagcompound.getTagList("Entities");
  245.            
  246.             NBTTagList var16 = new NBTTagList();
  247.            
  248.             for (int xx = 0; xx < map_sizeX; xx++) {
  249.                 for (int yy = 0; yy < map_sizeY; yy++) {
  250.                     for (int zz = 0; zz < map_sizeZ; zz++) {
  251.                         int index = yy * map_sizeX * map_sizeZ + zz * map_sizeX + xx;
  252.                        
  253.                         blockids[index] = (byte)build_blockIDArr[xx][yy][zz];
  254.                         metadata[index] = (byte)build_blockMetaArr[xx][yy][zz];
  255.                        
  256.                         World worldRef = FMLCommonHandler.instance().getMinecraftServerInstance().worldServerForDimension(dim);
  257.                        
  258.                         TileEntity tEnt = worldRef.getBlockTileEntity(map_coord_minX+xx,map_coord_minY+yy,map_coord_minZ+zz);
  259.                         if (tEnt != null) {
  260.                             NBTTagCompound var10 = new NBTTagCompound();
  261.                            
  262.                             if (tEnt instanceof SchematicData) {
  263.                                 ((SchematicData)tEnt).writeToNBT(var10, this);
  264.                             } else {
  265.                                 tEnt.writeToNBT(var10);
  266.                             }
  267.                            
  268.                             //adjust coords to be relative to the schematic file
  269.                             var10.setInteger("x", xx);
  270.                             var10.setInteger("y", yy);
  271.                             var10.setInteger("z", zz);
  272.                            
  273.                             var16.appendTag(var10);
  274.                         }
  275.                         //check for tile entity to write out!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  276.                        
  277.                     }
  278.                 }
  279.             }
  280.    
  281.             levelData.setTag("TileEntities", var16);
  282.            
  283.             //somehow get all entities within bounds
  284.             //loadedentity list bounds check
  285.            
  286.             levelData.setByteArray("Blocks", blockids);
  287.             levelData.setByteArray("Data", metadata);
  288.            
  289.             levelData.setShort("Width", (short)map_sizeX);
  290.             levelData.setShort("Height", (short)map_sizeY);
  291.             levelData.setShort("Length", (short)map_sizeZ);
  292.            
  293.             levelData.setShort("surfaceOffset", (short)map_surfaceOffset);
  294.            
  295.             saveLevelData(file);
  296.         } catch (Exception ex) {
  297.             ex.printStackTrace();
  298.         }
  299.     }
  300.    
  301.     public void saveLevelData(String level) {
  302.        
  303.         try {
  304.            
  305.             if (levelData != null) {
  306.                
  307.                 FileOutputStream fos = new FileOutputStream(level + ".schematic");
  308.                
  309.                 CompressedStreamTools.writeCompressed(levelData, fos);
  310.                
  311.                 fos.close();
  312.             }
  313.            
  314.         } catch (Exception ex) {
  315.             ex.printStackTrace();
  316.         }
  317.        
  318.     }
  319.    
  320.     public boolean fillBuildData(Build parBuild) {
  321.        
  322.         FileInputStream fis = null;
  323.        
  324.         try {
  325.             InputStream is = new FileInputStream(parBuild.file);
  326.            
  327.             fis = new FileInputStream(parBuild.file);
  328.            
  329.             NBTTagCompound nbttagcompound = CompressedStreamTools.readCompressed(fis);
  330.            
  331.             byte metadata[] = nbttagcompound.getByteArray("Data");
  332.             byte blockids[] = nbttagcompound.getByteArray("Blocks");
  333.            
  334.            
  335.            
  336.             if (fis != null) {
  337.                 fis.close();
  338.             }
  339.            
  340.         } catch (Exception ex) {
  341.             ex.printStackTrace();
  342.         } finally {
  343.            
  344.            
  345.         }
  346.         return true;
  347.     }
  348. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement