Skip to content

Commit

Permalink
Add global range parameter
Browse files Browse the repository at this point in the history
relates to #299
  • Loading branch information
DrexHD committed Jan 10, 2025
1 parent 9275fc2 commit 88b3745
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 25 deletions.
4 changes: 2 additions & 2 deletions docs/parameters.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ An identifier is Minecraft's ID system of a namespace and a path divided by `:`.

## Range
Key - `range:`
Value - `Integer > 1`
Value - `Integer > 1` | `@global`
Negative Allowed - `No`
Multiple Allowed - `No`
Example - `range:5`
Example - `range:5`, `range:@global`

This parameter allows you to filter your selection based on your location.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ data class ActionSearchParams(
throw SimpleCommandExceptionType(Text.translatable("error.ledger.unspecific.range")).create()
}
val range = (max(bounds.blockCountX, max(bounds.blockCountY, bounds.blockCountZ)) + 1) / 2
if (range > Ledger.config[SearchSpec.maxRange]) {
if (range > Ledger.config[SearchSpec.maxRange] && bounds != GLOBAL) {
throw SimpleCommandExceptionType(
Text.translatable("error.ledger.unspecific.range_to_big", Ledger.config[SearchSpec.maxRange])
).create()
Expand All @@ -50,6 +50,8 @@ data class ActionSearchParams(
}

companion object {
val GLOBAL: BlockBox =
BlockBox(-Int.MAX_VALUE, -Int.MAX_VALUE, -Int.MAX_VALUE, Int.MAX_VALUE, Int.MAX_VALUE, Int.MAX_VALUE)
inline fun build(block: Builder.() -> Unit) = Builder().apply(block).build()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,18 @@ object SearchParamArgument {

when (param) {
"range" -> {
val range = value as Int - 1
builder.bounds = BlockBox.create(
BlockPos.ofFloored(source.position).subtract(Vec3i(range, range, range)),
BlockPos.ofFloored(source.position).add(Vec3i(range, range, range))
)
val world = Negatable.allow(source.world.registryKey.value)
if (builder.worlds == null) builder.worlds = mutableSetOf(world) else builder.worlds!!.add(world)
val range = value as Int?
if (range != null) {
val range = range - 1
builder.bounds = BlockBox.create(
BlockPos.ofFloored(source.position).subtract(Vec3i(range, range, range)),
BlockPos.ofFloored(source.position).add(Vec3i(range, range, range))
)
val world = Negatable.allow(source.world.registryKey.value)
if (builder.worlds == null) builder.worlds = mutableSetOf(world) else builder.worlds!!.add(world)
} else {
builder.bounds = ActionSearchParams.GLOBAL
}
}
"world" -> {
val world = value as Negatable<Identifier>
Expand Down Expand Up @@ -211,9 +216,13 @@ object SearchParamArgument {
}

open fun getRemaining(s: String): Int {
val reader = StringReader(s)
parameter.parse(reader)
return reader.remainingLength
try {
val reader = StringReader(s)
parameter.parse(reader)
return reader.remainingLength
} catch (_: CommandSyntaxException) {
return 0
}
}

@Throws(CommandSyntaxException::class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,53 @@ package com.github.quiltservertools.ledger.commands.parameters
import com.mojang.brigadier.StringReader
import com.mojang.brigadier.arguments.IntegerArgumentType
import com.mojang.brigadier.context.CommandContext
import com.mojang.brigadier.exceptions.CommandSyntaxException
import com.mojang.brigadier.suggestion.Suggestions
import com.mojang.brigadier.suggestion.SuggestionsBuilder
import net.minecraft.command.CommandSource
import net.minecraft.server.command.ServerCommandSource
import java.util.concurrent.CompletableFuture

private const val MAX_SIZE = 9
private const val GLOBAL = "@global"

class RangeParameter : SimpleParameter<Int>() {
override fun parse(stringReader: StringReader): Int = IntegerArgumentType.integer(1).parse(stringReader)
class RangeParameter : SimpleParameter<Int?>() {
override fun parse(stringReader: StringReader): Int? {
val start: Int = stringReader.cursor

while (stringReader.canRead() && stringReader.peek() != ' ') {
stringReader.skip()
}

val argument = stringReader.string.substring(start, stringReader.cursor)
if (argument == GLOBAL) {
return null
} else {
stringReader.cursor = start
return IntegerArgumentType.integer(1).parse(stringReader)
}
}

override fun getSuggestions(
context: CommandContext<ServerCommandSource>,
builder: SuggestionsBuilder
): CompletableFuture<Suggestions> {
val remaining = builder.remaining.lowercase()
for (i in 1..MAX_SIZE) builder.suggest(remaining + i)
val reader = StringReader(remaining)

CommandSource.suggestMatching(listOf(GLOBAL), builder)
var suggestNumber = false
try {
reader.readInt()
suggestNumber = true
} catch (_: CommandSyntaxException) {
suggestNumber = remaining.isEmpty()
}

if (suggestNumber) {
for (i in 1..MAX_SIZE) builder.suggest(remaining + i)
}

return builder.buildFuture()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,13 @@ package com.github.quiltservertools.ledger.commands.parameters
import com.mojang.brigadier.StringReader
import com.mojang.brigadier.arguments.BoolArgumentType
import com.mojang.brigadier.context.CommandContext
import com.mojang.brigadier.exceptions.CommandSyntaxException
import com.mojang.brigadier.suggestion.Suggestions
import com.mojang.brigadier.suggestion.SuggestionsBuilder
import net.minecraft.server.command.ServerCommandSource
import java.util.concurrent.CompletableFuture

class RollbackStatusParameter : SimpleParameter<Boolean>() {
override fun parse(stringReader: StringReader): Boolean = try {
BoolArgumentType.bool().parse(stringReader)
} catch (e: CommandSyntaxException) {
stringReader.readString()
false // TODO Maybe rework parser to alert errors and not require a default value
}
override fun parse(stringReader: StringReader): Boolean = BoolArgumentType.bool().parse(stringReader)

override fun getSuggestions(
context: CommandContext<ServerCommandSource>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ object DatabaseManager {
private fun buildQueryParams(params: ActionSearchParams): Op<Boolean> {
var op: Op<Boolean> = Op.TRUE

if (params.bounds != null) {
if (params.bounds != null && params.bounds != ActionSearchParams.GLOBAL) {
op = op.and { Tables.Actions.x.between(params.bounds.minX, params.bounds.maxX) }
op = op.and { Tables.Actions.y.between(params.bounds.minY, params.bounds.maxY) }
op = op.and { Tables.Actions.z.between(params.bounds.minZ, params.bounds.maxZ) }
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/data/ledger/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"error.ledger.unknown_param": "Unknown parameter %s",
"error.ledger.no_preview": "No current preview",
"error.ledger.unspecific.range": "Please specify a range.",
"error.ledger.unspecific.range_to_big": "The maximum range is %s.",
"error.ledger.unspecific.range_to_big": "The maximum range is %s. Use @global to confirm running on the entire server!",
"error.ledger.unspecific.source_or_time": "Please specify a source or time (after).",

"text.ledger.action.block-place": "placed",
Expand Down

0 comments on commit 88b3745

Please sign in to comment.