Skip to content

Commit

Permalink
fix: store custom block per world
Browse files Browse the repository at this point in the history
  • Loading branch information
acrylic-style committed Feb 8, 2024
1 parent 67245b8 commit 4c96748
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 12 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ plugins {
}

group = "net.azisaba"
version = "6.9.3"
version = "6.9.4"

java {
toolchain.languageVersion.set(JavaLanguageVersion.of(8))
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/github/mori01231/lifecore/LifeCore.kt
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ class LifeCore : JavaPlugin() {
dataFolder.resolve("drop-protect.yml").writeText(dropProtectConfig.encode())
}

// save every 10 seconds
Bukkit.getScheduler().runTaskTimerAsynchronously(this, 20 * 10, 20 * 10) {
customBlockManager.saveAll()
}

databaseConfig = config.getYamlMap("database").decode(DatabaseConfig.serializer())
dataFolder.resolve("config-vote.yml").let {
if (!it.exists()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import org.bukkit.ChatColor
import org.bukkit.Location
import org.bukkit.Material
import org.bukkit.NamespacedKey
import org.bukkit.World
import org.bukkit.block.data.Directional
import org.bukkit.block.data.type.Leaves
import org.bukkit.entity.ArmorStand
Expand All @@ -28,7 +29,7 @@ class CustomBlockManager(val plugin: LifeCore) {
}

private val blocks = mutableListOf<CustomBlock>()
private val region = LRUCache<Pair<Int, Int>, CustomBlockRegion>(500)
private val region = mutableMapOf<String, LRUCache<Pair<Int, Int>, CustomBlockRegion>>()

fun getBlocks() = blocks.toList()

Expand All @@ -49,31 +50,31 @@ class CustomBlockManager(val plugin: LifeCore) {
private fun getRegionPos(worldPos: Int) = worldPos shr 9

fun getState(location: Location): CustomBlockState? {
val region = loadRegion(getRegionPos(location.blockX), getRegionPos(location.blockZ))
val region = loadRegion(location.world, getRegionPos(location.blockX), getRegionPos(location.blockZ))
return region.getState(location.blockX, location.blockY, location.blockZ)
}

fun setState(location: Location, state: CustomBlockState?) {
val region = loadRegion(getRegionPos(location.blockX), getRegionPos(location.blockZ))
val region = loadRegion(location.world, getRegionPos(location.blockX), getRegionPos(location.blockZ))
if (state == null) {
findArmorStand(location)?.remove()
location.block.type = Material.AIR
} else {
spawnBlock(location, state.getBlock(), state.axis)
}
region.setState(location.blockX, location.blockY, location.blockZ, state)
region.save()
}

private fun loadRegion(x: Int, z: Int): CustomBlockRegion {
val loaded = region.getOrPut(x to z) {
val file = File(regionDir, "$x.$z.json")
private fun loadRegion(world: World, x: Int, z: Int): CustomBlockRegion {
val wld = region.getOrPut(world.name) { LRUCache(100) }
val loaded = wld.getOrPut(x to z) {
val file = File(regionDir, "${world.name}/$x.$z.json")
if (file.exists()) {
plugin.logger.info("Loading region $x, $z")
Json.decodeFromString(CustomBlockRegion.serializer(), file.readText())
} else {
plugin.logger.info("Creating region $x, $z")
CustomBlockRegion(x, z)
CustomBlockRegion(world.name, x, z)
}
}
if (loaded.dirty) {
Expand Down Expand Up @@ -130,6 +131,10 @@ class CustomBlockManager(val plugin: LifeCore) {
}
}

fun saveAll() {
region.values.forEach { it.values.forEach { r -> r.save() } }
}

fun findArmorStand(location: Location): ArmorStand? {
val x = location.blockX + 0.5
val y = location.blockY.toDouble()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import kotlinx.serialization.json.Json
import java.io.File

@Serializable
data class CustomBlockRegion(val x: Int, val z: Int) {
data class CustomBlockRegion(val world: String, val x: Int, val z: Int) {
private val states = mutableMapOf<Long, CustomBlockState>()
var dirty: Boolean = false
private set
Expand Down Expand Up @@ -36,10 +36,11 @@ data class CustomBlockRegion(val x: Int, val z: Int) {

fun save() {
if (dirty) {
val file = File(CustomBlockManager.regionDir, "$x.$z.json.writing")
val file = File(CustomBlockManager.regionDir, "$world/$x.$z.json.writing")
file.parentFile.mkdirs()
dirty = false
file.writeText(Json.encodeToString(this))
file.renameTo(File(CustomBlockManager.regionDir, "$x.$z.json"))
file.renameTo(File(CustomBlockManager.regionDir, "$world/$x.$z.json"))
}
}
}

0 comments on commit 4c96748

Please sign in to comment.