(String directory)
| 113 | |
| 114 | |
| 115 | public synchronized final void save(String directory) { |
| 116 | if (!hasUnsavedChanges) { |
| 117 | return; |
| 118 | } |
| 119 | removeExpired(); |
| 120 | try { |
| 121 | Path path = Paths.get(directory); |
| 122 | if (!Files.exists(path)) { |
| 123 | Files.createDirectories(path); |
| 124 | |
| 125 | } |
| 126 | System.out.println("Saving region " + x + "," + z + " to disk " + path); |
| 127 | Path regionFile = getRegionFile(path, this.x, this.z); |
| 128 | if (!Files.exists(regionFile)) { |
| 129 | Files.createFile(regionFile); |
| 130 | } |
| 131 | try ( |
| 132 | FileOutputStream fileOut = new FileOutputStream(regionFile.toFile()); |
| 133 | GZIPOutputStream gzipOut = new GZIPOutputStream(fileOut, 16384); |
| 134 | DataOutputStream out = new DataOutputStream(gzipOut) |
| 135 | ) { |
| 136 | out.writeInt(CACHED_REGION_MAGIC); |
| 137 | for (int x = 0; x < 32; x++) { |
| 138 | for (int z = 0; z < 32; z++) { |
| 139 | CachedChunk chunk = this.chunks[x][z]; |
| 140 | if (chunk == null) { |
| 141 | out.write(CHUNK_NOT_PRESENT); |
| 142 | } else { |
| 143 | out.write(CHUNK_PRESENT); |
| 144 | byte[] chunkBytes = chunk.toByteArray(); |
| 145 | out.write(chunkBytes); |
| 146 | // Messy, but fills the empty 0s that should be trailing to fill up the space. |
| 147 | out.write(new byte[chunk.sizeInBytes - chunkBytes.length]); |
| 148 | } |
| 149 | } |
| 150 | } |
| 151 | for (int x = 0; x < 32; x++) { |
| 152 | for (int z = 0; z < 32; z++) { |
| 153 | if (chunks[x][z] != null) { |
| 154 | for (int i = 0; i < 256; i++) { |
| 155 | out.writeUTF(BlockUtils.blockToString(chunks[x][z].getOverview()[i].getBlock())); |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | for (int x = 0; x < 32; x++) { |
| 161 | for (int z = 0; z < 32; z++) { |
| 162 | if (chunks[x][z] != null) { |
| 163 | Map<String, List<BlockPos>> locs = chunks[x][z].getRelativeBlocks(); |
| 164 | out.writeShort(locs.entrySet().size()); |
| 165 | for (Map.Entry<String, List<BlockPos>> entry : locs.entrySet()) { |
| 166 | out.writeUTF(entry.getKey()); |
| 167 | out.writeShort(entry.getValue().size()); |
| 168 | for (BlockPos pos : entry.getValue()) { |
| 169 | out.writeByte((byte) (pos.getZ() << 4 | pos.getX())); |
| 170 | out.writeInt(pos.getY()-dimension.minY()); |
| 171 | } |
| 172 | } |
nothing calls this directly
no test coverage detected