Added chunkgeneration command

- /generatechunks [x1] [z1] [x2] [z2] renders the cunks in the given area
master
Trivernis 5 years ago
parent 5cbe0bedb3
commit 019bfe7cf0

@ -14,6 +14,9 @@ import org.bukkit.event.player.PlayerGameModeChangeEvent
import org.bukkit.event.world.WorldSaveEvent
import org.bukkit.potion.PotionEffectType
import net.md_5.bungee.api.chat.TextComponent
import net.trivernis.superutils.lib.ChunkGenerationManager
import org.bukkit.event.player.PlayerJoinEvent
import org.bukkit.event.player.PlayerQuitEvent
class EventListener(private val config: FileConfiguration, private val essentials: Essentials?,
private val commandC: CommandC, private val server: Server): Listener {

@ -3,6 +3,7 @@ package net.trivernis.superutils
import com.earth2me.essentials.Essentials
import com.onarandombox.MultiverseCore.MultiverseCore
import net.trivernis.superutils.commands.*
import net.trivernis.superutils.lib.ChunkGenerationManager
import org.bukkit.plugin.java.JavaPlugin
class SuperUtils : JavaPlugin() {
@ -18,6 +19,7 @@ class SuperUtils : JavaPlugin() {
getCommand("superutils reload")?.setExecutor(CommandReload(this))
getCommand("scheduleshutdown")?.setExecutor(CommandScheduleShutdown(this))
getCommand("c")?.setExecutor(commandC)
getCommand("generatechunks")?.setExecutor(CommandGenerateChunks(this, server))
if (essentials != null) {
logger.info("Registering short forms for Essentials plugin features.")

@ -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
}
}

@ -35,6 +35,11 @@ commands:
permission: superutils.unstuck
permission-message: You do not have permission!
usage: /unstuck
generateChunks:
description: Generates chunks for given coordinates
permission: superutils.generatechunks
permission-message: You do not have permission
usage: /generatechunks [{x1} {x2} {y1} {y2}]
permissions:
superutils.reload:
description: Allows reload command
@ -54,6 +59,9 @@ permissions:
superutils.unstuck:
description: Allow unstuck command
default: op
superuitls.generatechunks:
description: Allows chunkgen command
default: op
superutils.*:
description: Wildcard permission
default: op

Loading…
Cancel
Save