restructure some container closing logic

This commit is contained in:
Bixilon 2023-01-13 11:47:01 +01:00
parent 80ed4978fb
commit b7a053b83d
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
11 changed files with 53 additions and 23 deletions

View File

@ -0,0 +1,20 @@
/*
* 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
/**
* This container is managed by the client and always be present
* Container closing means in this context hiding it
*/
interface ClientContainer

View File

@ -33,6 +33,7 @@ import de.bixilon.minosoft.data.registries.containers.ContainerType
import de.bixilon.minosoft.data.registries.identified.Namespaces.minecraft import de.bixilon.minosoft.data.registries.identified.Namespaces.minecraft
import de.bixilon.minosoft.data.registries.identified.ResourceLocation import de.bixilon.minosoft.data.registries.identified.ResourceLocation
import de.bixilon.minosoft.data.text.ChatComponent import de.bixilon.minosoft.data.text.ChatComponent
import de.bixilon.minosoft.modding.event.events.container.ContainerCloseEvent
import de.bixilon.minosoft.protocol.network.connection.play.PlayConnection import de.bixilon.minosoft.protocol.network.connection.play.PlayConnection
import de.bixilon.minosoft.protocol.packets.c2s.play.container.CloseContainerC2SP import de.bixilon.minosoft.protocol.packets.c2s.play.container.CloseContainerC2SP
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap
@ -215,20 +216,27 @@ open class Container(
action.revert(connection, id ?: return, this) action.revert(connection, id ?: return, this)
} }
fun onClose() { fun close(force: Boolean = false) {
floatingItem = null // ToDo: Does not seem correct connection.events.fire(ContainerCloseEvent(connection, this))
onClose()
val id = id ?: return val id = id ?: return
if (id != PlayerInventory.CONTAINER_ID) { if (id != PlayerInventory.CONTAINER_ID || this is ClientContainer) {
connection.player.containers -= id connection.player.containers -= id
} }
// minecraft behavior, when opening the inventory an open packet is never sent, but a close is
if (connection.player.openedContainer == this) {
if (force && connection.player.openedContainer == this) {
connection.player.openedContainer = null
connection.sendPacket(CloseContainerC2SP(id)) connection.sendPacket(CloseContainerC2SP(id))
} }
} }
protected open fun onClose() {
floatingItem = null // ToDo: Does not seem correct
}
override fun iterator(): Iterator<Map.Entry<Int, ItemStack>> { override fun iterator(): Iterator<Map.Entry<Int, ItemStack>> {
return slots.iterator() return slots.iterator()
} }

View File

@ -16,6 +16,7 @@ package de.bixilon.minosoft.data.container.types
import de.bixilon.kutil.collections.CollectionUtil.lockMapOf import de.bixilon.kutil.collections.CollectionUtil.lockMapOf
import de.bixilon.kutil.collections.map.LockMap import de.bixilon.kutil.collections.map.LockMap
import de.bixilon.kutil.observer.map.MapObserver.Companion.observeMap import de.bixilon.kutil.observer.map.MapObserver.Companion.observeMap
import de.bixilon.minosoft.data.container.ClientContainer
import de.bixilon.minosoft.data.container.Container import de.bixilon.minosoft.data.container.Container
import de.bixilon.minosoft.data.container.EquipmentSlots import de.bixilon.minosoft.data.container.EquipmentSlots
import de.bixilon.minosoft.data.container.click.SlotSwapContainerAction import de.bixilon.minosoft.data.container.click.SlotSwapContainerAction
@ -40,7 +41,7 @@ import de.bixilon.minosoft.protocol.network.connection.play.PlayConnection
import de.bixilon.minosoft.util.KUtil.toResourceLocation import de.bixilon.minosoft.util.KUtil.toResourceLocation
// https://c4k3.github.io/wiki.vg/images/1/13/Inventory-slots.png // https://c4k3.github.io/wiki.vg/images/1/13/Inventory-slots.png
class PlayerInventory(connection: PlayConnection) : Container(connection = connection, type = TYPE) { class PlayerInventory(connection: PlayConnection) : Container(connection = connection, type = TYPE), ClientContainer {
override val sections: Array<ContainerSection> get() = SECTIONS override val sections: Array<ContainerSection> get() = SECTIONS
val equipment: LockMap<EquipmentSlots, ItemStack> = lockMapOf() val equipment: LockMap<EquipmentSlots, ItemStack> = lockMapOf()

View File

@ -107,7 +107,7 @@ class LocalPlayerEntity(
} }
} }
var openedContainer: Container? = null var openedContainer: Container? by observed(null)
val itemCooldown: MutableMap<Item, ItemCooldown> = synchronizedMapOf() val itemCooldown: MutableMap<Item, ItemCooldown> = synchronizedMapOf()

View File

