outsource container actions from container

This commit is contained in:
Bixilon 2023-01-13 15:46:47 +01:00
parent 493f1acf7d
commit a446472b67
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
16 changed files with 126 additions and 106 deletions

View File

@ -33,7 +33,7 @@ class CloneContainerActionTest {
fun testEmpty() {
val connection = createConnection()
val container = createContainer(connection)
container.invokeAction(CloneContainerAction(0))
container.actions.invoke(CloneContainerAction(0))
assertNull(container.floatingItem)
connection.assertNoPacket()
}
@ -42,7 +42,7 @@ class CloneContainerActionTest {
val connection = createConnection()
val container = createContainer(connection)
container.floatingItem = ItemStack(EggTestO.item, count = 7)
container.invokeAction(CloneContainerAction(6))
container.actions.invoke(CloneContainerAction(6))
assertEquals(container.floatingItem, ItemStack(EggTestO.item, count = 7))
assertNull(container[6])
connection.assertNoPacket()
@ -53,7 +53,7 @@ class CloneContainerActionTest {
val container = createContainer(connection)
container[6] = ItemStack(AppleTestO.item, count = 7)
container.floatingItem = ItemStack(EggTestO.item, count = 7)
container.invokeAction(CloneContainerAction(6))
container.actions.invoke(CloneContainerAction(6))
assertEquals(container.floatingItem, ItemStack(EggTestO.item, count = 7))
assertEquals(container[6], ItemStack(AppleTestO.item, count = 7))
connection.assertNoPacket()
@ -63,7 +63,7 @@ class CloneContainerActionTest {
val connection = createConnection()
val container = createContainer(connection)
container[1] = ItemStack(AppleTestO.item)
container.invokeAction(CloneContainerAction(1))
container.actions.invoke(CloneContainerAction(1))
assertEquals(container.floatingItem, ItemStack(AppleTestO.item, count = 64))
// TODO: Not sending any packet in 1.18.2?
connection.assertOnlyPacket(ContainerClickC2SP(9, container.serverRevision, 1, 3, 0, 0, slotsOf(), ItemStack(AppleTestO.item, count = 64)))
@ -73,7 +73,7 @@ class CloneContainerActionTest {
val connection = createConnection()
val container = createContainer(connection)
container[3] = ItemStack(AppleTestO.item, count = 8)
container.invokeAction(CloneContainerAction(3))
container.actions.invoke(CloneContainerAction(3))
assertEquals(container.floatingItem, ItemStack(AppleTestO.item, count = 64))
// TODO: Not sending any packet in 1.18.2?
connection.assertOnlyPacket(ContainerClickC2SP(9, container.serverRevision, 3, 3, 0, 0, slotsOf(), ItemStack(AppleTestO.item, count = 64)))
@ -83,7 +83,7 @@ class CloneContainerActionTest {
val connection = createConnection()
val container = createContainer(connection)
container[8] = ItemStack(EggTestO.item, count = 9)
container.invokeAction(CloneContainerAction(8))
container.actions.invoke(CloneContainerAction(8))
assertEquals(container.floatingItem, ItemStack(EggTestO.item, count = 16))
// TODO: Not sending any packet in 1.18.2?
connection.assertOnlyPacket(ContainerClickC2SP(9, container.serverRevision, 8, 3, 0, 0, slotsOf(), ItemStack(EggTestO.item, count = 16)))
@ -94,9 +94,9 @@ class CloneContainerActionTest {
val container = createContainer(connection)
container[8] = ItemStack(EggTestO.item, count = 9)
val action = CloneContainerAction(8)
container.invokeAction(action)
container.actions.invoke(action)
assertEquals(container.floatingItem, ItemStack(EggTestO.item, count = 16))
container.revertAction(action)
container.actions.revert(action)
assertNull(container.floatingItem)
}
}

View File

@ -33,7 +33,7 @@ class DropContainerActionTest {
fun dropEmptySingle() {
val connection = createConnection()
val container = createContainer(connection)
container.invokeAction(DropContainerAction(7, false))
container.actions.invoke(DropContainerAction(7, false))
assertNull(container.floatingItem)
connection.assertNoPacket()
}
@ -41,7 +41,7 @@ class DropContainerActionTest {
fun dropEmptyStack() {
val connection = createConnection()
val container = createContainer(connection)
container.invokeAction(DropContainerAction(9, true))
container.actions.invoke(DropContainerAction(9, true))
assertNull(container.floatingItem)
connection.assertNoPacket()
}
@ -50,7 +50,7 @@ class DropContainerActionTest {
val connection = createConnection()
val container = createContainer(connection)
container[9] = ItemStack(AppleTestO.item, count = 8)
container.invokeAction(DropContainerAction(9, false))
container.actions.invoke(DropContainerAction(9, false))
assertNull(container.floatingItem)
assertEquals(container[9], ItemStack(AppleTestO.item, count = 7))
connection.assertOnlyPacket(ContainerClickC2SP(9, container.serverRevision, 9, 4, 0, 0, slotsOf(9 to ItemStack(AppleTestO.item, count = 7)), null))
@ -60,7 +60,7 @@ class DropContainerActionTest {
val connection = createConnection()
val container = createContainer(connection)
container[9] = ItemStack(AppleTestO.item, count = 1)
container.invokeAction(DropContainerAction(9, false))
container.actions.invoke(DropContainerAction(9, false))
assertNull(container.floatingItem)
assertEquals(container[9], null)
connection.assertOnlyPacket(ContainerClickC2SP(9, container.serverRevision, 9, 4, 0, 0, slotsOf(9 to null), null))
@ -70,7 +70,7 @@ class DropContainerActionTest {
val connection = createConnection()
val container = createContainer(connection)
container[9] = ItemStack(AppleTestO.item, count = 12)
container.invokeAction(DropContainerAction(9, true))
container.actions.invoke(DropContainerAction(9, true))
assertNull(container.floatingItem)
assertEquals(container[9], null)
connection.assertOnlyPacket(ContainerClickC2SP(9, container.serverRevision, 9, 4, 1, 0, slotsOf(9 to null), null))
@ -81,8 +81,8 @@ class DropContainerActionTest {
val container = createContainer(connection)
container[8] = ItemStack(EggTestO.item, count = 9)
val action = DropContainerAction(8, false)
container.invokeAction(action)
container.revertAction(action)
container.actions.invoke(action)
container.actions.revert(action)
assertEquals(container[8], ItemStack(EggTestO.item, count = 9))
}
@ -91,8 +91,8 @@ class DropContainerActionTest {
val container = createContainer(connection)
container[8] = ItemStack(EggTestO.item, count = 9)
val action = DropContainerAction(8, true)
container.invokeAction(action)
container.revertAction(action)
container.actions.invoke(action)
container.actions.revert(action)
assertEquals(container[8], ItemStack(EggTestO.item, count = 9))
}
}

View File

@ -36,7 +36,7 @@ class FastMoveContainerActionTest {
fun empty() {
val connection = createConnection()
val container = createChest(connection)
container.invokeAction(FastMoveContainerAction(0))
container.actions.invoke(FastMoveContainerAction(0))
assertNull(container.floatingItem)
assertEquals(container.slots, slotsOf())
connection.assertNoPacket()
@ -46,7 +46,7 @@ class FastMoveContainerActionTest {
val connection = createConnection()
val container = createChest(connection)
container[54] = ItemStack(AppleTestO.item, 9)
container.invokeAction(FastMoveContainerAction(54))
container.actions.invoke(FastMoveContainerAction(54))
assertNull(container.floatingItem)
assertEquals(container.slots, slotsOf(0 to ItemStack(AppleTestO.item, 9)))
connection.assertOnlyPacket(ContainerClickC2SP(9, container.serverRevision, 54, 1, 0, 0, slotsOf(54 to null, 0 to ItemStack(AppleTestO.item, count = 9)), null))
@ -56,7 +56,7 @@ class FastMoveContainerActionTest {
val connection = createConnection()
val container = createChest(connection)
container[0] = ItemStack(AppleTestO.item, 9)
container.invokeAction(FastMoveContainerAction(0))
container.actions.invoke(FastMoveContainerAction(0))
assertNull(container.floatingItem)
assertEquals(container.slots, slotsOf(62 to ItemStack(AppleTestO.item, 9)))
connection.assertOnlyPacket(ContainerClickC2SP(9, container.serverRevision, 0, 1, 0, 0, slotsOf(0 to null, 62 to ItemStack(AppleTestO.item, count = 9)), null))
@ -78,7 +78,7 @@ class FastMoveContainerActionTest {
container[0] = ItemStack(AppleTestO.item, 9)
container.invokeAction(FastMoveContainerAction(0))
container.actions.invoke(FastMoveContainerAction(0))
assertNull(container.floatingItem)
assertNull(container[0])
assertEquals(container[53], ItemStack(AppleTestO.item, 9))
@ -97,7 +97,7 @@ class FastMoveContainerActionTest {
container[0] = ItemStack(CoalTest0.item, 63)
container.invokeAction(FastMoveContainerAction(0))
container.actions.invoke(FastMoveContainerAction(0))
assertNull(container.floatingItem)
assertEquals(
container.slots, slotsOf(
@ -121,7 +121,7 @@ class FastMoveContainerActionTest {
container[30] = ItemStack(EggTestO.item, 12)
container.invokeAction(FastMoveContainerAction(30))
container.actions.invoke(FastMoveContainerAction(30))
assertNull(container.floatingItem)
assertEquals(container.slots, slotsOf(3 to ItemStack(EggTestO.item, 12)))
@ -134,7 +134,7 @@ class FastMoveContainerActionTest {
container[30] = ItemStack(CoalTest0.item, 12)
container.invokeAction(FastMoveContainerAction(30))
container.actions.invoke(FastMoveContainerAction(30))
assertNull(container.floatingItem)
assertEquals(container.slots, slotsOf(1 to ItemStack(CoalTest0.item, 12)))
@ -146,7 +146,7 @@ class FastMoveContainerActionTest {
val connection = createConnection()
val container = createInventory(connection)
container[9] = ItemStack(AppleTestO.item)
container.invokeAction(FastMoveContainerAction(9))
container.actions.invoke(FastMoveContainerAction(9))
assertNull(container.floatingItem)
assertEquals(container.slots, slotsOf(36 to ItemStack(AppleTestO.item)))
}
@ -155,7 +155,7 @@ class FastMoveContainerActionTest {
val connection = createConnection()
val container = createInventory(connection)
container[1] = ItemStack(AppleTestO.item)
container.invokeAction(FastMoveContainerAction(1))
container.actions.invoke(FastMoveContainerAction(1))
assertNull(container.floatingItem)
assertEquals(container.slots, slotsOf(9 to ItemStack(AppleTestO.item)))
}
@ -164,7 +164,7 @@ class FastMoveContainerActionTest {
val connection = createConnection()
val container = createInventory(connection)
container[36] = ItemStack(AppleTestO.item)
container.invokeAction(FastMoveContainerAction(36))
container.actions.invoke(FastMoveContainerAction(36))
assertNull(container.floatingItem)
assertEquals(container.slots, slotsOf(9 to ItemStack(AppleTestO.item)))
}

View File

@ -33,7 +33,7 @@ class PickAllContainerActionTest {
fun testEmpty() {
val connection = createConnection()
val container = createContainer(connection)
container.invokeAction(PickAllContainerAction(0))
container.actions.invoke(PickAllContainerAction(0))
assertEquals(container.slots, slotsOf())
assertNull(container.floatingItem)
connection.assertNoPacket()
@ -43,7 +43,7 @@ class PickAllContainerActionTest {
val connection = createConnection()
val container = createContainer(connection)
container.floatingItem = ItemStack(EggTestO.item, count = 7)
container.invokeAction(PickAllContainerAction(6))
container.actions.invoke(PickAllContainerAction(6))
assertEquals(container.slots, slotsOf())
// connection.assertOnlyPacket(ContainerClickC2SP(9, container.serverRevision, 0, 6, 0, 0, slotsOf(), ItemStack(EggTestO.item, count = 7)))
}
@ -53,7 +53,7 @@ class PickAllContainerActionTest {
val container = createContainer(connection)
container.floatingItem = ItemStack(AppleTestO.item, 1)
container[1] = ItemStack(AppleTestO.item, 2)
container.invokeAction(PickAllContainerAction(0))
container.actions.invoke(PickAllContainerAction(0))
assertEquals(container.slots, slotsOf())
assertEquals(container.floatingItem, ItemStack(AppleTestO.item, count = 3))
connection.assertOnlyPacket(ContainerClickC2SP(9, container.serverRevision, 0, 6, 0, 0, slotsOf(1 to null), ItemStack(AppleTestO.item, count = 3)))
@ -65,7 +65,7 @@ class PickAllContainerActionTest {
container.floatingItem = ItemStack(AppleTestO.item, 1)
container[1] = ItemStack(EggTestO.item, 1)
container[2] = ItemStack(AppleTestO.item, 2)
container.invokeAction(PickAllContainerAction(0))
container.actions.invoke(PickAllContainerAction(0))
assertEquals(container.slots, slotsOf(1 to ItemStack(EggTestO.item, 1)))
assertEquals(container.floatingItem, ItemStack(AppleTestO.item, count = 3))
@ -77,7 +77,7 @@ class PickAllContainerActionTest {
val container = createContainer(connection)
container.floatingItem = ItemStack(AppleTestO.item, 64)
container[5] = ItemStack(AppleTestO.item, 2)
container.invokeAction(PickAllContainerAction(0))
container.actions.invoke(PickAllContainerAction(0))
assertEquals(container.slots, slotsOf(5 to ItemStack(AppleTestO.item, 2)))
assertEquals(container.floatingItem, ItemStack(AppleTestO.item, count = 64))
connection.assertNoPacket()
@ -88,7 +88,7 @@ class PickAllContainerActionTest {
val container = createContainer(connection)
container[0] = ItemStack(AppleTestO.item, 63)
container.floatingItem = ItemStack(AppleTestO.item, 3)
container.invokeAction(PickAllContainerAction(5))
container.actions.invoke(PickAllContainerAction(5))
assertEquals(container.slots, slotsOf(0 to ItemStack(AppleTestO.item, 2)))
assertEquals(container.floatingItem, ItemStack(AppleTestO.item, count = 64))
connection.assertOnlyPacket(ContainerClickC2SP(9, container.serverRevision, 5, 6, 0, 0, slotsOf(0 to ItemStack(AppleTestO.item, count = 2)), ItemStack(AppleTestO.item, count = 64)))
@ -101,10 +101,10 @@ class PickAllContainerActionTest {
container[0] = ItemStack(AppleTestO.item, 7)
container.floatingItem = ItemStack(AppleTestO.item, 9)
val action = PickAllContainerAction(1)
container.invokeAction(action)
container.actions.invoke(action)
assertEquals(container.slots, slotsOf())
assertEquals(container.floatingItem, ItemStack(AppleTestO.item, count = 16))
container.revertAction(action)
container.actions.revert(action)
assertEquals(container.slots, slotsOf(0 to ItemStack(AppleTestO.item, 7)))
assertEquals(container.floatingItem, ItemStack(AppleTestO.item, count = 9))
}
@ -117,11 +117,11 @@ class PickAllContainerActionTest {
container[1] = ItemStack(AppleTestO.item, 8)
container.floatingItem = ItemStack(AppleTestO.item, 4)
val action = PickAllContainerAction(2)
container.invokeAction(action)
container.actions.invoke(action)
assertNull(container[0])
assertNull(container[1])
assertEquals(container.floatingItem, ItemStack(AppleTestO.item, count = 19))
container.revertAction(action)
container.actions.revert(action)
assertEquals(container[0], ItemStack(AppleTestO.item, count = 7))
assertEquals(container[1], ItemStack(AppleTestO.item, count = 8))
assertEquals(container.floatingItem, ItemStack(AppleTestO.item, 4))

View File

@ -33,7 +33,7 @@ class SimpleContainerActionTest {
fun testEmpty() {
val connection = createConnection()
val container = createContainer(connection)
container.invokeAction(SimpleContainerAction(0, SimpleContainerAction.ContainerCounts.ALL))
container.actions.invoke(SimpleContainerAction(0, SimpleContainerAction.ContainerCounts.ALL))
assertEquals(container.slots, slotsOf())
assertNull(container.floatingItem)
// connection.assertOnlyPacket(ContainerClickC2SP(9, container.serverRevision, 0, 0, 0, 0, slotsOf(), null))
@ -44,7 +44,7 @@ class SimpleContainerActionTest {
val connection = createConnection()
val container = createContainer(connection)
container.floatingItem = ItemStack(AppleTestO.item, count = 7)
container.invokeAction(SimpleContainerAction(0, SimpleContainerAction.ContainerCounts.ALL))
container.actions.invoke(SimpleContainerAction(0, SimpleContainerAction.ContainerCounts.ALL))
assertEquals(container.slots, slotsOf(0 to ItemStack(AppleTestO.item, count = 7)))
assertNull(container.floatingItem)
connection.assertOnlyPacket(ContainerClickC2SP(9, container.serverRevision, 0, 0, 0, 0, slotsOf(0 to ItemStack(AppleTestO.item, count = 7)), null))
@ -54,7 +54,7 @@ class SimpleContainerActionTest {
val connection = createConnection()
val container = createContainer(connection)
container.floatingItem = ItemStack(AppleTestO.item, count = 7)
container.invokeAction(SimpleContainerAction(0, SimpleContainerAction.ContainerCounts.PART))
container.actions.invoke(SimpleContainerAction(0, SimpleContainerAction.ContainerCounts.PART))
assertEquals(container.slots, slotsOf(0 to ItemStack(AppleTestO.item, count = 1)))
assertEquals(container.floatingItem, ItemStack(AppleTestO.item, count = 6))
connection.assertOnlyPacket(ContainerClickC2SP(9, container.serverRevision, 0, 0, 1, 0, slotsOf(0 to ItemStack(AppleTestO.item, count = 1)), ItemStack(AppleTestO.item, count = 6)))
@ -65,7 +65,7 @@ class SimpleContainerActionTest {
val container = createContainer(connection)
container.floatingItem = ItemStack(AppleTestO.item, count = 7)
container[8] = ItemStack(AppleTestO.item, count = 2)
container.invokeAction(SimpleContainerAction(8, SimpleContainerAction.ContainerCounts.ALL))
container.actions.invoke(SimpleContainerAction(8, SimpleContainerAction.ContainerCounts.ALL))
assertEquals(container.slots, slotsOf(8 to ItemStack(AppleTestO.item, count = 9)))
assertNull(container.floatingItem)
connection.assertOnlyPacket(ContainerClickC2SP(9, container.serverRevision, 8, 0, 0, 0, slotsOf(8 to ItemStack(AppleTestO.item, count = 9)), null))
@ -76,7 +76,7 @@ class SimpleContainerActionTest {
val container = createContainer(connection)
container.floatingItem = ItemStack(AppleTestO.item, count = 7)
container[12] = ItemStack(AppleTestO.item, count = 3)
container.invokeAction(SimpleContainerAction(12, SimpleContainerAction.ContainerCounts.PART))
container.actions.invoke(SimpleContainerAction(12, SimpleContainerAction.ContainerCounts.PART))
assertEquals(container.slots, slotsOf(12 to ItemStack(AppleTestO.item, count = 4)))
assertEquals(container.floatingItem, ItemStack(AppleTestO.item, count = 6))
connection.assertOnlyPacket(ContainerClickC2SP(9, container.serverRevision, 12, 0, 1, 0, slotsOf(12 to ItemStack(AppleTestO.item, count = 4)), ItemStack(AppleTestO.item, count = 6)))
@ -87,7 +87,7 @@ class SimpleContainerActionTest {
val container = createContainer(connection)
container.floatingItem = ItemStack(EggTestO.item, count = 14)
container[12] = ItemStack(EggTestO.item, count = 15)
container.invokeAction(SimpleContainerAction(12, SimpleContainerAction.ContainerCounts.ALL))
container.actions.invoke(SimpleContainerAction(12, SimpleContainerAction.ContainerCounts.ALL))
assertEquals(container.slots, slotsOf(12 to ItemStack(EggTestO.item, count = 16)))
assertEquals(container.floatingItem, ItemStack(EggTestO.item, 13))
connection.assertOnlyPacket(ContainerClickC2SP(9, container.serverRevision, 12, 0, 0, 0, slotsOf(12 to ItemStack(EggTestO.item, count = 16)), ItemStack(EggTestO.item, count = 13)))
@ -97,7 +97,7 @@ class SimpleContainerActionTest {
val connection = createConnection()
val container = createContainer(connection)
container[12] = ItemStack(AppleTestO.item, count = 3)
container.invokeAction(SimpleContainerAction(12, SimpleContainerAction.ContainerCounts.ALL))
container.actions.invoke(SimpleContainerAction(12, SimpleContainerAction.ContainerCounts.ALL))
assertEquals(container.slots, slotsOf())
assertEquals(container.floatingItem, ItemStack(AppleTestO.item, count = 3))
connection.assertOnlyPacket(ContainerClickC2SP(9, container.serverRevision, 12, 0, 0, 0, slotsOf(12 to null), ItemStack(AppleTestO.item, count = 3)))
@ -107,7 +107,7 @@ class SimpleContainerActionTest {
val connection = createConnection()
val container = createContainer(connection)
container[12] = ItemStack(AppleTestO.item, count = 8)
container.invokeAction(SimpleContainerAction(12, SimpleContainerAction.ContainerCounts.PART))
container.actions.invoke(SimpleContainerAction(12, SimpleContainerAction.ContainerCounts.PART))
assertEquals(container.slots, slotsOf(12 to ItemStack(AppleTestO.item, 4)))
assertEquals(container.floatingItem, ItemStack(AppleTestO.item, count = 4))
connection.assertOnlyPacket(ContainerClickC2SP(9, container.serverRevision, 12, 0, 1, 0, slotsOf(12 to ItemStack(AppleTestO.item, 4)), ItemStack(AppleTestO.item, count = 4)))
@ -117,7 +117,7 @@ class SimpleContainerActionTest {
val connection = createConnection()
val container = createContainer(connection)
container[12] = ItemStack(AppleTestO.item, count = 9)
container.invokeAction(SimpleContainerAction(12, SimpleContainerAction.ContainerCounts.PART))
container.actions.invoke(SimpleContainerAction(12, SimpleContainerAction.ContainerCounts.PART))
assertEquals(container.slots, slotsOf(12 to ItemStack(AppleTestO.item, 4)))
assertEquals(container.floatingItem, ItemStack(AppleTestO.item, count = 5))
connection.assertOnlyPacket(ContainerClickC2SP(9, container.serverRevision, 12, 0, 1, 0, slotsOf(12 to ItemStack(AppleTestO.item, 4)), ItemStack(AppleTestO.item, count = 5)))

View File

@ -13,13 +13,11 @@
package de.bixilon.minosoft.data.container
import de.bixilon.kutil.collections.CollectionUtil.synchronizedBiMapOf
import de.bixilon.kutil.collections.map.bi.SynchronizedBiMap
import de.bixilon.kutil.concurrent.lock.thread.ThreadLock
import de.bixilon.kutil.observer.DataObserver.Companion.observe
import de.bixilon.kutil.observer.DataObserver.Companion.observed
import de.bixilon.kutil.observer.map.MapObserver.Companion.observedMap
import de.bixilon.minosoft.data.container.actions.ContainerAction
import de.bixilon.minosoft.data.container.actions.ContainerActions
import de.bixilon.minosoft.data.container.actions.types.SlotSwapContainerAction
import de.bixilon.minosoft.data.container.sections.ContainerSection
import de.bixilon.minosoft.data.container.slots.DefaultSlotType
@ -45,9 +43,8 @@ abstract class Container(
var propertiesRevision by observed(0L)
var revision by observed(0L)
var serverRevision = 0
private var lastActionId = 0
var actions: SynchronizedBiMap<Int, ContainerAction> = synchronizedBiMapOf()
var floatingItem: ItemStack? by observed(null)
val actions = ContainerActions(this)
val id: Int?
get() = connection.player.containers.getKey(this)
@ -189,30 +186,6 @@ abstract class Container(
internalCommit()
}
@Synchronized
fun createAction(action: ContainerAction): Int {
val nextId = lastActionId++
actions[nextId] = action
return nextId
}
@Synchronized
fun invokeAction(action: ContainerAction) {
action.invoke(connection, id ?: return, this)
}
fun acknowledgeAction(actionId: Int) {
actions.remove(actionId)
}
fun revertAction(actionId: Int) {
actions.remove(actionId)?.let { revertAction(it) }
}
fun revertAction(action: ContainerAction) {
action.revert(connection, id ?: return, this)
}
fun close(force: Boolean = false) {
connection.events.fire(ContainerCloseEvent(connection, this))
onClose()

View File

@ -0,0 +1,47 @@
/*
* Minosoft
* Copyright (C) 2020-2023 Moritz Zwerger
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
*/
package de.bixilon.minosoft.data.container.actions
import de.bixilon.kutil.collections.CollectionUtil.synchronizedBiMapOf
import de.bixilon.kutil.collections.map.bi.SynchronizedBiMap
import de.bixilon.minosoft.data.container.Container
import java.util.concurrent.atomic.AtomicInteger
class ContainerActions(private val container: Container) {
private var id = AtomicInteger()
private val actions: SynchronizedBiMap<Int, ContainerAction> = synchronizedBiMapOf()
fun createId(action: ContainerAction): Int {
val id = id.getAndIncrement()
actions[id] = action
return id
}
fun invoke(action: ContainerAction) {
action.invoke(container.connection, container.id ?: return, container)
}
fun acknowledge(actionId: Int) {
actions.remove(actionId)
}
fun revert(actionId: Int) {
actions.remove(actionId)?.let { revert(it) }
}
fun revert(action: ContainerAction) {
action.revert(container.connection, container.id ?: return, container)
}
}

View File

@ -32,7 +32,7 @@ class CloneContainerAction(
this.copied = itemStack
// TODO (1.18.2): use creative inventory packet
connection.sendPacket(ContainerClickC2SP(containerId, container.serverRevision, slot, 3, 0, container.createAction(this), slotsOf(), itemStack))
connection.sendPacket(ContainerClickC2SP(containerId, container.serverRevision, slot, 3, 0, container.actions.createId(this), slotsOf(), itemStack))
container.floatingItem = itemStack
}

View File

@ -38,7 +38,7 @@ class DropContainerAction(
item.item.decreaseCount()
}
val actionId = container.createAction(this)
val actionId = container.actions.createId(this)
connection.sendPacket(ContainerClickC2SP(containerId, container.serverRevision, slot, 4, if (stack) 1 else 0, actionId, slotsOf(slot to item), null))
// TODO (1.18.2): use creative inventory packet

View File

@ -95,7 +95,7 @@ class FastMoveContainerAction(
}
}
connection.sendPacket(ContainerClickC2SP(containerId, container.serverRevision, this.slot, 1, 0, container.createAction(this), changes, null))
connection.sendPacket(ContainerClickC2SP(containerId, container.serverRevision, this.slot, 1, 0, container.actions.createId(this), changes, null))
} finally {
container.commit()
}

View File

@ -66,7 +66,7 @@ class PickAllContainerAction(
return
}
container.floatingItem = floating
connection.sendPacket(ContainerClickC2SP(containerId, container.serverRevision, this.slot, 6, 0, container.createAction(this), changes, floating))
connection.sendPacket(ContainerClickC2SP(containerId, container.serverRevision, this.slot, 6, 0, container.actions.createId(this), changes, floating))
} finally {
container.commit()
}

View File

@ -46,7 +46,7 @@ class SimpleContainerAction(
}
container.floatingItem = floatingItem
connection.sendPacket(ContainerClickC2SP(containerId, container.serverRevision, slot, 0, count.ordinal, container.createAction(this), slotsOf(slot to item), floatingItem))
connection.sendPacket(ContainerClickC2SP(containerId, container.serverRevision, slot, 0, count.ordinal, container.actions.createId(this), slotsOf(slot to item), floatingItem))
}
private fun putItem(connection: PlayConnection, containerId: Int, container: Container, floatingItem: ItemStack) {
@ -58,7 +58,7 @@ class SimpleContainerAction(
} else {
floatingItem.item._count-- // don't use decrease, item + container is already locked
}
return connection.sendPacket(ContainerClickC2SP(containerId, container.serverRevision, null, 0, count.ordinal, container.createAction(this), slotsOf(), null))
return connection.sendPacket(ContainerClickC2SP(containerId, container.serverRevision, null, 0, count.ordinal, container.actions.createId(this), slotsOf(), null))
}
var target = container[slot]
val slotType = container.getSlotType(slot)
@ -90,7 +90,7 @@ class SimpleContainerAction(
} else {
container.floatingItem = null
}
connection.sendPacket(ContainerClickC2SP(containerId, container.serverRevision, slot, 0, count.ordinal, container.createAction(this), slotsOf(slot to target), container.floatingItem))
connection.sendPacket(ContainerClickC2SP(containerId, container.serverRevision, slot, 0, count.ordinal, container.actions.createId(this), slotsOf(slot to target), container.floatingItem))
return
}
if (target != null && slotType?.canRemove(container, slot, target) != true) {
@ -106,13 +106,13 @@ class SimpleContainerAction(
if (count == ContainerCounts.ALL || (!matches && target != null)) {
container.floatingItem = target
container[slot] = floatingItem
connection.sendPacket(ContainerClickC2SP(containerId, container.serverRevision, slot, 0, count.ordinal, container.createAction(this), slotsOf(slot to floatingItem), target))
connection.sendPacket(ContainerClickC2SP(containerId, container.serverRevision, slot, 0, count.ordinal, container.actions.createId(this), slotsOf(slot to floatingItem), target))
} else {
floatingItem.item.count--
container.floatingItem = floatingItem
target = floatingItem.copy(count = 1)
container[slot] = target
connection.sendPacket(ContainerClickC2SP(containerId, container.serverRevision, slot, 0, count.ordinal, container.createAction(this), slotsOf(slot to target), floatingItem))
connection.sendPacket(ContainerClickC2SP(containerId, container.serverRevision, slot, 0, count.ordinal, container.actions.createId(this), slotsOf(slot to target), floatingItem))
}
}

View File

@ -37,7 +37,7 @@ class SlotSwapContainerAction(
container[this.sourceId] = target
container[targetId] = source
connection.sendPacket(ContainerClickC2SP(containerId, container.serverRevision, sourceId, 2, this.target.button, container.createAction(this), slotsOf(sourceId to target, targetId to source), source))
connection.sendPacket(ContainerClickC2SP(containerId, container.serverRevision, sourceId, 2, this.target.button, container.actions.createId(this), slotsOf(sourceId to target, targetId to source), source))
} finally {
container.commit()

View File

@ -97,7 +97,7 @@ class ItemElement(
return true
}
if (button == MouseButtons.LEFT && count == 2 && itemsElement.container[slotId] == null) {
itemsElement.container.invokeAction(PickAllContainerAction(slotId))
itemsElement.container.actions.invoke(PickAllContainerAction(slotId))
return true
}
@ -106,11 +106,11 @@ class ItemElement(
if (guiRenderer.connection.player.gamemode != Gamemodes.CREATIVE) {
return true
}
itemsElement.container.invokeAction(CloneContainerAction(slotId))
itemsElement.container.actions.invoke(CloneContainerAction(slotId))
return true
}
if (button == MouseButtons.LEFT || button == MouseButtons.RIGHT) {
itemsElement.container.invokeAction(if (shiftDown) {
itemsElement.container.actions.invoke(if (shiftDown) {
FastMoveContainerAction(slotId)
} else {
SimpleContainerAction(slotId, if (button == MouseButtons.LEFT) SimpleContainerAction.ContainerCounts.ALL else SimpleContainerAction.ContainerCounts.PART)
@ -125,7 +125,7 @@ class ItemElement(
return this
}
if (button == MouseButtons.LEFT && count == 2) {
itemsElement.container.invokeAction(PickAllContainerAction(slotId))
itemsElement.container.actions.invoke(PickAllContainerAction(slotId))
return this
}
if (draggable !is FloatingItem) {
@ -133,9 +133,9 @@ class ItemElement(
}
val shiftDown = guiRenderer.isKeyDown(ModifierKeys.SHIFT)
if (shiftDown && button == MouseButtons.LEFT) {
itemsElement.container.invokeAction(FastMoveContainerAction(slotId))
itemsElement.container.actions.invoke(FastMoveContainerAction(slotId))
} else if (button == MouseButtons.LEFT || button == MouseButtons.RIGHT) {
itemsElement.container.invokeAction(SimpleContainerAction(slotId, if (button == MouseButtons.LEFT) SimpleContainerAction.ContainerCounts.ALL else SimpleContainerAction.ContainerCounts.PART))
itemsElement.container.actions.invoke(SimpleContainerAction(slotId, if (button == MouseButtons.LEFT) SimpleContainerAction.ContainerCounts.ALL else SimpleContainerAction.ContainerCounts.PART))
return this
}
return this
@ -148,18 +148,18 @@ class ItemElement(
val container = itemsElement.container
when (key) {
// ToDo: Make this configurable
KeyCodes.KEY_Q -> container.invokeAction(DropContainerAction(slotId, guiRenderer.isKeyDown(ModifierKeys.CONTROL)))
KeyCodes.KEY_Q -> container.actions.invoke(DropContainerAction(slotId, guiRenderer.isKeyDown(ModifierKeys.CONTROL)))
KeyCodes.KEY_1 -> container.invokeAction(SlotSwapContainerAction(slotId, SlotSwapContainerAction.SwapTargets.HOTBAR_1))
KeyCodes.KEY_2 -> container.invokeAction(SlotSwapContainerAction(slotId, SlotSwapContainerAction.SwapTargets.HOTBAR_2))
KeyCodes.KEY_3 -> container.invokeAction(SlotSwapContainerAction(slotId, SlotSwapContainerAction.SwapTargets.HOTBAR_3))
KeyCodes.KEY_4 -> container.invokeAction(SlotSwapContainerAction(slotId, SlotSwapContainerAction.SwapTargets.HOTBAR_4))
KeyCodes.KEY_5 -> container.invokeAction(SlotSwapContainerAction(slotId, SlotSwapContainerAction.SwapTargets.HOTBAR_5))
KeyCodes.KEY_6 -> container.invokeAction(SlotSwapContainerAction(slotId, SlotSwapContainerAction.SwapTargets.HOTBAR_6))
KeyCodes.KEY_7 -> container.invokeAction(SlotSwapContainerAction(slotId, SlotSwapContainerAction.SwapTargets.HOTBAR_7))
KeyCodes.KEY_8 -> container.invokeAction(SlotSwapContainerAction(slotId, SlotSwapContainerAction.SwapTargets.HOTBAR_8))
KeyCodes.KEY_9 -> container.invokeAction(SlotSwapContainerAction(slotId, SlotSwapContainerAction.SwapTargets.HOTBAR_9))
KeyCodes.KEY_F -> container.invokeAction(SlotSwapContainerAction(slotId, SlotSwapContainerAction.SwapTargets.OFFHAND))
KeyCodes.KEY_1 -> container.actions.invoke(SlotSwapContainerAction(slotId, SlotSwapContainerAction.SwapTargets.HOTBAR_1))
KeyCodes.KEY_2 -> container.actions.invoke(SlotSwapContainerAction(slotId, SlotSwapContainerAction.SwapTargets.HOTBAR_2))
KeyCodes.KEY_3 -> container.actions.invoke(SlotSwapContainerAction(slotId, SlotSwapContainerAction.SwapTargets.HOTBAR_3))
KeyCodes.KEY_4 -> container.actions.invoke(SlotSwapContainerAction(slotId, SlotSwapContainerAction.SwapTargets.HOTBAR_4))
KeyCodes.KEY_5 -> container.actions.invoke(SlotSwapContainerAction(slotId, SlotSwapContainerAction.SwapTargets.HOTBAR_5))
KeyCodes.KEY_6 -> container.actions.invoke(SlotSwapContainerAction(slotId, SlotSwapContainerAction.SwapTargets.HOTBAR_6))
KeyCodes.KEY_7 -> container.actions.invoke(SlotSwapContainerAction(slotId, SlotSwapContainerAction.SwapTargets.HOTBAR_7))
KeyCodes.KEY_8 -> container.actions.invoke(SlotSwapContainerAction(slotId, SlotSwapContainerAction.SwapTargets.HOTBAR_8))
KeyCodes.KEY_9 -> container.actions.invoke(SlotSwapContainerAction(slotId, SlotSwapContainerAction.SwapTargets.HOTBAR_9))
KeyCodes.KEY_F -> container.actions.invoke(SlotSwapContainerAction(slotId, SlotSwapContainerAction.SwapTargets.OFFHAND))
else -> Unit
}

View File

@ -54,7 +54,7 @@ class FloatingItem(
return
}
if (target == null) {
container?.invokeAction(SimpleContainerAction(null, if (button == MouseButtons.LEFT) SimpleContainerAction.ContainerCounts.ALL else SimpleContainerAction.ContainerCounts.PART))
container?.actions?.invoke(SimpleContainerAction(null, if (button == MouseButtons.LEFT) SimpleContainerAction.ContainerCounts.ALL else SimpleContainerAction.ContainerCounts.PART))
return
}
}

View File

@ -1,6 +1,6 @@
/*
* Minosoft
* Copyright (C) 2020-2022 Moritz Zwerger
* Copyright (C) 2020-2023 Moritz Zwerger
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
@ -29,9 +29,9 @@ class ContainerActionS2CP(buffer: PlayInByteBuffer) : PlayS2CPacket {
override fun handle(connection: PlayConnection) {
val container = connection.player.containers[containerId] ?: return
if (accepted) {
container.acknowledgeAction(actionId)
container.actions.acknowledge(actionId)
} else {
container.revertAction(actionId)
container.actions.revert(actionId)
}
}