Skip to content

Commit dc30c3c

Browse files
committed
stack replenish module
1 parent 8113f26 commit dc30c3c

File tree

2 files changed

+77
-1
lines changed

2 files changed

+77
-1
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Copyright 2025 Lambda
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
package com.lambda.module.modules.player
19+
20+
import com.lambda.config.AutomationConfig.Companion.setDefaultAutomationConfig
21+
import com.lambda.config.applyEdits
22+
import com.lambda.context.SafeContext
23+
import com.lambda.event.events.TickEvent
24+
import com.lambda.event.listener.SafeListener.Companion.listen
25+
import com.lambda.interaction.managers.inventory.InventoryRequest.Companion.inventoryRequest
26+
import com.lambda.module.Module
27+
import com.lambda.module.tag.ModuleTag
28+
import com.lambda.util.item.ItemStackUtils.slotId
29+
import com.lambda.util.player.SlotUtils.hotbar
30+
import com.lambda.util.player.SlotUtils.storage
31+
import net.minecraft.item.ItemStack
32+
import net.minecraft.item.Items
33+
34+
object StackReplenish : Module(
35+
name = "StackReplenish",
36+
description = "Automatically refills stacks from your inventory",
37+
tag = ModuleTag.PLAYER
38+
) {
39+
private val minStackPercent by setting("Min Stack Percentage", 30, 0..100, 1, "Minimum percentage of a complete stack before refilling", "%")
40+
private val offhand by setting("Offhand", false, "Replenishes the players offhand stack")
41+
42+
init {
43+
setDefaultAutomationConfig {
44+
applyEdits {
45+
hideAllGroupsExcept(inventoryConfig)
46+
inventoryConfig.apply {
47+
::immediateAccessOnly.edit { defaultValue(false) }
48+
hide(::disposables, ::swapWithDisposables, ::providerPriority, ::storePriority)
49+
}
50+
}
51+
}
52+
53+
listen<TickEvent.Pre> {
54+
if (player.currentScreenHandler.cursorStack.item !== Items.AIR) return@listen
55+
player.hotbar.forEach { stack -> checkReplenish(stack) }
56+
if (offhand) checkReplenish(player.offHandStack)
57+
}
58+
}
59+
60+
private fun SafeContext.checkReplenish(stack: ItemStack) {
61+
if (stack.count.toFloat() / stack.maxCount >= (minStackPercent.toFloat() / 100)) return
62+
if (!stack.isStackable) return
63+
64+
player.storage.forEach { invStack ->
65+
if (invStack.item !== stack.item) return@forEach
66+
val invId = invStack.slotId
67+
val completing = stack.count + invStack.count >= stack.maxCount
68+
val tooMany = invStack.count + stack.count > stack.maxCount
69+
inventoryRequest {
70+
moveSlot(invId, stack.slotId)
71+
if (tooMany) pickup(invId)
72+
}.submit()
73+
if (completing) return
74+
}
75+
}
76+
}

src/main/kotlin/com/lambda/util/item/ItemStackUtils.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ object ItemStackUtils {
8080
val List<ItemStack>.copy: List<ItemStack> get() = map { it.copy() }
8181

8282
context(safeContext: SafeContext)
83-
val ItemStack.slotId get() = safeContext.player.currentScreenHandler.slots.find { it.stack.equal(this) }?.id ?: -1
83+
val ItemStack.slotId get() = safeContext.player.currentScreenHandler.slots.find { it.stack === this }?.id ?: -1
8484
context(safeContext: SafeContext)
8585
val ItemStack.inventoryIndex get() = safeContext.player.inventory.getSlotWithStack(this)
8686
context(safeContext: SafeContext)

0 commit comments

Comments
 (0)