From e887067b7ecfa9d8e040008cca8d26c194f8c1a5 Mon Sep 17 00:00:00 2001 From: nea Date: Sat, 21 Oct 2023 17:28:00 +0200 Subject: Add tab completion to /warp --- .../firmament/mixins/MixinClientPacketHandler.java | 13 ++++- src/main/kotlin/moe/nea/firmament/Firmament.kt | 3 +- .../moe/nea/firmament/events/CommandEvent.kt | 6 +++ .../moe/nea/firmament/events/MaskCommands.kt | 17 ++++++ .../moe/nea/firmament/features/FeatureManager.kt | 2 + .../nea/firmament/features/chat/AutoCompletions.kt | 60 ++++++++++++++++++++++ .../moe/nea/firmament/repo/RepoDownloadManager.kt | 33 ++++++------ .../kotlin/moe/nea/firmament/repo/RepoManager.kt | 9 +++- src/main/kotlin/moe/nea/firmament/util/MC.kt | 20 +++++++- .../resources/assets/firmament/lang/en_us.json | 3 ++ src/main/resources/firmament.accesswidener | 1 + 11 files changed, 145 insertions(+), 22 deletions(-) create mode 100644 src/main/kotlin/moe/nea/firmament/events/MaskCommands.kt create mode 100644 src/main/kotlin/moe/nea/firmament/features/chat/AutoCompletions.kt (limited to 'src') diff --git a/src/main/java/moe/nea/firmament/mixins/MixinClientPacketHandler.java b/src/main/java/moe/nea/firmament/mixins/MixinClientPacketHandler.java index 220c76f..0dbf60c 100644 --- a/src/main/java/moe/nea/firmament/mixins/MixinClientPacketHandler.java +++ b/src/main/java/moe/nea/firmament/mixins/MixinClientPacketHandler.java @@ -6,6 +6,9 @@ package moe.nea.firmament.mixins; +import com.llamalad7.mixinextras.injector.ModifyExpressionValue; +import com.mojang.brigadier.CommandDispatcher; +import moe.nea.firmament.events.MaskCommands; import moe.nea.firmament.events.ParticleSpawnEvent; import net.minecraft.client.network.ClientPlayNetworkHandler; import net.minecraft.network.packet.s2c.play.ParticleS2CPacket; @@ -16,7 +19,15 @@ import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; @Mixin(ClientPlayNetworkHandler.class) -public class MixinClientPacketHandler { +public abstract class MixinClientPacketHandler { + + + @ModifyExpressionValue(method = "onCommandTree", at = @At(value = "NEW", target = "(Lcom/mojang/brigadier/tree/RootCommandNode;)Lcom/mojang/brigadier/CommandDispatcher;")) + public CommandDispatcher onOnCommandTree(CommandDispatcher dispatcher) { + MaskCommands.Companion.publish(new MaskCommands(dispatcher)); + return dispatcher; + } + @Inject(method = "onParticle", at = @At(value = "INVOKE", target = "Lnet/minecraft/network/NetworkThreadUtils;forceMainThread(Lnet/minecraft/network/packet/Packet;Lnet/minecraft/network/listener/PacketListener;Lnet/minecraft/util/thread/ThreadExecutor;)V", shift = At.Shift.AFTER), cancellable = true) public void onParticleSpawn(ParticleS2CPacket packet, CallbackInfo ci) { var event = new ParticleSpawnEvent( diff --git a/src/main/kotlin/moe/nea/firmament/Firmament.kt b/src/main/kotlin/moe/nea/firmament/Firmament.kt index 3681ab6..a81d66f 100644 --- a/src/main/kotlin/moe/nea/firmament/Firmament.kt +++ b/src/main/kotlin/moe/nea/firmament/Firmament.kt @@ -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 diff --git a/src/main/kotlin/moe/nea/firmament/events/CommandEvent.kt b/src/main/kotlin/moe/nea/firmament/events/CommandEvent.kt index c291ca4..5710245 100644 --- a/src/main/kotlin/moe/nea/firmament/events/CommandEvent.kt +++ b/src/main/kotlin/moe/nea/firmament/events/CommandEvent.kt @@ -16,9 +16,15 @@ import moe.nea.firmament.commands.literal data class CommandEvent( val dispatcher: CommandDispatcher, val ctx: CommandRegistryAccess, + val serverCommands: CommandDispatcher<*>?, ) : FirmamentEvent() { companion object : FirmamentEventBus() + 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.() -> Unit diff --git a/src/main/kotlin/moe/nea/firmament/events/MaskCommands.kt b/src/main/kotlin/moe/nea/firmament/events/MaskCommands.kt new file mode 100644 index 0000000..0ac0dde --- /dev/null +++ b/src/main/kotlin/moe/nea/firmament/events/MaskCommands.kt @@ -0,0 +1,17 @@ +/* + * SPDX-FileCopyrightText: 2023 Linnea Gräf + * + * 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() + + fun mask(name: String) { + dispatcher.root.children.removeIf { it.name.equals(name, ignoreCase = true) } + } +} diff --git a/src/main/kotlin/moe/nea/firmament/features/FeatureManager.kt b/src/main/kotlin/moe/nea/firmament/features/FeatureManager.kt index b9d343c..2683775 100644 --- a/src/main/kotlin/moe/nea/firmament/features/FeatureManager.kt +++ b/src/main/kotlin/moe/nea/firmament/features/FeatureManager.kt @@ -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(serializer(), "feature if (hasAutoloaded) return loadFeature(MinorTrolling) loadFeature(FairySouls) + loadFeature(AutoCompletions) // TODO: loadFeature(FishingWarning) loadFeature(SlotLocking) loadFeature(StorageOverlay) diff --git a/src/main/kotlin/moe/nea/firmament/features/chat/AutoCompletions.kt b/src/main/kotlin/moe/nea/firmament/features/chat/AutoCompletions.kt new file mode 100644 index 0000000..9912321 --- /dev/null +++ b/src/main/kotlin/moe/nea/firmament/features/chat/AutoCompletions.kt @@ -0,0 +1,60 @@ +/* + * SPDX-FileCopyrightText: 2023 Linnea Gräf + * + * 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}") + } + } + } + } + } + } + } +} diff --git a/src/main/kotlin/moe/nea/firmament/repo/RepoDownloadManager.kt b/src/main/kotlin/moe/nea/firmament/repo/RepoDownloadManager.kt index ffb4b6e..1c27a97 100644 --- a/src/main/kotlin/moe/nea/firmament/repo/RepoDownloadManager.kt +++ b/src/main/kotlin/moe/nea/firmament/repo/RepoDownloadManager.kt @@ -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) } } } } diff --git a/src/main/kotlin/moe/nea/firmament/repo/RepoManager.kt b/src/main/kotlin/moe/nea/firmament/repo/RepoManager.kt index 6c877ec..cad1a0f 100644 --- a/src/main/kotlin/moe/nea/firmament/repo/RepoManager.kt +++ b/src/main/kotlin/moe/nea/firmament/repo/RepoManager.kt @@ -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() } } diff --git a/src/main/kotlin/moe/nea/firmament/util/MC.kt b/src/main/kotlin/moe/nea/firmament/util/MC.kt index 50462f7..347a15e 100644 --- a/src/main/kotlin/moe/nea/firmament/util/MC.kt +++ b/src/main/kotlin/moe/nea/firmament/util/MC.kt @@ -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 diff --git a/src/main/resources/assets/firmament/lang/en_us.json b/src/main/resources/assets/firmament/lang/en_us.json index ddf459b..4e68efe 100644 --- a/src/main/resources/assets/firmament/lang/en_us.json +++ b/src/main/resources/assets/firmament/lang/en_us.json @@ -27,6 +27,9 @@ "firmament.repo.reload.disk": "Reloading repository from disk. This may lag a bit.", "firmament.repo.cache": "Recaching items", "firmament.repo.brokenitem": "Failed to render item: %s", + "firmament.config.auto-completions": "Hypixel Command Improvements", + "firmament.config.auto-completions.warp-complete": "Auto Complete /warp", + "firmament.config.auto-completions.warp-is": "Redirect /warp is to /warp island", "firmanent.config.edit": "Edit", "firmament.config.repo": "Firmament Repo Settings", "firmament.config.repo.autoUpdate": "Auto Update", diff --git a/src/main/resources/firmament.accesswidener b/src/main/resources/firmament.accesswidener index 08d4bff..c721b02 100644 --- a/src/main/resources/firmament.accesswidener +++ b/src/main/resources/firmament.accesswidener @@ -5,3 +5,4 @@ accessible class net/minecraft/client/font/TextRenderer$Drawer accessible class net/minecraft/client/render/model/ModelLoader$BakerImpl accessible method net/minecraft/client/render/model/ModelLoader$BakerImpl (Lnet/minecraft/client/render/model/ModelLoader;Ljava/util/function/BiFunction;Lnet/minecraft/util/Identifier;)V accessible field net/minecraft/client/texture/PlayerSkinProvider TEXTURES Ljava/lang/String; +accessible field net/minecraft/client/network/ClientPlayNetworkHandler lastSeenMessagesCollector Lnet/minecraft/network/message/LastSeenMessagesCollector; -- cgit