make Container abstract, remove some deprecated functions

This commit is contained in:
Bixilon 2023-01-13 15:37:41 +01:00
parent 0b2457e1d8
commit 1cf942f256
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
7 changed files with 88 additions and 54 deletions

View File

@ -14,6 +14,7 @@
package de.bixilon.minosoft.data.container
import de.bixilon.minosoft.data.container.types.PlayerInventory
import de.bixilon.minosoft.data.container.types.UnknownContainer
import de.bixilon.minosoft.data.container.types.generic.Generic9x3Container
import de.bixilon.minosoft.data.container.types.processing.smelting.FurnaceContainer
import de.bixilon.minosoft.data.registries.containers.ContainerFactory
@ -44,7 +45,7 @@ object ContainerTestUtil {
}
fun createContainer(connection: PlayConnection = createConnection()): Container {
val container = Container(connection, this.container)
val container = UnknownContainer(connection, this.container)
connection.player.containers[9] = container
return container
}
@ -65,7 +66,7 @@ object ContainerTestUtil {
override val identifier: ResourceLocation = minosoft("test")
override fun build(connection: PlayConnection, type: ContainerType, title: ChatComponent?): Container {
return Container(connection, type, title)
return UnknownContainer(connection, type, title)
}
}
}

View File

@ -27,17 +27,14 @@ import de.bixilon.minosoft.data.container.slots.SlotType
import de.bixilon.minosoft.data.container.stack.ItemStack
import de.bixilon.minosoft.data.container.stack.property.HolderProperty
import de.bixilon.minosoft.data.container.types.PlayerInventory
import de.bixilon.minosoft.data.registries.containers.ContainerFactory
import de.bixilon.minosoft.data.registries.containers.ContainerType
import de.bixilon.minosoft.data.registries.identified.Namespaces.minecraft
import de.bixilon.minosoft.data.registries.identified.ResourceLocation
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.packets.c2s.play.container.CloseContainerC2SP
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap
open class Container(
abstract class Container(
val connection: PlayConnection,
val type: ContainerType,
val title: ChatComponent? = null,
@ -104,14 +101,17 @@ open class Container(
}
}
@Deprecated("")
open fun _remove(slotId: Int): ItemStack? {
protected open fun onRemove(slotId: Int, stack: ItemStack) = Unit
private fun _remove(slotId: Int): ItemStack? {
val stack = slots.remove(slotId) ?: return null
onRemove(slotId, stack)
stack.holder?.container = null
return stack
}
open fun remove(slotId: Int): ItemStack? {
fun remove(slotId: Int): ItemStack? {
lock.lock()
val remove = _remove(slotId)
if (remove != null) {
@ -121,6 +121,10 @@ open class Container(
return remove
}
operator fun minusAssign(slotId: Int) {
remove(slotId)
}
open operator fun set(slotId: Int, itemStack: ItemStack?) {
lock.lock()
if (_set(slotId, itemStack)) {
@ -129,26 +133,30 @@ open class Container(
internalCommit()
}
open fun _set(slotId: Int, itemStack: ItemStack?): Boolean {
protected open fun onSet(slotId: Int, stack: ItemStack?) = Unit
protected fun _set(slotId: Int, stack: ItemStack?): Boolean {
val previous = slots[slotId]
if (itemStack == null) {
if (stack == null) {
// remove item
if (previous == null) {
return false
}
_remove(slotId)
return true
}
if (previous == itemStack) {
if (previous == stack) {
return false
}
slots[slotId] = itemStack // ToDo: Check for changes
var holder = itemStack.holder
slots[slotId] = stack // ToDo: Check for changes
var holder = stack.holder
if (holder == null) {
holder = HolderProperty(connection, this)
itemStack.holder = holder
stack.holder = holder
} else {
holder.container = this
}
onSet(slotId, stack)
return true
}
@ -256,12 +264,4 @@ open class Container(
}
open fun readProperty(property: Int, value: Int) = Unit
companion object : ContainerFactory<Container> {
override val identifier: ResourceLocation = minecraft("container")
override fun build(connection: PlayConnection, type: ContainerType, title: ChatComponent?): Container {
return Container(connection, type, title)
}
}
}

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.
*
@ -34,21 +34,15 @@ abstract class InventorySynchronizedContainer(
// ToDo: Add initial slots from inventory
}
override fun _remove(slotId: Int): ItemStack? {
val slot = super._remove(slotId) ?: return null
override fun onRemove(slotId: Int, stack: ItemStack) {
if (slotId in synchronizedSlots) {
playerInventory._remove(slotId - synchronizedSlots.first + inventorySlots.first)
playerInventory.remove(slotId - synchronizedSlots.first + inventorySlots.first)
}
return slot
}
override fun _set(slotId: Int, itemStack: ItemStack?): Boolean {
val changed = super._set(slotId, itemStack)
override fun onSet(slotId: Int, stack: ItemStack?) {
if (slotId in synchronizedSlots) {
playerInventory._set(slotId - synchronizedSlots.first + inventorySlots.first, itemStack)
playerInventory[slotId - synchronizedSlots.first + inventorySlots.first] = stack
}
return changed
}
}

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.
*
@ -25,32 +25,32 @@ class SlotSwapContainerAction(
override fun invoke(connection: PlayConnection, containerId: Int, container: Container) {
val targetId = container.getSlotSwap(target) ?: return
container.lock.lock()
container.lock()
try {
val source = container.slots[sourceId]
val target = container.slots[targetId]
val source = container[sourceId]
val target = container[targetId]
if (source == null && target == null) {
return
}
container._set(this.sourceId, target)
container._set(targetId, source)
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))
} finally {
container.lock.unlock()
container.commit()
}
}
override fun revert(connection: PlayConnection, containerId: Int, container: Container) {
val targetId = container.getSlotSwap(target) ?: return
container.lock.lock()
val target = container.slots[targetId]
val source = container.slots[sourceId]
container._set(sourceId, target)
container._set(targetId, source)
container.lock.unlock()
container.lock()
val target = container[targetId]
val source = container[sourceId]
container[sourceId] = target
container[targetId] = source
container.commit()
}
enum class SwapTargets(val button: Int) {

View File

@ -0,0 +1,39 @@
/*
* 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.types
import de.bixilon.minosoft.data.container.Container
import de.bixilon.minosoft.data.registries.containers.ContainerFactory
import de.bixilon.minosoft.data.registries.containers.ContainerType
import de.bixilon.minosoft.data.registries.identified.Namespaces.minecraft
import de.bixilon.minosoft.data.registries.identified.ResourceLocation
import de.bixilon.minosoft.data.text.ChatComponent
import de.bixilon.minosoft.protocol.network.connection.play.PlayConnection
@Deprecated("Its unknown")
class UnknownContainer(
connection: PlayConnection,
type: ContainerType,
title: ChatComponent? = null,
) : Container(connection, type, title) {
companion object : ContainerFactory<Container> {
override val identifier: ResourceLocation = minecraft("container")
override fun build(connection: PlayConnection, type: ContainerType, title: ChatComponent?): Container {
return UnknownContainer(connection, type, title)
}
}
}

View File

@ -13,6 +13,7 @@
package de.bixilon.minosoft.data.registries.containers
import de.bixilon.minosoft.data.container.Container
import de.bixilon.minosoft.data.container.types.UnknownContainer
import de.bixilon.minosoft.data.registries.identified.ResourceLocation
import de.bixilon.minosoft.data.registries.registries.Registries
import de.bixilon.minosoft.data.registries.registries.registry.RegistryItem
@ -31,7 +32,7 @@ data class ContainerType(
override fun deserialize(registries: Registries?, resourceLocation: ResourceLocation, data: Map<String, Any>): ContainerType {
return ContainerType(
identifier = resourceLocation,
factory = DefaultContainerFactories[resourceLocation] ?: Container,
factory = DefaultContainerFactories[resourceLocation] ?: UnknownContainer,
)
}
}

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.
*
@ -62,19 +62,18 @@ class ContainerItemsS2CP(buffer: PlayInByteBuffer) : PlayS2CPacket {
}
private fun updateContainer(container: Container) {
container.lock.lock()
container._clear()
container.lock()
container.clear()
for ((slotId, stack) in this.items.withIndex()) {
if (stack == null) {
continue
}
container._set(slotId, stack)
container[slotId] = stack
}
container.serverRevision = revision
this.floatingItem?.let { container.floatingItem = if (it.isEmpty) null else it.get() }
container.lock.unlock()
container.revision++
container.commit()
}
override fun handle(connection: PlayConnection) {