Skip to content

Commit 573b69c

Browse files
committed
Clean up InventoryEvents
1 parent 34b3fb1 commit 573b69c

File tree

10 files changed

+83
-55
lines changed

10 files changed

+83
-55
lines changed

common/src/main/java/com/lambda/mixin/MinecraftClientMixin.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import com.lambda.Lambda;
2121
import com.lambda.event.EventFlow;
2222
import com.lambda.event.events.ClientEvent;
23-
import com.lambda.event.events.ScreenHandlerEvent;
23+
import com.lambda.event.events.InventoryEvent;
2424
import com.lambda.event.events.TickEvent;
2525
import com.lambda.module.modules.player.Interact;
2626
import net.minecraft.client.MinecraftClient;
@@ -78,7 +78,7 @@ private void onStartup(CallbackInfo ci) {
7878
private void onScreenOpen(@Nullable Screen screen, CallbackInfo ci) {
7979
if (screen == null) return;
8080
if (screen instanceof ScreenHandlerProvider<?> handledScreen) {
81-
EventFlow.post(new ScreenHandlerEvent.Open(handledScreen.getScreenHandler()));
81+
EventFlow.post(new InventoryEvent.Open(handledScreen.getScreenHandler()));
8282
}
8383
}
8484

common/src/main/java/com/lambda/mixin/network/ClientPlayNetworkHandlerMixin.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
public class ClientPlayNetworkHandlerMixin {
3232
@Inject(method = "onUpdateSelectedSlot", at = @At(value = "TAIL"))
3333
private void onUpdateSelectedSlot(UpdateSelectedSlotS2CPacket packet, CallbackInfo ci) {
34-
EventFlow.post(new InventoryEvent.SelectedSlotUpdate(packet.getSlot()));
34+
EventFlow.post(new InventoryEvent.SelectedHotbarSlotUpdate(packet.getSlot()));
3535
}
3636

3737
@Inject(method = "onScreenHandlerSlotUpdate", at = @At(value = "TAIL"))

common/src/main/java/com/lambda/mixin/render/ScreenHandlerMixin.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
package com.lambda.mixin.render;
1919

2020
import com.lambda.event.EventFlow;
21-
import com.lambda.event.events.ScreenHandlerEvent;
21+
import com.lambda.event.events.InventoryEvent;
2222
import net.minecraft.item.ItemStack;
2323
import net.minecraft.screen.ScreenHandler;
2424
import org.spongepowered.asm.mixin.Mixin;
@@ -32,6 +32,6 @@
3232
public class ScreenHandlerMixin {
3333
@Inject(method = "updateSlotStacks", at = @At("TAIL"))
3434
private void onUpdateSlotStacksHead(int revision, List<ItemStack> stacks, ItemStack cursorStack, CallbackInfo ci) {
35-
EventFlow.post(new ScreenHandlerEvent.Update(revision, stacks, cursorStack));
35+
EventFlow.post(new InventoryEvent.FullUpdate(revision, stacks, cursorStack));
3636
}
3737
}

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

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,71 @@ package com.lambda.event.events
1919

2020
import com.lambda.event.Event
2121
import net.minecraft.item.ItemStack
22+
import net.minecraft.screen.ScreenHandler
2223

24+
/**
25+
* Represents various events related to inventory interactions, updates, and state changes.
26+
*
27+
* This sealed class encompasses different types of inventory events that occur during gameplay,
28+
* such as opening or closing an inventory, updating inventory contents, or changing hotbar slots.
29+
* Each event provides specific contextual data relevant to the inventory interaction.
30+
*/
2331
sealed class InventoryEvent {
24-
class SelectedSlotUpdate(val slot: Int) : Event
25-
class SlotUpdate(val syncId: Int, val revision: Int, val slot: Int, val stack: ItemStack) : Event
32+
/**
33+
* Represents an event triggered when an inventory or screen is opened.
34+
*
35+
* This event provides access to the associated [ScreenHandler], which manages the interaction
36+
* logic between the inventory and the player. It is typically used to initialize or handle
37+
* logic when the screen linked to a player's inventory is opened.
38+
*
39+
* @property screenHandler The screen handler associated with the opened inventory.
40+
*/
41+
class Open(val screenHandler: ScreenHandler) : Event
42+
43+
/**
44+
* Represents an event triggered when an inventory or screen is closed.
45+
*
46+
* This event provides access to the associated [ScreenHandler], which manages the interaction
47+
* logic between the inventory and the player. It is typically used to finalize or clean up logic
48+
* when a player's inventory screen is closed.
49+
*
50+
* @property screenHandler The screen handler associated with the closed inventory.
51+
*/
52+
class Close(val screenHandler: ScreenHandler) : Event
53+
54+
/**
55+
* Represents an update event for an inventory, typically triggered when inventory contents or states
56+
* change during gameplay.
57+
*
58+
* @property revision The revision number indicating the current state of the inventory.
59+
* @property stacks A list of item stacks representing the contents of the inventory slots.
60+
* @property cursorStack The item stack currently held by the cursor.
61+
*/
62+
data class FullUpdate(
63+
val revision: Int,
64+
val stacks: List<ItemStack>,
65+
val cursorStack: ItemStack,
66+
) : Event
67+
68+
/**
69+
* Represents an event triggered when a specific inventory slot is updated.
70+
*
71+
* @property syncId The synchronization ID related to the container or inventory.
72+
* @property revision The revision number representing the state of the inventory.
73+
* @property slot The index of the updated inventory slot.
74+
* @property stack The new item stack in the updated slot.
75+
*/
76+
class SlotUpdate(
77+
val syncId: Int,
78+
val revision: Int,
79+
val slot: Int,
80+
val stack: ItemStack
81+
) : Event
82+
83+
/**
84+
* Represents an event triggered when a player switches their selected hotbar slot.
85+
*
86+
* @property slot The index of the newly selected hotbar slot.
87+
*/
88+
class SelectedHotbarSlotUpdate(val slot: Int) : Event
2689
}

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

Lines changed: 0 additions & 33 deletions
This file was deleted.

common/src/main/kotlin/com/lambda/interaction/material/container/ContainerManager.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
package com.lambda.interaction.material.container
1919

2020
import com.lambda.core.Loadable
21+
import com.lambda.event.events.InventoryEvent
2122
import com.lambda.event.events.PlayerEvent
22-
import com.lambda.event.events.ScreenHandlerEvent
2323
import com.lambda.event.listener.SafeListener.Companion.listen
2424
import com.lambda.interaction.material.StackSelection
2525
import com.lambda.interaction.material.StackSelection.Companion.select
@@ -58,7 +58,7 @@ object ContainerManager : Loadable {
5858
lastInteractedBlockEntity = it.blockHitResult.blockPos.blockEntity(world)
5959
}
6060

61-
listen<ScreenHandlerEvent.Close> { event ->
61+
listen<InventoryEvent.Close> { event ->
6262
if (event.screenHandler !is GenericContainerScreenHandler) return@listen
6363

6464
val handler = event.screenHandler

common/src/main/kotlin/com/lambda/interaction/material/transfer/transaction/PickFromInventoryTransaction.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class PickFromInventoryTransaction @Ta5kBuilder constructor(
3636
confirming = true
3737
}
3838

39-
listen<InventoryEvent.SelectedSlotUpdate> {
39+
listen<InventoryEvent.SelectedHotbarSlotUpdate> {
4040
finish()
4141
}
4242
}

common/src/main/kotlin/com/lambda/module/modules/debug/InventoryDebug.kt

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,13 @@
1818
package com.lambda.module.modules.debug
1919

2020
import com.lambda.Lambda.LOG
21+
import com.lambda.event.events.InventoryEvent
2122
import com.lambda.event.events.PacketEvent
22-
import com.lambda.event.events.ScreenHandlerEvent
23-
import com.lambda.event.events.WorldEvent
2423
import com.lambda.event.listener.SafeListener.Companion.listen
2524
import com.lambda.module.Module
2625
import com.lambda.module.tag.ModuleTag
2726
import com.lambda.util.Communication.info
2827
import com.lambda.util.DynamicReflectionSerializer.dynamicString
29-
import net.minecraft.entity.player.PlayerInventory
3028
import net.minecraft.network.packet.c2s.play.*
3129
import net.minecraft.network.packet.s2c.play.InventoryS2CPacket
3230
import net.minecraft.network.packet.s2c.play.UpdateSelectedSlotS2CPacket
@@ -37,19 +35,19 @@ object InventoryDebug : Module(
3735
defaultTags = setOf(ModuleTag.DEBUG)
3836
) {
3937
init {
40-
listen<ScreenHandlerEvent.Open> { event ->
38+
listen<InventoryEvent.Open> { event ->
4139
info("Opened screen handler: ${event.screenHandler::class.simpleName}")
4240

4341
LOG.info("\n" + event.screenHandler.slots.joinToString("\n") {
4442
"${it.inventory::class.simpleName} ${it.index} ${it.x} ${it.y}"
4543
})
4644
}
4745

48-
listen<ScreenHandlerEvent.Close> {
46+
listen<InventoryEvent.Close> {
4947
info("Closed screen handler: ${it.screenHandler::class.simpleName}")
5048
}
5149

52-
listen<ScreenHandlerEvent.Update> {
50+
listen<InventoryEvent.FullUpdate> {
5351
info("Updated screen handler: ${it.revision}, ${it.stacks}, ${it.cursorStack}")
5452
}
5553

common/src/main/kotlin/com/lambda/module/modules/player/InventoryTweaks.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
package com.lambda.module.modules.player
1919

2020
import com.lambda.context.SafeContext
21+
import com.lambda.event.events.InventoryEvent
2122
import com.lambda.event.events.PlayerEvent
22-
import com.lambda.event.events.ScreenHandlerEvent
2323
import com.lambda.event.listener.SafeListener.Companion.listen
2424
import com.lambda.module.Module
2525
import com.lambda.module.tag.ModuleTag
@@ -68,7 +68,7 @@ object InventoryTweaks : Module(
6868
}.run()
6969
}
7070

71-
listen<ScreenHandlerEvent.Close> { event ->
71+
listen<InventoryEvent.Close> { event ->
7272
if (event.screenHandler != lastOpenScreen) return@listen
7373
lastOpenScreen = null
7474
placedPos?.let {

common/src/main/kotlin/com/lambda/task/tasks/OpenContainer.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ package com.lambda.task.tasks
1919

2020
import com.lambda.config.groups.RotationConfig
2121
import com.lambda.config.groups.InteractionConfig
22+
import com.lambda.event.events.InventoryEvent
2223
import com.lambda.event.events.RotationEvent
23-
import com.lambda.event.events.ScreenHandlerEvent
2424
import com.lambda.event.listener.SafeListener.Companion.listen
2525
import com.lambda.interaction.visibilty.VisibilityChecker.lookAtBlock
2626
import com.lambda.module.modules.client.TaskFlowModule
@@ -56,7 +56,7 @@ class OpenContainer @Ta5kBuilder constructor(
5656
}
5757

5858
init {
59-
listen<ScreenHandlerEvent.Open> {
59+
listen<InventoryEvent.Open> {
6060
if (state != State.OPENING) return@listen
6161

6262
screenHandler = it.screenHandler
@@ -65,14 +65,14 @@ class OpenContainer @Ta5kBuilder constructor(
6565
if (!waitForSlotLoad) success(it.screenHandler)
6666
}
6767

68-
listen<ScreenHandlerEvent.Close> {
68+
listen<InventoryEvent.Close> {
6969
if (screenHandler != it.screenHandler) return@listen
7070

7171
state = State.SCOPING
7272
screenHandler = null
7373
}
7474

75-
listen<ScreenHandlerEvent.Update> {
75+
listen<InventoryEvent.FullUpdate> {
7676
if (state != State.SLOT_LOADING) return@listen
7777

7878
screenHandler?.let {

0 commit comments

Comments
 (0)