Skip to content

Commit 3db3423

Browse files
committed
Rest of "a big technical change"
- Moved commands to Kotlin and used BrigadierX library to organize the codes.
1 parent 97d90e1 commit 3db3423

File tree

9 files changed

+98
-104
lines changed

9 files changed

+98
-104
lines changed

build.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ dependencies {
4545
def calcLib = "com.github.octocynth:calculator-lib:8c69c67b0e"
4646
implementation calcLib
4747
include calcLib
48+
49+
var brigadierX = "com.github.taskeren:brigadierX:${brigadier_x_version}"
50+
implementation brigadierX
51+
include brigadierX
4852
}
4953

5054
base {

gradle.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,4 @@ fabric_kotlin_version=1.10.16+kotlin.1.9.21
2424
kotlin_version=1.9.21
2525
cloth_config_version=13.0.114
2626
modmenu_version=9.0.0
27+
brigadier_x_version=1.3.2

src/main/java/cn/taskeren/minequery/command/CmdCalculator.java

Lines changed: 0 additions & 39 deletions
This file was deleted.

src/main/java/cn/taskeren/minequery/command/CmdKey2CmdSettings.java

Lines changed: 0 additions & 19 deletions
This file was deleted.

src/main/java/cn/taskeren/minequery/command/CmdLocationCalc.java

Lines changed: 0 additions & 40 deletions
This file was deleted.

src/main/kotlin/cn/taskeren/minequery/MineQuery.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package cn.taskeren.minequery
22

3-
import cn.taskeren.minequery.command.CmdCalculator
4-
import cn.taskeren.minequery.command.CmdKey2CmdSettings
5-
import cn.taskeren.minequery.command.CmdLocationCalc
3+
import cn.taskeren.minequery.command.CommandCalculator
4+
import cn.taskeren.minequery.command.CommandKey2CommandSettings
5+
import cn.taskeren.minequery.command.CommandLocationCalculator
66
import cn.taskeren.minequery.config.MineQueryConfig
77
import cn.taskeren.minequery.features.*
88
import me.shedaniel.autoconfig.AutoConfig
@@ -48,9 +48,9 @@ object MineQuery : ClientModInitializer {
4848

4949
// register commands
5050
ClientCommandRegistrationCallback.EVENT.register { dispatcher, _ ->
51-
dispatcher.register(CmdCalculator.getBuilder())
52-
dispatcher.register(CmdLocationCalc.getBuilder())
53-
dispatcher.register(CmdKey2CmdSettings.getBuilder())
51+
dispatcher.register(CommandCalculator)
52+
dispatcher.register(CommandLocationCalculator)
53+
dispatcher.register(CommandKey2CommandSettings)
5454
}
5555
}
5656

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package cn.taskeren.minequery.command
2+
3+
import cn.taskeren.brigadierx.argument
4+
import cn.taskeren.brigadierx.executesX
5+
import cn.taskeren.brigadierx.newLiteralArgBuilder
6+
import com.mojang.brigadier.arguments.StringArgumentType
7+
import io.github.endreman0.calculator.Calculator
8+
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource
9+
import net.minecraft.text.Text
10+
import net.minecraft.util.Formatting
11+
12+
private const val KEY_EQUATION = "equation"
13+
14+
internal val CommandCalculator = newLiteralArgBuilder<FabricClientCommandSource>("=") {
15+
argument(KEY_EQUATION, StringArgumentType.greedyString()) {
16+
executesX { ctx ->
17+
val equation = StringArgumentType.getString(ctx, KEY_EQUATION)
18+
val feedback = runCatching {
19+
val type = Calculator.calculate(equation)
20+
Text.translatable(
21+
"text.minequery.calculator.done", "${Formatting.GRAY}$equation", "${Formatting.GREEN}${type}"
22+
)
23+
}.getOrElse {
24+
Text.translatable(
25+
"text.minequery.calculator.error", "${Formatting.RED}${Formatting.ITALIC}${it.message}"
26+
)
27+
}
28+
ctx.source.sendFeedback(feedback)
29+
}
30+
}
31+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package cn.taskeren.minequery.command
2+
3+
import cn.taskeren.brigadierx.executesX
4+
import cn.taskeren.brigadierx.newLiteralArgBuilder
5+
import cn.taskeren.minequery.gui.Key2CommandSettingScreen
6+
import com.mojang.brigadier.Command
7+
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource
8+
9+
internal val CommandKey2CommandSettings = newLiteralArgBuilder<FabricClientCommandSource>("key2cmd") {
10+
executesX {
11+
// according to https://www.reddit.com/r/fabricmc/comments/w165i1/changing_the_clients_screen_from_a_command_help/
12+
// any screen will be closed after command execution, so I need to open the screen after the end of the command.
13+
it.source.client.send {
14+
Key2CommandSettingScreen.show()
15+
}
16+
Command.SINGLE_SUCCESS
17+
}
18+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package cn.taskeren.minequery.command
2+
3+
import cn.taskeren.brigadierx.argument
4+
import cn.taskeren.brigadierx.executesX
5+
import cn.taskeren.brigadierx.literal
6+
import cn.taskeren.brigadierx.newLiteralArgBuilder
7+
import cn.taskeren.minequery.command.client_pos.ClientPosArgumentType
8+
import com.mojang.brigadier.Command
9+
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource
10+
import net.minecraft.text.Text
11+
import kotlin.math.floor
12+
13+
internal val CommandLocationCalculator =
14+
newLiteralArgBuilder<FabricClientCommandSource>("localc") {
15+
literal("toNether") {
16+
argument("location", ClientPosArgumentType.columnPos()) {
17+
executes {
18+
val pos = ClientPosArgumentType.getColumnPos(it, "location")
19+
val x = floor(pos.x.toDouble() / 8)
20+
val z = floor(pos.z.toDouble() / 8)
21+
it.source.sendFeedback(Text.translatable("text.minequery.localc.to_nether", x, z))
22+
Command.SINGLE_SUCCESS
23+
}
24+
}
25+
}
26+
27+
literal("toOverworld") {
28+
argument("location", ClientPosArgumentType.columnPos()) {
29+
executesX {
30+
val pos = ClientPosArgumentType.getColumnPos(it, "location")
31+
val x = floor(pos.x.toDouble() * 8)
32+
val z = floor(pos.z.toDouble() * 8)
33+
it.source.sendFeedback(Text.translatable("text.minequery.localc.to_overworld", x, z))
34+
Command.SINGLE_SUCCESS
35+
}
36+
}
37+
}
38+
}

0 commit comments

Comments
 (0)