Atijaf

WorldData

Apr 26th, 2016
32
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class WorldData extends WorldSavedData {
  2.    
  3.     private static final String IDENTITY = "PlacedBlocksData";
  4.    
  5.     public WorldData()
  6.     {
  7.         super(IDENTITY);
  8.     }
  9.    
  10.     public static WorldData get(World world)
  11.     {
  12.         WorldData data = (WorldData) world.getPerWorldStorage().loadData(WorldData.class, IDENTITY);
  13.         if(data == null)
  14.         {
  15.             data = new WorldData();
  16.             world.getPerWorldStorage().setData(IDENTITY, data);
  17.         }
  18.         return data;
  19.     }
  20.  
  21.  
  22.     @Override
  23.     public void writeToNBT(NBTTagCompound nbt)
  24.     {
  25.         NBTTagList chunkList = new NBTTagList();
  26.        
  27.         long data;// <--- Store all data within that
  28.        
  29.         /** Loops through each dimension */
  30.         int numOfDimensions = ChunkBlockPos.dimensionToChunkPos.size();
  31.         for(int dimensionID = 0; dimensionID < numOfDimensions; dimensionID++)
  32.         {
  33.             /** Loops through each chunk within said dimension */
  34.             int numOfChunks = ChunkBlockPos.dimensionToChunkPos.get(dimensionID).size();
  35.             for(int chunkID = 0; chunkID < numOfChunks; chunkID++)
  36.             {
  37.                 //Store data about the chunk in left most bits of data
  38.                 data = (ChunkBlockPos.dimensionToChunkPos.get(dimensionID).get(chunkID).getTrueValue() << 16);
  39.                 /** Loops through each block within the chunk (Inside the hashmap)*/
  40.                 Iterator mapIter = ChunkBlockPos.dimensionToChunkPos.get(dimensionID).get(chunkID).subBlockPos.keySet().iterator();
  41.                 while(mapIter.hasNext())
  42.                 {
  43.                     data |= ((ChunkBlockPos)mapIter.next()).blockPos & 0xFFFF;
  44.                    
  45.                     NBTTagCompound blockData = new NBTTagCompound();
  46.                     blockData.setLong("DATA", data);
  47.                     chunkList.appendTag(blockData);
  48.                 }
  49.             }
  50.         }
  51.         nbt.setTag(IDENTITY, chunkList);
  52.        
  53.        
  54.     }
  55.    
  56.     @Override
  57.     public void readFromNBT(NBTTagCompound nbt)
  58.     {
  59.         NBTTagList list = nbt.getTagList(IDENTITY, NBT.TAG_LIST);
  60.        
  61.         for(int i = 0; i < list.tagCount(); i++)
  62.         {
  63.             int[] data = getBlockInfoFromData(list.getCompoundTagAt(i).getLong("DATA"));
  64.            
  65.             //Create the chunkBlockPos
  66.             ChunkBlockPos chunkBlockPos = new ChunkBlockPos(data[3],data[5],data[4]);
  67.            
  68.             //Create the chunkPos
  69.             ChunkPos chunkPos = new ChunkPos(data[1], data[2], data[0]);
  70.            
  71.             //Add data to ram
  72.             ChunkBlockPos.InsertEntry(chunkBlockPos, chunkPos);
  73.         }
  74.     }
  75.    
  76.     /**
  77.      * @return [0]-Dimension [1]-ChunkX [2]-ChunkZ [3]-blockX [4]-BlockZ [5]-BlockY
  78.      */
  79.     public static final int[] getBlockInfoFromData(long data)
  80.     {
  81.         //Needs careful testing
  82.         int dimesion = (int) ((data & 0xF000000000000000L) >>> 60);
  83.         int chunkX = (int) ((data & 0xFFFFFC000000000L) >>> 38);
  84.         int chunkZ = (int) ((data & 0x3FFFFF0000L) >>> 16);
  85.         int blockX = (int) ((data & 0xF000) >>> 12);
  86.         int blockZ = (int) ((data & 0xF00) >>> 8);
  87.         int blockY = (int) ((data & 0xFF));
  88.        
  89.         return new int[]{dimesion, chunkX, chunkZ, blockX, blockZ, blockY};
  90.        
  91.     }
  92. }
RAW Paste Data