network: remove container from memory when closed

This commit is contained in:
Bixilon 2022-02-23 21:58:00 +01:00
parent 21a3769c3c
commit e6b3f81a76
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4

View File

@ -26,6 +26,7 @@ import de.bixilon.minosoft.data.inventory.stack.property.HolderProperty
import de.bixilon.minosoft.data.text.ChatComponent
import de.bixilon.minosoft.protocol.network.connection.play.PlayConnection
import de.bixilon.minosoft.protocol.packets.c2s.play.container.CloseContainerC2SP
import de.bixilon.minosoft.protocol.protocol.ProtocolDefinition
open class Container(
protected val connection: PlayConnection,
@ -42,6 +43,9 @@ open class Container(
var actions: SynchronizedBiMap<Int, ContainerAction> = synchronizedBiMapOf()
var floatingItem: ItemStack? by watched(null)
val id: Int?
get() = connection.player.containers.getKey(this)
init {
this::floatingItem.observe(this) { it?.holder?.container = this }
}
@ -170,7 +174,7 @@ open class Container(
@Synchronized
fun invokeAction(action: ContainerAction) {
action.invoke(connection, connection.player.containers.getKey(this) ?: return, this)
action.invoke(connection, id ?: return, this)
}
fun acknowledgeAction(actionId: Int) {
@ -178,14 +182,18 @@ open class Container(
}
fun revertAction(actionId: Int) {
actions.remove(actionId)?.revert(connection, connection.player.containers.getKey(this) ?: return, this)
actions.remove(actionId)?.revert(connection, id ?: return, this)
}
fun onClose() {
floatingItem = null // ToDo: Does not seem correct
val id = id ?: return
// minecraft behavior, when opening the inventory an open packet is never sent, but a close is
connection.sendPacket(CloseContainerC2SP(connection.player.containers.getKey(this) ?: return))
if (id != ProtocolDefinition.PLAYER_CONTAINER_ID) {
connection.player.containers -= id
}
connection.sendPacket(CloseContainerC2SP(id))
}
override fun iterator(): Iterator<Map.Entry<Int, ItemStack>> {