Skip to content

Commit 5924c10

Browse files
committed
Synchronized creative stack creation
1 parent 20840f0 commit 5924c10

File tree

3 files changed

+69
-11
lines changed

3 files changed

+69
-11
lines changed

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

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import com.lambda.Lambda.mc
2121
import com.lambda.context.SafeContext
2222
import com.lambda.interaction.material.StackSelection
2323
import com.lambda.interaction.material.container.MaterialContainer
24+
import com.lambda.interaction.material.transfer.TransactionExecutor
25+
import com.lambda.interaction.material.transfer.TransactionExecutor.Companion
2426
import com.lambda.task.Task
2527
import com.lambda.util.item.ItemStackUtils.equal
2628
import com.lambda.util.text.buildText
@@ -47,13 +49,15 @@ data object CreativeContainer : MaterialContainer(Rank.CREATIVE) {
4749
throw NotInCreativeModeException()
4850
}
4951

50-
player.currentScreenHandler?.slots?.let { slots ->
51-
selection.filterSlots(slots).forEach {
52-
interaction.clickCreativeStack(ItemStack.EMPTY, it.id)
52+
TransactionExecutor.transfer {
53+
player.currentScreenHandler?.slots?.let { slots ->
54+
selection.filterSlots(slots).forEach {
55+
clickCreativeStack(ItemStack.EMPTY, it.id)
56+
}
5357
}
54-
}
55-
56-
success()
58+
}.finally {
59+
success()
60+
}.execute(this@CreativeDeposit)
5761
}
5862
}
5963

@@ -71,11 +75,11 @@ data object CreativeContainer : MaterialContainer(Rank.CREATIVE) {
7175
throw NotInCreativeModeException()
7276
}
7377

74-
interaction.clickCreativeStack(
75-
optimalStack,
76-
36 + player.inventory.selectedSlot
77-
)
78-
success()
78+
TransactionExecutor.transfer {
79+
clickCreativeStack(optimalStack, 36 + player.inventory.selectedSlot)
80+
}.finally {
81+
success()
82+
}.execute(this@CreativeWithdrawal)
7983
return
8084
}
8185

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import com.lambda.event.events.TickEvent
2222
import com.lambda.event.listener.SafeListener.Companion.listen
2323
import com.lambda.interaction.material.transfer.transaction.*
2424
import com.lambda.task.Task
25+
import net.minecraft.item.ItemStack
2526
import net.minecraft.screen.slot.SlotActionType
2627

2728
class TransactionExecutor @Ta5kBuilder constructor(
@@ -132,6 +133,11 @@ class TransactionExecutor @Ta5kBuilder constructor(
132133
pickup(targetSlotId, 0)
133134
}
134135

136+
@InvTransfer
137+
fun clickCreativeStack(stack: ItemStack, slotId: Int) {
138+
transactions.add(ClickCreativeStackTransaction(stack, slotId))
139+
}
140+
135141
companion object {
136142
@InvTransfer
137143
fun transfer(block: TransactionExecutor.() -> Unit) =
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright 2024 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.material.transfer.transaction
19+
20+
import com.lambda.event.events.InventoryEvent
21+
import com.lambda.event.events.TickEvent
22+
import com.lambda.event.listener.SafeListener.Companion.listen
23+
import com.lambda.interaction.material.transfer.InventoryTransaction
24+
import com.lambda.util.player.SlotUtils.clickSlot
25+
import net.minecraft.item.ItemStack
26+
import net.minecraft.screen.slot.SlotActionType
27+
28+
class ClickCreativeStackTransaction @Ta5kBuilder constructor(
29+
private val stack: ItemStack,
30+
private val slotId: Int,
31+
) : InventoryTransaction() {
32+
override val name: String get() = "Creating stack $stack at #$slotId"
33+
private var confirming = false
34+
35+
init {
36+
listen<TickEvent.Pre> {
37+
if (confirming) return@listen
38+
39+
interaction.clickCreativeStack(stack, slotId)
40+
confirming = true
41+
}
42+
43+
listen<InventoryEvent.SlotUpdate> {
44+
if (it.slot != slotId) return@listen
45+
finish()
46+
}
47+
}
48+
}

0 commit comments

Comments
 (0)