diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/server/world/LegacyWorldStorage.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/server/world/LegacyWorldStorage.kt index 60607ced..326c1403 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/server/world/LegacyWorldStorage.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/server/world/LegacyWorldStorage.kt @@ -111,47 +111,63 @@ sealed class LegacyWorldStorage() : WorldStorage() { }, Starbound.EXECUTOR) } - override fun saveEntities(pos: ChunkPos, data: Collection): Boolean { - Starbound.EXECUTOR.execute { + override fun saveEntities(pos: ChunkPos, data: Collection, now: Boolean): Boolean { + if (now) { val chunkX = pos.x val chunkY = pos.y val key = ByteKey(2, (chunkX shr 8).toByte(), chunkX.toByte(), (chunkY shr 8).toByte(), chunkY.toByte()) write(key, writeEntities(data)) + } else { + Starbound.EXECUTOR.execute { + val chunkX = pos.x + val chunkY = pos.y + val key = ByteKey(2, (chunkX shr 8).toByte(), chunkX.toByte(), (chunkY shr 8).toByte(), chunkY.toByte()) + + write(key, writeEntities(data)) + } } return true } - override fun saveCells(pos: ChunkPos, data: Object2DArray, state: ChunkState): Boolean { - Starbound.EXECUTOR.execute { - val buff = FastByteArrayOutputStream() - val stream = DataOutputStream(BufferedOutputStream(DeflaterOutputStream(buff))) + private fun saveCells0(pos: ChunkPos, data: Object2DArray, state: ChunkState) { + val buff = FastByteArrayOutputStream() + val stream = DataOutputStream(BufferedOutputStream(DeflaterOutputStream(buff))) - stream.writeVarInt(when (state) { - ChunkState.FRESH -> 0 - ChunkState.EMPTY -> 0 - ChunkState.TERRAIN -> 1 - ChunkState.MICRO_DUNGEONS -> 2 - ChunkState.CAVE_LIQUID -> 3 - ChunkState.FULL -> 4 - }) + stream.writeVarInt(when (state) { + ChunkState.FRESH -> 0 + ChunkState.EMPTY -> 0 + ChunkState.TERRAIN -> 1 + ChunkState.MICRO_DUNGEONS -> 2 + ChunkState.CAVE_LIQUID -> 3 + ChunkState.FULL -> 4 + }) - stream.writeVarInt(418) + stream.writeVarInt(418) - for (y in 0 until CHUNK_SIZE) { - for (x in 0 until CHUNK_SIZE) { - val cell = data.getOrNull(x, y) ?: AbstractCell.NULL - cell.writeLegacy(stream) - } + for (y in 0 until CHUNK_SIZE) { + for (x in 0 until CHUNK_SIZE) { + val cell = data.getOrNull(x, y) ?: AbstractCell.NULL + cell.writeLegacy(stream) } + } - val chunkX = pos.x - val chunkY = pos.y - val key = ByteKey(1, (chunkX shr 8).toByte(), chunkX.toByte(), (chunkY shr 8).toByte(), chunkY.toByte()) + val chunkX = pos.x + val chunkY = pos.y + val key = ByteKey(1, (chunkX shr 8).toByte(), chunkX.toByte(), (chunkY shr 8).toByte(), chunkY.toByte()) - stream.close() - write(key, buff.array.copyOf(buff.length)) + stream.close() + write(key, buff.array.copyOf(buff.length)) + } + + override fun saveCells(pos: ChunkPos, data: Object2DArray, state: ChunkState, now: Boolean): Boolean { + if (now) { + saveCells0(pos, data, state) + } else { + Starbound.EXECUTOR.execute { + saveCells0(pos, data, state) + } } return true diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/server/world/NativeWorldStorage.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/server/world/NativeWorldStorage.kt index a28fa927..243463a2 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/server/world/NativeWorldStorage.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/server/world/NativeWorldStorage.kt @@ -23,12 +23,12 @@ class NativeWorldStorage() : WorldStorage() { TODO("Not yet implemented") } - override fun saveEntities(pos: ChunkPos, data: Collection): Boolean { - return super.saveEntities(pos, data) + override fun saveEntities(pos: ChunkPos, data: Collection, now: Boolean): Boolean { + return super.saveEntities(pos, data, now) } - override fun saveCells(pos: ChunkPos, data: Object2DArray, state: ChunkState): Boolean { - return super.saveCells(pos, data, state) + override fun saveCells(pos: ChunkPos, data: Object2DArray, state: ChunkState, now: Boolean): Boolean { + return super.saveCells(pos, data, state, now) } override fun saveMetadata(data: Metadata): Boolean { diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/server/world/ServerChunk.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/server/world/ServerChunk.kt index aae3a5ec..374a7b45 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/server/world/ServerChunk.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/server/world/ServerChunk.kt @@ -546,7 +546,7 @@ class ServerChunk(world: ServerWorld, pos: ChunkPos) : Chunk { + private fun writeToStorage(now: Boolean = false): Collection { if (!cells.isInitialized() || state <= ChunkState.EMPTY) return emptyList() @@ -555,8 +555,8 @@ class ServerChunk(world: ServerWorld, pos: ChunkPos) : Chunk): Boolean { + open fun saveEntities(pos: ChunkPos, data: Collection, now: Boolean = false): Boolean { return false } - open fun saveCells(pos: ChunkPos, data: Object2DArray, state: ChunkState): Boolean { + open fun saveCells(pos: ChunkPos, data: Object2DArray, state: ChunkState, now: Boolean = false): Boolean { return false } @@ -148,12 +148,12 @@ abstract class WorldStorage : Closeable { return chainOptionalFutures(children) { it.loadMetadata() } } - override fun saveEntities(pos: ChunkPos, data: Collection): Boolean { - return children.any { it.saveEntities(pos, data) } + override fun saveEntities(pos: ChunkPos, data: Collection, now: Boolean): Boolean { + return children.any { it.saveEntities(pos, data, now) } } - override fun saveCells(pos: ChunkPos, data: Object2DArray, state: ChunkState): Boolean { - return children.any { it.saveCells(pos, data, state) } + override fun saveCells(pos: ChunkPos, data: Object2DArray, state: ChunkState, now: Boolean): Boolean { + return children.any { it.saveCells(pos, data, state, now) } } override fun saveMetadata(data: Metadata): Boolean {