@ -1,6 +1,6 @@
/* /*
* Minosoft * 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. * 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.
* *
@ -66,6 +66,6 @@ abstract class ContainerScreen<C : Container>(
override fun onClose() { override fun onClose() {
super.onClose() super.onClose()
container.onClose() container.close()
} }
} }

View File

@ -1,6 +1,6 @@
/* /*
* Minosoft * 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. * 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.
* *
@ -19,6 +19,5 @@ import de.bixilon.minosoft.protocol.network.connection.play.PlayConnection
class ContainerCloseEvent( class ContainerCloseEvent(
connection: PlayConnection, connection: PlayConnection,
val containerId: Int,
val container: Container, val container: Container,
) : PlayConnectionEvent(connection) ) : PlayConnectionEvent(connection)

View File

@ -1,6 +1,6 @@
/* /*
* Minosoft * 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. * 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.
* *
@ -19,6 +19,5 @@ import de.bixilon.minosoft.protocol.network.connection.play.PlayConnection
class ContainerOpenEvent( class ContainerOpenEvent(
connection: PlayConnection, connection: PlayConnection,
val containerId: Int,
val container: Container, val container: Container,
) : PlayConnectionEvent(connection) ) : PlayConnectionEvent(connection)

View File

@ -120,7 +120,7 @@ class ConnectionUtil(
connection.world.particleRenderer?.removeAllParticles() connection.world.particleRenderer?.removeAllParticles()
connection.player.openedContainer?.let { connection.player.openedContainer?.let {
connection.player.openedContainer = null connection.player.openedContainer = null
connection.events.fire(ContainerCloseEvent(connection, it.id ?: -1, it)) connection.events.fire(ContainerCloseEvent(connection, it))
} }
connection.player.healthCondition = HealthCondition() connection.player.healthCondition = HealthCondition()
connection.world.time = WorldTime() connection.world.time = WorldTime()

View File

@ -1,6 +1,6 @@
/* /*
* Minosoft * 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. * 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.
* *
@ -27,7 +27,10 @@ class OpenHorseContainerS2CP(buffer: PlayInByteBuffer) : PlayS2CPacket {
val entityId: Int = buffer.readInt() val entityId: Int = buffer.readInt()
override fun handle(connection: PlayConnection) { override fun handle(connection: PlayConnection) {
// ToDo val entity = connection.world.entities[entityId] ?: return
// TODO
Log.log(LogMessageType.NETWORK_PACKETS_IN, LogLevels.WARN) { "Can not open entity container: $entity" }
} }
override fun log(reducedLog: Boolean) { override fun log(reducedLog: Boolean) {

View File

@ -13,7 +13,6 @@
package de.bixilon.minosoft.protocol.packets.s2c.play.container package de.bixilon.minosoft.protocol.packets.s2c.play.container
import de.bixilon.minosoft.data.container.types.PlayerInventory import de.bixilon.minosoft.data.container.types.PlayerInventory
import de.bixilon.minosoft.modding.event.events.container.ContainerCloseEvent
import de.bixilon.minosoft.protocol.network.connection.play.PlayConnection import de.bixilon.minosoft.protocol.network.connection.play.PlayConnection
import de.bixilon.minosoft.protocol.packets.factory.LoadPacket import de.bixilon.minosoft.protocol.packets.factory.LoadPacket
import de.bixilon.minosoft.protocol.packets.s2c.PlayS2CPacket import de.bixilon.minosoft.protocol.packets.s2c.PlayS2CPacket
@ -27,13 +26,13 @@ class CloseContainerS2CP(buffer: PlayInByteBuffer) : PlayS2CPacket {
val containerId: Int = buffer.readUnsignedByte() val containerId: Int = buffer.readUnsignedByte()
override fun handle(connection: PlayConnection) { override fun handle(connection: PlayConnection) {
if (containerId == PlayerInventory.CONTAINER_ID) { val container = connection.player.containers[containerId] ?: return
if (container is PlayerInventory || containerId == PlayerInventory.CONTAINER_ID) {
// server can not close inventory // server can not close inventory
return return
} }
val container = connection.player.containers[containerId] ?: return container.close(force = true)
container.onClose()
connection.events.fire(ContainerCloseEvent(connection, containerId, container))
} }
override fun log(reducedLog: Boolean) { override fun log(reducedLog: Boolean) {

View File

@ -79,9 +79,10 @@ class OpenContainerS2CP(buffer: PlayInByteBuffer) : PlayS2CPacket {
container.floatingItem = it.floating container.floatingItem = it.floating
} }
connection.player.containers[containerId] = container connection.player.containers[containerId] = container
connection.player.openedContainer = container
connection.events.fire(ContainerOpenEvent(connection, containerId, container))
connection.player.openedContainer = container
connection.events.fire(ContainerOpenEvent(connection, container))
} }
override fun log(reducedLog: Boolean) { override fun log(reducedLog: Boolean) {