Skip to content

Commit

Permalink
Feature: Reopen last storage command (#2900)
Browse files Browse the repository at this point in the history
Co-authored-by: CalMWolfs <[email protected]>
Co-authored-by: Cal <[email protected]>
Co-authored-by: hannibal2 <[email protected]>
  • Loading branch information
4 people authored Dec 15, 2024
1 parent 497c1ce commit ce0f411
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String>, 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
}
8 changes: 8 additions & 0 deletions src/main/java/at/hannibal2/skyhanni/utils/HypixelCommands.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down

0 comments on commit ce0f411

Please sign in to comment.