mirror of
https://gitlab.bixilon.de/bixilon/minosoft.git
synced 2025-09-16 19:05:02 -04:00
make Container abstract, remove some deprecated functions
This commit is contained in:
parent
0b2457e1d8
commit
1cf942f256
@ -14,6 +14,7 @@
|
|||||||
package de.bixilon.minosoft.data.container
|
package de.bixilon.minosoft.data.container
|
||||||
|
|
||||||
import de.bixilon.minosoft.data.container.types.PlayerInventory
|
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.generic.Generic9x3Container
|
||||||
import de.bixilon.minosoft.data.container.types.processing.smelting.FurnaceContainer
|
import de.bixilon.minosoft.data.container.types.processing.smelting.FurnaceContainer
|
||||||
import de.bixilon.minosoft.data.registries.containers.ContainerFactory
|
import de.bixilon.minosoft.data.registries.containers.ContainerFactory
|
||||||
@ -44,7 +45,7 @@ object ContainerTestUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun createContainer(connection: PlayConnection = createConnection()): Container {
|
fun createContainer(connection: PlayConnection = createConnection()): Container {
|
||||||
val container = Container(connection, this.container)
|
val container = UnknownContainer(connection, this.container)
|
||||||
connection.player.containers[9] = container
|
connection.player.containers[9] = container
|
||||||
return container
|
return container
|
||||||
}
|
}
|
||||||
@ -65,7 +66,7 @@ object ContainerTestUtil {
|
|||||||
override val identifier: ResourceLocation = minosoft("test")
|
override val identifier: ResourceLocation = minosoft("test")
|
||||||
|
|
||||||
override fun build(connection: PlayConnection, type: ContainerType, title: ChatComponent?): Container {
|
override fun build(connection: PlayConnection, type: ContainerType, title: ChatComponent?): Container {
|
||||||
return Container(connection, type, title)
|
return UnknownContainer(connection, type, title)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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.ItemStack
|
||||||
import de.bixilon.minosoft.data.container.stack.property.HolderProperty
|
import de.bixilon.minosoft.data.container.stack.property.HolderProperty
|
||||||
import de.bixilon.minosoft.data.container.types.PlayerInventory
|
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.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.data.text.ChatComponent
|
||||||
import de.bixilon.minosoft.modding.event.events.container.ContainerCloseEvent
|
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
|
||||||
|
|
||||||
open class Container(
|
abstract class Container(
|
||||||
val connection: PlayConnection,
|
val connection: PlayConnection,
|
||||||
val type: ContainerType,
|
val type: ContainerType,
|
||||||
val title: ChatComponent? = null,
|
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
|
val stack = slots.remove(slotId) ?: return null
|
||||||
|
onRemove(slotId, stack)
|
||||||
stack.holder?.container = null
|
stack.holder?.container = null
|
||||||
return stack
|
return stack
|
||||||
}
|
}
|
||||||
|
|
||||||
open fun remove(slotId: Int): ItemStack? {
|
fun remove(slotId: Int): ItemStack? {
|
||||||
lock.lock()
|
lock.lock()
|
||||||
val remove = _remove(slotId)
|
val remove = _remove(slotId)
|
||||||
if (remove != null) {
|
if (remove != null) {
|
||||||
@ -121,6 +121,10 @@ open class Container(
|
|||||||
return remove
|
return remove
|
||||||
}
|
}
|
||||||
|
|
||||||
|
operator fun minusAssign(slotId: Int) {
|
||||||
|
remove(slotId)
|
||||||
|
}
|
||||||
|
|
||||||
open operator fun set(slotId: Int, itemStack: ItemStack?) {
|
open operator fun set(slotId: Int, itemStack: ItemStack?) {
|
||||||
lock.lock()
|
lock.lock()
|
||||||
if (_set(slotId, itemStack)) {
|
if (_set(slotId, itemStack)) {
|
||||||
@ -129,26 +133,30 @@ open class Container(
|
|||||||
internalCommit()
|
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]
|
val previous = slots[slotId]
|
||||||
if (itemStack == null) {
|
if (stack == null) {
|
||||||
|
// remove item
|
||||||
if (previous == null) {
|
if (previous == null) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
_remove(slotId)
|
_remove(slotId)
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
if (previous == itemStack) {
|
if (previous == stack) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
slots[slotId] = itemStack // ToDo: Check for changes
|
slots[slotId] = stack // ToDo: Check for changes
|
||||||
var holder = itemStack.holder
|
var holder = stack.holder
|
||||||
if (holder == null) {
|
if (holder == null) {
|
||||||
holder = HolderProperty(connection, this)
|
holder = HolderProperty(connection, this)
|
||||||
itemStack.holder = holder
|
stack.holder = holder
|
||||||
} else {
|
} else {
|
||||||
holder.container = this
|
holder.container = this
|
||||||
}
|
}
|
||||||
|
onSet(slotId, stack)
|
||||||
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
@ -256,12 +264,4 @@ open class Container(
|
|||||||
}
|
}
|
||||||
|
|
||||||
open fun readProperty(property: Int, value: Int) = Unit
|
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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -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.
|
||||||
*
|
*
|
||||||
@ -34,21 +34,15 @@ abstract class InventorySynchronizedContainer(
|
|||||||
// ToDo: Add initial slots from inventory
|
// ToDo: Add initial slots from inventory
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun _remove(slotId: Int): ItemStack? {
|
override fun onRemove(slotId: Int, stack: ItemStack) {
|
||||||
val slot = super._remove(slotId) ?: return null
|
|
||||||
if (slotId in synchronizedSlots) {
|
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 {
|
override fun onSet(slotId: Int, stack: ItemStack?) {
|
||||||
val changed = super._set(slotId, itemStack)
|
|
||||||
if (slotId in synchronizedSlots) {
|
if (slotId in synchronizedSlots) {
|
||||||
playerInventory._set(slotId - synchronizedSlots.first + inventorySlots.first, itemStack)
|
playerInventory[slotId - synchronizedSlots.first + inventorySlots.first] = stack
|
||||||
}
|
}
|
||||||
|
|
||||||
return changed
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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.
|
||||||
*
|
*
|
||||||
@ -25,32 +25,32 @@ class SlotSwapContainerAction(
|
|||||||
|
|
||||||
override fun invoke(connection: PlayConnection, containerId: Int, container: Container) {
|
override fun invoke(connection: PlayConnection, containerId: Int, container: Container) {
|
||||||
val targetId = container.getSlotSwap(target) ?: return
|
val targetId = container.getSlotSwap(target) ?: return
|
||||||
container.lock.lock()
|
container.lock()
|
||||||
try {
|
try {
|
||||||
val source = container.slots[sourceId]
|
val source = container[sourceId]
|
||||||
val target = container.slots[targetId]
|
val target = container[targetId]
|
||||||
|
|
||||||
if (source == null && target == null) {
|
if (source == null && target == null) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
container._set(this.sourceId, target)
|
container[this.sourceId] = target
|
||||||
container._set(targetId, source)
|
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.createAction(this), slotsOf(sourceId to target, targetId to source), source))
|
||||||
|
|
||||||
} finally {
|
} finally {
|
||||||
container.lock.unlock()
|
container.commit()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun revert(connection: PlayConnection, containerId: Int, container: Container) {
|
override fun revert(connection: PlayConnection, containerId: Int, container: Container) {
|
||||||
val targetId = container.getSlotSwap(target) ?: return
|
val targetId = container.getSlotSwap(target) ?: return
|
||||||
container.lock.lock()
|
container.lock()
|
||||||
val target = container.slots[targetId]
|
val target = container[targetId]
|
||||||
val source = container.slots[sourceId]
|
val source = container[sourceId]
|
||||||
container._set(sourceId, target)
|
container[sourceId] = target
|
||||||
container._set(targetId, source)
|
container[targetId] = source
|
||||||
container.lock.unlock()
|
container.commit()
|
||||||
}
|
}
|
||||||
|
|
||||||
enum class SwapTargets(val button: Int) {
|
enum class SwapTargets(val button: Int) {
|
||||||
|
@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -13,6 +13,7 @@
|
|||||||
package de.bixilon.minosoft.data.registries.containers
|
package de.bixilon.minosoft.data.registries.containers
|
||||||
|
|
||||||
import de.bixilon.minosoft.data.container.Container
|
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.identified.ResourceLocation
|
||||||
import de.bixilon.minosoft.data.registries.registries.Registries
|
import de.bixilon.minosoft.data.registries.registries.Registries
|
||||||
import de.bixilon.minosoft.data.registries.registries.registry.RegistryItem
|
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 {
|
override fun deserialize(registries: Registries?, resourceLocation: ResourceLocation, data: Map<String, Any>): ContainerType {
|
||||||
return ContainerType(
|
return ContainerType(
|
||||||
identifier = resourceLocation,
|
identifier = resourceLocation,
|
||||||
factory = DefaultContainerFactories[resourceLocation] ?: Container,
|
factory = DefaultContainerFactories[resourceLocation] ?: UnknownContainer,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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.
|
||||||
*
|
*
|
||||||
@ -62,19 +62,18 @@ class ContainerItemsS2CP(buffer: PlayInByteBuffer) : PlayS2CPacket {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun updateContainer(container: Container) {
|
private fun updateContainer(container: Container) {
|
||||||
container.lock.lock()
|
container.lock()
|
||||||
container._clear()
|
container.clear()
|
||||||
|
|
||||||
for ((slotId, stack) in this.items.withIndex()) {
|
for ((slotId, stack) in this.items.withIndex()) {
|
||||||
if (stack == null) {
|
if (stack == null) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
container._set(slotId, stack)
|
container[slotId] = stack
|
||||||
}
|
}
|
||||||
container.serverRevision = revision
|
container.serverRevision = revision
|
||||||
this.floatingItem?.let { container.floatingItem = if (it.isEmpty) null else it.get() }
|
this.floatingItem?.let { container.floatingItem = if (it.isEmpty) null else it.get() }
|
||||||
container.lock.unlock()
|
container.commit()
|
||||||
container.revision++
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun handle(connection: PlayConnection) {
|
override fun handle(connection: PlayConnection) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user