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()
var edit: ContainerEdit? = null
init {
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() {
lock.lock()
@ -74,19 +90,7 @@ abstract class Container(
edit?.addChange()
}
internalCommit()
}
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
commitChange()
}
operator fun get(slotId: Int): ItemStack? {
@ -105,16 +109,14 @@ abstract class Container(
val stack = slots.remove(slotId) ?: return null
onRemove(slotId, stack)
stack.holder?.container = null
edit?.addChange()
return stack
}
fun remove(slotId: Int): ItemStack? {
lock.lock()
val remove = _remove(slotId)
if (remove != null) {
edit?.addChange()
}
internalCommit()
commitChange()
return remove
}
@ -122,12 +124,12 @@ abstract class Container(
remove(slotId)
}
open operator fun set(slotId: Int, itemStack: ItemStack?) {
open operator fun set(slotId: Int, stack: ItemStack?) {
lock.lock()
if (_set(slotId, itemStack)) {
if (_set(slotId, stack)) {
edit?.addChange()
}
internalCommit()
commitChange()
}
protected open fun onSet(slotId: Int, stack: ItemStack?) = Unit
@ -163,12 +165,12 @@ abstract class Container(
return
}
lock.lock()
for ((slotId, itemStack) in slots) {
if (_set(slotId, itemStack)) {
for ((slotId, stack) in slots) {
if (_set(slotId, stack)) {
edit?.addChange()
}
}
internalCommit()
commitChange()
}
fun _clear() {
@ -183,7 +185,7 @@ abstract class Container(
fun clear() {
lock.lock()
_clear()
internalCommit()
commitChange()
}
fun close(force: Boolean = false) {
@ -207,10 +209,6 @@ abstract class Container(
floatingItem = null // ToDo: Does not seem correct
}
override fun iterator(): Iterator<Map.Entry<Int, ItemStack>> {
return slots.iterator()
}
fun lock() {
lock.lock()
if (edit == null) {
@ -218,7 +216,7 @@ abstract class Container(
}
}
fun internalCommit() {
fun commitChange() {
val edit = this.edit
lock.unlock()
if (edit == null) {
@ -236,5 +234,7 @@ abstract class Container(
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) {
stack.lock.lock()
field.setValue(thisRef, property, value)
stack.internalCommit()
stack.commitChange()
}
}

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.
*
@ -37,15 +37,15 @@ object ItemStackUtil {
nbt: MutableJsonObject? = null,
): ItemStack {
val itemStack = ItemStack(item, count)
val stack = ItemStack(item, count)
if (connection != null || container != null) {
itemStack.holder = HolderProperty(connection, container)
stack.holder = HolderProperty(connection, container)
}
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) {
container.floatingItem?.let { return }
val clicked = container[slot] ?: return
val itemStack = clicked.copy(count = clicked.item.item.maxStackSize)
this.copied = itemStack
val stack = clicked.copy(count = clicked.item.item.maxStackSize)
this.copied = stack
// 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) {

View File

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

View File

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

View File

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

View File

@ -49,13 +49,13 @@ class CampfireBlockEntity(connection: PlayConnection) : BlockEntity(connection)
items[index] = null
continue
}
val itemStack = ItemStackUtil.of(
val stack = ItemStackUtil.of(
item = connection.registries.item[slot["id"].unsafeCast<String>()]!!,
connection = connection,
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.ProtocolVersions
class ItemParticleData(val itemStack: ItemStack?, type: ParticleType) : ParticleData(type) {
class ItemParticleData(val stack: ItemStack?, type: ParticleType) : ParticleData(type) {
override fun toString(): String {
return "$type: $itemStack"
return "$type: $stack"
}
companion object : ParticleDataFactory<ItemParticleData> {

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.
*
@ -31,7 +31,7 @@ class ContainerItemS2CP(buffer: PlayInByteBuffer) : PlayS2CPacket {
-1
}
val slot = buffer.readShort().toInt()
val itemStack = buffer.readItemStack()
val stack = buffer.readItemStack()
override fun handle(connection: PlayConnection) {
val container = connection.player.containers[containerId]
@ -39,24 +39,24 @@ class ContainerItemS2CP(buffer: PlayInByteBuffer) : PlayS2CPacket {
if (container == null) {
val incomplete = connection.player.incompleteContainers.synchronizedGetOrPut(containerId) { IncompleteContainer() }
if (slot < 0) {
incomplete.floating = itemStack
} else if (itemStack == null) {
incomplete.floating = stack
} else if (stack == null) {
incomplete.slots -= slot
} else {
incomplete.slots[slot] = itemStack
incomplete.slots[slot] = stack
}
return
}
if (slot < 0) {
container.floatingItem = itemStack
container.floatingItem = stack
} else {
container[slot] = itemStack
container[slot] = stack
}
container.serverRevision = revision
}
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) {
val entity = connection.world.entities[entityId] ?: return
for ((slot, itemStack) in equipment) {
if (itemStack == null) {
for ((slot, stack) in equipment) {
if (stack == null) {
entity.equipment.remove(slot)
} 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 (itemStack == null || !itemStack._valid) {
if (stack == null || !stack._valid) {
writeShort(-1)
return
}
writeShort(connection.registries.item.getId(itemStack.item.item))
writeByte(itemStack.item.count)
writeShort(itemStack._durability?.durability ?: 0) // ToDo: This is meta data in general and not just durability
writeNBT(itemStack.getNBT())
writeShort(connection.registries.item.getId(stack.item.item))
writeByte(stack.item.count)
writeShort(stack._durability?.durability ?: 0) // ToDo: This is meta data in general and not just durability
writeNBT(stack.getNBT())
return
}
val valid = itemStack != null && itemStack._valid
val valid = stack != null && stack._valid
writeBoolean(valid)
if (!valid) {
return
}
itemStack!!
writeVarInt(connection.registries.item.getId(itemStack.item.item))
writeByte(itemStack.item.count)
writeNBT(itemStack.getNBT())
stack!!
writeVarInt(connection.registries.item.getId(stack.item.item))
writeByte(stack.item.count)
writeNBT(stack.getNBT())
}
fun writeEntityId(entityId: Int) {

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