57 lines
2.1 KiB
Kotlin
57 lines
2.1 KiB
Kotlin
package cc.maxmc.blastingcrisis.game
|
|
|
|
import cc.maxmc.blastingcrisis.misc.Area
|
|
import cc.maxmc.blastingcrisis.misc.WeightRandom
|
|
import cc.maxmc.blastingcrisis.misc.debug
|
|
import cc.maxmc.blastingcrisis.misc.pluginScope
|
|
import kotlinx.coroutines.Job
|
|
import kotlinx.coroutines.delay
|
|
import kotlinx.coroutines.launch
|
|
import org.bukkit.Bukkit
|
|
import org.bukkit.Material
|
|
import taboolib.common.util.random
|
|
import taboolib.library.xseries.XMaterial
|
|
import taboolib.module.configuration.Configuration
|
|
import taboolib.platform.BukkitPlugin
|
|
|
|
class GameOreGenerator(config: Configuration) {
|
|
private val rate: Int = config.getInt("rate")
|
|
private val maxTry: Int = config.getInt("try")
|
|
private val ores: MutableMap<XMaterial, Int> = HashMap<XMaterial, Int>().also {
|
|
config.getConfigurationSection("ores")!!.getKeys(false).forEach { key ->
|
|
it += XMaterial.valueOf(key) to (config["ores.$key"]!! as Int)
|
|
}
|
|
}
|
|
private val random = WeightRandom(ores.map { it.key to it.value })
|
|
|
|
fun enable(area: Area): Job {
|
|
return pluginScope.launch {
|
|
while (true) {
|
|
val airPercent = area.getAirPercentage()
|
|
debug("remain air: $airPercent.")
|
|
if (airPercent > 0.5) {
|
|
generate(area)
|
|
}
|
|
delay(rate * 50L)
|
|
}
|
|
}
|
|
}
|
|
|
|
private tailrec fun generate(area: Area, times: Int = 0) {
|
|
if (times > maxTry) return
|
|
val x = random(area.locMin.blockX, area.locTop.blockX)
|
|
val y = random(area.locMin.blockY, area.locTop.blockY)
|
|
val z = random(area.locMin.blockZ, area.locTop.blockZ)
|
|
val block = area.locMin.world.getBlockAt(x, y, z)
|
|
debug("trying generate at ($x, $y, $z) - ${block.type}")
|
|
|
|
if (block.type == Material.AIR) {
|
|
Bukkit.getScheduler().runTask(BukkitPlugin.getInstance()) {
|
|
block.type = random.random().parseMaterial()
|
|
debug("generating ${block.type} at ($x, $y, $z)")
|
|
}
|
|
} else {
|
|
return generate(area, times + 1)
|
|
}
|
|
}
|
|
} |