diff --git a/src/main/kotlin/net/trivernis/superutils/EventListener.kt b/src/main/kotlin/net/trivernis/superutils/EventListener.kt index e6078e6..4aeb27b 100644 --- a/src/main/kotlin/net/trivernis/superutils/EventListener.kt +++ b/src/main/kotlin/net/trivernis/superutils/EventListener.kt @@ -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 { diff --git a/src/main/kotlin/net/trivernis/superutils/SuperUtils.kt b/src/main/kotlin/net/trivernis/superutils/SuperUtils.kt index 3cf0127..d016c6e 100644 --- a/src/main/kotlin/net/trivernis/superutils/SuperUtils.kt +++ b/src/main/kotlin/net/trivernis/superutils/SuperUtils.kt @@ -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.") diff --git a/src/main/kotlin/net/trivernis/superutils/commands/CommandGenerateChunks.kt b/src/main/kotlin/net/trivernis/superutils/commands/CommandGenerateChunks.kt new file mode 100644 index 0000000..186c999 --- /dev/null +++ b/src/main/kotlin/net/trivernis/superutils/commands/CommandGenerateChunks.kt @@ -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): 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; + } +} \ No newline at end of file diff --git a/src/main/kotlin/net/trivernis/superutils/lib/ChunkGenerationManager.kt b/src/main/kotlin/net/trivernis/superutils/lib/ChunkGenerationManager.kt new file mode 100644 index 0000000..1c525fd --- /dev/null +++ b/src/main/kotlin/net/trivernis/superutils/lib/ChunkGenerationManager.kt @@ -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 + var genTask: BukkitTask? = null + + /** + * Starts the chunk generation task. + */ + public fun start(start: Pair, end: Pair, 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, upperLeft: Pair, lowerRight: Pair): Boolean { + return chunk.first > upperLeft.first && chunk.first < lowerRight.first && + chunk.second > upperLeft.second && chunk.second < lowerRight.second + } +} \ No newline at end of file diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 0ae7d46..d2321e5 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -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