Skip to content

Commit 61d6528

Browse files
committed
inventory manager start :doom:, and cleanup. Also moved the onStart call when in creative mode before onBlockBreak to keep proper order
1 parent 56844cf commit 61d6528

File tree

9 files changed

+179
-58
lines changed

9 files changed

+179
-58
lines changed

common/src/main/kotlin/com/lambda/config/groups/InventoryConfig.kt

Lines changed: 13 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,23 @@ package com.lambda.config.groups
2020
import com.lambda.interaction.material.ContainerSelection
2121
import com.lambda.interaction.material.ContainerSelection.Companion.selectContainer
2222
import com.lambda.interaction.material.StackSelection
23-
import com.lambda.interaction.material.StackSelection.Companion.selectStack
2423
import com.lambda.interaction.material.container.MaterialContainer
25-
import com.lambda.util.item.ItemUtils
24+
import com.lambda.interaction.request.RequestConfig
25+
import com.lambda.interaction.request.inventory.InventoryRequest
2626
import net.minecraft.block.Block
27-
import net.minecraft.item.Item
28-
import net.minecraft.item.Items
29-
import net.minecraft.item.ToolItem
30-
import net.minecraft.item.ToolMaterial
31-
import net.minecraft.item.ToolMaterials
3227

33-
interface InventoryConfig {
34-
val disposables: Set<Block>
35-
val swapWithDisposables: Boolean
36-
val providerPriority: Priority
37-
val storePriority: Priority
28+
abstract class InventoryConfig(
29+
prio: Int
30+
) : RequestConfig<InventoryRequest>(prio) {
31+
abstract val disposables: Set<Block>
32+
abstract val swapWithDisposables: Boolean
33+
abstract val providerPriority: Priority
34+
abstract val storePriority: Priority
3835

39-
val accessShulkerBoxes: Boolean
40-
val accessEnderChest: Boolean
41-
val accessChests: Boolean
42-
val accessStashes: Boolean
36+
abstract val accessShulkerBoxes: Boolean
37+
abstract val accessEnderChest: Boolean
38+
abstract val accessChests: Boolean
39+
abstract val accessStashes: Boolean
4340

4441
val containerSelection: ContainerSelection get() = selectContainer {
4542
val allowedContainers = mutableSetOf<MaterialContainer.Rank>().apply {
@@ -52,27 +49,6 @@ interface InventoryConfig {
5249
ofAnyType(*allowedContainers.toTypedArray())
5350
}
5451

55-
val useWoodenTools: Boolean
56-
val useStoneTools: Boolean
57-
val useIronTools: Boolean
58-
val useDiamondTools: Boolean
59-
val useNetheriteTools: Boolean
60-
val useGoldTools: Boolean
61-
val useShears: Boolean
62-
val useFlintAndSteel: Boolean
63-
64-
val allowedTools get() = mutableSetOf<Item>().apply {
65-
addAll(ItemUtils.tools)
66-
if (!useWoodenTools) removeIf { it is ToolItem && it.material == ToolMaterials.WOOD }
67-
if (!useStoneTools) removeIf { it is ToolItem && it.material == ToolMaterials.STONE }
68-
if (!useIronTools) removeIf { it is ToolItem && it.material == ToolMaterials.IRON }
69-
if (!useDiamondTools) removeIf { it is ToolItem && it.material == ToolMaterials.DIAMOND }
70-
if (!useNetheriteTools) removeIf { it is ToolItem && it.material == ToolMaterials.NETHERITE }
71-
if (!useGoldTools) removeIf { it is ToolItem && it.material == ToolMaterials.GOLD }
72-
if (!useShears) removeIf { it == Items.SHEARS }
73-
if (!useFlintAndSteel) removeIf { it == Items.FLINT_AND_STEEL }
74-
}
75-
7652
enum class Priority {
7753
WithMinItems,
7854
WithMaxItems;

common/src/main/kotlin/com/lambda/config/groups/InventorySettings.kt

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,32 +18,30 @@
1818
package com.lambda.config.groups
1919

2020
import com.lambda.config.Configurable
21+
import com.lambda.interaction.request.inventory.InventoryManager
22+
import com.lambda.interaction.request.inventory.InventoryRequest
2123
import com.lambda.util.item.ItemUtils
2224

2325
class InventorySettings(
2426
c: Configurable,
27+
prio: Int = 0,
2528
vis: () -> Boolean = { true },
26-
) : InventoryConfig {
29+
) : InventoryConfig(prio) {
2730
val page by c.setting("Inventory Page", Page.Container, "The page to open when the module is enabled", vis)
2831

2932
override val disposables by c.setting("Disposables", ItemUtils.defaultDisposables, "Items that will be included when checking for a free slot / are allowed to be droped when inventory is full") { vis() && page == Page.Container}
3033
override val swapWithDisposables by c.setting("Swap With Disposables", true, "Swap items with disposable ones") { vis() && page == Page.Container}
31-
override val providerPriority by c.setting("Provider Priority", InventoryConfig.Priority.WithMinItems, "What container to prefer when retrieving the item from") { vis() && page == Page.Container}
32-
override val storePriority by c.setting("Store Priority", InventoryConfig.Priority.WithMinItems, "What container to prefer when storing the item to") { vis() && page == Page.Container}
34+
override val providerPriority by c.setting("Provider Priority", Priority.WithMinItems, "What container to prefer when retrieving the item from") { vis() && page == Page.Container}
35+
override val storePriority by c.setting("Store Priority", Priority.WithMinItems, "What container to prefer when storing the item to") { vis() && page == Page.Container}
3336

3437
override val accessShulkerBoxes by c.setting("Access Shulker Boxes", true, "Allow access to the player's shulker boxes") { vis() && page == Page.Access}
3538
override val accessEnderChest by c.setting("Access Ender Chest", false, "Allow access to the player's ender chest") { vis() && page == Page.Access}
3639
override val accessChests by c.setting("Access Chests", false, "Allow access to the player's normal chests") { vis() && page == Page.Access}
3740
override val accessStashes by c.setting("Access Stashes", false, "Allow access to the player's stashes") { vis() && page == Page.Access}
3841

39-
override val useWoodenTools by c.setting("Use Wooden Tools", false, "Use wooden tools to mine blocks") { vis() && page == Page.Tools}
40-
override val useStoneTools by c.setting("Use Stone Tools", false, "Use stone tools to mine blocks") { vis() && page == Page.Tools}
41-
override val useIronTools by c.setting("Use Iron Tools", false, "Use iron tools to mine blocks") { vis() && page == Page.Tools}
42-
override val useDiamondTools by c.setting("Use Diamond Tools", true, "Use diamond tools to mine blocks") { vis() && page == Page.Tools}
43-
override val useNetheriteTools by c.setting("Use Netherite Tools", true, "Use netherite tools to mine blocks") { vis() && page == Page.Tools}
44-
override val useGoldTools by c.setting("Use Gold Tools", false, "Use gold tools to mine blocks") { vis() && page == Page.Tools}
45-
override val useShears by c.setting("Use Shears", true, "Use shears to mine blocks") { vis() && page == Page.Tools}
46-
override val useFlintAndSteel by c.setting("Use Flint and Steel", true, "Use flint and steel to mine blocks?") { vis() && page == Page.Tools}
42+
override fun requestInternal(request: InventoryRequest, queueIfClosed: Boolean) {
43+
InventoryManager.request(request)
44+
}
4745

4846
enum class Page {
4947
Container, Access, Tools

common/src/main/kotlin/com/lambda/event/events/UpdateManagerEvent.kt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ package com.lambda.event.events
2020
import com.lambda.event.Event
2121

2222
sealed class UpdateManagerEvent {
23-
class Rotation : Event
24-
class Hotbar : Event
25-
class Break : Event
26-
class Place : Event
23+
data object Rotation : Event
24+
data object Inventory : Event
25+
data object Hotbar : Event
26+
data object Break : Event
27+
data object Place : Event
2728
}

common/src/main/kotlin/com/lambda/interaction/request/breaking/BreakManager.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -629,8 +629,8 @@ object BreakManager : RequestHandler<BreakRequest>(
629629

630630
if (gamemode.isCreative) {
631631
lastPosStarted = ctx.expectedPos
632-
onBlockBreak(info)
633632
info.request.onStart?.invoke(ctx.expectedPos)
633+
onBlockBreak(info)
634634
info.startBreakPacket(world, interaction)
635635
breakCooldown = info.breakConfig.breakDelay
636636
return true
@@ -683,5 +683,5 @@ object BreakManager : RequestHandler<BreakRequest>(
683683
return inRange && correctMaterial
684684
}
685685

686-
override fun preEvent(): Event = UpdateManagerEvent.Break().post()
686+
override fun preEvent(): Event = UpdateManagerEvent.Break.post()
687687
}

common/src/main/kotlin/com/lambda/interaction/request/hotbar/HotbarManager.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ object HotbarManager : RequestHandler<HotbarRequest>(
4242
TickEvent.Input.Pre,
4343
TickEvent.Input.Post,
4444
TickEvent.Player.Post,
45-
// ToDo: Post interact
4645
onClose = { checkResetSwap() }
4746
) {
4847
val serverSlot get() = runSafe {
@@ -122,5 +121,5 @@ object HotbarManager : RequestHandler<HotbarRequest>(
122121
}
123122
}
124123

125-
override fun preEvent(): Event = UpdateManagerEvent.Hotbar().post()
124+
override fun preEvent(): Event = UpdateManagerEvent.Hotbar.post()
126125
}
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
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.interaction.request.inventory
19+
20+
import com.lambda.Lambda.mc
21+
import com.lambda.context.SafeContext
22+
import com.lambda.event.EventFlow.post
23+
import com.lambda.event.events.TickEvent
24+
import com.lambda.event.events.UpdateManagerEvent
25+
import com.lambda.event.listener.SafeListener.Companion.listen
26+
import com.lambda.interaction.request.RequestHandler
27+
import net.minecraft.screen.slot.Slot
28+
import net.minecraft.screen.slot.SlotActionType
29+
30+
object InventoryManager : RequestHandler<InventoryRequest>(
31+
1,
32+
TickEvent.Pre,
33+
TickEvent.Input.Pre,
34+
TickEvent.Input.Post,
35+
TickEvent.Player.Post
36+
) {
37+
private var actionsThisTick = 0
38+
private var maxActionsThisTick = 0
39+
40+
override fun load(): String {
41+
super.load()
42+
43+
listen<TickEvent.Post>(priority = Int.MIN_VALUE) {
44+
actionsThisTick = 0
45+
}
46+
47+
return "Loaded Inventory Manager!"
48+
}
49+
50+
override fun SafeContext.handleRequest(request: InventoryRequest) {
51+
if (actionsThisTick + request.actions.size >= maxActionsThisTick) return
52+
if (request.actions.any { !it.canPerform() }) return
53+
request.actions.forEach { action ->
54+
actionsThisTick++
55+
if (!action.perform().done) return
56+
}
57+
}
58+
59+
sealed class InventoryAction {
60+
abstract val slot: Slot
61+
var done = false
62+
private set
63+
64+
fun perform(): InventoryAction {
65+
done = internalPerform()
66+
return this
67+
}
68+
69+
abstract fun internalPerform(): Boolean
70+
abstract fun canPerform(): Boolean
71+
72+
data class Swap(override val slot: Slot, val to: Slot) : InventoryAction() {
73+
override fun internalPerform() = clickSlot(slot.id, 0, SlotActionType.SWAP)
74+
override fun canPerform() = slot.isNotEmpty || to.isNotEmpty
75+
}
76+
77+
data class QuickMove(override val slot: Slot) : InventoryAction() {
78+
override fun internalPerform() = clickSlot(slot.id, 0, SlotActionType.QUICK_MOVE)
79+
override fun canPerform() = slot.isNotEmpty
80+
}
81+
82+
data class Throw(override val slot: Slot) : InventoryAction() {
83+
override fun internalPerform() = clickSlot(slot.id, 0, SlotActionType.THROW)
84+
override fun canPerform() = slot.isNotEmpty
85+
}
86+
87+
data class Distribute(override val slot: Slot, val slots: List<Slot>) : InventoryAction() {
88+
override fun internalPerform(): Boolean {
89+
TODO("Not yet implemented")
90+
}
91+
92+
override fun canPerform() = slot.isNotEmpty && slots.all { it.canInsert(slot.stack) }
93+
}
94+
data class Clone(override val slot: Slot) : InventoryAction() {
95+
override fun internalPerform(): Boolean {
96+
TODO("Not yet implemented")
97+
}
98+
99+
override fun canPerform() = slot.isNotEmpty && mc.player?.isCreative == true
100+
}
101+
102+
fun clickSlot(slotId: Int, button: Int, action: SlotActionType): Boolean {
103+
mc.interactionManager?.let { interaction ->
104+
mc.player?.playerScreenHandler?.syncId?.let { syncId ->
105+
interaction.clickSlot(syncId, slotId, button, action, mc.player)
106+
return true
107+
}
108+
}
109+
return false
110+
}
111+
}
112+
113+
private val Slot.isEmpty get() = !hasStack()
114+
private val Slot.isNotEmpty get() = hasStack()
115+
116+
override fun preEvent() = UpdateManagerEvent.Inventory.post()
117+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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.interaction.request.inventory
19+
20+
import com.lambda.config.groups.InventoryConfig
21+
import com.lambda.interaction.request.Request
22+
23+
class InventoryRequest(
24+
val actions: List<InventoryManager.InventoryAction>,
25+
inventory: InventoryConfig,
26+
prio: Int
27+
) : Request(prio, inventory) {
28+
override val done: Boolean
29+
get() = actions.all { it.done }
30+
}

common/src/main/kotlin/com/lambda/interaction/request/placing/PlaceManager.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,5 +365,5 @@ object PlaceManager : RequestHandler<PlaceRequest>(
365365
)
366366
}
367367

368-
override fun preEvent(): Event = UpdateManagerEvent.Place().post()
368+
override fun preEvent(): Event = UpdateManagerEvent.Place.post()
369369
}

common/src/main/kotlin/com/lambda/interaction/request/rotation/RotationManager.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,5 +290,5 @@ object RotationManager : RequestHandler<RotationRequest>(
290290
}
291291
}
292292

293-
override fun preEvent(): Event = UpdateManagerEvent.Rotation().post()
293+
override fun preEvent(): Event = UpdateManagerEvent.Rotation.post()
294294
}

0 commit comments

Comments
 (0)