Skip to content

Commit

Permalink
Enforce specific selectors on rollback / restore (#277)
Browse files Browse the repository at this point in the history
  • Loading branch information
DrexHD authored Aug 15, 2024
1 parent 019fb8b commit 50deb8b
Show file tree
Hide file tree
Showing 11 changed files with 56 additions and 30 deletions.
2 changes: 2 additions & 0 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ Found under `[search]`
`timeZone` [Default: "UTC"] sets the timezone to display timestamps in when hovered.
This uses the Java TimeZone format. You can provide offsets ("UTC", "UTC+3"), but the "continent/region" format is preferred. A full list can be found [here](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones).

`maxRange` [Default: 100] controls the maximum range allowed in rollback / restore

### Message theme

Found under `[color]`
Expand Down
2 changes: 0 additions & 2 deletions docs/parameters.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ Multiple Allowed - `No`
Example - `range:5`

This parameter allows you to filter your selection based on your location.
This will not filter by your dimension, so you may still get results from other worlds.
Use the [dimension parameter](#dimension) to filter by world.

## Source
Key - `source:`
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package com.github.quiltservertools.ledger.actionutils

import com.github.quiltservertools.ledger.Ledger
import com.github.quiltservertools.ledger.config.SearchSpec
import com.github.quiltservertools.ledger.utility.Negatable
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType
import net.minecraft.text.Text
import net.minecraft.util.Identifier
import net.minecraft.util.math.BlockBox
import java.time.Instant
import java.util.UUID
import java.util.*
import kotlin.math.max

data class ActionSearchParams(
val bounds: BlockBox?,
Expand All @@ -29,17 +34,20 @@ data class ActionSearchParams(
builder.worlds
)

fun isEmpty() = listOf(
bounds,
before,
after,
actions,
objects,
sourceNames,
sourcePlayerIds,
worlds,
rolledBack
).all { it == null }
fun ensureSpecific() {
if (bounds == null) {
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]) {
throw SimpleCommandExceptionType(
Text.translatable("error.ledger.unspecific.range_to_big", Ledger.config[SearchSpec.maxRange])
).create()
}
if (sourceNames == null && sourcePlayerIds == null && after == null && before == null) {
throw SimpleCommandExceptionType(Text.translatable("error.ledger.unspecific.source_or_time")).create()
}
}

companion object {
inline fun build(block: Builder.() -> Unit) = Builder().apply(block).build()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import com.mojang.brigadier.suggestion.Suggestions
import com.mojang.brigadier.suggestion.SuggestionsBuilder
import net.minecraft.server.command.CommandManager
import net.minecraft.server.command.ServerCommandSource
import net.minecraft.text.Text
import net.minecraft.util.Identifier
import net.minecraft.util.math.BlockBox
import net.minecraft.util.math.BlockPos
Expand Down Expand Up @@ -190,7 +191,7 @@ object SearchParamArgument {
val input = builder.remaining.lowercase()
for (param in paramSuggesters.keys) {
if (param.startsWith(input)) {
builder.suggest("$param:")
builder.suggest("$param:", Text.translatable("text.ledger.parameter.$param.description"))
}
}
return builder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,10 @@ object PreviewCommand : BuildableCommand {
.build()
}

private fun preview(context: Context, params: ActionSearchParams?, type: Preview.Type): Int {
private fun preview(context: Context, params: ActionSearchParams, type: Preview.Type): Int {
val source = context.source
val player = source.playerOrThrow

if (params == null) return -1

params.ensureSpecific()
Ledger.launch(Dispatchers.IO) {
MessageUtils.warnBusy(source)
val actions = DatabaseManager.previewActions(params, type)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ import com.github.quiltservertools.ledger.commands.BuildableCommand
import com.github.quiltservertools.ledger.commands.CommandConsts
import com.github.quiltservertools.ledger.commands.arguments.SearchParamArgument
import com.github.quiltservertools.ledger.database.DatabaseManager
import com.github.quiltservertools.ledger.utility.*
import com.github.quiltservertools.ledger.utility.Context
import com.github.quiltservertools.ledger.utility.LiteralNode
import com.github.quiltservertools.ledger.utility.MessageUtils
import com.github.quiltservertools.ledger.utility.TextColorPallet
import com.github.quiltservertools.ledger.utility.launchMain
import com.github.quiltservertools.ledger.utility.literal
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import me.lucko.fabric.api.permissions.v0.Permissions
Expand All @@ -26,7 +31,7 @@ object RestoreCommand : BuildableCommand {

fun restore(context: Context, params: ActionSearchParams): Int {
val source = context.source

params.ensureSpecific()
Ledger.launch(Dispatchers.IO) {
MessageUtils.warnBusy(source)
val actions = DatabaseManager.restoreActions(params)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ import com.github.quiltservertools.ledger.commands.BuildableCommand
import com.github.quiltservertools.ledger.commands.CommandConsts
import com.github.quiltservertools.ledger.commands.arguments.SearchParamArgument
import com.github.quiltservertools.ledger.database.DatabaseManager
import com.github.quiltservertools.ledger.utility.*
import com.github.quiltservertools.ledger.utility.Context
import com.github.quiltservertools.ledger.utility.LiteralNode
import com.github.quiltservertools.ledger.utility.MessageUtils
import com.github.quiltservertools.ledger.utility.TextColorPallet
import com.github.quiltservertools.ledger.utility.launchMain
import com.github.quiltservertools.ledger.utility.literal
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import me.lucko.fabric.api.permissions.v0.Permissions
Expand All @@ -24,11 +29,9 @@ object RollbackCommand : BuildableCommand {
.build()
}

fun rollback(context: Context, params: ActionSearchParams?): Int {
fun rollback(context: Context, params: ActionSearchParams): Int {
val source = context.source

if (params == null) return -1

params.ensureSpecific()
Ledger.launch(Dispatchers.IO) {
MessageUtils.warnBusy(source)
val actions = DatabaseManager.rollbackActions(params)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ object SearchCommand : BuildableCommand {

private fun search(context: Context, params: ActionSearchParams): Int {
val source = context.source
if (params.isEmpty()) {
source.sendError(Text.translatable("error.ledger.command.no_params"))
return -1
}

Ledger.launch {
Ledger.searchCache[source.name] = params
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ object SearchSpec : ConfigSpec() {
val pageSize by required<Int>()
val purgePermissionLevel by required<Int>()
val timeZone by required<ZoneId>()
val maxRange by required<Int>()
}
12 changes: 12 additions & 0 deletions src/main/resources/data/ledger/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@
"error.ledger.no_more_pages": "No more pages",
"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.source_or_time": "Please specify a source or time (after).",

"text.ledger.action.block-place": "placed",
"text.ledger.action.block-break": "broke",
Expand All @@ -56,6 +59,15 @@
"text.ledger.action.entity-kill": "killed",
"text.ledger.action.entity-change": "changed",

"text.ledger.parameter.action.description": "Filter by type of performed action.",
"text.ledger.parameter.after.description": "Filter by time (after the provided time).",
"text.ledger.parameter.before.description": "Filter by time (before the provided time). Usually you want to use after.",
"text.ledger.parameter.object.description": "Filter by matching an object. Could be a block, item or entity.",
"text.ledger.parameter.range.description": "Filter by location (square radius around your location).",
"text.ledger.parameter.rolledback.description": "Filter by rolled back state.",
"text.ledger.parameter.source.description": "Filter by source (player name or source with '@').",
"text.ledger.parameter.world.description": "Filter by provided dimension.",

"text.ledger.network.protocols_mismatched": "Protocol versions mismatched: client mod is Ledger protocol version %d and the client version is %d",
"text.ledger.network.no_mod_info": "Unable to determine mod information for your client mod",

Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/ledger.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ pageSize = 8
purgePermissionLevel = 4
# Time zone to display timestamps in (UTC, UTC+1, America/Los_Angeles)
timeZone = "UTC"
# Maximum range allowed in rollback / restore commands
maxRange = 100

[color]
# Colors in hex format
Expand Down

0 comments on commit 50deb8b

Please sign in to comment.