Add command for toggling config options

This commit is contained in:
nea
2023-09-06 03:10:44 +02:00
parent 1229227051
commit 0f9ec29648
5 changed files with 196 additions and 133 deletions

View File

@@ -11,14 +11,14 @@ import com.mojang.brigadier.builder.ArgumentBuilder
import com.mojang.brigadier.builder.RequiredArgumentBuilder
import com.mojang.brigadier.context.CommandContext
import com.mojang.brigadier.suggestion.SuggestionProvider
import java.lang.reflect.ParameterizedType
import java.lang.reflect.Type
import java.lang.reflect.TypeVariable
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource
import kotlinx.coroutines.launch
import moe.nea.firmament.Firmament
import moe.nea.firmament.util.MinecraftDispatcher
import moe.nea.firmament.util.iterate
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource
import java.lang.reflect.ParameterizedType
import java.lang.reflect.Type
import java.lang.reflect.TypeVariable
typealias DefaultSource = FabricClientCommandSource
@@ -68,7 +68,14 @@ data class TypeSafeArg<T : Any>(val name: String, val argument: ArgumentType<T>)
fun <S> get(ctx: CommandContext<S>): T {
try {
return ctx.getArgument(name, argClass) as T
} catch (e: Exception) {
if (ctx.child != null) {
return get(ctx.child)
}
throw e
}
}
}
@@ -86,9 +93,9 @@ fun <T : ArgumentBuilder<DefaultSource, T>, AT : Any> T.thenArgument(
block: RequiredArgumentBuilder<DefaultSource, AT>.(TypeSafeArg<AT>) -> Unit
): T = then(argument(name, argument, block))
fun <T : RequiredArgumentBuilder<DefaultSource, String>> T.suggestsList(provider: () -> Iterable<String>) {
fun <T : RequiredArgumentBuilder<DefaultSource, String>> T.suggestsList(provider: CommandContext<DefaultSource>.() -> Iterable<String>) {
suggests(SuggestionProvider<DefaultSource> { context, builder ->
provider()
provider(context)
.asSequence()
.filter { it.startsWith(builder.remaining, ignoreCase = true) }
.forEach {

View File

@@ -9,21 +9,18 @@ package moe.nea.firmament.commands
import com.mojang.brigadier.CommandDispatcher
import com.mojang.brigadier.arguments.StringArgumentType.string
import io.ktor.client.statement.*
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource
import net.minecraft.text.Text
import moe.nea.firmament.apis.UrsaManager
import moe.nea.firmament.features.inventory.storageoverlay.StorageOverlayScreen
import moe.nea.firmament.features.world.FairySouls
import moe.nea.firmament.gui.config.AllConfigsGui
import moe.nea.firmament.gui.config.BooleanHandler
import moe.nea.firmament.gui.config.ManagedOption
import moe.nea.firmament.gui.profileviewer.ProfileViewer
import moe.nea.firmament.repo.HypixelStaticData
import moe.nea.firmament.repo.RepoManager
import moe.nea.firmament.util.FirmFormatters
import moe.nea.firmament.util.MC
import moe.nea.firmament.util.SBData
import moe.nea.firmament.util.ScreenUtil
import moe.nea.firmament.util.SkyblockId
import moe.nea.firmament.util.unformattedString
import moe.nea.firmament.util.*
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource
import net.minecraft.text.Text
fun firmamentCommand() = literal("firmament") {
@@ -31,11 +28,65 @@ fun firmamentCommand() = literal("firmament") {
thenExecute {
AllConfigsGui.showAllGuis()
}
thenLiteral("toggle") {
thenArgument("config", string()) { config ->
suggestsList {
AllConfigsGui.allConfigs.asSequence().map { it.name }.asIterable()
}
thenArgument("property", string()) { property ->
suggestsList {
(AllConfigsGui.allConfigs.find { it.name == this[config] }?:return@suggestsList listOf())
.allOptions.entries.asSequence().filter { it.value.handler is BooleanHandler }.map { it.key }
.asIterable()
}
thenExecute {
val config = this[config]
val property = this[property]
val configObj = AllConfigsGui.allConfigs.find { it.name == config }
if (configObj == null) {
source.sendFeedback(Text.translatable("firmament.command.toggle.no-config-found", config))
return@thenExecute
}
val propertyObj = configObj.allOptions[property]
if (propertyObj == null) {
source.sendFeedback(
Text.translatable(
"firmament.command.toggle.no-property-found",
property
)
)
return@thenExecute
}
if (propertyObj.handler !is BooleanHandler) {
source.sendFeedback(
Text.translatable(
"firmament.command.toggle.not-a-toggle",
property
)
)
return@thenExecute
}
propertyObj as ManagedOption<Boolean>
propertyObj.value = !propertyObj.value
configObj.save()
source.sendFeedback(
Text.translatable(
"firmament.command.toggle.toggled",
configObj.labelText,
propertyObj.labelText,
Text.translatable("firmament.toggle.${propertyObj.value}")
)
)
}
}
}
}
}
thenLiteral("storage") {
thenExecute {
ScreenUtil.setScreenLater(StorageOverlayScreen())
MC.player?.networkHandler?.sendChatCommand("ec")
MC.player?.networkHandler?.sendChatCommand("storage")
}
}
thenLiteral("repo") {

View File

@@ -11,34 +11,31 @@ import io.github.cottonmc.cotton.gui.client.CottonClientScreen
import io.github.cottonmc.cotton.gui.client.LightweightGuiDescription
import io.github.cottonmc.cotton.gui.widget.WBox
import io.github.cottonmc.cotton.gui.widget.WButton
import io.github.cottonmc.cotton.gui.widget.WClippedPanel
import io.github.cottonmc.cotton.gui.widget.WGridPanel
import io.github.cottonmc.cotton.gui.widget.WLabel
import io.github.cottonmc.cotton.gui.widget.WPanel
import io.github.cottonmc.cotton.gui.widget.WPanelWithInsets
import io.github.cottonmc.cotton.gui.widget.WScrollPanel
import io.github.cottonmc.cotton.gui.widget.data.Axis
import io.github.cottonmc.cotton.gui.widget.data.Insets
import io.ktor.http.*
import kotlin.streams.asSequence
import net.minecraft.client.gui.screen.Screen
import net.minecraft.text.Text
import moe.nea.firmament.features.FeatureManager
import moe.nea.firmament.gui.WFixedPanel
import moe.nea.firmament.gui.WSplitPanel
import moe.nea.firmament.gui.WTightScrollPanel
import moe.nea.firmament.repo.RepoManager
import moe.nea.firmament.util.MC
import moe.nea.firmament.util.ScreenUtil.setScreenLater
import net.minecraft.client.gui.screen.Screen
import net.minecraft.text.Text
import kotlin.streams.asSequence
object AllConfigsGui {
val allConfigs
get() = listOf(
RepoManager.Config
) + FeatureManager.allFeatures.mapNotNull { it.config }
fun makeScreen(parent: Screen? = null): CottonClientScreen {
val lwgd = LightweightGuiDescription()
var screen: CottonClientScreen? = null
val configs = listOf(
RepoManager.Config
) + FeatureManager.allFeatures.mapNotNull { it.config }
val configs = allConfigs
val box = WBox(Axis.VERTICAL)
configs.forEach { config ->
val panel = WSplitPanel(

View File

@@ -148,6 +148,8 @@ abstract class ManagedConfig(override val name: String) : ManagedConfigElement()
latestGuiAppender?.reloadables?.forEach { it() }
}
val labelText = Text.translatable("firmament.config.${name}")
fun getConfigEditor(parent: Screen? = null): CottonClientScreen {
val lwgd = LightweightGuiDescription()
var screen: Screen? = null
@@ -165,7 +167,7 @@ abstract class ManagedConfig(override val name: String) : ManagedConfigElement()
}
}
})
it.add(WLabel(Text.translatable("firmament.config.${name}")).also {
it.add(WLabel(labelText).also {
it.verticalAlignment = VerticalAlignment.CENTER
})
})

View File

@@ -1,6 +1,12 @@
{
"firmament.dev.resourcerebuild.start": "Invoking gradle resource rebuild (./gradlew :processResources)",
"firmament.dev.resourcerebuild.done": "Gradle resource rebuild done in %s",
"firmament.command.toggle.no-config-found": "Could not find config %s",
"firmament.command.toggle.no-property-found": "Could not find property %s",
"firmament.command.toggle.not-a-toggle": "Property %s is not a toggle",
"firmament.command.toggle.toggled": "Toggled %s / %s %s",
"firmament.toggle.true": "On",
"firmament.toggle.false": "Off",
"firmament.config.developer": "Developer Settings",
"firmament.config.developer.auto-rebuild": "Automatically rebuild resources",
"firmament.price": "Checking price for %s",