container: rename some properties

This commit is contained in:
Bixilon 2023-01-13 15:53:32 +01:00
parent a446472b67
commit f00b612f8b
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
13 changed files with 79 additions and 79 deletions

View File

@ -51,11 +51,27 @@ abstract class Container(
open val sections: Array<ContainerSection> get() = emptyArray() open val sections: Array<ContainerSection> get() = emptyArray()
var edit: ContainerEdit? = null
init { init {
this::floatingItem.observe(this) { it?.holder?.container = this } this::floatingItem.observe(this) { it?.holder?.container = this }
} }
var edit: ContainerEdit? = null
open fun getSlotType(slotId: Int): SlotType? = DefaultSlotType
open fun getSlotSwap(slot: SlotSwapContainerAction.SwapTargets): Int? = null
open fun readProperty(property: Int, value: Int) = Unit
open fun getSection(slotId: Int): Int? {
for ((index, section) in sections.withIndex()) {
if (slotId in section) {
return index
}
}
return null
}
fun validate() { fun validate() {
lock.lock() lock.lock()
@ -74,19 +90,7 @@ abstract class Container(
edit?.addChange() edit?.addChange()
} }
internalCommit() commitChange()
}
open fun getSlotType(slotId: Int): SlotType? = DefaultSlotType
open fun getSlotSwap(slot: SlotSwapContainerAction.SwapTargets): Int? = null
open fun getSection(slotId: Int): Int? {
for ((index, section) in sections.withIndex()) {
if (slotId in section) {
return index
}
}
return null
} }
operator fun get(slotId: Int): ItemStack? { operator fun get(slotId: Int): ItemStack? {
@ -105,16 +109,14 @@ abstract class Container(
val stack = slots.remove(slotId) ?: return null val stack = slots.remove(slotId) ?: return null
onRemove(slotId, stack) onRemove(slotId, stack)
stack.holder?.container = null stack.holder?.container = null
edit?.addChange()
return stack return stack
} }
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) { commitChange()
edit?.addChange()
}
internalCommit()
return remove return remove
} }
@ -122,12 +124,12 @@ abstract class Container(
remove(slotId) remove(slotId)
} }
open operator fun set(slotId: Int, itemStack: ItemStack?) { open operator fun set(slotId: Int, stack: ItemStack?) {
lock.lock() lock.lock()
if (_set(slotId, itemStack)) { if (_set(slotId, stack)) {
edit?.addChange() edit?.addChange()
} }
internalCommit() commitChange()
} }
protected open fun onSet(slotId: Int, stack: ItemStack?) = Unit protected open fun onSet(slotId: Int, stack: ItemStack?) = Unit
@ -163,12 +165,12 @@ abstract class Container(
return return
} }
lock.lock() lock.lock()
for ((slotId, itemStack) in slots) { for ((slotId, stack) in slots) {
if (_set(slotId, itemStack)) { if (_set(slotId, stack)) {
edit?.addChange() edit?.addChange()
} }
} }
internalCommit() commitChange()
} }
fun _clear() { fun _clear() {
@ -183,7 +185,7 @@ abstract class Container(
fun clear() { fun clear() {
lock.lock() lock.lock()
_clear() _clear()
internalCommit() commitChange()
} }
fun close(force: Boolean = false) { fun close(force: Boolean = false) {
@ -207,10 +209,6 @@ abstract class Container(
floatingItem = null // ToDo: Does not seem correct floatingItem = null // ToDo: Does not seem correct
} }
override fun iterator(): Iterator<Map.Entry<Int, ItemStack>> {
return slots.iterator()
}
fun lock() { fun lock() {
lock.lock() lock.lock()
if (edit == null) { if (edit == null) {
@ -218,7 +216,7 @@ abstract class Container(
} }
} }
fun internalCommit() { fun commitChange() {
val edit = this.edit val edit = this.edit
lock.unlock() lock.unlock()
if (edit == null) { if (edit == null) {
@ -236,5 +234,7 @@ abstract class Container(
revision++ revision++
} }
open fun readProperty(property: Int, value: Int) = Unit override fun iterator(): Iterator<Map.Entry<Int, ItemStack>> {
return slots.iterator()
}
} }

View File

@ -35,6 +35,6 @@ class InventoryDelegate<T>(
override fun setValue(thisRef: Any, property: KProperty<*>, value: T) { override fun setValue(thisRef: Any, property: KProperty<*>, value: T) {
stack.lock.lock() stack.lock.lock()
field.setValue(thisRef, property, value) field.setValue(thisRef, property, value)
stack.internalCommit() stack.commitChange()
} }
} }

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.
* *
@ -37,15 +37,15 @@ object ItemStackUtil {
nbt: MutableJsonObject? = null, nbt: MutableJsonObject? = null,
): ItemStack { ): ItemStack {
val itemStack = ItemStack(item, count) val stack = ItemStack(item, count)
if (connection != null || container != null) { if (connection != null || container != null) {
itemStack.holder = HolderProperty(connection, container) stack.holder = HolderProperty(connection, container)
} }
if (durability != null) { if (durability != null) {
itemStack.durability._durability = durability stack.durability._durability = durability
} }
nbt?.let { itemStack.updateNbt(nbt) } nbt?.let { stack.updateNbt(nbt) }
return itemStack return stack
} }
} }

View File

@ -28,13 +28,13 @@ class CloneContainerAction(
override fun invoke(connection: PlayConnection, containerId: Int, container: Container) { override fun invoke(connection: PlayConnection, containerId: Int, container: Container) {
container.floatingItem?.let { return } container.floatingItem?.let { return }
val clicked = container[slot] ?: return val clicked = container[slot] ?: return
val itemStack = clicked.copy(count = clicked.item.item.maxStackSize) val stack = clicked.copy(count = clicked.item.item.maxStackSize)
this.copied = itemStack this.copied = stack
// TODO (1.18.2): use creative inventory packet // TODO (1.18.2): use creative inventory packet
connection.sendPacket(ContainerClickC2SP(containerId, container.serverRevision, slot, 3, 0, container.actions.createId(this), slotsOf(), itemStack)) connection.sendPacket(ContainerClickC2SP(containerId, container.serverRevision, slot, 3, 0, container.actions.createId(this), slotsOf(), stack))
container.floatingItem = itemStack container.floatingItem = stack
} }
override fun revert(connection: PlayConnection, containerId: Int, container: Container) { override fun revert(connection: PlayConnection, containerId: Int, container: Container) {

View File

@ -208,7 +208,7 @@ class ItemStack {
} }
} }
fun internalCommit() { fun commitChange() {
val container = holder?.container val container = holder?.container
if (!_valid) { if (!_valid) {
container?.validate() container?.validate()

View File

@ -30,13 +30,13 @@ class ItemProperty(
fun decreaseCount() { fun decreaseCount() {
stack.lock.lock() stack.lock.lock()
_count -= 1 _count -= 1
stack.internalCommit() stack.commitChange()
} }
fun increaseCount() { fun increaseCount() {
stack.lock.lock() stack.lock.lock()
_count += 1 _count += 1
stack.internalCommit() stack.commitChange()
} }
override fun isDefault(): Boolean = false override fun isDefault(): Boolean = false

View File

@ -74,8 +74,8 @@ class PlayerInventory(connection: PlayConnection) : Container(connection = conne
return this[slot.slot] return this[slot.slot]
} }
operator fun set(slot: EquipmentSlots, itemStack: ItemStack?) { operator fun set(slot: EquipmentSlots, stack: ItemStack?) {
this[slot.slot] = itemStack this[slot.slot] = stack
} }
operator fun get(hand: Hands): ItemStack? { operator fun get(hand: Hands): ItemStack? {
@ -89,8 +89,8 @@ class PlayerInventory(connection: PlayConnection) : Container(connection = conne
fun set(vararg slots: Pair<EquipmentSlots, ItemStack?>) { fun set(vararg slots: Pair<EquipmentSlots, ItemStack?>) {
val realSlots: MutableList<Pair<Int, ItemStack?>> = mutableListOf() val realSlots: MutableList<Pair<Int, ItemStack?>> = mutableListOf()
for ((slot, itemStack) in slots) { for ((slot, stack) in slots) {
realSlots += Pair(slot.slot, itemStack) realSlots += Pair(slot.slot, stack)
} }
set(*realSlots.toTypedArray()) set(*realSlots.toTypedArray())

View File

@ -49,13 +49,13 @@ class CampfireBlockEntity(connection: PlayConnection) : BlockEntity(connection)
items[index] = null items[index] = null
continue continue
} }
val itemStack = ItemStackUtil.of( val stack = ItemStackUtil.of(
item = connection.registries.item[slot["id"].unsafeCast<String>()]!!, item = connection.registries.item[slot["id"].unsafeCast<String>()]!!,
connection = connection, connection = connection,
count = slot["Count"]?.toInt() ?: 1, count = slot["Count"]?.toInt() ?: 1,
) )
items[slot["Slot"]!!.toInt()] = itemStack items[slot["Slot"]!!.toInt()] = stack
} }
} }

View File

@ -18,10 +18,10 @@ import de.bixilon.minosoft.data.registries.particle.ParticleType
import de.bixilon.minosoft.protocol.protocol.PlayInByteBuffer import de.bixilon.minosoft.protocol.protocol.PlayInByteBuffer
import de.bixilon.minosoft.protocol.protocol.ProtocolVersions import de.bixilon.minosoft.protocol.protocol.ProtocolVersions
class ItemParticleData(val itemStack: ItemStack?, type: ParticleType) : ParticleData(type) { class ItemParticleData(val stack: ItemStack?, type: ParticleType) : ParticleData(type) {
override fun toString(): String { override fun toString(): String {
return "$type: $itemStack" return "$type: $stack"
} }
companion object : ParticleDataFactory<ItemParticleData> { companion object : ParticleDataFactory<ItemParticleData> {

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.
* *
@ -31,7 +31,7 @@ class ContainerItemS2CP(buffer: PlayInByteBuffer) : PlayS2CPacket {
-1 -1
} }
val slot = buffer.readShort().toInt() val slot = buffer.readShort().toInt()
val itemStack = buffer.readItemStack() val stack = buffer.readItemStack()
override fun handle(connection: PlayConnection) { override fun handle(connection: PlayConnection) {
val container = connection.player.containers[containerId] val container = connection.player.containers[containerId]
@ -39,24 +39,24 @@ class ContainerItemS2CP(buffer: PlayInByteBuffer) : PlayS2CPacket {
if (container == null) { if (container == null) {
val incomplete = connection.player.incompleteContainers.synchronizedGetOrPut(containerId) { IncompleteContainer() } val incomplete = connection.player.incompleteContainers.synchronizedGetOrPut(containerId) { IncompleteContainer() }
if (slot < 0) { if (slot < 0) {
incomplete.floating = itemStack incomplete.floating = stack
} else if (itemStack == null) { } else if (stack == null) {
incomplete.slots -= slot incomplete.slots -= slot
} else { } else {
incomplete.slots[slot] = itemStack incomplete.slots[slot] = stack
} }
return return
} }
if (slot < 0) { if (slot < 0) {
container.floatingItem = itemStack container.floatingItem = stack
} else { } else {
container[slot] = itemStack container[slot] = stack
} }
container.serverRevision = revision container.serverRevision = revision
} }
override fun log(reducedLog: Boolean) { override fun log(reducedLog: Boolean) {
Log.log(LogMessageType.NETWORK_PACKETS_IN, level = LogLevels.VERBOSE) { "Container item (containerId=$containerId, revision=$revision, slot=$slot, itemStack=$itemStack)" } Log.log(LogMessageType.NETWORK_PACKETS_IN, level = LogLevels.VERBOSE) { "Container item (containerId=$containerId, revision=$revision, slot=$slot, stack=$stack)" }
} }
} }

View File

@ -54,11 +54,11 @@ class EntityEquipmentS2CP(buffer: PlayInByteBuffer) : PlayS2CPacket {
override fun handle(connection: PlayConnection) { override fun handle(connection: PlayConnection) {
val entity = connection.world.entities[entityId] ?: return val entity = connection.world.entities[entityId] ?: return
for ((slot, itemStack) in equipment) { for ((slot, stack) in equipment) {
if (itemStack == null) { if (stack == null) {
entity.equipment.remove(slot) entity.equipment.remove(slot)
} else { } else {
entity.equipment[slot] = itemStack entity.equipment[slot] = stack
} }
} }
} }

View File

@ -45,27 +45,27 @@ class PlayOutByteBuffer(val connection: PlayConnection) : OutByteBuffer() {
} }
} }
fun writeItemStack(itemStack: ItemStack?) { fun writeItemStack(stack: ItemStack?) {
if (versionId < ProtocolVersions.V_1_13_2_PRE1) { if (versionId < ProtocolVersions.V_1_13_2_PRE1) {
if (itemStack == null || !itemStack._valid) { if (stack == null || !stack._valid) {
writeShort(-1) writeShort(-1)
return return
} }
writeShort(connection.registries.item.getId(itemStack.item.item)) writeShort(connection.registries.item.getId(stack.item.item))
writeByte(itemStack.item.count) writeByte(stack.item.count)
writeShort(itemStack._durability?.durability ?: 0) // ToDo: This is meta data in general and not just durability writeShort(stack._durability?.durability ?: 0) // ToDo: This is meta data in general and not just durability
writeNBT(itemStack.getNBT()) writeNBT(stack.getNBT())
return return
} }
val valid = itemStack != null && itemStack._valid val valid = stack != null && stack._valid
writeBoolean(valid) writeBoolean(valid)
if (!valid) { if (!valid) {
return return
} }
itemStack!! stack!!
writeVarInt(connection.registries.item.getId(itemStack.item.item)) writeVarInt(connection.registries.item.getId(stack.item.item))
writeByte(itemStack.item.count) writeByte(stack.item.count)
writeNBT(itemStack.getNBT()) writeNBT(stack.getNBT())
} }
fun writeEntityId(entityId: Int) { fun writeEntityId(entityId: Int) {

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.
* *
@ -14,19 +14,19 @@ package de.bixilon.minosoft.recipes
import de.bixilon.minosoft.data.container.stack.ItemStack import de.bixilon.minosoft.data.container.stack.ItemStack
data class Ingredient(val itemStacks: Array<ItemStack?>) { data class Ingredient(val stacks: Array<ItemStack?>) {
override fun equals(other: Any?): Boolean { override fun equals(other: Any?): Boolean {
if (this === other) return true if (this === other) return true
if (javaClass != other?.javaClass) return false if (javaClass != other?.javaClass) return false
other as Ingredient other as Ingredient
if (!itemStacks.contentEquals(other.itemStacks)) return false if (!stacks.contentEquals(other.stacks)) return false
return true return true
} }
override fun hashCode(): Int { override fun hashCode(): Int {
return itemStacks.contentHashCode() return stacks.contentHashCode()
} }
} }