Added chunkgeneration command
- /generatechunks [x1] [z1] [x2] [z2] renders the cunks in the given areamaster
parent
5cbe0bedb3
commit
019bfe7cf0
@ -0,0 +1,29 @@
|
|||||||
|
package net.trivernis.superutils.commands
|
||||||
|
|
||||||
|
import net.trivernis.superutils.SuperUtils
|
||||||
|
import net.trivernis.superutils.lib.ChunkGenerationManager
|
||||||
|
import org.bukkit.Server
|
||||||
|
import org.bukkit.command.Command
|
||||||
|
import org.bukkit.command.CommandExecutor
|
||||||
|
import org.bukkit.command.CommandSender
|
||||||
|
import org.bukkit.entity.Player
|
||||||
|
|
||||||
|
class CommandGenerateChunks(private val superUtils: SuperUtils, private val server: Server): CommandExecutor {
|
||||||
|
private var generationManager: ChunkGenerationManager? = null
|
||||||
|
override fun onCommand(sender: CommandSender, command: Command, label: String, args: Array<out String>): Boolean {
|
||||||
|
if (sender is Player) {
|
||||||
|
if (generationManager != null && generationManager!!.getRunning()) {
|
||||||
|
generationManager?.stop()
|
||||||
|
sender.sendMessage("Stopped chunk generation.")
|
||||||
|
}
|
||||||
|
if (args.size == 4) {
|
||||||
|
generationManager = ChunkGenerationManager(superUtils, server, sender.world)
|
||||||
|
generationManager?.start(Pair(args[0].toInt(), args[1].toInt()), Pair(args[2].toInt(), args[3].toInt())) {
|
||||||
|
sender.sendMessage("Finished Generation.")
|
||||||
|
}
|
||||||
|
sender.sendMessage("Starting chunk generation...")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,76 @@
|
|||||||
|
package net.trivernis.superutils.lib
|
||||||
|
|
||||||
|
import com.sun.org.apache.xpath.internal.operations.Bool
|
||||||
|
import net.trivernis.superutils.SuperUtils
|
||||||
|
import org.bukkit.Chunk
|
||||||
|
import org.bukkit.Location
|
||||||
|
import org.bukkit.Server
|
||||||
|
import org.bukkit.World
|
||||||
|
import org.bukkit.scheduler.BukkitTask
|
||||||
|
import kotlin.math.abs
|
||||||
|
import kotlin.math.max
|
||||||
|
|
||||||
|
class ChunkGenerationManager(private val superUtils: SuperUtils, private val server: Server, private val world: World) {
|
||||||
|
var direction = 0
|
||||||
|
private var running = false;
|
||||||
|
lateinit var currentChunk: Pair<Int, Int>
|
||||||
|
var genTask: BukkitTask? = null
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Starts the chunk generation task.
|
||||||
|
*/
|
||||||
|
public fun start(start: Pair<Int, Int>, end: Pair<Int, Int>, callback: (() -> Unit)?) {
|
||||||
|
val upperLeft = Pair(minOf(start.first, end.first), minOf(start.second, end.second))
|
||||||
|
val lowerRight = Pair(maxOf(start.first, end.first), maxOf(start.second, end.second))
|
||||||
|
var progress = 0
|
||||||
|
val total = (abs(start.first - end.first) * abs(start.second - end.second))/(16*16)
|
||||||
|
server.consoleSender.sendMessage("upperLeft: ${upperLeft.first}, ${upperLeft.second}, " +
|
||||||
|
"lowerRight: ${lowerRight.first}, ${lowerRight.second}")
|
||||||
|
currentChunk = upperLeft
|
||||||
|
genTask = server.scheduler.runTaskTimer(superUtils, Runnable {
|
||||||
|
val chunk = world.getChunkAt(Location(world, currentChunk.first.toDouble(), 1.0, currentChunk.second.toDouble()))
|
||||||
|
if (!world.isChunkGenerated(chunk.x, chunk.z)) {
|
||||||
|
chunk.load(true)
|
||||||
|
chunk.unload(true)
|
||||||
|
}
|
||||||
|
if (currentChunk.first > lowerRight.first) {
|
||||||
|
if (currentChunk.second > lowerRight.second) {
|
||||||
|
genTask?.cancel()
|
||||||
|
running = false
|
||||||
|
server.consoleSender.sendMessage("Generation finished.")
|
||||||
|
callback?.invoke()
|
||||||
|
} else {
|
||||||
|
currentChunk = Pair(upperLeft.first, currentChunk.second + 16)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
currentChunk = Pair(currentChunk.first + 16, currentChunk.second)
|
||||||
|
}
|
||||||
|
progress ++
|
||||||
|
if (progress % 30 == 0) {
|
||||||
|
server.consoleSender.sendMessage("ChunkGen Progress: ${(progress.toDouble()/total.toDouble())*100}%")
|
||||||
|
server.consoleSender.sendMessage("Chunk ${chunk.x}, ${chunk.z}")
|
||||||
|
}
|
||||||
|
}, 10, 2)
|
||||||
|
this.running = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stops the generation task.
|
||||||
|
*/
|
||||||
|
public fun stop() {
|
||||||
|
genTask?.cancel()
|
||||||
|
this.running = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the running state
|
||||||
|
*/
|
||||||
|
public fun getRunning(): Boolean {
|
||||||
|
return this.running
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun inArea(chunk: Pair<Int, Int>, upperLeft: Pair<Int, Int>, lowerRight: Pair<Int, Int>): Boolean {
|
||||||
|
return chunk.first > upperLeft.first && chunk.first < lowerRight.first &&
|
||||||
|
chunk.second > upperLeft.second && chunk.second < lowerRight.second
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue