Add option to specify the inner radius of the area.

Doesn't completely work because it checks the area again
and generates the chunks that were skipped over.

Signed-off-by: trivernis <trivernis@protonmail.com>
feature/inner-radius
trivernis 4 years ago
parent ec7af49267
commit 8bea7f60cf
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

@ -41,36 +41,40 @@ class CmdGenerate(private val chunkmaster: Chunkmaster) : Subcommand {
*/ */
override fun execute(sender: CommandSender, args: List<String>): Boolean { override fun execute(sender: CommandSender, args: List<String>): Boolean {
var worldName = "" var worldName = ""
var blockRadius = -1 var blockRadius: Pair<Int, Int> = Pair(-1, 0)
var shape = "square" var shape = "square"
if (sender is Player) { if (sender is Player) {
worldName = sender.world.name worldName = sender.world.name
} }
if (args.isEmpty()) { if (args.isEmpty()) {
if (sender is Player) { return if (sender is Player) {
return createTask(sender, worldName, blockRadius, shape) createTask(sender, worldName, blockRadius.first, blockRadius.second, shape)
} else { } else {
sender.sendMessage(chunkmaster.langManager.getLocalized("WORLD_NAME_REQUIRED")) sender.sendMessage(chunkmaster.langManager.getLocalized("WORLD_NAME_REQUIRED"))
return false false
} }
} }
if (args[0].toIntOrNull() != null && sender.server.worlds.find { it.name == args[0] } == null) {
var parsedRadius = parseRadius(args[0])
if (parsedRadius != null && sender.server.worlds.find { it.name == args[0] } == null) {
if (sender !is Player) { if (sender !is Player) {
sender.sendMessage(chunkmaster.langManager.getLocalized("WORLD_NAME_REQUIRED")) sender.sendMessage(chunkmaster.langManager.getLocalized("WORLD_NAME_REQUIRED"))
return false return false
} }
blockRadius = args[0].toInt() blockRadius = parsedRadius
} else { } else {
worldName = args[0] worldName = args[0]
} }
if (args.size == 1) { if (args.size == 1) {
return createTask(sender, worldName, blockRadius, shape) return createTask(sender, worldName, blockRadius.first, blockRadius.second, shape)
} }
parsedRadius = parseRadius(args[1])
when { when {
args[1].toIntOrNull() != null -> blockRadius = args[1].toInt() parsedRadius != null -> blockRadius = parsedRadius
args[1] in shapes -> shape = args[1] args[1] in shapes -> shape = args[1]
else -> { else -> {
sender.sendMessage(chunkmaster.langManager.getLocalized("INVALID_ARGUMENT", 2, args[1])) sender.sendMessage(chunkmaster.langManager.getLocalized("INVALID_ARGUMENT", 2, args[1]))
@ -78,7 +82,7 @@ class CmdGenerate(private val chunkmaster: Chunkmaster) : Subcommand {
} }
} }
if (args.size == 2) { if (args.size == 2) {
return createTask(sender, worldName, blockRadius, shape) return createTask(sender, worldName, blockRadius.first, blockRadius.second, shape)
} }
if (args[2] in shapes) { if (args[2] in shapes) {
shape = args[2] shape = args[2]
@ -87,24 +91,29 @@ class CmdGenerate(private val chunkmaster: Chunkmaster) : Subcommand {
return false return false
} }
return createTask(sender, worldName, blockRadius, shape) return createTask(sender, worldName, blockRadius.first, blockRadius.second, shape)
} }
/** /**
* Creates the task with the given arguments. * Creates the task with the given arguments.
*/ */
private fun createTask(sender: CommandSender, worldName: String, blockRadius: Int, shape: String): Boolean { private fun createTask(sender: CommandSender, worldName: String, blockRadius: Int, startRadius: Int, shape: String): Boolean {
val world = chunkmaster.server.getWorld(worldName) val world = chunkmaster.server.getWorld(worldName)
val allTasks = chunkmaster.generationManager.allTasks val allTasks = chunkmaster.generationManager.allTasks
return if (world != null && (allTasks.find { it.generationTask.world == world }) == null) { return if (world != null && (allTasks.find { it.generationTask.world == world }) == null) {
chunkmaster.generationManager.addTask(world, if (blockRadius > 0) blockRadius / 16 else -1, shape) chunkmaster.generationManager.addTask(world, if (blockRadius > 0) blockRadius / 16 else -1, shape, startRadius/16)
sender.sendMessage( sender.sendMessage(
chunkmaster.langManager chunkmaster.langManager
.getLocalized( .getLocalized(
"TASK_CREATION_SUCCESS", "TASK_CREATION_SUCCESS",
worldName, worldName,
if (blockRadius > 0) { if (blockRadius > 0) {
chunkmaster.langManager.getLocalized("TASK_UNIT_RADIUS", blockRadius) chunkmaster.langManager.getLocalized("TASK_UNIT_RADIUS", blockRadius) + if (startRadius > 0) {
chunkmaster.langManager.getLocalized("TASK_CREATION_STARTING_AT", startRadius)
} else {
""
}
} else { } else {
chunkmaster.langManager.getLocalized("TASK_UNIT_WORLDBORDER") chunkmaster.langManager.getLocalized("TASK_UNIT_WORLDBORDER")
}, },
@ -120,4 +129,26 @@ class CmdGenerate(private val chunkmaster: Chunkmaster) : Subcommand {
return false return false
} }
} }
/**
* Tries to parse a radius that can also be a range
*/
private fun parseRadius(arg: String): Pair<Int, Int>? {
val radiusRegex = Regex("^(\\d+)(-(\\d+))?\$")
val matches = radiusRegex.matchEntire(arg)
if (matches != null) {
val firstRadius = matches.groupValues[1].toInt()
var radius = Pair(firstRadius, firstRadius)
if (matches.groupValues.size >2) {
radius = Pair(matches.groupValues[3].toInt(), firstRadius)
}
if (radius.first < radius.second) {
return null
}
return radius
}
return null
}
} }

@ -5,6 +5,7 @@ import net.trivernis.chunkmaster.lib.generation.taskentry.PausedTaskEntry
import net.trivernis.chunkmaster.lib.generation.taskentry.RunningTaskEntry import net.trivernis.chunkmaster.lib.generation.taskentry.RunningTaskEntry
import net.trivernis.chunkmaster.lib.generation.taskentry.TaskEntry import net.trivernis.chunkmaster.lib.generation.taskentry.TaskEntry
import net.trivernis.chunkmaster.lib.shapes.Circle import net.trivernis.chunkmaster.lib.shapes.Circle
import net.trivernis.chunkmaster.lib.shapes.Shape
import net.trivernis.chunkmaster.lib.shapes.Square import net.trivernis.chunkmaster.lib.shapes.Square
import org.bukkit.Server import org.bukkit.Server
import org.bukkit.World import org.bukkit.World
@ -58,18 +59,31 @@ class GenerationManager(private val chunkmaster: Chunkmaster, private val server
/** /**
* Adds a generation task * Adds a generation task
*/ */
fun addTask(world: World, radius: Int = -1, shape: String = "square"): Int { fun addTask(world: World, radius: Int = -1, shape: String = "square", startRadius: Int = 0): Int {
val foundTask = allTasks.find { it.generationTask.world == world } val foundTask = allTasks.find { it.generationTask.world == world }
if (foundTask == null) { if (foundTask == null) {
val center = worldProperties.getWorldCenter(world.name).join() val center = worldProperties.getWorldCenter(world.name).join()
val centerChunk = if (center == null) { val centerChunk = if (center == null) {
ChunkCoordinates(world.spawnLocation.chunk.x, world.spawnLocation.chunk.z) ChunkCoordinates(world.spawnLocation.chunk.x, world.spawnLocation.chunk.z)
} else { } else {
ChunkCoordinates(center.first, center.second) ChunkCoordinates(center.first, center.second)
} }
val generationTask = createGenerationTask(world, centerChunk, centerChunk, radius, shape, null) val shapeInstance = stringToShape(shape, centerChunk, centerChunk, radius)
var startCoordinates = Pair(centerChunk.x, centerChunk.z)
if (startRadius > 0) {
println(startRadius)
while (shapeInstance.currentRadius() != startRadius) {
startCoordinates = shapeInstance.next()
println(shapeInstance.currentRadius())
}
}
val generationTask = createGenerationTask(world, centerChunk, ChunkCoordinates(startCoordinates.first, startCoordinates.second), radius, shape, null)
val id = generationTasks.addGenerationTask(world.name, centerChunk, radius, shape).join() val id = generationTasks.addGenerationTask(world.name, centerChunk, radius, shape).join()
generationTask.onEndReached { generationTask.onEndReached {
@ -364,11 +378,7 @@ class GenerationManager(private val chunkmaster: Chunkmaster, private val server
shapeName: String, shapeName: String,
pendingChunks: List<ChunkCoordinates>? pendingChunks: List<ChunkCoordinates>?
): GenerationTask { ): GenerationTask {
val shape = when (shapeName) { val shape = stringToShape(shapeName, center, start, radius)
"circle" -> Circle(Pair(center.x, center.z), Pair(start.x, start.z), radius)
"square" -> Square(Pair(center.x, center.z), Pair(start.x, start.z), radius)
else -> Square(Pair(center.x, center.z), Pair(start.x, start.z), radius)
}
return DefaultGenerationTask( return DefaultGenerationTask(
chunkmaster, chunkmaster,
@ -380,4 +390,18 @@ class GenerationManager(private val chunkmaster: Chunkmaster, private val server
TaskState.GENERATING TaskState.GENERATING
) )
} }
private fun stringToShape(
shapeName: String,
center: ChunkCoordinates,
start: ChunkCoordinates,
radius: Int
): Shape {
val shape = when (shapeName) {
"circle" -> Circle(Pair(center.x, center.z), Pair(start.x, start.z), radius)
"square" -> Square(Pair(center.x, center.z), Pair(start.x, start.z), radius)
else -> Square(Pair(center.x, center.z), Pair(start.x, start.z), radius)
}
return shape
}
} }

@ -12,7 +12,8 @@ TASK_SAVE_FAILED = §cException when saving tasks: %s
WORLD_NAME_REQUIRED = §cYou need to provide a world name! WORLD_NAME_REQUIRED = §cYou need to provide a world name!
WORLD_NOT_FOUND = §cWorld §2%s §cnot found! WORLD_NOT_FOUND = §cWorld §2%s §cnot found!
TASK_ALREADY_EXISTS = §cA task for '%s' already exists! TASK_ALREADY_EXISTS = §cA task for '%s' already exists!
TASK_CREATION_SUCCESS = §9Generation Task for world §2%s§9 §2%s§9 and shape %s successfully created! TASK_CREATION_SUCCESS = §9Generation Task for world §2%s§r §2%s§r and shape §2%s§r successfully created!
TASK_CREATION_STARTING_AT = starting at radius §2%s§r
TASK_UNIT_WORLDBORDER = until world border TASK_UNIT_WORLDBORDER = until world border
TASK_UNIT_RADIUS = with a radius of %s TASK_UNIT_RADIUS = with a radius of %s
TASK_ID_REQUIRED = §cYou need to provide a task id! TASK_ID_REQUIRED = §cYou need to provide a task id!

Loading…
Cancel
Save