Skip to content

Commit 06d6296

Browse files
committed
creative stack handling
1 parent 54cac3f commit 06d6296

File tree

8 files changed

+49
-20
lines changed

8 files changed

+49
-20
lines changed

src/main/kotlin/com/lambda/interaction/construction/simulation/result/results/BreakResult.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ sealed class BreakResult : BuildResult() {
101101
override fun resolve() {
102102
selectStack {
103103
isItem(badItem).not()
104-
}.transferByTask(HotbarContainer)
104+
}.transferByTask(HotbarContainer)?.execute(task)
105105
}
106106

107107
override fun compareResult(other: ComparableResult<Rank>) =

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,7 @@ object ContainerManager : Loadable {
9898
context(automatedSafeContext: AutomatedSafeContext)
9999
fun StackSelection.transferByTask(destination: MaterialContainer) =
100100
with(automatedSafeContext) {
101-
findContainerWithMaterial(
102-
inventoryConfig.containerSelection
103-
)?.transferByTask(this@transferByTask, destination)
101+
findContainerWithMaterial()?.transferByTask(this@transferByTask, destination)
104102
}
105103

106104
context(_: SafeContext)
@@ -122,8 +120,8 @@ object ContainerManager : Loadable {
122120
containerSelection: ContainerSelection = automated.inventoryConfig.containerSelection,
123121
): List<MaterialContainer> =
124122
containers()
125-
.filter { it.materialAvailable(this) >= count }
126123
.filter { containerSelection.matches(it) }
124+
.filter { it.materialAvailable(this) >= count }
127125
.sortedWith(automated.inventoryConfig.providerPriority.materialComparator(this))
128126

129127
context(automatedSafeContext: AutomatedSafeContext)

src/main/kotlin/com/lambda/interaction/material/container/MaterialContainer.kt

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import com.lambda.util.text.buildText
3939
import com.lambda.util.text.highlighted
4040
import com.lambda.util.text.literal
4141
import com.lambda.util.text.text
42+
import net.minecraft.component.DataComponentTypes
4243
import net.minecraft.item.ItemStack
4344
import net.minecraft.screen.slot.Slot
4445
import net.minecraft.text.Text
@@ -58,6 +59,12 @@ abstract class MaterialContainer(
5859
it.stack.isEmpty
5960
}.thenByDescending {
6061
it.stack.item in automated.inventoryConfig.disposables
62+
}.thenByDescending {
63+
it.stack.item.components.contains(DataComponentTypes.TOOL)
64+
}.thenByDescending {
65+
it.stack.item.components.contains(DataComponentTypes.FOOD)
66+
}.thenByDescending {
67+
it.stack.isStackable
6168
}
6269

6370
@TextDsl
@@ -122,10 +129,11 @@ abstract class MaterialContainer(
122129
context(automatedSafeContext: AutomatedSafeContext)
123130
fun transfer(stackSelection: StackSelection, destination: MaterialContainer): Boolean =
124131
with(automatedSafeContext) {
125-
val fromSlot = stackSelection.filterSlots(slots).firstOrNull() ?: return false
132+
val fromSlot = getSlot(stackSelection) ?: return false
126133
val toSlot = destination.getReplaceSlot() ?: return false
127134
return inventoryRequest {
128-
transfer(fromSlot, toSlot)
135+
if (swapMethodPriority > destination.swapMethodPriority) transfer(fromSlot, toSlot)
136+
else with(destination) { transfer(toSlot, fromSlot) }
129137
}.submit().done
130138
}
131139

@@ -157,6 +165,10 @@ abstract class MaterialContainer(
157165
context(_: AutomatedSafeContext)
158166
open fun getReplaceSlot() = slots.sortedWith(replaceSorter).firstOrNull()
159167

168+
context(_: SafeContext)
169+
open fun getSlot(stackSelection: StackSelection): Slot? =
170+
stackSelection.filterSlots(slots).firstOrNull()
171+
160172
enum class Rank {
161173
MainHand,
162174
OffHand,

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

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ import com.lambda.interaction.material.StackSelection
2424
import com.lambda.interaction.material.container.MaterialContainer
2525
import com.lambda.util.text.buildText
2626
import com.lambda.util.text.literal
27+
import net.minecraft.entity.player.PlayerEntity
28+
import net.minecraft.inventory.SingleStackInventory
2729
import net.minecraft.item.ItemStack
2830
import net.minecraft.screen.slot.Slot
2931

@@ -37,14 +39,29 @@ data object CreativeContainer : MaterialContainer(Rank.Creative) {
3739

3840
override val description = buildText { literal("Creative") }
3941

40-
context(_: SafeContext)
41-
override fun InventoryRequest.InvRequestBuilder.transfer(from: Slot, toHere: Slot) {
42-
clickCreativeStack(toHere.stack, from.id)
42+
context(safeContext : SafeContext)
43+
override fun InventoryRequest.InvRequestBuilder.transfer(fromHere: Slot, toSlot: Slot) {
44+
clickCreativeStack(fromHere.stack, toSlot.id)
45+
safeContext.player.currentScreenHandler.slots[toSlot.id].stack = fromHere.stack
4346
}
4447

45-
context(_: SafeContext)
48+
context(safeContext: SafeContext)
49+
override fun getSlot(stackSelection: StackSelection) =
50+
stackSelection.optimalStack?.let { stack ->
51+
Slot(
52+
object : SingleStackInventory {
53+
override fun getStack() = stack
54+
override fun setStack(stack: ItemStack?) {}
55+
override fun markDirty() {}
56+
override fun canPlayerUse(player: PlayerEntity?) = false
57+
},
58+
0, 0, 0
59+
)
60+
}
61+
62+
context(safeContext: SafeContext)
4663
override fun materialAvailable(selection: StackSelection): Int =
47-
if (mc.player?.isCreative == true && selection.optimalStack != null) Int.MAX_VALUE else 0
64+
if (safeContext.player.isCreative && selection.optimalStack != null) Int.MAX_VALUE else 0
4865

4966
context(_: SafeContext)
5067
override fun spaceAvailable(selection: StackSelection): Int =

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ object HotbarContainer : MaterialContainer(Rank.Hotbar) {
4040
override val description = buildText { literal("Hotbar") }
4141

4242
context(safeContext: SafeContext)
43-
override fun InventoryRequest.InvRequestBuilder.transfer(from: Slot, toHere: Slot) {
44-
swap(from.id, safeContext.player.hotbarSlots.indexOf(toHere))
43+
override fun InventoryRequest.InvRequestBuilder.transfer(fromHere: Slot, toSlot: Slot) {
44+
swap(toSlot.id, safeContext.player.hotbarSlots.indexOf(toSlot))
4545
}
4646
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ object MainHandContainer : MaterialContainer(Rank.MainHand) {
4040
override val description = buildText { literal("MainHand") }
4141

4242
context(safeContext: SafeContext)
43-
override fun InventoryRequest.InvRequestBuilder.transfer(from: Slot, toHere: Slot) {
44-
swap(from.id, safeContext.player.inventory.selectedSlot)
43+
override fun InventoryRequest.InvRequestBuilder.transfer(fromHere: Slot, toSlot: Slot) {
44+
swap(toSlot.id, safeContext.player.inventory.selectedSlot)
4545
}
4646
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ object OffHandContainer : MaterialContainer(Rank.OffHand) {
4040
override val description = buildText { literal("OffHand") }
4141

4242
context(_: SafeContext)
43-
override fun InventoryRequest.InvRequestBuilder.transfer(from: Slot, toHere: Slot) {
44-
swap(from.id, 40)
43+
override fun InventoryRequest.InvRequestBuilder.transfer(fromHere: Slot, toSlot: Slot) {
44+
swap(toSlot.id, 40)
4545
}
4646
}

src/main/kotlin/com/lambda/task/tasks/ContainerTransferTask.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,12 @@ class ContainerTransferTask(
5959
return@listen
6060
}
6161

62-
stackSelection.filterSlots(slots).firstOrNull()?.let { fromSlot ->
62+
fromContainer.getSlot(stackSelection)?.let { fromSlot ->
6363
destination.getReplaceSlot()?.let { toSlot ->
6464
inventoryRequest {
65-
with(fromContainer) { transfer(fromSlot, toSlot) }
65+
if (fromContainer.swapMethodPriority > destination.swapMethodPriority)
66+
with(fromContainer) { transfer(fromSlot, toSlot) }
67+
else with(destination) { transfer(toSlot, toSlot) }
6668
onComplete { success() }
6769
}.submit()
6870
return@listen

0 commit comments

Comments
 (0)