Fixes and Generation units

- generate command now accepts a unit parameter
- fixed the skipping of not generated chunks
release/0.12-beta
Trivernis 5 years ago
parent 719f140a84
commit 350e774bb2

@ -7,6 +7,7 @@ import net.trivernis.chunkmaster.lib.Subcommand
import org.bukkit.command.Command import org.bukkit.command.Command
import org.bukkit.command.CommandSender import org.bukkit.command.CommandSender
import org.bukkit.entity.Player import org.bukkit.entity.Player
import kotlin.math.pow
class CmdGenerate(private val chunkmaster: Chunkmaster): Subcommand { class CmdGenerate(private val chunkmaster: Chunkmaster): Subcommand {
override val name = "generate" override val name = "generate"
@ -23,9 +24,19 @@ class CmdGenerate(private val chunkmaster: Chunkmaster): Subcommand {
if (args.size == 1) { if (args.size == 1) {
return sender.server.worlds.filter { it.name.indexOf(args[0]) == 0 } return sender.server.worlds.filter { it.name.indexOf(args[0]) == 0 }
.map {it.name}.toMutableList() .map {it.name}.toMutableList()
} else if (args.size == 2) {
if (args[0].toIntOrNull() != null) {
return units.filter {it.indexOf(args[1]) == 0}.toMutableList()
}
} else if (args.size > 2) {
if (args[1].toIntOrNull() != null) {
return units.filter {it.indexOf(args[2]) == 0}.toMutableList()
}
} }
return emptyList<String>().toMutableList() return emptyList<String>().toMutableList()
} }
val units = listOf("blockradius", "radius", "diameter")
/** /**
* Creates a new generation task for the world and chunk count. * Creates a new generation task for the world and chunk count.
@ -41,8 +52,24 @@ class CmdGenerate(private val chunkmaster: Chunkmaster): Subcommand {
} else { } else {
worldName = args[0] worldName = args[0]
} }
if (args.size > 1 && args[1].toIntOrNull() != null) { if (args.size > 1) {
stopAfter = args[1].toInt() if (args[1].toIntOrNull() != null) {
stopAfter = args[1].toInt()
} else if (args[1] in units) {
when (args[1]) {
"radius" -> {
stopAfter = ((stopAfter * 2)+1).toDouble().pow(2.0).toInt()
}
"diameter" -> {
stopAfter = stopAfter.toDouble().pow(2.0).toInt()
}
"blockradius" -> {
stopAfter = ((stopAfter*32)+1).toDouble().pow(2.0).toInt()
}
}
} else {
worldName = args[1]
}
} }
} else { } else {
worldName = sender.world.name worldName = sender.world.name

@ -28,7 +28,7 @@ class GenerationManager(private val chunkmaster: Chunkmaster, private val server
fun addTask(world: World, stopAfter: Int = -1): Int { fun addTask(world: World, stopAfter: Int = -1): Int {
val foundTask = allTasks.find { it.generationTask.world == world } val foundTask = allTasks.find { it.generationTask.world == world }
if (foundTask == null) { if (foundTask == null) {
val centerChunk = world.getChunkAt(world.spawnLocation) val centerChunk = ChunkCoordinates(world.spawnLocation.chunk.x, world.spawnLocation.chunk.z)
val generationTask = createGenerationTask(world, centerChunk, centerChunk, stopAfter) val generationTask = createGenerationTask(world, centerChunk, centerChunk, stopAfter)
val insertStatement = chunkmaster.sqliteConnection.prepareStatement( val insertStatement = chunkmaster.sqliteConnection.prepareStatement(
@ -59,7 +59,7 @@ class GenerationManager(private val chunkmaster: Chunkmaster, private val server
getIdStatement.close() getIdStatement.close()
generationTask.onEndReached { generationTask.onEndReached {
server.consoleSender.sendMessage("Task #${id} finished after ${generationTask.count} chunks.") chunkmaster.logger.info("Task #${id} finished after ${generationTask.count} chunks.")
removeTask(id) removeTask(id)
} }
@ -82,7 +82,13 @@ class GenerationManager(private val chunkmaster: Chunkmaster, private val server
/** /**
* Resumes a generation task * Resumes a generation task
*/ */
private fun resumeTask(world: World, center: Chunk, last: Chunk, id: Int, stopAfter: Int = -1) { private fun resumeTask(
world: World,
center: ChunkCoordinates,
last: ChunkCoordinates,
id: Int,
stopAfter: Int = -1
) {
if (!paused) { if (!paused) {
chunkmaster.logger.info("Resuming chunk generation task for world \"${world.name}\"") chunkmaster.logger.info("Resuming chunk generation task for world \"${world.name}\"")
val generationTask = createGenerationTask(world, center, last, stopAfter) val generationTask = createGenerationTask(world, center, last, stopAfter)
@ -92,7 +98,7 @@ class GenerationManager(private val chunkmaster: Chunkmaster, private val server
) )
tasks.add(RunningTaskEntry(id, task, generationTask)) tasks.add(RunningTaskEntry(id, task, generationTask))
generationTask.onEndReached { generationTask.onEndReached {
server.consoleSender.sendMessage("Task #${id} finished after ${generationTask.count} chunks.") chunkmaster.logger.info("Task #${id} finished after ${generationTask.count} chunks.")
removeTask(id) removeTask(id)
} }
} }
@ -143,7 +149,7 @@ class GenerationManager(private val chunkmaster: Chunkmaster, private val server
if (server.onlinePlayers.isEmpty()) { if (server.onlinePlayers.isEmpty()) {
startAll() // run startAll after 10 seconds if empty startAll() // run startAll after 10 seconds if empty
} }
}, 200) }, 600)
} }
/** /**
@ -174,14 +180,14 @@ class GenerationManager(private val chunkmaster: Chunkmaster, private val server
try { try {
val id = res.getInt("id") val id = res.getInt("id")
val world = server.getWorld(res.getString("world")) val world = server.getWorld(res.getString("world"))
val center = world!!.getChunkAt(res.getInt("center_x"), res.getInt("center_z")) val center = ChunkCoordinates(res.getInt("center_x"), res.getInt("center_z"))
val last = world.getChunkAt(res.getInt("last_x"), res.getInt("last_z")) val last = ChunkCoordinates(res.getInt("last_x"), res.getInt("last_z"))
val stopAfter = res.getInt("stop_after") val stopAfter = res.getInt("stop_after")
if (this.tasks.find { it.id == id } == null) { if (this.tasks.find { it.id == id } == null) {
resumeTask(world, center, last, id, stopAfter) resumeTask(world!!, center, last, id, stopAfter)
} }
} catch (error: NullPointerException) { } catch (error: NullPointerException) {
server.consoleSender.sendMessage("Failed to load Task ${res.getInt("id")}.") chunkmaster.logger.severe("Failed to load Task ${res.getInt("id")}.")
} }
} }
savedTasksStatement.close() savedTasksStatement.close()
@ -217,7 +223,7 @@ class GenerationManager(private val chunkmaster: Chunkmaster, private val server
for (task in tasks) { for (task in tasks) {
try { try {
val genTask = task.generationTask val genTask = task.generationTask
server.consoleSender.sendMessage( chunkmaster.logger.info(
"""Task #${task.id} running for "${genTask.world.name}". """Task #${task.id} running for "${genTask.world.name}".
|Progress ${task.generationTask.count} chunks |Progress ${task.generationTask.count} chunks
|${if (task.generationTask.stopAfter > 0) "(${(task.generationTask.count.toDouble() / |${if (task.generationTask.stopAfter > 0) "(${(task.generationTask.count.toDouble() /
@ -236,7 +242,7 @@ class GenerationManager(private val chunkmaster: Chunkmaster, private val server
updateStatement.execute() updateStatement.execute()
updateStatement.close() updateStatement.close()
} catch (error: Exception) { } catch (error: Exception) {
server.consoleSender.sendMessage("Exception when saving task progress ${error.message}") chunkmaster.logger.warning("Exception when saving task progress ${error.message}")
} }
} }
} }
@ -245,7 +251,12 @@ class GenerationManager(private val chunkmaster: Chunkmaster, private val server
* Creates a new generation task. This method is used to create a task depending * Creates a new generation task. This method is used to create a task depending
* on the server type (Paper/Spigot). * on the server type (Paper/Spigot).
*/ */
private fun createGenerationTask(world: World, center: Chunk, start: Chunk, stopAfter: Int): GenerationTask { private fun createGenerationTask(
world: World,
center: ChunkCoordinates,
start: ChunkCoordinates,
stopAfter: Int
): GenerationTask {
return if (PaperLib.isPaper()) { return if (PaperLib.isPaper()) {
GenerationTaskPaper(chunkmaster, world, center, start, stopAfter) GenerationTaskPaper(chunkmaster, world, center, start, stopAfter)
} else { } else {

@ -8,7 +8,8 @@ import org.bukkit.World
/** /**
* Interface for generation tasks. * Interface for generation tasks.
*/ */
abstract class GenerationTask(plugin: Chunkmaster, centerChunk: Chunk, startChunk: Chunk) : Runnable { abstract class GenerationTask(plugin: Chunkmaster, centerChunk: ChunkCoordinates, startChunk: ChunkCoordinates) :
Runnable {
abstract val stopAfter: Int abstract val stopAfter: Int
abstract val world: World abstract val world: World
@ -35,11 +36,12 @@ abstract class GenerationTask(plugin: Chunkmaster, centerChunk: Chunk, startChun
val nextChunkCoords = spiral.next() val nextChunkCoords = spiral.next()
return ChunkCoordinates(nextChunkCoords.first, nextChunkCoords.second) return ChunkCoordinates(nextChunkCoords.first, nextChunkCoords.second)
} }
var lastChunk: Chunk = startChunk
val lastChunk: Chunk
get() { get() {
return world.getChunkAt(lastChunkCoords.x, lastChunkCoords.z) return world.getChunkAt(lastChunkCoords.x, lastChunkCoords.z)
} }
private set
val nextChunk: Chunk val nextChunk: Chunk
get() { get() {
val next = nextChunkCoordinates val next = nextChunkCoordinates

@ -8,7 +8,7 @@ import io.papermc.lib.PaperLib
class GenerationTaskPaper( class GenerationTaskPaper(
private val plugin: Chunkmaster, override val world: World, private val plugin: Chunkmaster, override val world: World,
centerChunk: Chunk, private val startChunk: Chunk, centerChunk: ChunkCoordinates, private val startChunk: ChunkCoordinates,
override val stopAfter: Int = -1 override val stopAfter: Int = -1
) : GenerationTask(plugin, centerChunk, startChunk) { ) : GenerationTask(plugin, centerChunk, startChunk) {
@ -52,12 +52,15 @@ class GenerationTaskPaper(
} }
if (!PaperLib.isChunkGenerated(world, chunk.x, chunk.z)) { if (!PaperLib.isChunkGenerated(world, chunk.x, chunk.z)) {
for (i in 0 until chunksPerStep) { for (i in 0 until minOf(chunksPerStep, (stopAfter - count) - 1)) {
if (!PaperLib.isChunkGenerated(world, chunk.x, chunk.z)) { if (!PaperLib.isChunkGenerated(world, chunk.x, chunk.z)) {
pendingChunks.add(PaperLib.getChunkAtAsync(world, chunk.x, chunk.z, true)) pendingChunks.add(PaperLib.getChunkAtAsync(world, chunk.x, chunk.z, true))
} }
chunk = nextChunkCoordinates chunk = nextChunkCoordinates
} }
if (!PaperLib.isChunkGenerated(world, chunk.x, chunk.z)) {
pendingChunks.add(PaperLib.getChunkAtAsync(world, chunk.x, chunk.z, true))
}
} }
lastChunkCoords = chunk lastChunkCoords = chunk
count = spiral.count // set the count to the more accurate spiral count count = spiral.count // set the count to the more accurate spiral count

@ -6,7 +6,7 @@ import org.bukkit.World
class GenerationTaskSpigot( class GenerationTaskSpigot(
private val plugin: Chunkmaster, override val world: World, private val plugin: Chunkmaster, override val world: World,
centerChunk: Chunk, private val startChunk: Chunk, centerChunk: ChunkCoordinates, private val startChunk: ChunkCoordinates,
override val stopAfter: Int = -1 override val stopAfter: Int = -1
) : GenerationTask(plugin, centerChunk, startChunk) { ) : GenerationTask(plugin, centerChunk, startChunk) {
@ -37,14 +37,14 @@ class GenerationTaskSpigot(
return return
} }
var chunk = nextChunkCoordinates var chunk = lastChunkCoords
if (!world.isChunkGenerated(chunk.x, chunk.z)) { if (!world.isChunkGenerated(chunk.x, chunk.z)) {
for (i in 0 until chunksPerStep) { for (i in 0 until minOf(chunksPerStep, (stopAfter - count) - 1)) {
chunk = nextChunkCoordinates
val chunkInstance = world.getChunkAt(chunk.x, chunk.z) val chunkInstance = world.getChunkAt(chunk.x, chunk.z)
chunkInstance.load(true) chunkInstance.load(true)
loadedChunks.add(chunkInstance) loadedChunks.add(chunkInstance)
chunk = nextChunkCoordinates
} }
} }
lastChunkCoords = chunk lastChunkCoords = chunk

Loading…
Cancel
Save