Skip to content

Commit 765f4d8

Browse files
j10a1n15meowora
andauthored
feat: item refill (#3)
Co-authored-by: Mona <59416038+meowora@users.noreply.github.com>
1 parent 87e4cbf commit 765f4d8

4 files changed

Lines changed: 133 additions & 0 deletions

File tree

src/main/kotlin/me/owdding/mortem/config/Config.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import com.teamresourceful.resourcefulconfig.api.types.info.ResourcefulConfigLin
55
import com.teamresourceful.resourcefulconfig.api.types.options.TranslatableValue
66
import com.teamresourceful.resourcefulconfigkt.api.ConfigKt
77
import me.owdding.mortem.Mortem
8+
import me.owdding.mortem.config.category.MiscConfig
9+
import me.owdding.mortem.config.category.NotifierConfig
810
import me.owdding.mortem.config.category.OverlayConfig
911
import me.owdding.mortem.config.category.OverlayPositions
1012
import java.util.function.UnaryOperator
@@ -17,6 +19,8 @@ object Config : ConfigKt("mortem/config") {
1719

1820
init {
1921
category(OverlayConfig)
22+
category(NotifierConfig)
23+
category(MiscConfig)
2024

2125
category(OverlayPositions)
2226
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package me.owdding.mortem.config.category
2+
3+
import com.teamresourceful.resourcefulconfigkt.api.CategoryKt
4+
import me.owdding.mortem.features.ItemRefill
5+
6+
object MiscConfig : CategoryKt("misc") {
7+
override val name = Translated("mortem.config.misc")
8+
9+
var itemRefill by select(ItemRefill.RefillItems.ENDER_PEARL) {
10+
translation = "mortem.config.misc.item_refill"
11+
}
12+
13+
var automaticRefillOnEnter by boolean(false) {
14+
translation = "mortem.config.misc.automatic_refill_on_enter"
15+
condition = { false }
16+
}
17+
18+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package me.owdding.mortem.features
2+
3+
import me.owdding.ktmodules.Module
4+
import me.owdding.mortem.config.category.MiscConfig
5+
import me.owdding.mortem.utils.GfsQueue
6+
import me.owdding.mortem.utils.extensions.sendWithPrefix
7+
import tech.thatgravyboat.skyblockapi.api.events.base.Subscription
8+
import tech.thatgravyboat.skyblockapi.api.events.dungeon.DungeonEnterEvent
9+
import tech.thatgravyboat.skyblockapi.api.events.misc.RegisterCommandsEvent
10+
import tech.thatgravyboat.skyblockapi.api.events.misc.RegisterCommandsEvent.Companion.argument
11+
import tech.thatgravyboat.skyblockapi.api.remote.api.SkyBlockId
12+
import tech.thatgravyboat.skyblockapi.api.remote.api.SkyBlockId.Companion.getSkyBlockId
13+
import tech.thatgravyboat.skyblockapi.helpers.McPlayer
14+
import tech.thatgravyboat.skyblockapi.utils.command.EnumArgument
15+
import tech.thatgravyboat.skyblockapi.utils.text.Text
16+
17+
@Module
18+
object ItemRefill {
19+
20+
@Subscription
21+
fun onCommand(event: RegisterCommandsEvent) {
22+
event.register("mortem refill") {
23+
thenCallback("item", EnumArgument<RefillItems>()) {
24+
refill(argument<RefillItems>("item"))
25+
}
26+
27+
callback {
28+
refill(*MiscConfig.itemRefill)
29+
}
30+
}
31+
}
32+
33+
@Subscription
34+
fun onEnter(event: DungeonEnterEvent) {
35+
if (!MiscConfig.automaticRefillOnEnter) return
36+
37+
refill(*MiscConfig.itemRefill)
38+
}
39+
40+
private fun refill(vararg items: RefillItems, sendOutput: Boolean = true) {
41+
val output = items.mapNotNull {
42+
val count = countItem(it.id)
43+
if (count < it.maxStackSize) {
44+
GfsQueue.add(it.id, it.maxStackSize - count)
45+
"${it.maxStackSize - count}x ${it.name}"
46+
} else null
47+
}
48+
49+
if (sendOutput) {
50+
// TODO fancy colors
51+
Text.join("Item Refill | ", output.joinToString(", ")).sendWithPrefix()
52+
}
53+
}
54+
55+
private fun countItem(id: SkyBlockId) = McPlayer.inventory.filter { it.getSkyBlockId() == id }.sumOf { it.count }
56+
57+
enum class RefillItems(val maxStackSize: Int = 64, id: String? = null) {
58+
SPIRIT_LEAP(maxStackSize = 16),
59+
SUPERBOOM_TNT,
60+
ENDER_PEARL(maxStackSize = 16),
61+
DECOY,
62+
INFLATABLE_JERRY,
63+
;
64+
65+
val id = SkyBlockId.item(id ?: name)
66+
}
67+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package me.owdding.mortem.utils
2+
3+
import me.owdding.ktmodules.Module
4+
import tech.thatgravyboat.skyblockapi.api.events.base.Subscription
5+
import tech.thatgravyboat.skyblockapi.api.events.hypixel.ServerChangeEvent
6+
import tech.thatgravyboat.skyblockapi.api.events.time.TickEvent
7+
import tech.thatgravyboat.skyblockapi.api.remote.api.SkyBlockId
8+
import tech.thatgravyboat.skyblockapi.helpers.McClient
9+
import tech.thatgravyboat.skyblockapi.utils.time.currentInstant
10+
import tech.thatgravyboat.skyblockapi.utils.time.since
11+
import java.util.*
12+
import kotlin.time.Duration.Companion.seconds
13+
14+
@Module
15+
// TODO: MLIB
16+
// check wether item is actually in the sack
17+
// combine same ids
18+
// last message send by player to server
19+
// last server switch
20+
object GfsQueue {
21+
22+
private val queue: Queue<Pair<SkyBlockId, Int>> = LinkedList()
23+
private var lastFetch = currentInstant()
24+
25+
fun add(id: SkyBlockId, amount: Int) {
26+
queue.add(Pair(id, amount))
27+
}
28+
29+
@Subscription(ServerChangeEvent::class)
30+
fun onServerSwap() {
31+
lastFetch = currentInstant().plus(2.seconds)
32+
}
33+
34+
@Subscription
35+
fun onTick(event: TickEvent) {
36+
if (queue.isEmpty()) return
37+
if (lastFetch.since() <= 2.5.seconds) return
38+
39+
val (id, amount) = queue.poll() ?: return
40+
41+
McClient.sendCommand("/gfs ${id.skyblockId} $amount")
42+
lastFetch = currentInstant()
43+
}
44+
}

0 commit comments

Comments
 (0)