pick all container action

This commit is contained in:
Bixilon 2022-02-24 18:49:58 +01:00
parent 5919567c71
commit 559be7236d
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
3 changed files with 34 additions and 4 deletions

View File

@ -13,12 +13,42 @@
package de.bixilon.minosoft.data.inventory.click
import de.bixilon.minosoft.data.inventory.stack.ItemStack
import de.bixilon.minosoft.data.registries.other.containers.Container
import de.bixilon.minosoft.protocol.network.connection.play.PlayConnection
import de.bixilon.minosoft.protocol.packets.c2s.play.container.ContainerClickC2SP
/**
* If you double-click on an item in an inventory, all items of the same type will be stacked together and selected
*/
class PickAllContainerAction(
val slot: Int,
) : ContainerAction {
private val mode: Int get() = 6
private val button: Int = 0
override fun invoke(connection: PlayConnection, containerId: Int, container: Container) {
container.lock.lock()
try {
val clicked = container.slots.remove(slot) ?: return
var countLeft = clicked.item.item.maxStackSize - clicked.item._count
val changes: MutableMap<Int, ItemStack?> = mutableMapOf()
for ((slotId, slot) in container.slots) {
if (!slot.matches(slot)) {
continue
}
val countToRemove = minOf(slot.item._count, countLeft)
slot.item._count = countToRemove
countLeft -= countToRemove
slot.item._count += countToRemove
changes[slotId] = slot
if (countLeft <= 0) {
break
}
}
container._validate()
container.floatingItem = clicked
connection.sendPacket(ContainerClickC2SP(containerId, container.serverRevision, this.slot, 6, 0, container.createAction(this), changes, clicked))
} finally {
container.lock.unlock()
}
}
}

View File

@ -54,7 +54,7 @@ class SimpleContainerAction(
return connection.sendPacket(ContainerClickC2SP(containerId, container.serverRevision, null, 0, count.ordinal, container.createAction(this), mapOf(), null))
}
if (target != null && floatingItem.typeEquals(target)) {
if (target != null && floatingItem.matches(target)) {
// merge
val subtract = minOf(target.item.item.maxStackSize - target.item._count, floatingItem.item._count)
target.item._count += subtract

View File

@ -200,7 +200,7 @@ class ItemStack {
return item == other.item && _equals(other)
}
fun typeEquals(other: ItemStack?): Boolean {
fun matches(other: ItemStack?): Boolean {
if (other == null) {
return false
}