Add tab completion to /warp

This commit is contained in:
nea
2023-10-21 17:28:00 +02:00
parent bc758d17e2
commit e887067b7e
12 changed files with 146 additions and 23 deletions

View File

@@ -49,6 +49,7 @@ import moe.nea.firmament.events.registration.registerFirmamentChatEvents
import moe.nea.firmament.features.FeatureManager
import moe.nea.firmament.repo.HypixelStaticData
import moe.nea.firmament.repo.RepoManager
import moe.nea.firmament.util.MC
import moe.nea.firmament.util.SBData
import moe.nea.firmament.util.data.IDataHolder
@@ -107,7 +108,7 @@ object Firmament {
ctx: CommandRegistryAccess
) {
registerFirmamentCommand(dispatcher)
CommandEvent.publish(CommandEvent(dispatcher, ctx))
CommandEvent.publish(CommandEvent(dispatcher, ctx, MC.networkHandler?.commandDispatcher))
}
@JvmStatic

View File

@@ -16,9 +16,15 @@ import moe.nea.firmament.commands.literal
data class CommandEvent(
val dispatcher: CommandDispatcher<DefaultSource>,
val ctx: CommandRegistryAccess,
val serverCommands: CommandDispatcher<*>?,
) : FirmamentEvent() {
companion object : FirmamentEventBus<CommandEvent>()
fun deleteCommand(name: String) {
dispatcher.root.children.removeIf { it.name.equals(name, ignoreCase = false) }
serverCommands?.root?.children?.removeIf { it.name.equals(name, ignoreCase = false) }
}
fun register(
name: String,
block: CaseInsensitiveLiteralCommandNode.Builder<DefaultSource>.() -> Unit

View File

@@ -0,0 +1,17 @@
/*
* SPDX-FileCopyrightText: 2023 Linnea Gräf <nea@nea.moe>
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
package moe.nea.firmament.events
import com.mojang.brigadier.CommandDispatcher
data class MaskCommands(val dispatcher: CommandDispatcher<*>) : FirmamentEvent() {
companion object : FirmamentEventBus<MaskCommands>()
fun mask(name: String) {
dispatcher.root.children.removeIf { it.name.equals(name, ignoreCase = true) }
}
}

View File

@@ -9,6 +9,7 @@ package moe.nea.firmament.features
import kotlinx.serialization.Serializable
import kotlinx.serialization.serializer
import moe.nea.firmament.Firmament
import moe.nea.firmament.features.chat.AutoCompletions
import moe.nea.firmament.features.chat.ChatLinks
import moe.nea.firmament.features.chat.QuickCommands
import moe.nea.firmament.features.debug.DebugView
@@ -48,6 +49,7 @@ object FeatureManager : DataHolder<FeatureManager.Config>(serializer(), "feature
if (hasAutoloaded) return
loadFeature(MinorTrolling)
loadFeature(FairySouls)
loadFeature(AutoCompletions)
// TODO: loadFeature(FishingWarning)
loadFeature(SlotLocking)
loadFeature(StorageOverlay)

View File

@@ -0,0 +1,60 @@
/*
* SPDX-FileCopyrightText: 2023 Linnea Gräf <nea@nea.moe>
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
package moe.nea.firmament.features.chat
import com.mojang.brigadier.arguments.StringArgumentType.string
import moe.nea.firmament.commands.get
import moe.nea.firmament.commands.suggestsList
import moe.nea.firmament.commands.thenArgument
import moe.nea.firmament.commands.thenExecute
import moe.nea.firmament.events.CommandEvent
import moe.nea.firmament.events.MaskCommands
import moe.nea.firmament.features.FirmamentFeature
import moe.nea.firmament.gui.config.ManagedConfig
import moe.nea.firmament.repo.RepoManager
import moe.nea.firmament.util.MC
object AutoCompletions : FirmamentFeature {
object TConfig : ManagedConfig(identifier) {
val provideWarpTabCompletion by toggle("warp-complete") { true }
val replaceWarpIsByWarpIsland by toggle("warp-is") { true }
}
override val config: ManagedConfig?
get() = TConfig
override val identifier: String
get() = "auto-completions"
override fun onLoad() {
MaskCommands.subscribe {
if (TConfig.provideWarpTabCompletion) {
it.mask("warp")
}
}
CommandEvent.subscribe {
if (TConfig.provideWarpTabCompletion) {
it.deleteCommand("warp")
it.register("warp") {
thenArgument("to", string()) { toArg ->
suggestsList {
RepoManager.neuRepo.constants?.islands?.warps?.flatMap { listOf(it.warp) + it.aliases } ?: listOf()
}
thenExecute {
val warpName = get(toArg)
if (warpName == "is" && TConfig.replaceWarpIsByWarpIsland) {
MC.sendServerCommand("warp island")
} else {
MC.sendServerCommand("warp ${warpName}")
}
}
}
}
}
}
}
}

View File

@@ -31,17 +31,17 @@ object RepoDownloadManager {
val repoMetadataLocation = Firmament.DATA_DIR.resolve("loaded-repo-sha.txt")
private fun loadSavedVersionHash(): String? =
if (repoSavedLocation.exists()) {
if (repoMetadataLocation.exists()) {
try {
repoMetadataLocation.readText().trim()
} catch (e: IOException) {
null
}
} else {
if (repoSavedLocation.exists()) {
if (repoMetadataLocation.exists()) {
try {
repoMetadataLocation.readText().trim()
} catch (e: IOException) {
null
}
} else null
} else {
null
}
} else null
private fun saveVersionHash(versionHash: String) {
latestSavedVersionHash = versionHash
@@ -56,7 +56,7 @@ object RepoDownloadManager {
private suspend fun requestLatestGithubSha(): String? {
val response =
Firmament.httpClient.get("https://api.github.com/repos/${RepoManager.Config.username}/${RepoManager.Config.reponame}/commits/${RepoManager.Config.branch}")
Firmament.httpClient.get("https://api.github.com/repos/${RepoManager.Config.username}/${RepoManager.Config.reponame}/commits/${RepoManager.Config.branch}")
if (response.status.value != 200) {
return null
}
@@ -83,7 +83,8 @@ object RepoDownloadManager {
}
val currentSha = loadSavedVersionHash()
if (latestSha != currentSha || force) {
val requestUrl = "https://github.com/${RepoManager.Config.username}/${RepoManager.Config.reponame}/archive/$latestSha.zip"
val requestUrl =
"https://github.com/${RepoManager.Config.username}/${RepoManager.Config.reponame}/archive/$latestSha.zip"
logger.info("Planning to upgrade repository from $currentSha to $latestSha from $requestUrl")
val zipFile = downloadGithubArchive(requestUrl)
logger.info("Download repository zip file to $zipFile. Deleting old repository")
@@ -106,17 +107,15 @@ object RepoDownloadManager {
val entry = cis.nextEntry ?: break
if (entry.isDirectory) continue
val extractedLocation =
repoSavedLocation.resolve(
entry.name.substringAfter('/', missingDelimiterValue = "")
)
repoSavedLocation.resolve(
entry.name.substringAfter('/', missingDelimiterValue = "")
)
if (repoSavedLocation !in extractedLocation.iterate { it.parent }) {
logger.error("Firmament detected an invalid zip file. This is a potential security risk, please report this in the Firmament discord.")
throw RuntimeException("Firmament detected an invalid zip file. This is a potential security risk, please report this in the Firmament discord.")
}
extractedLocation.parent.createDirectories()
cis.use {
extractedLocation.outputStream().use { cis.copyTo(it) }
}
extractedLocation.outputStream().use { cis.copyTo(it) }
}
}
}

View File

@@ -99,8 +99,13 @@ object RepoManager {
Firmament.coroutineScope.launch {
progressBar.reportProgress("Downloading", 0, null)
CottonHud.add(progressBar)
RepoDownloadManager.downloadUpdate(force)
progressBar.reportProgress("Download complete", 1, 1)
try {
RepoDownloadManager.downloadUpdate(force)
progressBar.reportProgress("Download complete", 1, 1)
} finally {
CottonHud.remove(progressBar)
}
reload()
}
}

View File

@@ -7,13 +7,16 @@
package moe.nea.firmament.util
import io.github.moulberry.repo.data.Coordinate
import java.time.Instant
import java.util.concurrent.ConcurrentLinkedQueue
import net.minecraft.client.MinecraftClient
import net.minecraft.client.gui.screen.ingame.HandledScreen
import net.minecraft.network.message.ArgumentSignatureDataMap
import net.minecraft.network.message.LastSeenMessagesCollector.LastSeenMessages
import net.minecraft.network.packet.c2s.play.CommandExecutionC2SPacket
import net.minecraft.text.Text
import net.minecraft.util.math.BlockPos
import moe.nea.firmament.events.TickEvent
import moe.nea.firmament.mixins.accessor.AccessorHandledScreen
object MC {
@@ -34,10 +37,25 @@ object MC {
messageQueue.add(text)
}
fun sendServerCommand(command: String) {
val nh = player?.networkHandler ?: return
val lastSeenMessages: LastSeenMessages = nh.lastSeenMessagesCollector.collect()
nh.sendPacket(
CommandExecutionC2SPacket(
command,
Instant.now(),
0L,
ArgumentSignatureDataMap.EMPTY,
lastSeenMessages.update()
)
)
}
fun sendCommand(command: String) {
player?.networkHandler?.sendCommand(command)
}
inline val networkHandler get() = player?.networkHandler
inline val instance get() = MinecraftClient.getInstance()
inline val keyboard get() = MinecraftClient.getInstance().keyboard
inline val textureManager get() = MinecraftClient.getInstance().textureManager