From ce0f4111591fab06216d7c25188ef8d7244032ec Mon Sep 17 00:00:00 2001 From: aphased <152172867+aphased@users.noreply.github.com> Date: Sun, 15 Dec 2024 13:40:36 +0100 Subject: [PATCH] Feature: Reopen last storage command (#2900) Co-authored-by: CalMWolfs <94038482+CalMWolfs@users.noreply.github.com> Co-authored-by: Cal Co-authored-by: hannibal2 <24389977+hannibal00212@users.noreply.github.com> --- .../config/features/misc/MiscConfig.java | 11 ++- .../features/commands/OpenLastStorage.kt | 96 +++++++++++++++++++ .../skyhanni/utils/HypixelCommands.kt | 8 ++ 3 files changed, 114 insertions(+), 1 deletion(-) create mode 100644 src/main/java/at/hannibal2/skyhanni/features/commands/OpenLastStorage.kt diff --git a/src/main/java/at/hannibal2/skyhanni/config/features/misc/MiscConfig.java b/src/main/java/at/hannibal2/skyhanni/config/features/misc/MiscConfig.java index 0869262959c0..cc5b0302be52 100644 --- a/src/main/java/at/hannibal2/skyhanni/config/features/misc/MiscConfig.java +++ b/src/main/java/at/hannibal2/skyhanni/config/features/misc/MiscConfig.java @@ -319,6 +319,15 @@ public class MiscConfig { @Expose public HideFarEntitiesConfig hideFarEntities = new HideFarEntitiesConfig(); + @Expose + @ConfigOption( + name = "Open Last Storage", + desc = "Allows running §e/shlastopened §7as a command to open the last storage you opened. " + + "Also allows §e/ec - §7and §e/bp - §7to open the last Ender Chest and Backpack you opened.") + @FeatureToggle + @ConfigEditorBoolean + public boolean openLastStorage = true; + @Expose @ConfigOption(name = "Maintain Volume During Warnings", desc = "Do not change game volume levels when warning sounds are played.") @ConfigEditorBoolean @@ -340,7 +349,7 @@ public class MiscConfig { @Expose @ConfigOption(name = "Computer Time Offset Warning", desc = "Sends a Chat Warning if your computer time is not synchronized with the actual time.\n" + - "§cMaking sure your computer time is correct is important for SkyHanni to display times correctly." + "§cMaking sure your computer time is correct is important for SkyHanni to display times correctly." ) @ConfigEditorBoolean @FeatureToggle diff --git a/src/main/java/at/hannibal2/skyhanni/features/commands/OpenLastStorage.kt b/src/main/java/at/hannibal2/skyhanni/features/commands/OpenLastStorage.kt new file mode 100644 index 000000000000..b0650e5d967e --- /dev/null +++ b/src/main/java/at/hannibal2/skyhanni/features/commands/OpenLastStorage.kt @@ -0,0 +1,96 @@ +package at.hannibal2.skyhanni.features.commands + +import at.hannibal2.skyhanni.SkyHanniMod +import at.hannibal2.skyhanni.api.event.HandleEvent +import at.hannibal2.skyhanni.config.commands.CommandCategory +import at.hannibal2.skyhanni.config.commands.CommandRegistrationEvent +import at.hannibal2.skyhanni.events.MessageSendToServerEvent +import at.hannibal2.skyhanni.skyhannimodule.SkyHanniModule +import at.hannibal2.skyhanni.utils.ChatUtils +import at.hannibal2.skyhanni.utils.ChatUtils.senderIsSkyhanni +import at.hannibal2.skyhanni.utils.HypixelCommands +import at.hannibal2.skyhanni.utils.NumberUtil.formatIntOrUserError + +@SkyHanniModule +object OpenLastStorage { + + private val config get() = SkyHanniMod.feature.misc + + private enum class StorageType(val validPages: IntRange, val runCommand: (Int) -> Unit, vararg val commands: String) { + ENDER_CHEST(1..9, { HypixelCommands.enderChest(it) }, "/enderchest", "/ec"), + BACKPACK(0..18, { HypixelCommands.backPack(it) }, "/backpack", "/bp"), + ; + + val storageName = name.lowercase().replace("_", " ") + var lastPage: Int? = null + fun isValidPage(page: Int) = page in validPages + + companion object { + fun fromCommand(command: String): StorageType? { + return entries.find { command in it.commands } + } + } + } + + // Default to Ender Chest as last storage type, since every profile on any account has at least one partial ender chest page unlocked + private var lastStorageType = StorageType.ENDER_CHEST + + private fun openLastStoragePage(type: StorageType) { + type.lastPage?.let { type.runCommand(it) } + + val message = type.lastPage?.let { page -> + "Opened last ${type.storageName} $page." + } ?: "No last ${type.storageName} to open." + ChatUtils.chat(message) + } + + @HandleEvent + fun onMessageSendToServer(event: MessageSendToServerEvent) { + if (!isEnabled()) return + if (event.senderIsSkyhanni()) return + val args = event.message.lowercase().split(" ") + val type = StorageType.fromCommand(args[0]) ?: return + + if (handleStorage(args, type)) { + event.cancel() + } + } + + @HandleEvent + fun onCommandRegistration(event: CommandRegistrationEvent) { + event.register("shlastopened") { + description = "Opens the storage page last accessed by either /ec or /bp" + category = CommandCategory.USERS_ACTIVE + aliases = listOf("shlo") + callback { + if (isEnabled()) { + openLastStoragePage(lastStorageType) + } else { + ChatUtils.chatAndOpenConfig( + "This feature is disabled, enable it in the config if you want to use it.", + config::openLastStorage, + ) + } + } + } + } + + private fun handleStorage(args: List, type: StorageType): Boolean { + if (args.getOrNull(1) == "-") { + openLastStoragePage(type) + return true + } + + if (args.size <= 1) { + // No argument means open the first page of the respective storage + type.lastPage = 1 + } else { + val pageNumber = args[1].formatIntOrUserError() ?: return false + type.lastPage = pageNumber.takeIf { type.isValidPage(it) } + } + lastStorageType = type + return false + } + + private fun isEnabled() = config.openLastStorage +} diff --git a/src/main/java/at/hannibal2/skyhanni/utils/HypixelCommands.kt b/src/main/java/at/hannibal2/skyhanni/utils/HypixelCommands.kt index f774847129ff..de95479930ea 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/HypixelCommands.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/HypixelCommands.kt @@ -102,6 +102,14 @@ object HypixelCommands { send("wiki $text") } + fun backPack(position: Int) { + send("bp $position") + } + + fun enderChest(position: Int) { + send("ec $position") + } + fun partyWarp() { send("party warp") }